diff options
author | Patrick Vacek <patrickvacek@gmail.com> | 2019-03-21 15:28:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-21 15:28:55 +0100 |
commit | d39d8b167797243fcf067c6faa6b4220d697c71c (patch) | |
tree | d4aca54f1e8ff8e2a3409c2df2506f1f17152d3e /lib/oeqa/selftest/cases/testutils.py | |
parent | 1e569e4def74e8b161833c4874888a7e514b67b1 (diff) | |
parent | e1867d7a7bd52d79fa3d051bb7b16f50934b88e6 (diff) | |
download | meta-updater-d39d8b167797243fcf067c6faa6b4220d697c71c.tar.gz |
Merge pull request #507 from advancedtelematic/feat/OTA-2142/aktualizr-resource-control
aktualizr resource control
Diffstat (limited to 'lib/oeqa/selftest/cases/testutils.py')
-rw-r--r-- | lib/oeqa/selftest/cases/testutils.py | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/lib/oeqa/selftest/cases/testutils.py b/lib/oeqa/selftest/cases/testutils.py new file mode 100644 index 0000000..90ba653 --- /dev/null +++ b/lib/oeqa/selftest/cases/testutils.py | |||
@@ -0,0 +1,128 @@ | |||
1 | import os | ||
2 | import logging | ||
3 | import re | ||
4 | import subprocess | ||
5 | from time import sleep | ||
6 | |||
7 | from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars | ||
8 | from qemucommand import QemuCommand | ||
9 | |||
10 | |||
11 | def qemu_launch(efi=False, machine=None, imagename=None): | ||
12 | logger = logging.getLogger("selftest") | ||
13 | if imagename is None: | ||
14 | imagename = 'core-image-minimal' | ||
15 | logger.info('Running bitbake to build {}'.format(imagename)) | ||
16 | bitbake(imagename) | ||
17 | # Create empty object. | ||
18 | args = type('', (), {})() | ||
19 | args.imagename = imagename | ||
20 | args.mac = None | ||
21 | # Could use DEPLOY_DIR_IMAGE here but it's already in the machine | ||
22 | # subdirectory. | ||
23 | args.dir = 'tmp/deploy/images' | ||
24 | args.efi = efi | ||
25 | args.machine = machine | ||
26 | qemu_use_kvm = get_bb_var("QEMU_USE_KVM") | ||
27 | if qemu_use_kvm and \ | ||
28 | (qemu_use_kvm == 'True' and 'x86' in machine or | ||
29 | get_bb_var('MACHINE') in qemu_use_kvm.split()): | ||
30 | args.kvm = True | ||
31 | else: | ||
32 | args.kvm = None # Autodetect | ||
33 | args.no_gui = True | ||
34 | args.gdb = False | ||
35 | args.pcap = None | ||
36 | args.overlay = None | ||
37 | args.dry_run = False | ||
38 | args.secondary_network = False | ||
39 | |||
40 | qemu = QemuCommand(args) | ||
41 | cmdline = qemu.command_line() | ||
42 | print('Booting image with run-qemu-ota...') | ||
43 | s = subprocess.Popen(cmdline) | ||
44 | sleep(10) | ||
45 | return qemu, s | ||
46 | |||
47 | |||
48 | def qemu_terminate(s): | ||
49 | try: | ||
50 | s.terminate() | ||
51 | except KeyboardInterrupt: | ||
52 | pass | ||
53 | |||
54 | |||
55 | def qemu_send_command(port, command): | ||
56 | command = ['ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@localhost -p ' + | ||
57 | str(port) + ' "' + command + '"'] | ||
58 | s2 = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
59 | stdout, stderr = s2.communicate(timeout=60) | ||
60 | return stdout, stderr, s2.returncode | ||
61 | |||
62 | |||
63 | def akt_native_run(testInst, cmd, **kwargs): | ||
64 | # run a command supplied by aktualizr-native and checks that: | ||
65 | # - the executable exists | ||
66 | # - the command runs without error | ||
67 | # NOTE: the base test class must have built aktualizr-native (in | ||
68 | # setUpClass, for example) | ||
69 | bb_vars = get_bb_vars(['SYSROOT_DESTDIR', 'base_prefix', 'libdir', 'bindir'], | ||
70 | 'aktualizr-native') | ||
71 | sysroot = bb_vars['SYSROOT_DESTDIR'] + bb_vars['base_prefix'] | ||
72 | sysrootbin = bb_vars['SYSROOT_DESTDIR'] + bb_vars['bindir'] | ||
73 | libdir = bb_vars['libdir'] | ||
74 | |||
75 | program, *_ = cmd.split(' ') | ||
76 | p = '{}/{}'.format(sysrootbin, program) | ||
77 | testInst.assertTrue(os.path.isfile(p), msg="No {} found ({})".format(program, p)) | ||
78 | env = dict(os.environ) | ||
79 | env['LD_LIBRARY_PATH'] = libdir | ||
80 | result = runCmd(cmd, env=env, native_sysroot=sysroot, ignore_status=True, **kwargs) | ||
81 | testInst.assertEqual(result.status, 0, "Status not equal to 0. output: %s" % result.output) | ||
82 | |||
83 | |||
84 | def verifyNotProvisioned(testInst, machine): | ||
85 | print('Checking output of aktualizr-info:') | ||
86 | ran_ok = False | ||
87 | for delay in [5, 5, 5, 5, 10, 10, 10, 10]: | ||
88 | stdout, stderr, retcode = testInst.qemu_command('aktualizr-info') | ||
89 | if retcode == 0 and stderr == b'': | ||
90 | ran_ok = True | ||
91 | break | ||
92 | sleep(delay) | ||
93 | testInst.assertTrue(ran_ok, 'aktualizr-info failed: ' + stderr.decode() + stdout.decode()) | ||
94 | |||
95 | # Verify that device has NOT yet provisioned. | ||
96 | testInst.assertIn(b'Couldn\'t load device ID', stdout, | ||
97 | 'Device already provisioned!? ' + stderr.decode() + stdout.decode()) | ||
98 | testInst.assertIn(b'Couldn\'t load ECU serials', stdout, | ||
99 | 'Device already provisioned!? ' + stderr.decode() + stdout.decode()) | ||
100 | testInst.assertIn(b'Provisioned on server: no', stdout, | ||
101 | 'Device already provisioned!? ' + stderr.decode() + stdout.decode()) | ||
102 | testInst.assertIn(b'Fetched metadata: no', stdout, | ||
103 | 'Device already provisioned!? ' + stderr.decode() + stdout.decode()) | ||
104 | |||
105 | |||
106 | def verifyProvisioned(testInst, machine): | ||
107 | # Verify that device HAS provisioned. | ||
108 | ran_ok = False | ||
109 | for delay in [5, 5, 5, 5, 10, 10, 10, 10]: | ||
110 | stdout, stderr, retcode = testInst.qemu_command('aktualizr-info') | ||
111 | if retcode == 0 and stderr == b'' and stdout.decode().find('Fetched metadata: yes') >= 0: | ||
112 | ran_ok = True | ||
113 | break | ||
114 | sleep(delay) | ||
115 | testInst.assertTrue(ran_ok, 'aktualizr-info failed: ' + stderr.decode() + stdout.decode()) | ||
116 | |||
117 | testInst.assertIn(b'Device ID: ', stdout, 'Provisioning failed: ' + stderr.decode() + stdout.decode()) | ||
118 | testInst.assertIn(b'Primary ecu hardware ID: ' + machine.encode(), stdout, | ||
119 | 'Provisioning failed: ' + stderr.decode() + stdout.decode()) | ||
120 | testInst.assertIn(b'Fetched metadata: yes', stdout, 'Provisioning failed: ' + stderr.decode() + stdout.decode()) | ||
121 | p = re.compile(r'Device ID: ([a-z0-9-]*)\n') | ||
122 | m = p.search(stdout.decode()) | ||
123 | testInst.assertTrue(m, 'Device ID could not be read: ' + stderr.decode() + stdout.decode()) | ||
124 | testInst.assertGreater(m.lastindex, 0, 'Device ID could not be read: ' + stderr.decode() + stdout.decode()) | ||
125 | logger = logging.getLogger("selftest") | ||
126 | logger.info('Device successfully provisioned with ID: ' + m.group(1)) | ||
127 | |||
128 | # vim:set ts=4 sw=4 sts=4 expandtab: | ||