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()
59 @with_secondary(start=
False)
60 @with_aktualizr(start=
False, output_logs=
False)
61 def test_secondary_update(uptane_repo, secondary, aktualizr, **kwargs):
62 '''Test Secondary update if a boot order of Secondary and Primary is undefined'''
67 while ii < number_of_updates
and test_result:
69 secondary_image_filename =
"secondary_image_filename_{}.img".format(ii)
70 secondary_image_hash = uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
72 logger.debug(
"Trying to update ECU {} with the image {}".
73 format(secondary.id, (secondary_image_hash, secondary_image_filename)))
76 with secondary, aktualizr:
77 aktualizr.wait_for_completion()
79 test_result = secondary_image_hash == aktualizr.get_current_image_info(secondary.id)
80 logger.debug(
"Update result: {}".format(
"success" if test_result
else "failed"))
86 @with_uptane_backend()
89 @with_secondary(start=
False)
90 @with_aktualizr(start=
False, run_mode=
'once', output_logs=
True)
91 def test_secondary_ostree_update(uptane_repo, secondary, aktualizr, treehub, sysroot, director, **kwargs):
92 """Test Secondary ostree update if a boot order of Secondary and Primary is undefined"""
95 target_rev = treehub.revision
96 uptane_repo.add_ostree_target(secondary.id, target_rev)
100 aktualizr.wait_for_completion()
102 pending_rev = aktualizr.get_current_pending_image_info(secondary.id)
104 if pending_rev != target_rev:
105 logger.error(
"Pending version {} != the target one {}".format(pending_rev, target_rev))
108 sysroot.update_revision(pending_rev)
109 secondary.emulate_reboot()
113 aktualizr.wait_for_completion()
115 installed_rev = aktualizr.get_current_image_info(secondary.id)
117 if installed_rev != target_rev:
118 logger.error(
"Installed version {} != the target one {}".format(installed_rev, target_rev))
121 return director.get_install_result()
124 @with_uptane_backend()
125 @with_secondary(start=
False)
126 @with_aktualizr(start=
False, output_logs=
False, wait_timeout=0.1)
127 def test_primary_timeout_during_first_run(uptane_repo, secondary, aktualizr, **kwargs):
128 """Test Aktualizr's timeout of waiting for Secondaries during initial boot"""
130 secondary_image_filename =
"secondary_image_filename_001.img"
131 secondary_image_hash = uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
133 logger.debug(
"Checking Aktualizr behaviour if it timeouts while waiting for a connection from the secondary")
138 aktualizr.wait_for_completion()
140 info = aktualizr.get_info()
143 not_provisioned =
'Provisioned on server: no' in info
145 return not_provisioned
and not aktualizr.is_ecu_registered(secondary.id)
148 @with_uptane_backend()
149 @with_secondary(start=
False)
150 @with_aktualizr(start=
False, output_logs=
False)
151 def test_primary_timeout_after_device_is_registered(uptane_repo, secondary, aktualizr, **kwargs):
152 '''Test Aktualizr's timeout of waiting for Secondaries after the device/aktualizr was registered at the backend'''
155 with aktualizr, secondary:
156 aktualizr.wait_for_completion()
159 if not aktualizr.is_ecu_registered(secondary.id):
163 if secondary.is_running():
168 primary_image_filename =
"primary_image_filename_001.img"
169 primary_image_hash = uptane_repo.add_image(id=aktualizr.id, image_filename=primary_image_filename)
177 aktualizr.update_wait_timeout(0.1)
179 aktualizr.wait_for_completion()
181 return (aktualizr.get_current_primary_image_info() == primary_image_hash)\
182 and not aktualizr.is_ecu_registered(secondary.id)
186 if __name__ ==
'__main__':
187 logging.basicConfig(level=logging.INFO)
189 parser = argparse.ArgumentParser(description=
'Test IP Secondary')
190 parser.add_argument(
'-b',
'--build-dir', help=
'build directory', default=
'build')
191 parser.add_argument(
'-s',
'--src-dir', help=
'source directory', default=
'.')
192 parser.add_argument(
'-o',
'--ostree', help=
'ostree support', default=
'OFF')
194 input_params = parser.parse_args()
196 KeyStore.base_dir = path.abspath(input_params.src_dir)
197 initial_cwd = getcwd()
198 chdir(input_params.build_dir)
201 test_secondary_update,
202 test_secondary_update_if_secondary_starts_first,
203 test_secondary_update_if_primary_starts_first,
204 test_primary_timeout_during_first_run,
205 test_primary_timeout_after_device_is_registered
208 if input_params.ostree ==
'ON':
209 test_suite.append(test_secondary_ostree_update)
211 test_suite_run_result =
True
212 for test
in test_suite:
213 logger.info(
'>>> Running {}...'.format(test.__name__))
214 test_run_result = test()
215 logger.info(
'>>> {}: {}\n'.format(
'OK' if test_run_result
else 'FAILED', test.__name__))
216 test_suite_run_result = test_suite_run_result
and test_run_result
219 exit(0
if test_suite_run_result
else 1)