summaryrefslogtreecommitdiffstats
path: root/lib/oeqa/selftest/updater.py
blob: 9a3efee14fa3e67ca1e853e10d55035e72d54065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# pylint: disable=C0111,C0325
import os
import logging
import subprocess
import unittest
from time import sleep

from oeqa.selftest.base import oeSelfTest
from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars
from oeqa.selftest.qemucommand import QemuCommand


class SotaToolsTests(oeSelfTest):

    @classmethod
    def setUpClass(cls):
        logger = logging.getLogger("selftest")
        logger.info('Running bitbake to build aktualizr-native tools')
        bitbake('aktualizr-native')

    def test_push_help(self):
        bb_vars = get_bb_vars(['SYSROOT_DESTDIR', 'bindir', 'libdir'], 'aktualizr-native')
        l = bb_vars['libdir']
        p = bb_vars['SYSROOT_DESTDIR'] + bb_vars['bindir'] + "/garage-push"
        self.assertTrue(os.path.isfile(p), msg = "No garage-push found (%s)" % p)
        result = runCmd('LD_LIBRARY_PATH=%s %s --help' % (l, p), ignore_status=True)
        self.assertEqual(result.status, 0, "Status not equal to 0. output: %s" % result.output)

    def test_deploy_help(self):
        bb_vars = get_bb_vars(['SYSROOT_DESTDIR', 'bindir', 'libdir'], 'aktualizr-native')
        l = bb_vars['libdir']
        p = bb_vars['SYSROOT_DESTDIR'] + bb_vars['bindir'] + "/garage-deploy"
        self.assertTrue(os.path.isfile(p), msg = "No garage-deploy found (%s)" % p)
        result = runCmd('LD_LIBRARY_PATH=%s %s --help' % (l, p), ignore_status=True)
        self.assertEqual(result.status, 0, "Status not equal to 0. output: %s" % result.output)

    def test_garagesign_help(self):
        bb_vars = get_bb_vars(['SYSROOT_DESTDIR', 'bindir', 'libdir'], 'aktualizr-native')
        l = bb_vars['libdir']
        p = bb_vars['SYSROOT_DESTDIR'] + bb_vars['bindir'] + "/garage-sign"
        self.assertTrue(os.path.isfile(p), msg = "No garage-sign found (%s)" % p)
        result = runCmd('LD_LIBRARY_PATH=%s %s --help' % (l, p), ignore_status=True)
        self.assertEqual(result.status, 0, "Status not equal to 0. output: %s" % result.output)

class HsmTests(oeSelfTest):

    def test_hsm(self):
        self.write_config('SOTA_CLIENT_FEATURES="hsm"')
        bitbake('core-image-minimal')


