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
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)
62 @with_aktualizr(start=
False, output_logs=
False)
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''' 68 secondary_image_filename =
"secondary_image_filename.img" 69 secondary_image_hash = uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
71 logger.debug(
"Trying to update ECU {} with the image {}".
72 format(secondary.id, (secondary_image_hash, secondary_image_filename)))
75 with secondary, aktualizr:
76 aktualizr.wait_for_completion()
78 if not director.get_install_result():
79 logger.error(
"Installation result is not successful")
83 if secondary_image_hash != aktualizr.get_current_image_info(secondary.id):
84 logger.error(
"Target image hash doesn't match the currently installed hash")
88 update_file = path.join(secondary.storage_dir.name,
"firmware.txt")
89 if not path.exists(update_file):
90 logger.error(
"Expected updated file does not exist: {}".format(update_file))
93 if secondary_image_filename != director.get_ecu_manifest_filepath(secondary.id[1]):
94 logger.error(
"Target name doesn't match a filepath value of the reported manifest: {}".format(director.get_manifest()))
101 @with_uptane_backend()
104 @with_secondary(start=
False, output_logs=
True)
105 @with_aktualizr(start=
False, run_mode=
'once', output_logs=
True)
106 def test_secondary_ostree_update(uptane_repo, secondary, aktualizr, treehub, sysroot, director, **kwargs):
107 """Test Secondary OSTree update if a boot order of Secondary and Primary is undefined""" 109 target_rev = treehub.revision
110 expected_targetname = uptane_repo.add_ostree_target(secondary.id, target_rev,
"GARAGE_TARGET_NAME")
114 aktualizr.wait_for_completion()
116 pending_rev = aktualizr.get_current_pending_image_info(secondary.id)
118 if pending_rev != target_rev:
119 logger.error(
"Pending version {} != the target one {}".format(pending_rev, target_rev))
122 sysroot.update_revision(pending_rev)
123 secondary.emulate_reboot()
125 aktualizr.set_mode(
'full')
128 director.wait_for_install()
130 if not director.get_install_result():
131 logger.error(
"Installation result is not successful")
134 installed_rev = aktualizr.get_current_image_info(secondary.id)
136 if installed_rev != target_rev:
137 logger.error(
"Installed version {} != the target one {}".format(installed_rev, target_rev))
140 if expected_targetname != director.get_ecu_manifest_filepath(secondary.id[1]):
142 "Target name doesn't match a filepath value of the reported manifest: expected: {}, actual: {}".
143 format(expected_targetname, director.get_ecu_manifest_filepath(secondary.id[1])))
150 @with_uptane_backend()
153 @with_secondary(start=
False, output_logs=
False, force_reboot=
True)
154 @with_aktualizr(start=
False, run_mode=
'once', output_logs=
True)
155 def test_secondary_ostree_reboot(uptane_repo, secondary, aktualizr, treehub, sysroot, director, **kwargs):
156 target_rev = treehub.revision
157 uptane_repo.add_ostree_target(secondary.id, target_rev,
"GARAGE_TARGET_NAME")
161 aktualizr.wait_for_completion()
162 secondary.wait_for_completion()
164 pending_rev = aktualizr.get_current_pending_image_info(secondary.id)
166 if pending_rev != target_rev:
167 logger.error(
"Pending version {} != the target one {}".format(pending_rev, target_rev))
170 sysroot.update_revision(pending_rev)
172 aktualizr.set_mode(
'full')
175 director.wait_for_install()
177 if not director.get_install_result():
178 logger.error(
"Installation result is not successful")
181 installed_rev = aktualizr.get_current_image_info(secondary.id)
183 if installed_rev != target_rev:
184 logger.error(
"Installed version {} != the target one {}".format(installed_rev, target_rev))
190 @with_uptane_backend()
192 @with_secondary(start=
False)
193 @with_aktualizr(start=
False, secondary_wait_sec=1, output_logs=
False)
194 def test_secondary_install_timeout(uptane_repo, secondary, aktualizr, director, **kwargs):
195 '''Test that secondary install fails after a timeout if the secondary never connects''' 198 with aktualizr, secondary:
199 aktualizr.wait_for_completion()
202 if not aktualizr.is_ecu_registered(secondary.id):
206 if secondary.is_running():
210 secondary_image_filename =
"secondary_image_filename_001.img" 211 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
213 aktualizr.update_wait_timeout(0.1)
215 aktualizr.wait_for_completion()
217 manifest = director.get_manifest()
218 result_code = manifest[
"signed"][
"installation_report"][
"report"][
"result"][
"code"]
219 if result_code !=
"INTERNAL_ERROR":
220 logger.error(
"Wrong result code {}".format(result_code))
223 return not director.get_install_result()
227 @with_uptane_backend()
228 @with_secondary(start=
False)
229 @with_aktualizr(start=
False, output_logs=
False, wait_timeout=0.1)
230 def test_primary_timeout_during_first_run(uptane_repo, secondary, aktualizr, **kwargs):
231 """Test Aktualizr's timeout of waiting for Secondaries during initial boot""" 233 secondary_image_filename =
"secondary_image_filename_001.img" 234 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
236 logger.debug(
"Checking Aktualizr behaviour if it timeouts while waiting for a connection from the secondary")
241 aktualizr.wait_for_completion()
243 info = aktualizr.get_info()
246 not_provisioned =
'Provisioned on server: no' in info
248 return not_provisioned
and not aktualizr.is_ecu_registered(secondary.id)
251 @with_uptane_backend()
253 @with_secondary(start=
False)
254 @with_aktualizr(start=
False, output_logs=
True)
255 def test_primary_wait_secondary_install(uptane_repo, secondary, aktualizr, director, **kwargs):
256 """Test that Primary waits for Secondary to connect before installing""" 259 with secondary, aktualizr:
260 aktualizr.wait_for_completion()
262 secondary_image_filename =
"secondary_image_filename.img" 263 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
268 aktualizr.wait_for_completion()
270 if not director.get_install_result():
271 logger.error(
"Installation result is not successful")
277 @with_uptane_backend()
278 @with_secondary(start=
False, output_logs=
False)
279 @with_aktualizr(start=
False, output_logs=
False)
280 def test_primary_timeout_after_device_is_registered(uptane_repo, secondary, aktualizr, **kwargs):
281 '''Test Aktualizr's timeout of waiting for Secondaries after the device/aktualizr was registered at the backend''' 284 with aktualizr, secondary:
285 aktualizr.wait_for_completion()
288 if not aktualizr.is_ecu_registered(secondary.id):
292 if secondary.is_running():
297 primary_image_filename =
"primary_image_filename_001.img" 298 primary_image_hash = uptane_repo.add_image(id=aktualizr.id, image_filename=primary_image_filename)
306 aktualizr.update_wait_timeout(0.1)
308 aktualizr.wait_for_completion()
310 return aktualizr.get_current_primary_image_info() == primary_image_hash
313 @with_uptane_backend()
314 @with_secondary(start=
False)
315 @with_secondary(start=
False, arg_name=
'secondary2')
316 @with_aktualizr(start=
False, output_logs=
True)
317 def test_primary_multiple_secondaries(uptane_repo, secondary, secondary2, aktualizr, **kwargs):
318 '''Test Aktualizr with multiple ip secondaries''' 320 with aktualizr, secondary, secondary2:
321 aktualizr.wait_for_completion()
323 if not aktualizr.is_ecu_registered(secondary.id)
or not aktualizr.is_ecu_registered(secondary2.id):
327 secondary_image_filename =
"secondary_image_filename.img" 328 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
329 uptane_repo.add_image(id=secondary2.id, image_filename=secondary_image_filename)
334 aktualizr.wait_for_completion()
336 if not director.get_install_result():
337 logger.error(
"Installation result is not successful")
344 if __name__ ==
'__main__':
345 logging.basicConfig(level=logging.INFO)
347 parser = argparse.ArgumentParser(description=
'Test IP Secondary')
348 parser.add_argument(
'-b',
'--build-dir', help=
'build directory', default=
'build')
349 parser.add_argument(
'-s',
'--src-dir', help=
'source directory', default=
'.')
350 parser.add_argument(
'-o',
'--ostree', help=
'OSTree support', action=
'store_true')
352 input_params = parser.parse_args()
354 KeyStore.base_dir = path.abspath(input_params.src_dir)
355 initial_cwd = getcwd()
356 chdir(input_params.build_dir)
359 test_secondary_update,
360 test_secondary_update_if_secondary_starts_first,
361 test_secondary_update_if_primary_starts_first,
362 test_secondary_install_timeout,
363 test_primary_timeout_during_first_run,
364 test_primary_timeout_after_device_is_registered,
365 test_primary_multiple_secondaries,
368 if input_params.ostree:
370 test_secondary_ostree_update,
371 test_secondary_ostree_reboot,
374 with TestRunner(test_suite)
as runner:
375 test_suite_run_result = runner.run()
378 exit(0
if test_suite_run_result
else 1)