Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
test_treehub_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_sysroot, with_treehub, DownloadInterruptionHandler, MalformedImageHandler, RedirectHandler, TestRunner
9 
10 logger = logging.getLogger(__file__)
11 
12 
13 """
14  Verifies whether aktualizr is updatable after failure of object(s) download from Treehub/OSTree repo
15  with follow-up successful download.
16 
17  Currently, it's tested against two types of object download failure:
18  - download interruption - object download is interrupted once, after that it's successful
19  - malformed object - object download is successful but it's malformed. It happens once after that it's successful
20 """
21 @with_uptane_backend(start_generic_server=True)
22 @with_director()
23 @with_treehub(handlers=[
24  DownloadInterruptionHandler(url='/objects/41/5ce9717fc7a5f4d743a4f911e11bd3ed83930e46756303fd13a3eb7ed35892.filez'),
25  MalformedImageHandler(url='/objects/41/5ce9717fc7a5f4d743a4f911e11bd3ed83930e46756303fd13a3eb7ed35892.filez'),
26  RedirectHandler(number_of_redirects=1000, url='/objects/41/5ce9717fc7a5f4d743a4f911e11bd3ed83930e46756303fd13a3eb7ed35892.filez'),
27 
28  # TODO: OSTree object download is not resilient to `Slow Retrieval Attack`
29  # https://saeljira.it.here.com/browse/OTA-3737
30  #SlowRetrievalHandler(url='/objects/6b/1604b586fcbe052bbc0bd9e1c8040f62e085ca2e228f37df957ac939dff361.filez'),
31 
32 ])
33 @with_sysroot()
34 @with_aktualizr(start=False, run_mode='once')
35 def test_treehub_update_after_image_download_failure(uptane_repo,
36  aktualizr,
37  director,
38  uptane_server,
39  sysroot, treehub):
40  target_rev = treehub.revision
41  uptane_repo.add_ostree_target(aktualizr.id, target_rev)
42  with aktualizr:
43  aktualizr.wait_for_completion()
44 
45  pending_rev = aktualizr.get_primary_pending_version()
46  if pending_rev != target_rev:
47  logger.error("Pending version {} != the target one {}".format(pending_rev, target_rev))
48  return False
49 
50  sysroot.update_revision(pending_rev)
51  aktualizr.emulate_reboot()
52 
53  with aktualizr:
54  aktualizr.wait_for_completion()
55 
56  result = director.get_install_result() and (target_rev == aktualizr.get_current_primary_image_info())
57  return result
58 
59 
60 """
61  Verifies that aktualizr does not install an image which contains files with wrong checksums
62 """
63 @with_uptane_backend(start_generic_server=True)
64 @with_director()
65 @with_treehub(handlers=[
66  MalformedImageHandler(url='/objects/41/5ce9717fc7a5f4d743a4f911e11bd3ed83930e46756303fd13a3eb7ed35892.filez',
67  number_of_failures=-1, fake_filez=True),
68 
69 ])
70 @with_sysroot()
71 @with_aktualizr(start=False, run_mode='once', output_logs=True)
72 def test_treehub_update_if_bad_ostree_checksum(uptane_repo,
73  aktualizr,
74  director,
75  uptane_server,
76  sysroot, treehub):
77  target_rev = treehub.revision
78  uptane_repo.add_ostree_target(aktualizr.id, target_rev)
79  with aktualizr:
80  aktualizr.wait_for_completion()
81 
82  pending_rev = aktualizr.get_primary_pending_version()
83  if pending_rev == target_rev:
84  logger.error("Pending version {} == the target one {}".format(pending_rev, target_rev))
85  return False
86  return True
87 
88 
89 if __name__ == "__main__":
90  logging.basicConfig(level=logging.INFO)
91 
92  parser = argparse.ArgumentParser(description='Test backend failure')
93  parser.add_argument('-b', '--build-dir', help='build directory', default='build')
94  parser.add_argument('-s', '--src-dir', help='source directory', default='.')
95  input_params = parser.parse_args()
96 
97  KeyStore.base_dir = input_params.src_dir
98  initial_cwd = getcwd()
99  chdir(input_params.build_dir)
100 
101  test_suite = [
102  test_treehub_update_after_image_download_failure,
103  test_treehub_update_if_bad_ostree_checksum
104  ]
105 
106  with TestRunner(test_suite) as runner:
107  test_suite_run_result = runner.run()
108 
109  chdir(initial_cwd)
110  exit(0 if test_suite_run_result else 1)