]>
Commit | Line | Data |
---|---|---|
5e7f6db7 JS |
1 | #!/usr/bin/python3 |
2 | ||
6632e137 | 3 | import configparser |
5e7f6db7 | 4 | import libvirt |
6632e137 | 5 | import logging |
5e7f6db7 | 6 | import os |
6c352a80 JS |
7 | import time |
8 | ||
b560f31a JS |
9 | from . import recipe |
10 | from . import virtual_environ | |
1795f5e8 | 11 | from . import settings |
4ac093dc | 12 | from . import cmd |
5e7f6db7 | 13 | |
1ed8ca9f | 14 | logger = logging.getLogger("nitsi.test") |
5e7f6db7 | 15 | |
61b44c10 JS |
16 | |
17 | class TestException(Exception): | |
18 | def __init__(self, message): | |
19 | self.message = message | |
20 | ||
ee227ea1 | 21 | class Test(): |
7aa9213e | 22 | def __init__(self, log_path, dir=None, recipe_file=None, settings_file=None, settings=None): |
978854bf | 23 | # init settings var |
7aa9213e | 24 | self.settings = settings |
978854bf | 25 | |
ab5b8849 | 26 | self.log_path = log_path |
5e7f6db7 | 27 | |
ab5b8849 JS |
28 | # Init all vars with None |
29 | self.settings_file = None | |
30 | self.recipe_file = None | |
31 | self.path = None | |
5e7f6db7 | 32 | |
ab5b8849 JS |
33 | # We need at least a path to a recipe file or a dir to a test |
34 | if not dir and not recipe: | |
35 | raise TestException("Did not get a path to a test or to a recipe file") | |
36 | ||
37 | # We cannot decide which to use when we get both | |
38 | if (dir and recipe_file) or (dir and settings_file): | |
39 | raise TestException("Get dir and path to recipe or settings file") | |
40 | ||
41 | if dir: | |
42 | try: | |
43 | if not os.path.isabs(dir): | |
44 | self.path = os.path.abspath(dir) | |
45 | except BaseException as e: | |
46 | logger.error("Could not get absolute path") | |
47 | raise e | |
48 | ||
49 | logger.debug("Path of this test is: {}".format(self.path)) | |
50 | ||
51 | self.recipe_file = "{}/recipe".format(self.path) | |
52 | self.settings_file = "{}/settings".format(self.path) | |
53 | ||
b4936764 | 54 | |
aadbafc3 | 55 | # We can also go on without a settings file |
9461f9f4 | 56 | self.settings_file = self.check_file(self.settings_file) |
5e7f6db7 | 57 | |
9461f9f4 JS |
58 | self.recipe_file = self.check_file(self.recipe_file) |
59 | if not self.recipe_file: | |
60 | raise TestException("No recipe file found") | |
fea304c4 | 61 | |
5e7f6db7 | 62 | |
ab5b8849 JS |
63 | # Init logging |
64 | if dir: | |
2468cf99 JS |
65 | self.log = logger.getChild(os.path.basename(self.path)) |
66 | # We get a recipe when we get here | |
67 | else: | |
ab5b8849 | 68 | self.log = logger.getChild(os.path.basename(self.recipe_file)) |
978854bf | 69 | |
7aa9213e | 70 | # Parse config and settings: |
ab5b8849 | 71 | if self.settings_file: |
7aa9213e | 72 | self.settings.set_config_values_from_file(self.settings_file, type="settings-file")# |
129c0b0f | 73 | |
7aa9213e JS |
74 | # Check settings |
75 | self.settings.check_config_values() | |
5e7f6db7 | 76 | |
9461f9f4 JS |
77 | # Checks the file: |
78 | # is the path valid ? | |
79 | # returns an absolut path, when the file is valid, None when not | |
80 | def check_file(self, file): | |
81 | if file: | |
82 | if not os.path.isfile(file): | |
83 | err_msg = "No such file: {}".format(self.recipe_file) | |
84 | logger.error(err_msg) | |
85 | return None | |
86 | if not os.path.isabs(file): | |
87 | file = os.path.abspath(file) | |
88 | ||
89 | return file | |
90 | return None | |
91 | ||
3dac9881 | 92 | def virtual_environ_setup_stage_1(self): |
7aa9213e | 93 | self.virtual_environ = virtual_environ.VirtualEnviron(self.settings.get_config_value("virtual_environ_path")) |
5e7f6db7 JS |
94 | |
95 | self.virtual_networks = self.virtual_environ.get_networks() | |
96 | ||
97 | self.virtual_machines = self.virtual_environ.get_machines() | |
98 | ||
3dac9881 | 99 | def virtual_environ_setup_stage_2(self): |
fbaed8c3 JS |
100 | # built up which machines which are used in our recipe |
101 | used_machines = [] | |
102 | ||
103 | for line in self.recipe.recipe: | |
104 | if not line[0] in used_machines: | |
105 | used_machines.append(line[0]) | |
106 | ||
107 | self.log.debug("Machines used in this recipe {}".format(used_machines)) | |
108 | ||
109 | self.used_machine_names = used_machines | |
110 | ||
111 | for machine in self.used_machine_names: | |
112 | if not machine in self.virtual_environ.machine_names: | |
113 | raise TestException("{} is listed as machine in the recipe, but the virtual environmet does not have such a machine".format(machine)) | |
114 | ||
115 | ||
5e7f6db7 | 116 | def virtual_environ_start(self): |
3fa89b7c JS |
117 | for name in self.virtual_environ.network_names: |
118 | self.virtual_networks[name].define() | |
119 | self.virtual_networks[name].start() | |
5e7f6db7 | 120 | |
fbaed8c3 | 121 | for name in self.used_machine_names: |
3fa89b7c JS |
122 | self.virtual_machines[name].define() |
123 | self.virtual_machines[name].create_snapshot() | |
8428a452 | 124 | # We can only copy files when we know which and to which dir |
7aa9213e JS |
125 | if self.settings.get_config_value("copy_from") and self.settings.get_config_value("copy_to"): |
126 | self.virtual_machines[name].copy_in(self.settings.get_config_value("copy_from"), self.settings.get_config_value("copy_to")) | |
3fa89b7c | 127 | self.virtual_machines[name].start() |
5e7f6db7 | 128 | |
6c352a80 JS |
129 | # Time to which all serial output log entries are relativ |
130 | log_start_time = time.time() | |
131 | ||
fc35cba1 JS |
132 | # Number of chars of the longest machine name |
133 | longest_machine_name = self.virtual_environ.longest_machine_name | |
134 | ||
25572214 | 135 | self.log.info("Try to intialize the serial connection, connect and login on all machines") |
fbaed8c3 | 136 | for name in self.used_machine_names: |
25572214 JS |
137 | self.log.info("Try to initialize the serial connection connect and login on {}".format(name)) |
138 | self.virtual_machines[name].serial_init(log_file="{}/test.log".format(self.log_path), | |
fc35cba1 JS |
139 | log_start_time=log_start_time, |
140 | longest_machine_name=longest_machine_name) | |
25572214 | 141 | self.virtual_machines[name].serial_connect() |
5e7f6db7 | 142 | |
3fa89b7c | 143 | def load_recipe(self): |
2fa4467d | 144 | self.log.info("Going to load the recipe") |
3fa89b7c | 145 | try: |
5945ce2a JS |
146 | self.recipe = recipe.Recipe(self.recipe_file, |
147 | fallback_machines=self.virtual_environ.machine_names) | |
148 | ||
4bc54b45 JS |
149 | for line in self.recipe.recipe: |
150 | self.log.debug(line) | |
2fa4467d JS |
151 | |
152 | self.log.debug("This was the recipe") | |
4bc54b45 | 153 | except BaseException as e: |
3fa89b7c | 154 | self.log.error("Failed to load recipe") |
4bc54b45 | 155 | raise e |
3fa89b7c | 156 | |
35aab3f6 | 157 | # This functions tries to handle an error of the test (eg. when 'echo "Hello World"' failed) |
4ac093dc JS |
158 | # in an interactive way |
159 | # returns False when the test should exit right now, and True when the test should go on | |
160 | def interactive_error_handling(self): | |
7aa9213e | 161 | if not self.settings.get_config_value("interactive_error_handling"): |
4ac093dc JS |
162 | return False |
163 | ||
164 | _cmd = cmd.CMD(intro="You are droppped into an interative debugging shell because of the previous errors", | |
165 | help={"exit": "Exit the test rigth now", | |
166 | "continue": "Continues the test without any error handling, so do not expect that the test succeeds.", | |
167 | "debug": "Disconnects from the serial console and prints the devices to manually connect to the virtual machines." \ | |
168 | "This is useful when you can fix th error with some manual commands. Please disconnect from the serial consoles and " \ | |
169 | "choose 'exit or 'continue' when you are done"}) | |
170 | ||
171 | command = _cmd.get_input(valid_commands=["continue", "exit", "debug"]) | |
172 | ||
173 | if command == "continue": | |
174 | # The test should go on but we do not any debugging, so we return True | |
175 | return True | |
176 | elif command == "exit": | |
177 | # The test should exit right now (normal behaviour) | |
178 | return False | |
179 | ||
180 | # If we get here we are in debugging mode | |
181 | # Disconnect from the serial console: | |
182 | ||
183 | for name in self.used_machine_names: | |
184 | _cmd.print_to_cmd("Disconnect from the serial console of {}".format(name)) | |
185 | self.virtual_machines[name].serial_disconnect() | |
186 | ||
187 | # Print the serial device for each machine | |
188 | for name in self.used_machine_names: | |
189 | device = self.virtual_machines[name].get_serial_device() | |
190 | _cmd.print_to_cmd("Serial device of {} is {}".format(name, device)) | |
191 | ||
192 | _cmd.print_to_cmd("You can now connect to all serial devices, and send custom commands to the virtual machines." \ | |
193 | "Please type 'continue' or 'exit' when you disconnected from als serial devices and want to go on.") | |
194 | ||
195 | command = _cmd.get_input(valid_commands=["continue", "exit"]) | |
196 | ||
197 | if command == "exit": | |
198 | return False | |
199 | ||
200 | # We should continue whit the test | |
201 | # Reconnect to the serial devices | |
202 | ||
203 | for name in self.used_machine_names: | |
204 | self.log.info("Try to reconnect to {}".format(name)) | |
205 | self.virtual_machines[name].serial_connect() | |
206 | ||
207 | return True | |
208 | ||
3fa89b7c JS |
209 | def run_recipe(self): |
210 | for line in self.recipe.recipe: | |
211 | return_value = self.virtual_machines[line[0]].cmd(line[2]) | |
bce7d520 JS |
212 | self.log.debug("Return value is: {}".format(return_value)) |
213 | if return_value != "0" and line[1] == "": | |
4ac093dc JS |
214 | err_msg = "Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value) |
215 | # Try to handle this error in an interactive way, if we cannot go on | |
216 | # raise an exception and exit | |
217 | if not self.interactive_error_handling(): | |
218 | raise TestException(err_msg) | |
219 | ||
bce7d520 | 220 | elif return_value == "0" and line[1] == "!": |
4ac093dc JS |
221 | err_msg = "Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value) |
222 | self.log.error(err_msg) | |
223 | # Try to handle this error in an interactive way, if we cannot go on | |
224 | # raise an exception and exit | |
225 | if not self.interactive_error_handling(): | |
226 | raise TestException(err_msg) | |
bce7d520 JS |
227 | else: |
228 | self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value)) | |
3fa89b7c JS |
229 | |
230 | def virtual_environ_stop(self): | |
fbaed8c3 | 231 | for name in self.used_machine_names: |
f9178ad3 JS |
232 | # We just catch exception here to avoid |
233 | # that we stop the cleanup process if only one command fails | |
234 | try: | |
235 | self.virtual_machines[name].shutdown() | |
236 | self.virtual_machines[name].revert_snapshot() | |
237 | self.virtual_machines[name].undefine() | |
238 | except BaseException as e: | |
239 | self.log.exception(e) | |
3fa89b7c JS |
240 | |
241 | for name in self.virtual_environ.network_names: | |
f9178ad3 JS |
242 | try: |
243 | self.virtual_networks[name].undefine() | |
244 | except BaseException as e: | |
245 | self.log.exception(e) | |
246 |