7 from os
import getcwd, chdir, path
9 from test_fixtures
import with_aktualizr, with_uptane_backend, KeyStore, with_secondary, with_treehub,\
10 with_sysroot, with_director, TestRunner, IPSecondary
12 logger = logging.getLogger(
"IPSecondaryTest")
16 @with_uptane_backend()
17 @with_secondary(start=
True)
18 @with_aktualizr(start=
False, output_logs=
False)
19 def test_secondary_update_if_secondary_starts_first(uptane_repo, secondary, aktualizr, **kwargs):
20 '''Test Secondary update if Secondary is booted before Primary''' 23 secondary_image_filename =
"secondary_image_filename_001.img" 24 secondary_image_hash = uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
26 logger.debug(
"Trying to update ECU {} with the image {}".
27 format(secondary.id, (secondary_image_hash, secondary_image_filename)))
31 aktualizr.wait_for_completion()
33 test_result = secondary_image_hash == aktualizr.get_current_image_info(secondary.id)
34 logger.debug(
"Update result: {}".format(
"success" if test_result
else "failed"))
38 @with_uptane_backend()
39 @with_secondary(start=
False)
40 @with_aktualizr(start=
True, output_logs=
False)
41 def test_secondary_update_if_primary_starts_first(uptane_repo, secondary, aktualizr, **kwargs):
42 '''Test Secondary update if Secondary is booted after Primary''' 45 secondary_image_filename =
"secondary_image_filename_001.img" 46 secondary_image_hash = uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
48 logger.debug(
"Trying to update ECU {} with the image {}".
49 format(secondary.id, (secondary_image_hash, secondary_image_filename)))
52 aktualizr.wait_for_completion()
54 test_result = secondary_image_hash == aktualizr.get_current_image_info(secondary.id)
55 logger.debug(
"Update result: {}".format(
"success" if test_result
else "failed"))
59 @with_uptane_backend()
61 @with_secondary(start=
False, output_logs=
True)
62 @with_aktualizr(start=
False, output_logs=
True)
63 def test_secondary_update(uptane_repo, secondary, aktualizr, director, **kwargs):
64 '''Test Secondary update if a boot order of Secondary and Primary is undefined''' 67 secondary_image_filename =
"secondary_image_filename.img" 68 secondary_image_hash = uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
70 logger.debug(
"Trying to update ECU {} with the image {}".
71 format(secondary.id, (secondary_image_hash, secondary_image_filename)))
74 with secondary, aktualizr:
75 aktualizr.wait_for_completion()
77 if not director.get_install_result():
78 logger.error(
"Installation result is not successful")
82 if secondary_image_hash != aktualizr.get_current_image_info(secondary.id):
83 logger.error(
"Target image hash doesn't match the currently installed hash")
87 update_file = path.join(secondary.storage_dir.name,
"firmware.txt")
88 if not path.exists(update_file):
89 logger.error(
"Expected updated file does not exist: {}".format(update_file))
92 if secondary_image_filename != director.get_ecu_manifest_filepath(secondary.id[1]):
93 logger.error(
"Target name doesn't match a filepath value of the reported manifest: {}".format(director.get_manifest()))
99 @with_uptane_backend()
100 @with_secondary(start=
True, id=(
'hwid1',
'serial1'), output_logs=
False)
101 @with_aktualizr(start=
False, output_logs=
True)
102 def test_add_secondary(uptane_repo, secondary, aktualizr, **kwargs):
103 '''Test adding a Secondary after registration''' 106 aktualizr.wait_for_completion()
108 if not aktualizr.is_ecu_registered(secondary.id):
109 logger.error(
"Secondary ECU is not registered.")
112 with IPSecondary(output_logs=
False, id=(
'hwid1',
'serial2'))
as secondary2:
115 aktualizr.add_secondary(secondary2)
117 aktualizr.wait_for_completion()
119 if not aktualizr.is_ecu_registered(secondary.id)
or not aktualizr.is_ecu_registered(secondary2.id):
120 logger.error(
"Secondary ECU is not registered.")
126 @with_uptane_backend()
127 @with_secondary(start=
True, id=(
'hwid1',
'serial1'), output_logs=
False)
128 @with_secondary(start=
True, id=(
'hwid1',
'serial2'), output_logs=
False, arg_name=
'secondary2')
129 @with_aktualizr(start=
False, output_logs=
True)
130 def test_remove_secondary(uptane_repo, secondary, secondary2, aktualizr, **kwargs):
131 '''Test removing a Secondary after registration''' 134 aktualizr.wait_for_completion()
136 if not aktualizr.is_ecu_registered(secondary.id)
or not aktualizr.is_ecu_registered(secondary2.id):
137 logger.error(
"Secondary ECU is not registered.")
140 aktualizr.remove_secondary(secondary2)
142 aktualizr.wait_for_completion()
144 if not aktualizr.is_ecu_registered(secondary.id):
145 logger.error(
"Secondary ECU is not registered.")
147 if aktualizr.is_ecu_registered(secondary2.id):
148 logger.error(
"Secondary ECU is unexpectedly still registered.")
154 @with_uptane_backend()
155 @with_secondary(start=
True, id=(
'hwid1',
'serial1'), output_logs=
False)
156 @with_secondary(start=
True, id=(
'hwid1',
'serial2'), output_logs=
False, arg_name=
'secondary2')
157 @with_aktualizr(start=
False, output_logs=
True)
158 def test_replace_secondary(uptane_repo, secondary, secondary2, aktualizr, **kwargs):
159 '''Test replacing a Secondary after registration''' 162 aktualizr.wait_for_completion()
164 if not aktualizr.is_ecu_registered(secondary.id)
or not aktualizr.is_ecu_registered(secondary2.id):
165 logger.error(
"Secondary ECU is not registered.")
168 aktualizr.remove_secondary(secondary2)
170 with IPSecondary(output_logs=
False, id=(
'hwid1',
'serial3'))
as secondary3:
173 aktualizr.add_secondary(secondary3)
175 aktualizr.wait_for_completion()
177 if not aktualizr.is_ecu_registered(secondary.id)
or not aktualizr.is_ecu_registered(secondary3.id):
178 logger.error(
"Secondary ECU is not registered.")
180 if aktualizr.is_ecu_registered(secondary2.id):
181 logger.error(
"Secondary ECU is unexpectedly still registered.")
187 @with_uptane_backend()
188 @with_secondary(start=
True, id=(
'hwid1',
'serial1'), output_logs=
False)
189 @with_aktualizr(start=
False, output_logs=
True)
190 def test_replace_secondary_same_port(uptane_repo, secondary, aktualizr, **kwargs):
191 '''Test replacing a Secondary that reuses the same port''' 193 port = IPSecondary.get_free_port()
194 with IPSecondary(output_logs=
False, id=(
'hwid1',
'serial2'), port=port)
as secondary2:
197 aktualizr.add_secondary(secondary2)
199 aktualizr.wait_for_completion()
201 if not aktualizr.is_ecu_registered(secondary.id)
or not aktualizr.is_ecu_registered(secondary2.id):
202 logger.error(
"Secondary ECU is not registered.")
205 aktualizr.remove_secondary(secondary2)
207 with IPSecondary(output_logs=
False, id=(
'hwid1',
'serial3'), port=port)
as secondary3:
210 aktualizr.add_secondary(secondary3)
212 aktualizr.wait_for_completion()
214 if not aktualizr.is_ecu_registered(secondary.id)
or not aktualizr.is_ecu_registered(secondary3.id):
215 logger.error(
"Secondary ECU is not registered.")
217 if aktualizr.is_ecu_registered(secondary2.id):
218 logger.error(
"Secondary ECU is unexpectedly still registered.")
224 @with_uptane_backend()
225 @with_secondary(start=
True, id=(
'hwid1',
'serial1'), output_logs=
False)
226 @with_aktualizr(start=
False, output_logs=
True)
227 def test_change_secondary_port(uptane_repo, secondary, aktualizr, **kwargs):
228 '''Test changing a Secondary's port but not the ECU serial''' 230 with IPSecondary(output_logs=
False, id=(
'hwid1',
'serial2'))
as secondary2:
233 aktualizr.add_secondary(secondary2)
235 aktualizr.wait_for_completion()
237 if not aktualizr.is_ecu_registered(secondary.id)
or not aktualizr.is_ecu_registered(secondary2.id):
238 logger.error(
"Secondary ECU is not registered.")
241 aktualizr.remove_secondary(secondary2)
243 with IPSecondary(output_logs=
False, id=(
'hwid1',
'serial2'))
as secondary3:
246 aktualizr.add_secondary(secondary3)
248 aktualizr.wait_for_completion()
250 if secondary2.port == secondary3.port:
251 logger.error(
"Secondary ECU port unexpectedly did not change!")
254 if not aktualizr.is_ecu_registered(secondary.id)
or not aktualizr.is_ecu_registered(secondary3.id):
255 logger.error(
"Secondary ECU is not registered.")
262 @with_uptane_backend()
265 @with_secondary(start=
False, output_logs=
True)
266 @with_aktualizr(start=
False, run_mode=
'once', output_logs=
True)
267 def test_secondary_ostree_update(uptane_repo, secondary, aktualizr, treehub, sysroot, director, **kwargs):
268 """Test Secondary OSTree update if a boot order of Secondary and Primary is undefined""" 270 target_rev = treehub.revision
271 expected_targetname = uptane_repo.add_ostree_target(secondary.id, target_rev,
"GARAGE_TARGET_NAME")
275 aktualizr.wait_for_completion()
277 pending_rev = aktualizr.get_current_pending_image_info(secondary.id)
279 if pending_rev != target_rev:
280 logger.error(
"Pending version {} != the target one {}".format(pending_rev, target_rev))
283 sysroot.update_revision(pending_rev)
284 secondary.emulate_reboot()
286 aktualizr.set_mode(
'full')
289 director.wait_for_install()
291 if not director.get_install_result():
292 logger.error(
"Installation result is not successful")
295 installed_rev = aktualizr.get_current_image_info(secondary.id)
297 if installed_rev != target_rev:
298 logger.error(
"Installed version {} != the target one {}".format(installed_rev, target_rev))
301 if expected_targetname != director.get_ecu_manifest_filepath(secondary.id[1]):
303 "Target name doesn't match a filepath value of the reported manifest: expected: {}, actual: {}".
304 format(expected_targetname, director.get_ecu_manifest_filepath(secondary.id[1])))
311 @with_uptane_backend()
314 @with_secondary(start=
False, output_logs=
False, force_reboot=
True)
315 @with_aktualizr(start=
False, run_mode=
'once', output_logs=
True)
316 def test_secondary_ostree_reboot(uptane_repo, secondary, aktualizr, treehub, sysroot, director, **kwargs):
317 target_rev = treehub.revision
318 uptane_repo.add_ostree_target(secondary.id, target_rev,
"GARAGE_TARGET_NAME")
322 aktualizr.wait_for_completion()
323 secondary.wait_for_completion()
325 pending_rev = aktualizr.get_current_pending_image_info(secondary.id)
327 if pending_rev != target_rev:
328 logger.error(
"Pending version {} != the target one {}".format(pending_rev, target_rev))
331 sysroot.update_revision(pending_rev)
333 aktualizr.set_mode(
'full')
338 director.wait_for_install()
340 if not director.get_install_result():
341 logger.error(
"Installation result is not successful")
344 installed_rev = aktualizr.get_current_image_info(secondary.id)
346 if installed_rev != target_rev:
347 logger.error(
"Installed version {} != the target one {}".format(installed_rev, target_rev))
353 @with_uptane_backend()
355 @with_secondary(start=
False)
356 @with_aktualizr(start=
False, secondary_wait_sec=1, output_logs=
False)
357 def test_secondary_install_timeout(uptane_repo, secondary, aktualizr, director, **kwargs):
358 '''Test that secondary install fails after a timeout if the secondary never connects''' 361 with aktualizr, secondary:
362 aktualizr.wait_for_completion()
365 if not aktualizr.is_ecu_registered(secondary.id):
369 if secondary.is_running():
373 secondary_image_filename =
"secondary_image_filename_001.img" 374 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
376 aktualizr.update_wait_timeout(0.1)
378 aktualizr.wait_for_completion()
380 manifest = director.get_manifest()
381 result_code = manifest[
"signed"][
"installation_report"][
"report"][
"result"][
"code"]
382 if result_code !=
"INTERNAL_ERROR":
383 logger.error(
"Wrong result code {}".format(result_code))
386 return not director.get_install_result()
389 @with_uptane_backend()
390 @with_secondary(start=
False)
391 @with_aktualizr(start=
False, output_logs=
False, wait_timeout=0.1)
392 def test_primary_timeout_during_first_run(uptane_repo, secondary, aktualizr, **kwargs):
393 """Test Aktualizr's timeout of waiting for Secondaries during initial boot""" 395 secondary_image_filename =
"secondary_image_filename_001.img" 396 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
398 logger.debug(
"Checking Aktualizr behaviour if it timeouts while waiting for a connection from the secondary")
403 aktualizr.wait_for_completion()
405 info = aktualizr.get_info()
408 not_provisioned =
'Provisioned on server: no' in info
410 return not_provisioned
and not aktualizr.is_ecu_registered(secondary.id)
413 @with_uptane_backend()
415 @with_secondary(start=
False)
416 @with_aktualizr(start=
False, output_logs=
True)
417 def test_primary_wait_secondary_install(uptane_repo, secondary, aktualizr, director, **kwargs):
418 """Test that Primary waits for Secondary to connect before installing""" 421 with secondary, aktualizr:
422 aktualizr.wait_for_completion()
424 secondary_image_filename =
"secondary_image_filename.img" 425 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
430 aktualizr.wait_for_completion()
432 if not director.get_install_result():
433 logger.error(
"Installation result is not successful")
439 @with_uptane_backend()
440 @with_secondary(start=
False, output_logs=
False)
441 @with_aktualizr(start=
False, output_logs=
False)
442 def test_primary_timeout_after_device_is_registered(uptane_repo, secondary, aktualizr, **kwargs):
443 '''Test Aktualizr's timeout of waiting for Secondaries after the device/aktualizr was registered at the backend''' 446 with aktualizr, secondary:
447 aktualizr.wait_for_completion()
450 if not aktualizr.is_ecu_registered(secondary.id):
454 if secondary.is_running():
459 primary_image_filename =
"primary_image_filename_001.img" 460 primary_image_hash = uptane_repo.add_image(id=aktualizr.id, image_filename=primary_image_filename)
468 aktualizr.update_wait_timeout(0.1)
470 aktualizr.wait_for_completion()
472 return aktualizr.get_current_primary_image_info() == primary_image_hash
475 @with_uptane_backend()
476 @with_secondary(start=
False)
477 @with_secondary(start=
False, arg_name=
'secondary2')
478 @with_aktualizr(start=
False, output_logs=
True)
479 def test_primary_multiple_secondaries(uptane_repo, secondary, secondary2, aktualizr, **kwargs):
480 '''Test Aktualizr with multiple IP secondaries''' 482 with aktualizr, secondary, secondary2:
483 aktualizr.wait_for_completion()
485 if not aktualizr.is_ecu_registered(secondary.id)
or not aktualizr.is_ecu_registered(secondary2.id):
489 secondary_image_filename =
"secondary_image_filename.img" 490 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
491 uptane_repo.add_image(id=secondary2.id, image_filename=secondary_image_filename)
496 aktualizr.wait_for_completion()
498 if not director.get_install_result():
499 logger.error(
"Installation result is not successful")
506 if __name__ ==
'__main__':
507 logging.basicConfig(level=logging.INFO)
509 parser = argparse.ArgumentParser(description=
'Test IP Secondary')
510 parser.add_argument(
'-b',
'--build-dir', help=
'build directory', default=
'build')
511 parser.add_argument(
'-s',
'--src-dir', help=
'source directory', default=
'.')
512 parser.add_argument(
'-o',
'--ostree', help=
'OSTree support', action=
'store_true')
514 input_params = parser.parse_args()
516 KeyStore.base_dir = path.abspath(input_params.src_dir)
517 initial_cwd = getcwd()
518 chdir(input_params.build_dir)
521 test_secondary_update,
522 test_secondary_update_if_secondary_starts_first,
523 test_secondary_update_if_primary_starts_first,
525 test_remove_secondary,
526 test_replace_secondary,
527 test_replace_secondary_same_port,
528 test_change_secondary_port,
529 test_secondary_install_timeout,
530 test_primary_timeout_during_first_run,
531 test_primary_timeout_after_device_is_registered,
532 test_primary_multiple_secondaries,
535 if input_params.ostree:
537 test_secondary_ostree_update,
538 test_secondary_ostree_reboot,
541 with TestRunner(test_suite)
as runner:
542 test_suite_run_result = runner.run()
545 exit(0
if test_suite_run_result
else 1)