class GeneralTests(oeSelfTest):

    def test_feature_sota(self):
        result = get_bb_var('DISTRO_FEATURES').find('sota')
        self.assertNotEqual(result, -1, 'Feature "sota" not set at DISTRO_FEATURES')

    def test_feature_systemd(self):
        result = get_bb_var('DISTRO_FEATURES').find('systemd')
        self.assertNotEqual(result, -1, 'Feature "systemd" not set at DISTRO_FEATURES')

    def test_credentials(self):
        self.write_config('SOTA_CLIENT_PROV = " aktualizr-auto-prov "')
        bitbake('core-image-minimal')
        credentials = get_bb_var('SOTA_PACKED_CREDENTIALS')
        # skip the test if the variable SOTA_PACKED_CREDENTIALS is not set
        if credentials is None:
            raise unittest.SkipTest("Variable 'SOTA_PACKED_CREDENTIALS' not set.")
        # Check if the file exists
        self.assertTrue(os.path.isfile(credentials), "File %s does not exist" % credentials)
        deploydir = get_bb_var('DEPLOY_DIR_IMAGE')
        imagename = get_bb_var('IMAGE_LINK_NAME', 'core-image-minimal')
        # Check if the credentials are included in the output image
        result = runCmd('tar -jtvf %s/%s.tar.bz2 | grep sota_provisioning_credentials.zip' %
                        (deploydir, imagename), ignore_status=True)
        self.assertEqual(result.status, 0, "Status not equal to 0. output: %s" % result.output)

    def test_java(self):
        result = runCmd('which java', ignore_status=True)
        self.assertEqual(result.status, 0, "Java not found.")

    def test_implicit_writer_help(self):
        bb_vars = get_bb_vars(['SYSROOT_DESTDIR', 'bindir', 'libdir'], 'aktualizr-native')
        l = bb_vars['libdir']
        p = bb_vars['SYSROOT_DESTDIR'] + bb_vars['bindir'] + "/aktualizr_implicit_writer"
        self.assertTrue(os.path.isfile(p), msg = "No aktualizr_implicit_writer found (%s)" % p)
        result = runCmd('LD_LIBRARY_PATH=%s %s --help' % (l, p), ignore_status=True)
        self.assertEqual(result.status, 0, "Status not equal to 0. output: %s" % result.output)

    def test_cert_provider_help(self):
        bb_vars = get_bb_vars(['SYSROOT_DESTDIR', 'bindir', 'libdir'], 'aktualizr-native')
        l = bb_vars['libdir']
        p = bb_vars['SYSROOT_DESTDIR'] + bb_vars['bindir'] + "/aktualizr_cert_provider"
        self.assertTrue(os.path.isfile(p), msg = "No aktualizr_cert_provider found (%s)" % p)
        result = runCmd('LD_LIBRARY_PATH=%s %s --help' % (l, p), ignore_status=True)
        self.assertEqual(result.status, 0, "Status not equal to 0. output: %s" % result.output)

    def test_add_package(self):
        print('')
        deploydir = get_bb_var('DEPLOY_DIR_IMAGE')
        imagename = get_bb_var('IMAGE_LINK_NAME', 'core-image-minimal')
        image_path = deploydir + '/' + imagename + '.otaimg'
        logger = logging.getLogger("selftest")

        logger.info('Running bitbake with man in the image package list')
        self.write_config('IMAGE_INSTALL_append = " man "')
        bitbake('-c cleanall man')
        bitbake('core-image-minimal')
        result = runCmd('oe-pkgdata-util find-path /usr/bin/man')
        self.assertEqual(result.output, 'man: /usr/bin/man')
        path1 = os.path.realpath(image_path)
        size1 = os.path.getsize(path1)
        logger.info('First image %s has size %i' % (path1, size1))

        logger.info('Running bitbake without man in the image package list')
        self.write_config('IMAGE_INSTALL_remove = " man "')
        bitbake('-c cleanall man')
        bitbake('core-image-minimal')
        result = runCmd('oe-pkgdata-util find-path /usr/bin/man', ignore_status=True)
        self.assertEqual(result.status, 1, "Status different than 1. output: %s" % result.output)
        self.assertEqual(result.output, 'ERROR: Unable to find any package producing path /usr/bin/man')
        path2 = os.path.realpath(image_path)
        size2 = os.path.getsize(path2)
        logger.info('Second image %s has size %i', path2, size2)
        self.assertNotEqual(path1, path2, "Image paths are identical; image was not rebuilt.")
        self.assertNotEqual(size1, size2, "Image sizes are identical; image was not rebuilt.")


