]>
git.ipfire.org Git - nitsi.git/blob - src/nitsi/test.py
196b2fbb398aa57b97172459fbdd968b148c0715
10 from . import virtual_environ
11 from . import settings
14 logger
= logging
.getLogger("nitsi.test")
17 class TestException(Exception):
18 def __init__(self
, message
):
19 self
.message
= message
22 def __init__(self
, log_path
, dir=None, recipe_file
=None, settings_file
=None, settings
=None):
24 self
.settings
= settings
26 self
.log_path
= log_path
28 # Init all vars with None
29 self
.settings_file
= None
30 self
.recipe_file
= None
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")
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")
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")
49 logger
.debug("Path of this test is: {}".format(self
.path
))
51 self
.recipe_file
= "{}/recipe".format(self
.path
)
52 self
.settings_file
= "{}/settings".format(self
.path
)
55 # We can also go on without a settings file
56 self
.settings_file
= self
.check_file(self
.settings_file
, log_level
=logging
.WARNING
)
58 self
.recipe_file
= self
.check_file(self
.recipe_file
)
59 if not self
.recipe_file
:
60 raise TestException("No recipe file found")
65 self
.log
= logger
.getChild(os
.path
.basename(self
.path
))
66 # We get a recipe when we get here
68 self
.log
= logger
.getChild(os
.path
.basename(self
.recipe_file
))
70 # Parse config and settings:
71 if self
.settings_file
:
72 self
.settings
.set_config_values_from_file(self
.settings_file
, type="settings-file")#
75 self
.settings
.check_config_values()
79 # returns an absolut path, when the file is valid, None when not
80 def check_file(self
, file, log_level
=logging
.ERROR
):
82 logger
.debug("File to check is: {}".format(file))
83 if not os
.path
.isfile(file):
84 err_msg
= "No such file: {}".format(file)
85 logger
.log(log_level
,err_msg
)
87 if not os
.path
.isabs(file):
88 file = os
.path
.abspath(file)
93 def virtual_environ_setup_stage_1(self
):
94 self
.virtual_environ
= virtual_environ
.VirtualEnviron(self
.settings
.get_config_value("virtual_environ_path"))
96 self
.virtual_networks
= self
.virtual_environ
.get_networks()
98 self
.virtual_machines
= self
.virtual_environ
.get_machines()
100 def virtual_environ_setup_stage_2(self
):
101 # built up which machines which are used in our recipe
104 for line
in self
.recipe
.recipe
:
105 if not line
[0] in used_machines
:
106 used_machines
.append(line
[0])
108 self
.log
.debug("Machines used in this recipe {}".format(used_machines
))
110 self
.used_machine_names
= used_machines
112 for machine
in self
.used_machine_names
:
113 if not machine
in self
.virtual_environ
.machine_names
:
114 raise TestException("{} is listed as machine in the recipe, but the virtual environmet does not have such a machine".format(machine
))
117 def virtual_environ_start(self
):
118 for name
in self
.virtual_environ
.network_names
:
119 self
.virtual_networks
[name
].define()
120 self
.virtual_networks
[name
].start()
122 for name
in self
.used_machine_names
:
123 self
.virtual_machines
[name
].define()
124 self
.virtual_machines
[name
].create_snapshot()
125 # We can only copy files when we know which and to which dir
126 if self
.settings
.get_config_value("copy_from") and self
.settings
.get_config_value("copy_to"):
127 self
.virtual_machines
[name
].copy_in(self
.settings
.get_config_value("copy_from"), self
.settings
.get_config_value("copy_to"))
128 self
.virtual_machines
[name
].start()
130 # Time to which all serial output log entries are relativ
131 log_start_time
= time
.time()
133 # Number of chars of the longest machine name
134 longest_machine_name
= self
.virtual_environ
.longest_machine_name
136 self
.log
.info("Try to intialize the serial connection, connect and login on all machines")
137 for name
in self
.used_machine_names
:
138 self
.log
.info("Try to initialize the serial connection connect and login on {}".format(name
))
139 self
.virtual_machines
[name
].serial_init(log_file
="{}/test.log".format(self
.log_path
),
140 log_start_time
=log_start_time
,
141 longest_machine_name
=longest_machine_name
)
142 self
.virtual_machines
[name
].serial_connect()
144 def load_recipe(self
):
145 self
.log
.info("Going to load the recipe")
147 self
.recipe
= recipe
.Recipe(self
.recipe_file
,
148 fallback_machines
=self
.virtual_environ
.machine_names
)
150 for line
in self
.recipe
.recipe
:
153 self
.log
.debug("This was the recipe")
154 except BaseException
as e
:
155 self
.log
.error("Failed to load recipe")
158 # This functions tries to handle an error of the test (eg. when 'echo "Hello World"' failed)
159 # in an interactive way
160 # returns False when the test should exit right now, and True when the test should go on
161 def interactive_error_handling(self
):
162 if not self
.settings
.get_config_value("interactive_error_handling"):
165 _cmd
= cmd
.CMD(intro
="You are droppped into an interative debugging shell because of the previous errors",
166 help={"exit": "Exit the test rigth now",
167 "continue": "Continues the test without any error handling, so do not expect that the test succeeds.",
168 "debug": "Disconnects from the serial console and prints the devices to manually connect to the virtual machines." \
169 "This is useful when you can fix th error with some manual commands. Please disconnect from the serial consoles and " \
170 "choose 'exit or 'continue' when you are done"})
172 command
= _cmd
.get_input(valid_commands
=["continue", "exit", "debug"])
174 if command
== "continue":
175 # The test should go on but we do not any debugging, so we return True
177 elif command
== "exit":
178 # The test should exit right now (normal behaviour)
181 # If we get here we are in debugging mode
182 # Disconnect from the serial console:
184 for name
in self
.used_machine_names
:
185 _cmd
.print_to_cmd("Disconnect from the serial console of {}".format(name
))
186 self
.virtual_machines
[name
].serial_disconnect()
188 # Print the serial device for each machine
189 for name
in self
.used_machine_names
:
190 device
= self
.virtual_machines
[name
].get_serial_device()
191 _cmd
.print_to_cmd("Serial device of {} is {}".format(name
, device
))
193 _cmd
.print_to_cmd("You can now connect to all serial devices, and send custom commands to the virtual machines." \
194 "Please type 'continue' or 'exit' when you disconnected from als serial devices and want to go on.")
196 command
= _cmd
.get_input(valid_commands
=["continue", "exit"])
198 if command
== "exit":
201 # We should continue whit the test
202 # Reconnect to the serial devices
204 for name
in self
.used_machine_names
:
205 self
.log
.info("Try to reconnect to {}".format(name
))
206 self
.virtual_machines
[name
].serial_connect()
210 def run_recipe(self
):
211 for line
in self
.recipe
.recipe
:
212 return_value
= self
.virtual_machines
[line
[0]].cmd(line
[2])
213 self
.log
.debug("Return value is: {}".format(return_value
))
214 if return_value
!= "0" and line
[1] == "":
215 err_msg
= "Failed to execute command '{}' on {}, return code: {}".format(line
[2],line
[0], return_value
)
216 # Try to handle this error in an interactive way, if we cannot go on
217 # raise an exception and exit
218 if not self
.interactive_error_handling():
219 raise TestException(err_msg
)
221 elif return_value
== "0" and line
[1] == "!":
222 err_msg
= "Succeded to execute command '{}' on {}, return code: {}".format(line
[2],line
[0],return_value
)
223 self
.log
.error(err_msg
)
224 # Try to handle this error in an interactive way, if we cannot go on
225 # raise an exception and exit
226 if not self
.interactive_error_handling():
227 raise TestException(err_msg
)
229 self
.log
.debug("Command '{}' on {} returned with: {}".format(line
[2],line
[0],return_value
))
231 def virtual_environ_stop(self
):
232 for name
in self
.used_machine_names
:
233 # We just catch exception here to avoid
234 # that we stop the cleanup process if only one command fails
236 self
.virtual_machines
[name
].shutdown()
237 self
.virtual_machines
[name
].revert_snapshot()
238 self
.virtual_machines
[name
].undefine()
239 except BaseException
as e
:
240 self
.log
.exception(e
)
242 for name
in self
.virtual_environ
.network_names
:
244 self
.virtual_networks
[name
].undefine()
245 except BaseException
as e
:
246 self
.log
.exception(e
)