Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
test_imagerepo_failure.py
1 #!/usr/bin/env python3
2 
3 import logging
4 import argparse
5 
6 from os import getcwd, chdir
7 from test_fixtures import KeyStore, with_uptane_backend, with_path, with_director, with_aktualizr,\
8  with_install_manager, with_imagerepo, with_images, MalformedImageHandler, \
9  DownloadInterruptionHandler, MalformedJsonHandler, DownloadInterruptionHandler, TestRunner
10 
11 
12 logger = logging.getLogger(__file__)
13 
14 
15 """
16 Verifies whether aktualizr is updatable after image metadata download failure
17 with follow-up successful metadata download.
18 
19 Currently, it's tested against two types of metadata download/parsing failure:
20  - download interruption - metadata file download is interrupted once|three times, after that it's successful
21  - malformed json - aktualizr receives malformed json/metadata as a response to the first request for metadata,
22  a response to subsequent request is successful
23 
24 Note: Aktualizr doesn't send any installation report in manifest in case of metadata download failure
25 """
26 @with_uptane_backend(start_generic_server=True)
27 @with_path(paths=['/1.root.json', '/timestamp.json', '/snapshot.json', '/targets.json'])
28 @with_imagerepo(handlers=[
29  DownloadInterruptionHandler(number_of_failures=1),
30  MalformedJsonHandler(number_of_failures=1),
31  DownloadInterruptionHandler(number_of_failures=3),
32  ])
33 @with_director(start=False)
34 @with_aktualizr(start=False, run_mode='full')
35 @with_install_manager()
36 def test_imagerepo_update_after_metadata_download_failure(install_mngr, director,
37  aktualizr, **kwargs):
38  with aktualizr:
39  with director:
40  install_result = director.wait_for_install()
41  logger.info('Director install result: {}'.format(install_result))
42  install_result = install_result and install_mngr.are_images_installed()
43  logger.info('Are images installed: {}'.format(install_result))
44  return install_result
45 
46 
47 """
48 Verifies whether aktualizr is updatable after image download failure
49 with follow-up successful download.
50 
51 Currently, it's tested against two types of image download failure:
52  - download interruption - file download is interrupted once, after that it's successful
53  - malformed image - image download is successful but it's malformed. It happens once after that it's successful
54 """
55 @with_uptane_backend(start_generic_server=True)
56 @with_images(images_to_install=[(('primary-hw-ID-001', 'primary-ecu-id'), 'primary-image.img')])
57 @with_imagerepo(handlers=[
58  DownloadInterruptionHandler(number_of_failures=1, url='/targets/primary-image.img'),
59  MalformedImageHandler(number_of_failures=1, url='/targets/primary-image.img'),
60  ])
61 @with_director(start=False)
62 @with_aktualizr(start=False, run_mode='full', id=('primary-hw-ID-001', 'primary-ecu-id'))
63 @with_install_manager()
64 def test_imagerepo_update_after_image_download_failure(install_mngr, director,
65  aktualizr, **kwargs):
66  with aktualizr:
67  with director:
68  install_result = director.wait_for_install()
69  install_result = install_result and install_mngr.are_images_installed()
70  return install_result
71 
72 
73 """
74  Verifies whether an update fails if repo metadata download fails or they are malformed
75  - download is interrupted three times
76  - malformed json is received
77 """
78 @with_uptane_backend(start_generic_server=True)
79 @with_path(paths=['/1.root.json', '/timestamp.json', '/snapshot.json', '/targets.json'])
80 @with_imagerepo(handlers=[
81  DownloadInterruptionHandler(number_of_failures=3),
82  MalformedJsonHandler(number_of_failures=1),
83  ])
84 @with_director()
85 @with_aktualizr(run_mode='once')
86 @with_install_manager()
87 def test_imagerepo_unsuccessful_download(install_mngr, aktualizr,
88  director, **kwargs):
89  aktualizr.wait_for_completion()
90  return not (director.get_install_result() or install_mngr.are_images_installed())
91 
92 
93 if __name__ == "__main__":
94  logging.basicConfig(level=logging.INFO)
95 
96  parser = argparse.ArgumentParser(description='Test backend failure')
97  parser.add_argument('-b', '--build-dir', help='build directory', default='build')
98  parser.add_argument('-s', '--src-dir', help='source directory', default='.')
99  input_params = parser.parse_args()
100 
101  KeyStore.base_dir = input_params.src_dir
102  initial_cwd = getcwd()
103  chdir(input_params.build_dir)
104 
105  test_suite = [
106  test_imagerepo_update_after_metadata_download_failure,
107  test_imagerepo_update_after_image_download_failure,
108  test_imagerepo_unsuccessful_download,
109  ]
110 
111  with TestRunner(test_suite) as runner:
112  test_suite_run_result = runner.run()
113 
114  chdir(initial_cwd)
115  exit(0 if test_suite_run_result else 1)