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)
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()
127 aktualizr.wait_for_completion()
129 if not director.get_install_result():
130 logger.error(
"Installation result is not successful")
133 installed_rev = aktualizr.get_current_image_info(secondary.id)
135 if installed_rev != target_rev:
136 logger.error(
"Installed version {} != the target one {}".format(installed_rev, target_rev))
139 if expected_targetname != director.get_ecu_manifest_filepath(secondary.id[1]):
141 "Target name doesn't match a filepath value of the reported manifest: expected: {}, actual: {}".
142 format(expected_targetname, director.get_ecu_manifest_filepath(secondary.id[1])))
148 @with_uptane_backend()
150 @with_secondary(start=
False)
151 @with_aktualizr(start=
False, secondary_wait_sec=1, output_logs=
False)
152 def test_secondary_install_timeout(uptane_repo, secondary, aktualizr, director, **kwargs):
153 '''Test that secondary install fails after a timeout if the secondary never connects'''
156 with aktualizr, secondary:
157 aktualizr.wait_for_completion()
160 if not aktualizr.is_ecu_registered(secondary.id):
164 if secondary.is_running():
168 secondary_image_filename =
"secondary_image_filename_001.img"
169 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
171 aktualizr.update_wait_timeout(0.1)
173 aktualizr.wait_for_completion()
175 manifest = director.get_manifest()
176 result_code = manifest[
"signed"][
"installation_report"][
"report"][
"result"][
"code"]
177 if result_code !=
"INTERNAL_ERROR":
178 logger.error(
"Wrong result code {}".format(result_code))
181 return not director.get_install_result()
185 @with_uptane_backend()
186 @with_secondary(start=
False)
187 @with_aktualizr(start=
False, output_logs=
False, wait_timeout=0.1)
188 def test_primary_timeout_during_first_run(uptane_repo, secondary, aktualizr, **kwargs):
189 """Test Aktualizr's timeout of waiting for Secondaries during initial boot"""
191 secondary_image_filename =
"secondary_image_filename_001.img"
192 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
194 logger.debug(
"Checking Aktualizr behaviour if it timeouts while waiting for a connection from the secondary")
199 aktualizr.wait_for_completion()
201 info = aktualizr.get_info()
204 not_provisioned =
'Provisioned on server: no' in info
206 return not_provisioned
and not aktualizr.is_ecu_registered(secondary.id)
209 @with_uptane_backend()
211 @with_secondary(start=
False)
212 @with_aktualizr(start=
False, output_logs=
True)
213 def test_primary_wait_secondary_install(uptane_repo, secondary, aktualizr, director, **kwargs):
214 """Test that Primary waits for Secondary to connect before installing"""
217 with secondary, aktualizr:
218 aktualizr.wait_for_completion()
220 secondary_image_filename =
"secondary_image_filename.img"
221 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
226 aktualizr.wait_for_completion()
228 if not director.get_install_result():
229 logger.error(
"Installation result is not successful")
235 @with_uptane_backend()
236 @with_secondary(start=
False, output_logs=
False)
237 @with_aktualizr(start=
False, output_logs=
False)
238 def test_primary_timeout_after_device_is_registered(uptane_repo, secondary, aktualizr, **kwargs):
239 '''Test Aktualizr's timeout of waiting for Secondaries after the device/aktualizr was registered at the backend'''
242 with aktualizr, secondary:
243 aktualizr.wait_for_completion()
246 if not aktualizr.is_ecu_registered(secondary.id):
250 if secondary.is_running():
255 primary_image_filename =
"primary_image_filename_001.img"
256 primary_image_hash = uptane_repo.add_image(id=aktualizr.id, image_filename=primary_image_filename)
264 aktualizr.update_wait_timeout(0.1)
266 aktualizr.wait_for_completion()
268 return aktualizr.get_current_primary_image_info() == primary_image_hash
271 @with_uptane_backend()
272 @with_secondary(start=
False)
273 @with_secondary(start=
False, arg_name=
'secondary2')
274 @with_aktualizr(start=
False, output_logs=
True)
275 def test_primary_multiple_secondaries(uptane_repo, secondary, secondary2, aktualizr, **kwargs):
276 '''Test Aktualizr with multiple ip secondaries'''
278 with aktualizr, secondary, secondary2:
279 aktualizr.wait_for_completion()
281 if not aktualizr.is_ecu_registered(secondary.id)
or not aktualizr.is_ecu_registered(secondary2.id):
285 secondary_image_filename =
"secondary_image_filename.img"
286 uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
287 uptane_repo.add_image(id=secondary2.id, image_filename=secondary_image_filename)
292 aktualizr.wait_for_completion()
294 if not director.get_install_result():
295 logger.error(
"Installation result is not successful")
302 if __name__ ==
'__main__':
303 logging.basicConfig(level=logging.INFO)
305 parser = argparse.ArgumentParser(description=
'Test IP Secondary')
306 parser.add_argument(
'-b',
'--build-dir', help=
'build directory', default=
'build')
307 parser.add_argument(
'-s',
'--src-dir', help=
'source directory', default=
'.')
308 parser.add_argument(
'-o',
'--ostree', help=
'ostree support', default=
'OFF')
310 input_params = parser.parse_args()
312 KeyStore.base_dir = path.abspath(input_params.src_dir)
313 initial_cwd = getcwd()
314 chdir(input_params.build_dir)
317 test_secondary_update,
318 test_secondary_update_if_secondary_starts_first,
319 test_secondary_update_if_primary_starts_first,
320 test_secondary_install_timeout,
321 test_primary_timeout_during_first_run,
322 test_primary_timeout_after_device_is_registered,
323 test_primary_multiple_secondaries,
326 if input_params.ostree ==
'ON':
327 test_suite.append(test_secondary_ostree_update)
329 test_suite_run_result = TestRunner(test_suite).run()
332 exit(0
if test_suite_run_result
else 1)