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')
336 director.wait_for_install()
338 if not director.get_install_result():
339 logger.error(
"Installation result is not successful")
342 installed_rev = aktualizr.get_current_image_info(secondary.id)
344 if installed_rev != target_rev:
345 logger.error(
"Installed version {} != the target one {}".format(installed_rev, target_rev))
351 @with_uptane_backend()
353 @with_secondary(start=
False)
354 @with_aktualizr(start=
False, secondary_wait_sec=1, output_logs=
False)
355 def test_secondary_install_timeout(uptane_repo, secondary, aktualizr, director, **kwargs):
356 '''Test that secondary install fails after a timeout if the secondary never connects'''
359 with aktualizr, secondary:
360 aktualizr.wait_for_completion()
363 if not aktualizr.is_ecu_registered(secondary.id):
367 if secondary.is_running():
371 secondary_image_filename =
"secondary_image_filename_001.img"
372 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
374 aktualizr.update_wait_timeout(0.1)
376 aktualizr.wait_for_completion()
378 manifest = director.get_manifest()
379 result_code = manifest[
"signed"][
"installation_report"][
"report"][
"result"][
"code"]
380 if result_code !=
"INTERNAL_ERROR":
381 logger.error(
"Wrong result code {}".format(result_code))
384 return not director.get_install_result()
387 @with_uptane_backend()
388 @with_secondary(start=
False)
389 @with_aktualizr(start=
False, output_logs=
False, wait_timeout=0.1)
390 def test_primary_timeout_during_first_run(uptane_repo, secondary, aktualizr, **kwargs):
391 """Test Aktualizr's timeout of waiting for Secondaries during initial boot"""
393 secondary_image_filename =
"secondary_image_filename_001.img"
394 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
396 logger.debug(
"Checking Aktualizr behaviour if it timeouts while waiting for a connection from the secondary")
401 aktualizr.wait_for_completion()
403 info = aktualizr.get_info()
406 not_provisioned =
'Provisioned on server: no' in info
408 return not_provisioned
and not aktualizr.is_ecu_registered(secondary.id)
411 @with_uptane_backend()
413 @with_secondary(start=
False)
414 @with_aktualizr(start=
False, output_logs=
True)
415 def test_primary_wait_secondary_install(uptane_repo, secondary, aktualizr, director, **kwargs):
416 """Test that Primary waits for Secondary to connect before installing"""
419 with secondary, aktualizr:
420 aktualizr.wait_for_completion()
422 secondary_image_filename =
"secondary_image_filename.img"
423 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
428 aktualizr.wait_for_completion()
430 if not director.get_install_result():
431 logger.error(
"Installation result is not successful")
437 @with_uptane_backend()
438 @with_secondary(start=
False, output_logs=
False)
439 @with_aktualizr(start=
False, output_logs=
False)
440 def test_primary_timeout_after_device_is_registered(uptane_repo, secondary, aktualizr, **kwargs):
441 '''Test Aktualizr's timeout of waiting for Secondaries after the device/aktualizr was registered at the backend'''
444 with aktualizr, secondary:
445 aktualizr.wait_for_completion()
448 if not aktualizr.is_ecu_registered(secondary.id):
452 if secondary.is_running():
457 primary_image_filename =
"primary_image_filename_001.img"
458 primary_image_hash = uptane_repo.add_image(id=aktualizr.id, image_filename=primary_image_filename)
466 aktualizr.update_wait_timeout(0.1)
468 aktualizr.wait_for_completion()
470 return aktualizr.get_current_primary_image_info() == primary_image_hash
473 @with_uptane_backend()
474 @with_secondary(start=
False)
475 @with_secondary(start=
False, arg_name=
'secondary2')
476 @with_aktualizr(start=
False, output_logs=
True)
477 def test_primary_multiple_secondaries(uptane_repo, secondary, secondary2, aktualizr, **kwargs):
478 '''Test Aktualizr with multiple IP secondaries'''
480 with aktualizr, secondary, secondary2:
481 aktualizr.wait_for_completion()
483 if not aktualizr.is_ecu_registered(secondary.id)
or not aktualizr.is_ecu_registered(secondary2.id):
487 secondary_image_filename =
"secondary_image_filename.img"
488 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
489 uptane_repo.add_image(id=secondary2.id, image_filename=secondary_image_filename)
494 aktualizr.wait_for_completion()
496 if not director.get_install_result():
497 logger.error(
"Installation result is not successful")
504 if __name__ ==
'__main__':
505 logging.basicConfig(level=logging.INFO)
507 parser = argparse.ArgumentParser(description=
'Test IP Secondary')
508 parser.add_argument(
'-b',
'--build-dir', help=
'build directory', default=
'build')
509 parser.add_argument(
'-s',
'--src-dir', help=
'source directory', default=
'.')
510 parser.add_argument(
'-o',
'--ostree', help=
'OSTree support', action=
'store_true')
512 input_params = parser.parse_args()
514 KeyStore.base_dir = path.abspath(input_params.src_dir)
515 initial_cwd = getcwd()
516 chdir(input_params.build_dir)
519 test_secondary_update,
520 test_secondary_update_if_secondary_starts_first,
521 test_secondary_update_if_primary_starts_first,
523 test_remove_secondary,
524 test_replace_secondary,
525 test_replace_secondary_same_port,
526 test_change_secondary_port,
527 test_secondary_install_timeout,
528 test_primary_timeout_during_first_run,
529 test_primary_timeout_after_device_is_registered,
530 test_primary_multiple_secondaries,
533 if input_params.ostree:
535 test_secondary_ostree_update,
536 test_secondary_ostree_reboot,
539 with TestRunner(test_suite)
as runner:
540 test_suite_run_result = runner.run()
543 exit(0
if test_suite_run_result
else 1)