Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
ipsecondary_test.py
1 #!/usr/bin/env python3
2 
3 import argparse
4 import logging
5 import time
6 
7 from os import getcwd, chdir, path
8 
9 from test_fixtures import with_aktualizr, with_uptane_backend, KeyStore, with_secondary, with_treehub,\
10  with_sysroot, with_director, TestRunner
11 
12 logger = logging.getLogger("IPSecondaryTest")
13 
14 
15 # The following is a test suit intended for IP Secondary integration testing
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'''
21 
22  # add a new image to the repo in order to update the secondary with it
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)
25 
26  logger.debug("Trying to update ECU {} with the image {}".
27  format(secondary.id, (secondary_image_hash, secondary_image_filename)))
28 
29  with aktualizr:
30  # run aktualizr once, secondary has been already running
31  aktualizr.wait_for_completion()
32 
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"))
35  return test_result
36 
37 
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'''
43 
44  # add a new image to the repo in order to update the secondary with it
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)
47 
48  logger.debug("Trying to update ECU {} with the image {}".
49  format(secondary.id, (secondary_image_hash, secondary_image_filename)))
50  with secondary:
51  # start secondary, aktualizr has been already started in 'once' mode
52  aktualizr.wait_for_completion()
53 
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"))
56  return test_result
57 
58 
59 @with_uptane_backend()
60 @with_director()
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'''
65 
66  test_result = True
67  # add a new image to the repo in order to update the secondary with it
68  secondary_image_filename = "secondary_image_filename.img"
69  secondary_image_hash = uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
70 
71  logger.debug("Trying to update ECU {} with the image {}".
72  format(secondary.id, (secondary_image_hash, secondary_image_filename)))
73 
74  # start Secondary and Aktualizr processes, aktualizr is started in 'once' mode
75  with secondary, aktualizr:
76  aktualizr.wait_for_completion()
77 
78  if not director.get_install_result():
79  logger.error("Installation result is not successful")
80  return False
81 
82  # check currently installed hash
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")
85  return False
86 
87  # check updated file
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))
91  return False
92 
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()))
95  return False
96 
97  return True
98 
99 
100 @with_treehub()
101 @with_uptane_backend()
102 @with_director()
103 @with_sysroot()
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"""
108 
109  target_rev = treehub.revision
110  expected_targetname = uptane_repo.add_ostree_target(secondary.id, target_rev, "GARAGE_TARGET_NAME")
111 
112  with secondary:
113  with aktualizr:
114  aktualizr.wait_for_completion()
115 
116  pending_rev = aktualizr.get_current_pending_image_info(secondary.id)
117 
118  if pending_rev != target_rev:
119  logger.error("Pending version {} != the target one {}".format(pending_rev, target_rev))
120  return False
121 
122  sysroot.update_revision(pending_rev)
123  secondary.emulate_reboot()
124 
125  with secondary:
126  with aktualizr:
127  aktualizr.wait_for_completion()
128 
129  if not director.get_install_result():
130  logger.error("Installation result is not successful")
131  return False
132 
133  installed_rev = aktualizr.get_current_image_info(secondary.id)
134 
135  if installed_rev != target_rev:
136  logger.error("Installed version {} != the target one {}".format(installed_rev, target_rev))
137  return False
138 
139  if expected_targetname != director.get_ecu_manifest_filepath(secondary.id[1]):
140  logger.error(
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])))
143  return False
144 
145  return True
146 
147 
148 @with_uptane_backend()
149 @with_director()
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'''
154 
155  # run aktualizr and secondary and wait until the device/aktualizr is registered
156  with aktualizr, secondary:
157  aktualizr.wait_for_completion()
158 
159  # the secondary must be registered
160  if not aktualizr.is_ecu_registered(secondary.id):
161  return False
162 
163  # make sure that the secondary is not running
164  if secondary.is_running():
165  return False
166 
167  # launch an update on secondary without it
168  secondary_image_filename = "secondary_image_filename_001.img"
169  uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
170 
171  aktualizr.update_wait_timeout(0.1)
172  with aktualizr:
173  aktualizr.wait_for_completion()
174 
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))
179  return False
180 
181  return not director.get_install_result()
182 
183 
184 
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"""
190 
191  secondary_image_filename = "secondary_image_filename_001.img"
192  uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
193 
194  logger.debug("Checking Aktualizr behaviour if it timeouts while waiting for a connection from the secondary")
195 
196  # just start the aktualizr and expect that it timeouts on waiting for a connection from the secondary
197  # so the secondary is not registered at the device and backend
198  with aktualizr:
199  aktualizr.wait_for_completion()
200 
201  info = aktualizr.get_info()
202  if info is None:
203  return False
204  not_provisioned = 'Provisioned on server: no' in info
205 
206  return not_provisioned and not aktualizr.is_ecu_registered(secondary.id)
207 
208 
209 @with_uptane_backend()
210 @with_director()
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"""
215 
216  # provision device with a secondary
217  with secondary, aktualizr:
218  aktualizr.wait_for_completion()
219 
220  secondary_image_filename = "secondary_image_filename.img"
221  uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
222 
223  with aktualizr:
224  time.sleep(10)
225  with secondary:
226  aktualizr.wait_for_completion()
227 
228  if not director.get_install_result():
229  logger.error("Installation result is not successful")
230  return False
231 
232  return True
233 
234 
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'''
240 
241  # run aktualizr and secondary and wait until the device/aktualizr is registered
242  with aktualizr, secondary:
243  aktualizr.wait_for_completion()
244 
245  # the secondary must be registered
246  if not aktualizr.is_ecu_registered(secondary.id):
247  return False
248 
249  # make sure that the secondary is not running
250  if secondary.is_running():
251  return False
252 
253  # run just aktualizr, the previously registered secondary is off
254  # and check if the primary ECU is updatable if the secondary is not connected
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)
257 
258  # if a new image for the not-connected secondary is specified in the target
259  # then nothing is going to be updated, including the image intended for
260  # healthy primary ECU
261  # secondary_image_filename = "secondary_image_filename_001.img"
262  # secondary_image_hash = uptane_repo.add_image(id=secondary.id, image_filename=secondary_image_filename)
263 
264  aktualizr.update_wait_timeout(0.1)
265  with aktualizr:
266  aktualizr.wait_for_completion()
267 
268  return aktualizr.get_current_primary_image_info() == primary_image_hash
269 
270 
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'''
277 
278  with aktualizr, secondary, secondary2:
279  aktualizr.wait_for_completion()
280 
281  if not aktualizr.is_ecu_registered(secondary.id) or not aktualizr.is_ecu_registered(secondary2.id):
282  return False
283 
284  return True
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)
288 
289  with aktualizr:
290  time.sleep(10)
291  with secondary:
292  aktualizr.wait_for_completion()
293 
294  if not director.get_install_result():
295  logger.error("Installation result is not successful")
296  return False
297 
298  return True
299 
300 
301 # test suit runner
302 if __name__ == '__main__':
303  logging.basicConfig(level=logging.INFO)
304 
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')
309 
310  input_params = parser.parse_args()
311 
312  KeyStore.base_dir = path.abspath(input_params.src_dir)
313  initial_cwd = getcwd()
314  chdir(input_params.build_dir)
315 
316  test_suite = [
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,
324  ]
325 
326  if input_params.ostree == 'ON':
327  test_suite.append(test_secondary_ostree_update)
328 
329  test_suite_run_result = TestRunner(test_suite).run()
330 
331  chdir(initial_cwd)
332  exit(0 if test_suite_run_result else 1)