summaryrefslogtreecommitdiffstats
path: root/lib/oeqa/selftest/cases/testutils.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/oeqa/selftest/cases/testutils.py')
-rw-r--r--lib/oeqa/selftest/cases/testutils.py128
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..2ad99ad
--- /dev/null
+++ b/lib/oeqa/selftest/cases/testutils.py
@@ -0,0 +1,128 @@
1import os
2import logging
3import re
4import subprocess
5from time import sleep
6
7from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars
8from qemucommand import QemuCommand
9
10
11def 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
48def qemu_terminate(s):
49 try:
50 s.terminate()
51 except KeyboardInterrupt:
52 pass
53
54
55def qemu_send_command(port, command, timeout=60):
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=timeout)
60 return stdout, stderr, s2.returncode
61
62
63def 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
84def 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
106def 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: