Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
test_misc_ostree_update.py
1 #!/usr/bin/env python3
2 
3 import logging
4 import argparse
5 import time
6 
7 from os import getcwd, chdir
8 
9 from test_fixtures import KeyStore, with_aktualizr, with_uptane_backend, with_secondary, with_director, with_imagerepo,\
10  with_sysroot, with_treehub, TestRunner
11 
12 
13 logger = logging.getLogger(__file__)
14 
15 
16 """
17  Test update of Primary and Secondary if their package manager differs, `ostree`
18  and binary (`none` or `fake`) respectively
19 
20  Aktualizr/Primary's package manager is set to `ostree`
21  Secondary's package manager is set to `fake` which means a file/binary update
22  Primary goal is to verify whether aktualizr succeeds with a binary/fake update of Secondary
23  while aktualizr/Primary is configured with OSTree package manager
24 """
25 @with_uptane_backend(start_generic_server=True)
26 @with_secondary(start=True)
27 @with_director()
28 @with_treehub()
29 @with_sysroot()
30 @with_aktualizr(start=False, run_mode='once', output_logs=True)
31 def test_primary_ostree_secondary_file_updates(uptane_repo, secondary, aktualizr, director, sysroot,
32  treehub, **kwargs):
33  target_rev = treehub.revision
34  # add an OSTree update for Primary
35  uptane_repo.add_ostree_target(aktualizr.id, target_rev)
36  # add a fake/binary update for Secondary
37  secondary_update_hash = uptane_repo.add_image(secondary.id, "secondary-update.bin")
38 
39  with aktualizr:
40  aktualizr.wait_for_completion()
41 
42  # check the Primary update, must be in pending state since it requires reboot
43  pending_rev = aktualizr.get_primary_pending_version()
44  if pending_rev != target_rev:
45  logger.error("Pending version {} != the target version {}".format(pending_rev, target_rev))
46  return False
47 
48  # check the Secondary update
49  current_secondary_image_hash = aktualizr.get_current_image_info(secondary.id)
50  if current_secondary_image_hash != secondary_update_hash:
51  logger.error("Current Secondary image {} != expected image {}".format(current_secondary_image_hash,
52  secondary_update_hash))
53  return False
54 
55  # emulate reboot and run aktualizr once more
56  sysroot.update_revision(pending_rev)
57  aktualizr.emulate_reboot()
58 
59  with aktualizr:
60  aktualizr.wait_for_completion()
61 
62  # check the Primary update after reboot
63  result = director.get_install_result() and (target_rev == aktualizr.get_current_primary_image_info())
64  return result
65 
66 
67 """
68  Test update of Secondary's OSTree repo if an OSTree target metadata are expired
69 
70  Metadata are valid at the moment of a new OSTree revision installation,
71  but are expired after that and before Secondary is rebooted,
72  we still expect that the installed update is applied in this case
73 """
74 @with_uptane_backend()
75 @with_director()
76 @with_treehub()
77 @with_sysroot()
78 @with_secondary(start=False)
79 @with_aktualizr(start=False, run_mode='once', output_logs=True)
80 def test_secondary_ostree_update_if_metadata_expires(uptane_repo, secondary, aktualizr, director, sysroot, treehub, **kwargs):
81  target_rev = treehub.revision
82  expires_within_sec = 10
83 
84  # add an OSTree update for Secondary
85  uptane_repo.add_ostree_target(secondary.id, target_rev, expires_within_sec=expires_within_sec)
86  start_time = time.time()
87 
88  with secondary:
89  with aktualizr:
90  aktualizr.wait_for_completion()
91 
92  # check the Primary update, must be in pending state since it requires reboot
93  pending_rev = aktualizr.get_current_pending_image_info(secondary.id)
94  if pending_rev != target_rev:
95  logger.error("Pending version {} != the target version {}".format(pending_rev, target_rev))
96  return False
97 
98  # wait until the target metadata are expired
99  time.sleep(max(0, expires_within_sec - (time.time() - start_time)))
100 
101  # emulate reboot and run aktualizr once more
102  sysroot.update_revision(pending_rev)
103  secondary.emulate_reboot()
104 
105  with secondary:
106  # Wait for Secondary to initialize. wait_for_completion won't work; it
107  # times out.
108  time.sleep(5)
109  with aktualizr:
110  aktualizr.wait_for_completion()
111 
112  # check the Secondary update after reboot
113  if not director.get_install_result():
114  logger.error("Installation result is not successful")
115  return False
116 
117  installed_rev = aktualizr.get_current_image_info(secondary.id)
118  if installed_rev != target_rev:
119  logger.error("Installed version {} != the target version {}".format(installed_rev, target_rev))
120  return False
121 
122  return True
123 
124 
125 """
126  Test update of Primary's OSTree repo if an OSTree target metadata are expired
127 
128  Metadata are valid at the moment of a new OSTree revision installation,
129  but are expired after that and before Primary is rebooted,
130  we still expect that the installed update is applied in this case
131 """
132 @with_uptane_backend()
133 @with_director()
134 @with_treehub()
135 @with_sysroot()
136 @with_aktualizr(start=False, run_mode='once', output_logs=True)
137 def test_primary_ostree_update_if_metadata_expires(uptane_repo, aktualizr, director, sysroot, treehub, **kwargs):
138  target_rev = treehub.revision
139  expires_within_sec = 10
140 
141  # add an OSTree update for Primary
142  uptane_repo.add_ostree_target(aktualizr.id, target_rev, expires_within_sec=expires_within_sec)
143  start_time = time.time()
144 
145  with aktualizr:
146  aktualizr.wait_for_completion()
147 
148  # check the Primary update, must be in pending state since it requires reboot
149  pending_rev = aktualizr.get_primary_pending_version()
150  if pending_rev != target_rev:
151  logger.error("Pending version {} != the target version {}".format(pending_rev, target_rev))
152  return False
153 
154  # wait until the target metadata are expired
155  time.sleep(max(0, expires_within_sec - (time.time() - start_time)))
156 
157  # emulate reboot and run aktualizr once more
158  sysroot.update_revision(pending_rev)
159  aktualizr.emulate_reboot()
160 
161  with aktualizr:
162  aktualizr.wait_for_completion()
163 
164  # check the Primary update after reboot
165  if not director.get_install_result():
166  logger.error("Installation result is not successful")
167  return False
168 
169  installed_rev = aktualizr.get_current_primary_image_info()
170  if installed_rev != target_rev:
171  logger.error("Installed version {} != the target version {}".format(installed_rev, target_rev))
172  return False
173 
174  return True
175 
176 
177 if __name__ == "__main__":
178  logging.basicConfig(level=logging.INFO)
179 
180  parser = argparse.ArgumentParser(description='Test backend failure')
181  parser.add_argument('-b', '--build-dir', help='build directory', default='build')
182  parser.add_argument('-s', '--src-dir', help='source directory', default='.')
183 
184  input_params = parser.parse_args()
185 
186  KeyStore.base_dir = input_params.src_dir
187  initial_cwd = getcwd()
188  chdir(input_params.build_dir)
189 
190  test_suite = [
191  test_primary_ostree_secondary_file_updates,
192  test_secondary_ostree_update_if_metadata_expires,
193  test_primary_ostree_update_if_metadata_expires
194  ]
195 
196  with TestRunner(test_suite) as runner:
197  test_suite_run_result = runner.run()
198 
199  chdir(initial_cwd)
200  exit(0 if test_suite_run_result else 1)