class QemuTests(oeSelfTest):

    @classmethod
    def setUpClass(cls):
        cls.qemu, cls.s = qemu_launch(machine='qemux86-64')

    @classmethod
    def tearDownClass(cls):
        qemu_terminate(cls.s)

    def run_command(self, command):
        return qemu_send_command(self.qemu.ssh_port, command)

    def test_hostname(self):
        print('')
        print('Checking machine name (hostname) of device:')
        stdout, stderr, retcode = self.run_command('hostname')
        machine = get_bb_var('MACHINE', 'core-image-minimal')
        self.assertEqual(stderr, b'', 'Error: ' + stderr.decode())
        # Strip off line ending.
        value_str = stdout.decode()[:-1]
        self.assertEqual(value_str, machine,
                         'MACHINE does not match hostname: ' + machine + ', ' + value_str)
        print(value_str)

    def test_var_sota(self):
        print('')
        print('Checking contents of /var/sota:')
        stdout, stderr, retcode = self.run_command('ls /var/sota')
        self.assertEqual(stderr, b'', 'Error: ' + stderr.decode())
        self.assertEqual(retcode, 0)
        print(stdout.decode())

    def test_aktualizr_info(self):
        print('Checking output of aktualizr-info:')
        ran_ok = False
        for delay in [0, 1, 2, 5, 10, 15]:
            sleep(delay)
            try:
                stdout, stderr, retcode = self.run_command('aktualizr-info')
                if retcode == 0 and stderr == b'':
                    ran_ok = True
                    break
            except IOError as e:
                print(e)
        self.assertTrue(ran_ok, 'aktualizr-info failed: ' + stdout.decode() + stderr.decode())


class GrubTests(oeSelfTest):

    def setUpLocal(self):
        # This is a bit of a hack but I can't see a better option.
        path = os.path.abspath(os.path.dirname(__file__))
        metadir = path + "/../../../../"
        grub_config = 'OSTREE_BOOTLOADER = "grub"\nMACHINE = "intel-corei7-64"'
        self.append_config(grub_config)
        self.meta_intel = metadir + "meta-intel"
        self.meta_minnow = metadir + "meta-updater-minnowboard"
        runCmd('bitbake-layers add-layer "%s"' % self.meta_intel)
        runCmd('bitbake-layers add-layer "%s"' % self.meta_minnow)
        self.qemu, self.s = qemu_launch(efi=True, machine='intel-corei7-64')

    def tearDownLocal(self):
        qemu_terminate(self.s)
        runCmd('bitbake-layers remove-layer "%s"' % self.meta_intel, ignore_status=True)
        runCmd('bitbake-layers remove-layer "%s"' % self.meta_minnow, ignore_status=True)

    def test_grub(self):
        print('')
        print('Checking machine name (hostname) of device:')
        value, err, retcode = qemu_send_command(self.qemu.ssh_port, 'hostname')
        machine = get_bb_var('MACHINE', 'core-image-minimal')
        self.assertEqual(err, b'', 'Error: ' + err.decode())
        self.assertEqual(retcode, 0)
        # Strip off line ending.
        value_str = value.decode()[:-1]
        self.assertEqual(value_str, machine,
                         'MACHINE does not match hostname: ' + machine + ', ' + value_str +
                         '\nIs tianocore ovmf installed?')
        print(value_str)


def qemu_launch(efi=False, machine=None):
    logger = logging.getLogger("selftest")
    logger.info('Running bitbake to build core-image-minimal')
    bitbake('core-image-minimal')
    # Create empty object.
    args = type('', (), {})()
    args.imagename = 'core-image-minimal'
    args.mac = None
    # Could use DEPLOY_DIR_IMAGE here but it's already in the machine
    # subdirectory.
    args.dir = 'tmp/deploy/images'
    args.efi = efi
    args.machine = machine
    args.kvm = None  # Autodetect
    args.no_gui = True
    args.gdb = False
    args.pcap = None
    args.overlay = None
    args.dry_run = False

    qemu = QemuCommand(args)
    cmdline = qemu.command_line()
    print('Booting image with run-qemu-ota...')
    s = subprocess.Popen(cmdline)
    sleep(10)
    return qemu, s

def qemu_terminate(s):
    try:
        s.terminate()
    except KeyboardInterrupt:
        pass

def qemu_send_command(port, command):
    command = ['ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@localhost -p ' +
               str(port) + ' "' + command + '"']
    s2 = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = s2.communicate()
    return stdout, stderr, s2.returncode

# vim:set ts=4 sw=4 sts=4 expandtab: