6 from os
import getcwd, chdir, path
8 from test_fixtures
import with_aktualizr, with_uptane_backend, KeyStore, with_secondary, with_treehub,\
9 with_sysroot, with_director
11 logger = logging.getLogger(
"IPSecondaryTest")
15 @with_uptane_backend()
16 @with_secondary(start=
True)
17 @with_aktualizr(start=
False, output_logs=
False)
18 def test_secondary_update_if_secondary_starts_first(uptane_repo, secondary, aktualizr, **kwargs):
19 '''Test Secondary update if Secondary is booted before Primary'''
22 secondary_image_filename =
"secondary_image_filename_001.img"
23 secondary_image_hash = uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
25 logger.debug(
"Trying to update ECU {} with the image {}".
26 format(secondary.id, (secondary_image_hash, secondary_image_filename)))
30 aktualizr.wait_for_completion()
32 test_result = secondary_image_hash == aktualizr.get_current_image_info(secondary.id)
33 logger.debug(
"Update result: {}".format(
"success" if test_result
else "failed"))
37 @with_uptane_backend()
38 @with_secondary(start=
False)
39 @with_aktualizr(start=
True, output_logs=
False)
40 def test_secondary_update_if_primary_starts_first(uptane_repo, secondary, aktualizr, **kwargs):
41 '''Test Secondary update if Secondary is booted after Primary'''
44 secondary_image_filename =
"secondary_image_filename_001.img"
45 secondary_image_hash = uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
47 logger.debug(
"Trying to update ECU {} with the image {}".
48 format(secondary.id, (secondary_image_hash, secondary_image_filename)))
51 aktualizr.wait_for_completion()
53 test_result = secondary_image_hash == aktualizr.get_current_image_info(secondary.id)
54 logger.debug(
"Update result: {}".format(
"success" if test_result
else "failed"))
58 @with_uptane_backend()
60 @with_secondary(start=
False)
61 @with_aktualizr(start=
False, output_logs=
False)
62 def test_secondary_update(uptane_repo, secondary, aktualizr, director, **kwargs):
63 '''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()))
100 @with_uptane_backend()
103 @with_secondary(start=
False)
104 @with_aktualizr(start=
False, run_mode=
'once', output_logs=
True)
105 def test_secondary_ostree_update(uptane_repo, secondary, aktualizr, treehub, sysroot, director, **kwargs):
106 """Test Secondary ostree update if a boot order of Secondary and Primary is undefined"""
108 target_rev = treehub.revision
109 expected_targetname = uptane_repo.add_ostree_target(secondary.id, target_rev,
"GARAGE_TARGET_NAME")
113 aktualizr.wait_for_completion()
115 pending_rev = aktualizr.get_current_pending_image_info(secondary.id)
117 if pending_rev != target_rev:
118 logger.error(
"Pending version {} != the target one {}".format(pending_rev, target_rev))
121 sysroot.update_revision(pending_rev)
122 secondary.emulate_reboot()
126 aktualizr.wait_for_completion()
128 if not director.get_install_result():
129 logger.error(
"Installation result is not successful")
132 installed_rev = aktualizr.get_current_image_info(secondary.id)
134 if installed_rev != target_rev:
135 logger.error(
"Installed version {} != the target one {}".format(installed_rev, target_rev))
138 if expected_targetname != director.get_ecu_manifest_filepath(secondary.id[1]):
140 "Target name doesn't match a filepath value of the reported manifest: expected: {}, actual: {}".
141 format(expected_targetname, director.get_ecu_manifest_filepath(secondary.id[1])))
147 @with_uptane_backend()
148 @with_secondary(start=
False)
149 @with_aktualizr(start=
False, output_logs=
False, wait_timeout=0.1)
150 def test_primary_timeout_during_first_run(uptane_repo, secondary, aktualizr, **kwargs):
151 """Test Aktualizr's timeout of waiting for Secondaries during initial boot"""
153 secondary_image_filename =
"secondary_image_filename_001.img"
154 secondary_image_hash = uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
156 logger.debug(
"Checking Aktualizr behaviour if it timeouts while waiting for a connection from the secondary")
161 aktualizr.wait_for_completion()
163 info = aktualizr.get_info()
166 not_provisioned =
'Provisioned on server: no' in info
168 return not_provisioned
and not aktualizr.is_ecu_registered(secondary.id)
171 @with_uptane_backend()
172 @with_secondary(start=
False)
173 @with_aktualizr(start=
False, output_logs=
False)
174 def test_primary_timeout_after_device_is_registered(uptane_repo, secondary, aktualizr, **kwargs):
175 '''Test Aktualizr's timeout of waiting for Secondaries after the device/aktualizr was registered at the backend'''
178 with aktualizr, secondary:
179 aktualizr.wait_for_completion()
182 if not aktualizr.is_ecu_registered(secondary.id):
186 if secondary.is_running():
191 primary_image_filename =
"primary_image_filename_001.img"
192 primary_image_hash = uptane_repo.add_image(id=aktualizr.id, image_filename=primary_image_filename)
200 aktualizr.update_wait_timeout(0.1)
202 aktualizr.wait_for_completion()
204 return (aktualizr.get_current_primary_image_info() == primary_image_hash)\
205 and not aktualizr.is_ecu_registered(secondary.id)
209 if __name__ ==
'__main__':
210 logging.basicConfig(level=logging.INFO)
212 parser = argparse.ArgumentParser(description=
'Test IP Secondary')
213 parser.add_argument(
'-b',
'--build-dir', help=
'build directory', default=
'build')
214 parser.add_argument(
'-s',
'--src-dir', help=
'source directory', default=
'.')
215 parser.add_argument(
'-o',
'--ostree', help=
'ostree support', default=
'OFF')
217 input_params = parser.parse_args()
219 KeyStore.base_dir = path.abspath(input_params.src_dir)
220 initial_cwd = getcwd()
221 chdir(input_params.build_dir)
224 test_secondary_update,
225 test_secondary_update_if_secondary_starts_first,
226 test_secondary_update_if_primary_starts_first,
227 test_primary_timeout_during_first_run,
228 test_primary_timeout_after_device_is_registered
231 if input_params.ostree ==
'ON':
232 test_suite.append(test_secondary_ostree_update)
234 test_suite_run_result =
True
235 for test
in test_suite:
236 logger.info(
'>>> Running {}...'.format(test.__name__))
237 test_run_result = test()
238 logger.info(
'>>> {}: {}\n'.format(
'OK' if test_run_result
else 'FAILED', test.__name__))
239 test_suite_run_result = test_suite_run_result
and test_run_result
242 exit(0
if test_suite_run_result
else 1)