summaryrefslogtreecommitdiffstats
path: root/lib/oeqa/selftest/cases/updater.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/oeqa/selftest/cases/updater.py')
-rw-r--r--lib/oeqa/selftest/cases/updater.py605
1 files changed, 525 insertions, 80 deletions
diff --git a/lib/oeqa/selftest/cases/updater.py b/lib/oeqa/selftest/cases/updater.py
index 91ac9fc..e459ffb 100644
--- a/lib/oeqa/selftest/cases/updater.py
+++ b/lib/oeqa/selftest/cases/updater.py
@@ -1,6 +1,7 @@
1# pylint: disable=C0111,C0325 1# pylint: disable=C0111,C0325
2import os 2import os
3import logging 3import logging
4import re
4import subprocess 5import subprocess
5import unittest 6import unittest
6from time import sleep 7from time import sleep
@@ -20,32 +21,13 @@ class SotaToolsTests(OESelftestTestCase):
20 bitbake('aktualizr-native') 21 bitbake('aktualizr-native')
21 22
22 def test_push_help(self): 23 def test_push_help(self):
23 bb_vars = get_bb_vars(['SYSROOT_DESTDIR', 'bindir'], 'aktualizr-native') 24 akt_native_run(self, 'garage-push --help')
24 p = bb_vars['SYSROOT_DESTDIR'] + bb_vars['bindir'] + "/" + "garage-push"
25 self.assertTrue(os.path.isfile(p), msg = "No garage-push found (%s)" % p)
26 result = runCmd('%s --help' % p, ignore_status=True)
27 self.assertEqual(result.status, 0, "Status not equal to 0. output: %s" % result.output)
28 25
29 def test_deploy_help(self): 26 def test_deploy_help(self):
30 bb_vars = get_bb_vars(['SYSROOT_DESTDIR', 'bindir'], 'aktualizr-native') 27 akt_native_run(self, 'garage-deploy --help')
31 p = bb_vars['SYSROOT_DESTDIR'] + bb_vars['bindir'] + "/" + "garage-deploy"
32 self.assertTrue(os.path.isfile(p), msg = "No garage-deploy found (%s)" % p)
33 result = runCmd('%s --help' % p, ignore_status=True)
34 self.assertEqual(result.status, 0, "Status not equal to 0. output: %s" % result.output)
35 28
36 def test_garagesign_help(self): 29 def test_garagesign_help(self):
37 bb_vars = get_bb_vars(['SYSROOT_DESTDIR', 'bindir'], 'aktualizr-native') 30 akt_native_run(self, 'garage-sign --help')
38 p = bb_vars['SYSROOT_DESTDIR'] + bb_vars['bindir'] + "/" + "garage-sign"
39 self.assertTrue(os.path.isfile(p), msg = "No garage-sign found (%s)" % p)
40 result = runCmd('%s --help' % p, ignore_status=True)
41 self.assertEqual(result.status, 0, "Status not equal to 0. output: %s" % result.output)
42
43
44class HsmTests(OESelftestTestCase):
45
46 def test_hsm(self):
47 self.write_config('SOTA_CLIENT_FEATURES="hsm"')
48 bitbake('core-image-minimal')
49 31
50 32
51class GeneralTests(OESelftestTestCase): 33class GeneralTests(OESelftestTestCase):
@@ -59,6 +41,9 @@ class GeneralTests(OESelftestTestCase):
59 self.assertNotEqual(result, -1, 'Feature "systemd" not set at DISTRO_FEATURES') 41 self.assertNotEqual(result, -1, 'Feature "systemd" not set at DISTRO_FEATURES')
60 42
61 def test_credentials(self): 43 def test_credentials(self):
44 logger = logging.getLogger("selftest")
45 logger.info('Running bitbake to build core-image-minimal')
46 self.append_config('SOTA_CLIENT_PROV = "aktualizr-auto-prov"')
62 bitbake('core-image-minimal') 47 bitbake('core-image-minimal')
63 credentials = get_bb_var('SOTA_PACKED_CREDENTIALS') 48 credentials = get_bb_var('SOTA_PACKED_CREDENTIALS')
64 # skip the test if the variable SOTA_PACKED_CREDENTIALS is not set 49 # skip the test if the variable SOTA_PACKED_CREDENTIALS is not set
@@ -75,17 +60,17 @@ class GeneralTests(OESelftestTestCase):
75 60
76 def test_java(self): 61 def test_java(self):
77 result = runCmd('which java', ignore_status=True) 62 result = runCmd('which java', ignore_status=True)
78 self.assertEqual(result.status, 0, "Java not found.") 63 self.assertEqual(result.status, 0,
64 "Java not found. Do you have a JDK installed on your host machine?")
79 65
80 def test_add_package(self): 66 def test_add_package(self):
81 print('')
82 deploydir = get_bb_var('DEPLOY_DIR_IMAGE') 67 deploydir = get_bb_var('DEPLOY_DIR_IMAGE')
83 imagename = get_bb_var('IMAGE_LINK_NAME', 'core-image-minimal') 68 imagename = get_bb_var('IMAGE_LINK_NAME', 'core-image-minimal')
84 image_path = deploydir + '/' + imagename + '.otaimg' 69 image_path = deploydir + '/' + imagename + '.otaimg'
85 logger = logging.getLogger("selftest") 70 logger = logging.getLogger("selftest")
86 71
87 logger.info('Running bitbake with man in the image package list') 72 logger.info('Running bitbake with man in the image package list')
88 self.write_config('IMAGE_INSTALL_append = " man "') 73 self.append_config('IMAGE_INSTALL_append = " man "')
89 bitbake('-c cleanall man') 74 bitbake('-c cleanall man')
90 bitbake('core-image-minimal') 75 bitbake('core-image-minimal')
91 result = runCmd('oe-pkgdata-util find-path /usr/bin/man') 76 result = runCmd('oe-pkgdata-util find-path /usr/bin/man')
@@ -95,7 +80,7 @@ class GeneralTests(OESelftestTestCase):
95 logger.info('First image %s has size %i' % (path1, size1)) 80 logger.info('First image %s has size %i' % (path1, size1))
96 81
97 logger.info('Running bitbake without man in the image package list') 82 logger.info('Running bitbake without man in the image package list')
98 self.write_config('IMAGE_INSTALL_remove = " man "') 83 self.append_config('IMAGE_INSTALL_remove = " man "')
99 bitbake('-c cleanall man') 84 bitbake('-c cleanall man')
100 bitbake('core-image-minimal') 85 bitbake('core-image-minimal')
101 result = runCmd('oe-pkgdata-util find-path /usr/bin/man', ignore_status=True) 86 result = runCmd('oe-pkgdata-util find-path /usr/bin/man', ignore_status=True)
@@ -108,98 +93,512 @@ class GeneralTests(OESelftestTestCase):
108 self.assertNotEqual(size1, size2, "Image sizes are identical; image was not rebuilt.") 93 self.assertNotEqual(size1, size2, "Image sizes are identical; image was not rebuilt.")
109 94
110 95
111class QemuTests(OESelftestTestCase): 96class AktualizrToolsTests(OESelftestTestCase):
112 97
113 @classmethod 98 @classmethod
114 def setUpClass(cls): 99 def setUpClass(cls):
115 super(QemuTests, cls).setUpClass() 100 super(AktualizrToolsTests, cls).setUpClass()
116 cls.qemu, cls.s = qemu_launch(machine='qemux86-64') 101 logger = logging.getLogger("selftest")
102 logger.info('Running bitbake to build aktualizr-native tools')
103 bitbake('aktualizr-native')
117 104
118 @classmethod 105 def test_implicit_writer_help(self):
119 def tearDownClass(cls): 106 akt_native_run(self, 'aktualizr_implicit_writer --help')
120 qemu_terminate(cls.s) 107
108 def test_cert_provider_help(self):
109 akt_native_run(self, 'aktualizr_cert_provider --help')
110
111 def test_cert_provider_local_output(self):
112 logger = logging.getLogger("selftest")
113 logger.info('Running bitbake to build aktualizr-implicit-prov')
114 bitbake('aktualizr-implicit-prov')
115 bb_vars = get_bb_vars(['SOTA_PACKED_CREDENTIALS', 'T'], 'aktualizr-native')
116 creds = bb_vars['SOTA_PACKED_CREDENTIALS']
117 temp_dir = bb_vars['T']
118 bb_vars_prov = get_bb_vars(['STAGING_DIR_NATIVE', 'libdir'], 'aktualizr-implicit-prov')
119 config = bb_vars_prov['STAGING_DIR_NATIVE'] + bb_vars_prov['libdir'] + '/sota/sota_implicit_prov.toml'
120
121 akt_native_run(self, 'aktualizr_cert_provider -c {creds} -r -l {temp} -g {config}'
122 .format(creds=creds, temp=temp_dir, config=config))
123
124 # Might be nice if these names weren't hardcoded.
125 cert_path = temp_dir + '/client.pem'
126 self.assertTrue(os.path.isfile(cert_path), "Client certificate not found at %s." % cert_path)
127 self.assertTrue(os.path.getsize(cert_path) > 0, "Client certificate at %s is empty." % cert_path)
128 pkey_path = temp_dir + '/pkey.pem'
129 self.assertTrue(os.path.isfile(pkey_path), "Private key not found at %s." % pkey_path)
130 self.assertTrue(os.path.getsize(pkey_path) > 0, "Private key at %s is empty." % pkey_path)
131 ca_path = temp_dir + '/root.crt'
132 self.assertTrue(os.path.isfile(ca_path), "Client certificate not found at %s." % ca_path)
133 self.assertTrue(os.path.getsize(ca_path) > 0, "Client certificate at %s is empty." % ca_path)
134
135
136class AutoProvTests(OESelftestTestCase):
137
138 def setUpLocal(self):
139 layer = "meta-updater-qemux86-64"
140 result = runCmd('bitbake-layers show-layers')
141 if re.search(layer, result.output) is None:
142 # Assume the directory layout for finding other layers. We could also
143 # make assumptions by using 'show-layers', but either way, if the
144 # layers we need aren't where we expect them, we are out of like.
145 path = os.path.abspath(os.path.dirname(__file__))
146 metadir = path + "/../../../../../"
147 self.meta_qemu = metadir + layer
148 runCmd('bitbake-layers add-layer "%s"' % self.meta_qemu)
149 else:
150 self.meta_qemu = None
151 self.append_config('MACHINE = "qemux86-64"')
152 self.append_config('SOTA_CLIENT_PROV = " aktualizr-auto-prov "')
153 self.qemu, self.s = qemu_launch(machine='qemux86-64')
154
155 def tearDownLocal(self):
156 qemu_terminate(self.s)
157 if self.meta_qemu:
158 runCmd('bitbake-layers remove-layer "%s"' % self.meta_qemu, ignore_status=True)
121 159
122 def run_command(self, command): 160 def qemu_command(self, command):
123 return qemu_send_command(self.qemu.ssh_port, command) 161 return qemu_send_command(self.qemu.ssh_port, command)
124 162
125 def test_hostname(self): 163 def test_provisioning(self):
126 print('')
127 print('Checking machine name (hostname) of device:') 164 print('Checking machine name (hostname) of device:')
128 stdout, stderr, retcode = self.run_command('hostname') 165 stdout, stderr, retcode = self.qemu_command('hostname')
166 self.assertEqual(retcode, 0, "Unable to check hostname. " +
167 "Is an ssh daemon (such as dropbear or openssh) installed on the device?")
129 machine = get_bb_var('MACHINE', 'core-image-minimal') 168 machine = get_bb_var('MACHINE', 'core-image-minimal')
130 self.assertEqual(stderr, b'', 'Error: ' + stderr.decode()) 169 self.assertEqual(stderr, b'', 'Error: ' + stderr.decode())
131 # Strip off line ending. 170 # Strip off line ending.
132 value_str = stdout.decode()[:-1] 171 value = stdout.decode()[:-1]
133 self.assertEqual(value_str, machine, 172 self.assertEqual(value, machine,
134 'MACHINE does not match hostname: ' + machine + ', ' + value_str) 173 'MACHINE does not match hostname: ' + machine + ', ' + value)
135 print(value_str) 174 print(value)
136
137 def test_var_sota(self):
138 print('')
139 print('Checking contents of /var/sota:')
140 stdout, stderr, retcode = self.run_command('ls /var/sota')
141 self.assertEqual(stderr, b'', 'Error: ' + stderr.decode())
142 self.assertEqual(retcode, 0)
143 print(stdout.decode())
144
145 def test_aktualizr_info(self):
146 print('Checking output of aktualizr-info:') 175 print('Checking output of aktualizr-info:')
147 ran_ok = False 176 ran_ok = False
148 for delay in [0, 1, 2, 5, 10, 15]: 177 for delay in [0, 1, 2, 5, 10, 15]:
149 sleep(delay) 178 sleep(delay)
150 try: 179 stdout, stderr, retcode = self.qemu_command('aktualizr-info')
151 stdout, stderr, retcode = self.run_command('aktualizr-info') 180 if retcode == 0 and stderr == b'':
152 if retcode == 0 and stderr == b'': 181 ran_ok = True
153 ran_ok = True 182 break
154 break 183 self.assertTrue(ran_ok, 'aktualizr-info failed: ' + stderr.decode() + stdout.decode())
155 except IOError as e: 184
156 print(e) 185 verifyProvisioned(self, machine)
157 if not ran_ok: 186
158 print(stdout.decode()) 187
159 print(stderr.decode()) 188class RpiTests(OESelftestTestCase):
189
190 def setUpLocal(self):
191 # Add layers before changing the machine type, otherwise the sanity
192 # checker complains loudly.
193 layer_python = "meta-openembedded/meta-python"
194 layer_rpi = "meta-raspberrypi"
195 layer_upd_rpi = "meta-updater-raspberrypi"
196 result = runCmd('bitbake-layers show-layers')
197 # Assume the directory layout for finding other layers. We could also
198 # make assumptions by using 'show-layers', but either way, if the
199 # layers we need aren't where we expect them, we are out of like.
200 path = os.path.abspath(os.path.dirname(__file__))
201 metadir = path + "/../../../../../"
202 if re.search(layer_python, result.output) is None:
203 self.meta_python = metadir + layer_python
204 runCmd('bitbake-layers add-layer "%s"' % self.meta_python)
205 else:
206 self.meta_python = None
207 if re.search(layer_rpi, result.output) is None:
208 self.meta_rpi = metadir + layer_rpi
209 runCmd('bitbake-layers add-layer "%s"' % self.meta_rpi)
210 else:
211 self.meta_rpi = None
212 if re.search(layer_upd_rpi, result.output) is None:
213 self.meta_upd_rpi = metadir + layer_upd_rpi
214 runCmd('bitbake-layers add-layer "%s"' % self.meta_upd_rpi)
215 else:
216 self.meta_upd_rpi = None
217
218 # This is trickier that I would've thought. The fundamental problem is
219 # that the qemu layer changes the u-boot file extension to .rom, but
220 # raspberrypi still expects .bin. To prevent this, the qemu layer must
221 # be temporarily removed if it is present. It has to be removed by name
222 # without the complete path, but to add it back when we are done, we
223 # need the full path.
224 p = re.compile(r'meta-updater-qemux86-64\s*(\S*meta-updater-qemux86-64)\s')
225 m = p.search(result.output)
226 if m and m.lastindex > 0:
227 self.meta_qemu = m.group(1)
228 runCmd('bitbake-layers remove-layer meta-updater-qemux86-64')
229 else:
230 self.meta_qemu = None
231
232 self.append_config('MACHINE = "raspberrypi3"')
233 self.append_config('SOTA_CLIENT_PROV = " aktualizr-auto-prov "')
234
235 def tearDownLocal(self):
236 if self.meta_qemu:
237 runCmd('bitbake-layers add-layer "%s"' % self.meta_qemu, ignore_status=True)
238 if self.meta_upd_rpi:
239 runCmd('bitbake-layers remove-layer "%s"' % self.meta_upd_rpi, ignore_status=True)
240 if self.meta_rpi:
241 runCmd('bitbake-layers remove-layer "%s"' % self.meta_rpi, ignore_status=True)
242 if self.meta_python:
243 runCmd('bitbake-layers remove-layer "%s"' % self.meta_python, ignore_status=True)
244
245 def test_rpi(self):
246 logger = logging.getLogger("selftest")
247 logger.info('Running bitbake to build rpi-basic-image')
248 self.append_config('SOTA_CLIENT_PROV = "aktualizr-auto-prov"')
249 bitbake('rpi-basic-image')
250 credentials = get_bb_var('SOTA_PACKED_CREDENTIALS')
251 # Skip the test if the variable SOTA_PACKED_CREDENTIALS is not set.
252 if credentials is None:
253 raise unittest.SkipTest("Variable 'SOTA_PACKED_CREDENTIALS' not set.")
254 # Check if the file exists.
255 self.assertTrue(os.path.isfile(credentials), "File %s does not exist" % credentials)
256 deploydir = get_bb_var('DEPLOY_DIR_IMAGE')
257 imagename = get_bb_var('IMAGE_LINK_NAME', 'rpi-basic-image')
258 # Check if the credentials are included in the output image.
259 result = runCmd('tar -jtvf %s/%s.tar.bz2 | grep sota_provisioning_credentials.zip' %
260 (deploydir, imagename), ignore_status=True)
261 self.assertEqual(result.status, 0, "Status not equal to 0. output: %s" % result.output)
160 262
161 263
162class GrubTests(OESelftestTestCase): 264class GrubTests(OESelftestTestCase):
163 265
164 def setUpLocal(self): 266 def setUpLocal(self):
165 # This is a bit of a hack but I can't see a better option. 267 layer_intel = "meta-intel"
268 layer_minnow = "meta-updater-minnowboard"
269 result = runCmd('bitbake-layers show-layers')
270 # Assume the directory layout for finding other layers. We could also
271 # make assumptions by using 'show-layers', but either way, if the
272 # layers we need aren't where we expect them, we are out of like.
166 path = os.path.abspath(os.path.dirname(__file__)) 273 path = os.path.abspath(os.path.dirname(__file__))
167 metadir = path + "/../../../../../" 274 metadir = path + "/../../../../../"
168 grub_config = 'OSTREE_BOOTLOADER = "grub"\nMACHINE = "intel-corei7-64"' 275 if re.search(layer_intel, result.output) is None:
169 self.append_config(grub_config) 276 self.meta_intel = metadir + layer_intel
170 self.meta_intel = metadir + "meta-intel" 277 runCmd('bitbake-layers add-layer "%s"' % self.meta_intel)
171 self.meta_minnow = metadir + "meta-updater-minnowboard" 278 else:
172 runCmd('bitbake-layers add-layer "%s"' % self.meta_intel) 279 self.meta_intel = None
173 runCmd('bitbake-layers add-layer "%s"' % self.meta_minnow) 280 if re.search(layer_minnow, result.output) is None:
281 self.meta_minnow = metadir + layer_minnow
282 runCmd('bitbake-layers add-layer "%s"' % self.meta_minnow)
283 else:
284 self.meta_minnow = None
285 self.append_config('MACHINE = "intel-corei7-64"')
286 self.append_config('OSTREE_BOOTLOADER = "grub"')
287 self.append_config('SOTA_CLIENT_PROV = " aktualizr-auto-prov "')
174 self.qemu, self.s = qemu_launch(efi=True, machine='intel-corei7-64') 288 self.qemu, self.s = qemu_launch(efi=True, machine='intel-corei7-64')
175 289
176 def tearDownLocal(self): 290 def tearDownLocal(self):
177 qemu_terminate(self.s) 291 qemu_terminate(self.s)
178 runCmd('bitbake-layers remove-layer "%s"' % self.meta_intel, ignore_status=True) 292 if self.meta_intel:
179 runCmd('bitbake-layers remove-layer "%s"' % self.meta_minnow, ignore_status=True) 293 runCmd('bitbake-layers remove-layer "%s"' % self.meta_intel, ignore_status=True)
294 if self.meta_minnow:
295 runCmd('bitbake-layers remove-layer "%s"' % self.meta_minnow, ignore_status=True)
296
297 def qemu_command(self, command):
298 return qemu_send_command(self.qemu.ssh_port, command)
180 299
181 def test_grub(self): 300 def test_grub(self):
182 print('')
183 print('Checking machine name (hostname) of device:') 301 print('Checking machine name (hostname) of device:')
184 value, err, retcode = qemu_send_command(self.qemu.ssh_port, 'hostname') 302 stdout, stderr, retcode = self.qemu_command('hostname')
303 self.assertEqual(retcode, 0, "Unable to check hostname. " +
304 "Is an ssh daemon (such as dropbear or openssh) installed on the device?")
305 machine = get_bb_var('MACHINE', 'core-image-minimal')
306 self.assertEqual(stderr, b'', 'Error: ' + stderr.decode())
307 # Strip off line ending.
308 value = stdout.decode()[:-1]
309 self.assertEqual(value, machine,
310 'MACHINE does not match hostname: ' + machine + ', ' + value +
311 '\nIs TianoCore ovmf installed on your host machine?')
312 print(value)
313 print('Checking output of aktualizr-info:')
314 ran_ok = False
315 for delay in [0, 1, 2, 5, 10, 15]:
316 sleep(delay)
317 stdout, stderr, retcode = self.qemu_command('aktualizr-info')
318 if retcode == 0 and stderr == b'':
319 ran_ok = True
320 break
321 self.assertTrue(ran_ok, 'aktualizr-info failed: ' + stderr.decode() + stdout.decode())
322
323 verifyProvisioned(self, machine)
324
325
326class ImplProvTests(OESelftestTestCase):
327
328 def setUpLocal(self):
329 layer = "meta-updater-qemux86-64"
330 result = runCmd('bitbake-layers show-layers')
331 if re.search(layer, result.output) is None:
332 # Assume the directory layout for finding other layers. We could also
333 # make assumptions by using 'show-layers', but either way, if the
334 # layers we need aren't where we expect them, we are out of like.
335 path = os.path.abspath(os.path.dirname(__file__))
336 metadir = path + "/../../../../../"
337 self.meta_qemu = metadir + layer
338 runCmd('bitbake-layers add-layer "%s"' % self.meta_qemu)
339 else:
340 self.meta_qemu = None
341 self.append_config('MACHINE = "qemux86-64"')
342 self.append_config('SOTA_CLIENT_PROV = " aktualizr-implicit-prov "')
343 self.qemu, self.s = qemu_launch(machine='qemux86-64')
344
345 def tearDownLocal(self):
346 qemu_terminate(self.s)
347 if self.meta_qemu:
348 runCmd('bitbake-layers remove-layer "%s"' % self.meta_qemu, ignore_status=True)
349
350 def qemu_command(self, command):
351 return qemu_send_command(self.qemu.ssh_port, command)
352
353 def test_provisioning(self):
354 print('Checking machine name (hostname) of device:')
355 stdout, stderr, retcode = self.qemu_command('hostname')
356 self.assertEqual(retcode, 0, "Unable to check hostname. " +
357 "Is an ssh daemon (such as dropbear or openssh) installed on the device?")
358 machine = get_bb_var('MACHINE', 'core-image-minimal')
359 self.assertEqual(stderr, b'', 'Error: ' + stderr.decode())
360 # Strip off line ending.
361 value = stdout.decode()[:-1]
362 self.assertEqual(value, machine,
363 'MACHINE does not match hostname: ' + machine + ', ' + value)
364 print(value)
365 print('Checking output of aktualizr-info:')
366 ran_ok = False
367 for delay in [0, 1, 2, 5, 10, 15]:
368 stdout, stderr, retcode = self.qemu_command('aktualizr-info')
369 if retcode == 0 and stderr == b'':
370 ran_ok = True
371 break
372 self.assertTrue(ran_ok, 'aktualizr-info failed: ' + stderr.decode() + stdout.decode())
373 # Verify that device has NOT yet provisioned.
374 self.assertIn(b'Couldn\'t load device ID', stdout,
375 'Device already provisioned!? ' + stderr.decode() + stdout.decode())
376 self.assertIn(b'Couldn\'t load ECU serials', stdout,
377 'Device already provisioned!? ' + stderr.decode() + stdout.decode())
378 self.assertIn(b'Provisioned on server: no', stdout,
379 'Device already provisioned!? ' + stderr.decode() + stdout.decode())
380 self.assertIn(b'Fetched metadata: no', stdout,
381 'Device already provisioned!? ' + stderr.decode() + stdout.decode())
382
383 # Run cert_provider.
384 bb_vars = get_bb_vars(['SOTA_PACKED_CREDENTIALS'], 'aktualizr-native')
385 creds = bb_vars['SOTA_PACKED_CREDENTIALS']
386 bb_vars_prov = get_bb_vars(['STAGING_DIR_NATIVE', 'libdir'], 'aktualizr-implicit-prov')
387 config = bb_vars_prov['STAGING_DIR_NATIVE'] + bb_vars_prov['libdir'] + '/sota/sota_implicit_prov.toml'
388
389 akt_native_run(self, 'aktualizr_cert_provider -c {creds} -t root@localhost -p {port} -s -g {config}'
390 .format(creds=creds, port=self.qemu.ssh_port, config=config))
391
392 verifyProvisioned(self, machine)
393
394
395class HsmTests(OESelftestTestCase):
396
397 def setUpLocal(self):
398 layer = "meta-updater-qemux86-64"
399 result = runCmd('bitbake-layers show-layers')
400 if re.search(layer, result.output) is None:
401 # Assume the directory layout for finding other layers. We could also
402 # make assumptions by using 'show-layers', but either way, if the
403 # layers we need aren't where we expect them, we are out of like.
404 path = os.path.abspath(os.path.dirname(__file__))
405 metadir = path + "/../../../../../"
406 self.meta_qemu = metadir + layer
407 runCmd('bitbake-layers add-layer "%s"' % self.meta_qemu)
408 else:
409 self.meta_qemu = None
410 self.append_config('MACHINE = "qemux86-64"')
411 self.append_config('SOTA_CLIENT_PROV = "aktualizr-hsm-prov"')
412 self.append_config('SOTA_CLIENT_FEATURES = "hsm"')
413 self.qemu, self.s = qemu_launch(machine='qemux86-64')
414
415 def tearDownLocal(self):
416 qemu_terminate(self.s)
417 if self.meta_qemu:
418 runCmd('bitbake-layers remove-layer "%s"' % self.meta_qemu, ignore_status=True)
419
420 def qemu_command(self, command):
421 return qemu_send_command(self.qemu.ssh_port, command)
422
423 def test_provisioning(self):
424 print('Checking machine name (hostname) of device:')
425 stdout, stderr, retcode = self.qemu_command('hostname')
426 self.assertEqual(retcode, 0, "Unable to check hostname. " +
427 "Is an ssh daemon (such as dropbear or openssh) installed on the device?")
185 machine = get_bb_var('MACHINE', 'core-image-minimal') 428 machine = get_bb_var('MACHINE', 'core-image-minimal')
186 self.assertEqual(err, b'', 'Error: ' + err.decode()) 429 self.assertEqual(stderr, b'', 'Error: ' + stderr.decode())
187 self.assertEqual(retcode, 0)
188 # Strip off line ending. 430 # Strip off line ending.
189 value_str = value.decode()[:-1] 431 value = stdout.decode()[:-1]
190 self.assertEqual(value_str, machine, 432 self.assertEqual(value, machine,
191 'MACHINE does not match hostname: ' + machine + ', ' + value_str + 433 'MACHINE does not match hostname: ' + machine + ', ' + value +
192 '\nIs tianocore ovmf installed?') 434 '\nIs tianocore ovmf installed?')
193 print(value_str) 435 print(value)
436 print('Checking output of aktualizr-info:')
437 ran_ok = False
438 for delay in [0, 1, 2, 5, 10, 15]:
439 stdout, stderr, retcode = self.qemu_command('aktualizr-info')
440 if retcode == 0 and stderr == b'':
441 ran_ok = True
442 break
443 self.assertTrue(ran_ok, 'aktualizr-info failed: ' + stderr.decode() + stdout.decode())
444 # Verify that device has NOT yet provisioned.
445 self.assertIn(b'Couldn\'t load device ID', stdout,
446 'Device already provisioned!? ' + stderr.decode() + stdout.decode())
447 self.assertIn(b'Couldn\'t load ECU serials', stdout,
448 'Device already provisioned!? ' + stderr.decode() + stdout.decode())
449 self.assertIn(b'Provisioned on server: no', stdout,
450 'Device already provisioned!? ' + stderr.decode() + stdout.decode())
451 self.assertIn(b'Fetched metadata: no', stdout,
452 'Device already provisioned!? ' + stderr.decode() + stdout.decode())
453
454 # Verify that HSM is not yet initialized.
455 pkcs11_command = 'pkcs11-tool --module=/usr/lib/softhsm/libsofthsm2.so -O'
456 stdout, stderr, retcode = self.qemu_command(pkcs11_command)
457 self.assertNotEqual(retcode, 0, 'pkcs11-tool succeeded before initialization: ' +
458 stdout.decode() + stderr.decode())
459 softhsm2_command = 'softhsm2-util --show-slots'
460 stdout, stderr, retcode = self.qemu_command(softhsm2_command)
461 self.assertNotEqual(retcode, 0, 'softhsm2-tool succeeded before initialization: ' +
462 stdout.decode() + stderr.decode())
463
464 # Run cert_provider.
465 bb_vars = get_bb_vars(['SOTA_PACKED_CREDENTIALS'], 'aktualizr-native')
466 creds = bb_vars['SOTA_PACKED_CREDENTIALS']
467 bb_vars_prov = get_bb_vars(['STAGING_DIR_NATIVE', 'libdir'], 'aktualizr-hsm-prov')
468 config = bb_vars_prov['STAGING_DIR_NATIVE'] + bb_vars_prov['libdir'] + '/sota/sota_hsm_prov.toml'
469
470 akt_native_run(self, 'aktualizr_cert_provider -c {creds} -t root@localhost -p {port} -r -s -g {config}'
471 .format(creds=creds, port=self.qemu.ssh_port, config=config))
472
473 # Verify that HSM is able to initialize.
474 ran_ok = False
475 for delay in [5, 5, 5, 5, 10]:
476 sleep(delay)
477 p11_out, p11_err, p11_ret = self.qemu_command(pkcs11_command)
478 hsm_out, hsm_err, hsm_ret = self.qemu_command(softhsm2_command)
479 if p11_ret == 0 and hsm_ret == 0 and hsm_err == b'':
480 ran_ok = True
481 break
482 self.assertTrue(ran_ok, 'pkcs11-tool or softhsm2-tool failed: ' + p11_err.decode() +
483 p11_out.decode() + hsm_err.decode() + hsm_out.decode())
484 self.assertIn(b'present token', p11_err, 'pkcs11-tool failed: ' + p11_err.decode() + p11_out.decode())
485 self.assertIn(b'X.509 cert', p11_out, 'pkcs11-tool failed: ' + p11_err.decode() + p11_out.decode())
486 self.assertIn(b'Initialized: yes', hsm_out, 'softhsm2-tool failed: ' +
487 hsm_err.decode() + hsm_out.decode())
488 self.assertIn(b'User PIN init.: yes', hsm_out, 'softhsm2-tool failed: ' +
489 hsm_err.decode() + hsm_out.decode())
490
491 # Check that pkcs11 output matches sofhsm output.
492 p11_p = re.compile(r'Using slot [0-9] with a present token \((0x[0-9a-f]*)\)\s')
493 p11_m = p11_p.search(p11_err.decode())
494 self.assertTrue(p11_m, 'Slot number not found with pkcs11-tool: ' + p11_err.decode() + p11_out.decode())
495 self.assertGreater(p11_m.lastindex, 0, 'Slot number not found with pkcs11-tool: ' +
496 p11_err.decode() + p11_out.decode())
497 hsm_p = re.compile(r'Description:\s*SoftHSM slot ID (0x[0-9a-f]*)\s')
498 hsm_m = hsm_p.search(hsm_out.decode())
499 self.assertTrue(hsm_m, 'Slot number not found with softhsm2-tool: ' + hsm_err.decode() + hsm_out.decode())
500 self.assertGreater(hsm_m.lastindex, 0, 'Slot number not found with softhsm2-tool: ' +
501 hsm_err.decode() + hsm_out.decode())
502 self.assertEqual(p11_m.group(1), hsm_m.group(1), 'Slot number does not match: ' +
503 p11_err.decode() + p11_out.decode() + hsm_err.decode() + hsm_out.decode())
504
505 verifyProvisioned(self, machine)
506
507class SecondaryTests(OESelftestTestCase):
508 @classmethod
509 def setUpClass(cls):
510 super(SecondaryTests, cls).setUpClass()
511 logger = logging.getLogger("selftest")
512 logger.info('Running bitbake to build secondary-image')
513 bitbake('secondary-image')
194 514
515 def setUpLocal(self):
516 layer = "meta-updater-qemux86-64"
517 result = runCmd('bitbake-layers show-layers')
518 if re.search(layer, result.output) is None:
519 # Assume the directory layout for finding other layers. We could also
520 # make assumptions by using 'show-layers', but either way, if the
521 # layers we need aren't where we expect them, we are out of like.
522 path = os.path.abspath(os.path.dirname(__file__))
523 metadir = path + "/../../../../../"
524 self.meta_qemu = metadir + layer
525 runCmd('bitbake-layers add-layer "%s"' % self.meta_qemu)
526 else:
527 self.meta_qemu = None
528 self.append_config('MACHINE = "qemux86-64"')
529 self.append_config('SOTA_CLIENT_PROV = " aktualizr-auto-prov "')
530 self.qemu, self.s = qemu_launch(machine='qemux86-64', imagename='secondary-image')
531
532 def tearDownLocal(self):
533 qemu_terminate(self.s)
534 if self.meta_qemu:
535 runCmd('bitbake-layers remove-layer "%s"' % self.meta_qemu, ignore_status=True)
536
537 def qemu_command(self, command):
538 return qemu_send_command(self.qemu.ssh_port, command)
539
540 def test_secondary_present(self):
541 print('Checking aktualizr-secondary is present')
542 stdout, stderr, retcode = self.qemu_command('aktualizr-secondary --help')
543 self.assertEqual(retcode, 0, "Unable to run aktualizr-secondary --help")
544 self.assertEqual(stderr, b'', 'Error: ' + stderr.decode())
195 545
196def qemu_launch(efi=False, machine=None): 546 def test_secondary_listening(self):
547 print('Checking aktualizr-secondary service is listening')
548 stdout, stderr, retcode = self.qemu_command('echo test | nc localhost 9030')
549 self.assertEqual(retcode, 0, "Unable to connect to secondary")
550
551
552class PrimaryTests(OESelftestTestCase):
553 @classmethod
554 def setUpClass(cls):
555 super(PrimaryTests, cls).setUpClass()
556 logger = logging.getLogger("selftest")
557 logger.info('Running bitbake to build primary-image')
558 bitbake('primary-image')
559
560 def setUpLocal(self):
561 layer = "meta-updater-qemux86-64"
562 result = runCmd('bitbake-layers show-layers')
563 if re.search(layer, result.output) is None:
564 # Assume the directory layout for finding other layers. We could also
565 # make assumptions by using 'show-layers', but either way, if the
566 # layers we need aren't where we expect them, we are out of like.
567 path = os.path.abspath(os.path.dirname(__file__))
568 metadir = path + "/../../../../../"
569 self.meta_qemu = metadir + layer
570 runCmd('bitbake-layers add-layer "%s"' % self.meta_qemu)
571 else:
572 self.meta_qemu = None
573 self.append_config('MACHINE = "qemux86-64"')
574 self.append_config('SOTA_CLIENT_PROV = " aktualizr-auto-prov "')
575 self.append_config('SOTA_CLIENT_FEATURES = "secondary-network"')
576 self.qemu, self.s = qemu_launch(machine='qemux86-64', imagename='primary-image')
577
578 def tearDownLocal(self):
579 qemu_terminate(self.s)
580 if self.meta_qemu:
581 runCmd('bitbake-layers remove-layer "%s"' % self.meta_qemu, ignore_status=True)
582
583 def qemu_command(self, command):
584 return qemu_send_command(self.qemu.ssh_port, command)
585
586 def test_aktualizr_present(self):
587 print('Checking aktualizr is present')
588 stdout, stderr, retcode = self.qemu_command('aktualizr --help')
589 self.assertEqual(retcode, 0, "Unable to run aktualizr --help")
590 self.assertEqual(stderr, b'', 'Error: ' + stderr.decode())
591
592def qemu_launch(efi=False, machine=None, imagename=None):
197 logger = logging.getLogger("selftest") 593 logger = logging.getLogger("selftest")
198 logger.info('Running bitbake to build core-image-minimal') 594 logger.info('Running bitbake to build core-image-minimal')
199 bitbake('core-image-minimal') 595 bitbake('core-image-minimal')
200 # Create empty object. 596 # Create empty object.
201 args = type('', (), {})() 597 args = type('', (), {})()
202 args.imagename = 'core-image-minimal' 598 if imagename:
599 args.imagename = imagename
600 else:
601 args.imagename = 'core-image-minimal'
203 args.mac = None 602 args.mac = None
204 # Could use DEPLOY_DIR_IMAGE here but it's already in the machine 603 # Could use DEPLOY_DIR_IMAGE here but it's already in the machine
205 # subdirectory. 604 # subdirectory.
@@ -212,6 +611,7 @@ def qemu_launch(efi=False, machine=None):
212 args.pcap = None 611 args.pcap = None
213 args.overlay = None 612 args.overlay = None
214 args.dry_run = False 613 args.dry_run = False
614 args.secondary_network = False
215 615
216 qemu = QemuCommand(args) 616 qemu = QemuCommand(args)
217 cmdline = qemu.command_line() 617 cmdline = qemu.command_line()
@@ -220,17 +620,62 @@ def qemu_launch(efi=False, machine=None):
220 sleep(10) 620 sleep(10)
221 return qemu, s 621 return qemu, s
222 622
623
223def qemu_terminate(s): 624def qemu_terminate(s):
224 try: 625 try:
225 s.terminate() 626 s.terminate()
226 except KeyboardInterrupt: 627 except KeyboardInterrupt:
227 pass 628 pass
228 629
630
229def qemu_send_command(port, command): 631def qemu_send_command(port, command):
230 command = ['ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@localhost -p ' + 632 command = ['ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@localhost -p ' +
231 str(port) + ' "' + command + '"'] 633 str(port) + ' "' + command + '"']
232 s2 = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 634 s2 = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
233 stdout, stderr = s2.communicate() 635 stdout, stderr = s2.communicate(timeout=60)
234 return stdout, stderr, s2.returncode 636 return stdout, stderr, s2.returncode
235 637
638
639def akt_native_run(testInst, cmd, **kwargs):
640 # run a command supplied by aktualizr-native and checks that:
641 # - the executable exists
642 # - the command runs without error
643 # NOTE: the base test class must have built aktualizr-native (in
644 # setUpClass, for example)
645 bb_vars = get_bb_vars(['SYSROOT_DESTDIR', 'base_prefix', 'libdir', 'bindir'],
646 'aktualizr-native')
647 sysroot = bb_vars['SYSROOT_DESTDIR'] + bb_vars['base_prefix']
648 sysrootbin = bb_vars['SYSROOT_DESTDIR'] + bb_vars['bindir']
649 libdir = bb_vars['libdir']
650
651 program, *_ = cmd.split(' ')
652 p = '{}/{}'.format(sysrootbin, program)
653 testInst.assertTrue(os.path.isfile(p), msg="No {} found ({})".format(program, p))
654 env = dict(os.environ)
655 env['LD_LIBRARY_PATH'] = libdir
656 result = runCmd(cmd, env=env, native_sysroot=sysroot, ignore_status=True, **kwargs)
657 testInst.assertEqual(result.status, 0, "Status not equal to 0. output: %s" % result.output)
658
659
660def verifyProvisioned(testInst, machine):
661 # Verify that device HAS provisioned.
662 ran_ok = False
663 for delay in [5, 5, 5, 5, 10]:
664 sleep(delay)
665 stdout, stderr, retcode = testInst.qemu_command('aktualizr-info')
666 if retcode == 0 and stderr == b'' and stdout.decode().find('Fetched metadata: yes') >= 0:
667 ran_ok = True
668 break
669 testInst.assertIn(b'Device ID: ', stdout, 'Provisioning failed: ' + stderr.decode() + stdout.decode())
670 testInst.assertIn(b'Primary ecu hardware ID: ' + machine.encode(), stdout,
671 'Provisioning failed: ' + stderr.decode() + stdout.decode())
672 testInst.assertIn(b'Fetched metadata: yes', stdout, 'Provisioning failed: ' + stderr.decode() + stdout.decode())
673 p = re.compile(r'Device ID: ([a-z0-9-]*)\n')
674 m = p.search(stdout.decode())
675 testInst.assertTrue(m, 'Device ID could not be read: ' + stderr.decode() + stdout.decode())
676 testInst.assertGreater(m.lastindex, 0, 'Device ID could not be read: ' + stderr.decode() + stdout.decode())
677 logger = logging.getLogger("selftest")
678 logger.info('Device successfully provisioned with ID: ' + m.group(1))
679
680
236# vim:set ts=4 sw=4 sts=4 expandtab: 681# vim:set ts=4 sw=4 sts=4 expandtab: