]>
git.ipfire.org Git - nitsi.git/blob - src/nitsi/test.py
10 from . import virtual_environ
12 logger
= logging
.getLogger("nitsi.test")
15 class TestException(Exception):
16 def __init__(self
, message
):
17 self
.message
= message
20 def __init__(self
, log_path
, dir=None, recipe_file
=None, settings_file
=None, cmd_settings
=None):
24 # Set default values for the settings dict
25 self
.settings
["name"] = ""
26 self
.settings
["description"] = ""
27 self
.settings
["copy_from"] = None
28 self
.settings
["copy_to"] = None
29 self
.settings
["virtual_environ_path"] = None
31 self
.cmd_settings
= cmd_settings
32 self
.log_path
= log_path
34 # Init all vars with None
35 self
.settings_file
= None
36 self
.recipe_file
= None
39 # We need at least a path to a recipe file or a dir to a test
40 if not dir and not recipe
:
41 raise TestException("Did not get a path to a test or to a recipe file")
43 # We cannot decide which to use when we get both
44 if (dir and recipe_file
) or (dir and settings_file
):
45 raise TestException("Get dir and path to recipe or settings file")
49 if not os
.path
.isabs(dir):
50 self
.path
= os
.path
.abspath(dir)
51 except BaseException
as e
:
52 logger
.error("Could not get absolute path")
55 logger
.debug("Path of this test is: {}".format(self
.path
))
57 self
.recipe_file
= "{}/recipe".format(self
.path
)
58 self
.settings_file
= "{}/settings".format(self
.path
)
61 if not os
.path
.isabs(recipe_file
):
62 self
.recipe_file
= os
.path
.abspath(recipe_file
)
64 self
.recipe_file
= recipe_file
67 if not os
.path
.isabs(settings_file
):
68 self
.settings_file
= os
.path
.abspath(settings_file
)
70 self
.settings_file
= settings_file
72 if not os
.path
.isfile(self
.settings_file
):
73 logger
.error("No such file: {}".format(self
.settings_file
))
74 raise TestException("No settings file found")
76 if not os
.path
.isfile(self
.recipe_file
):
77 logger
.error("No such file: {}".format(self
.recipe_file
))
78 raise TestException("No recipe file found")
82 self
.log
= logger
.getChild(os
.path
.basename(self
.path
))
85 self
.log
= logger
.getChild(os
.path
.basename(self
.recipe_file
))
87 def read_settings(self
):
88 if self
.settings_file
:
89 self
.log
.debug("Going to read all settings from the ini file")
91 self
.config
= configparser
.ConfigParser()
92 self
.config
.read(self
.settings_file
)
93 except BaseException
as e
:
94 self
.log
.error("Failed to parse the config")
97 self
.settings
["name"] = self
.config
.get("GENERAL","name", fallback
="")
98 self
.settings
["description"] = self
.config
.get("GENERAL", "description", fallback
="")
99 self
.settings
["copy_to"] = self
.config
.get("GENERAL", "copy_to", fallback
=None)
100 self
.settings
["copy_from"] = self
.config
.get("GENERAL", "copy_from", fallback
=None)
101 self
.settings
["virtual_environ_path"] = self
.config
.get("VIRTUAL_ENVIRONMENT", "path", fallback
=None)
103 if not self
.settings
["virtual_environ_path"]:
104 self
.log
.error("No path for virtual environment found.")
105 raise TestException("No path for virtual environment found.")
107 self
.settings
["virtual_environ_path"] = os
.path
.normpath(self
.path
+ "/" + self
.settings
["virtual_environ_path"])
109 # Parse copy_from setting
110 if self
.settings
["copy_from"]:
111 self
.log
.debug("Going to parse the copy_from setting.")
112 self
.settings
["copy_from"] = self
.settings
["copy_from"].split(",")
115 for file in self
.settings
["copy_from"]:
117 # If file is empty we do not want to add it to the list
119 # If we get an absolut path we do nothing
120 # If not we add self.path to get an absolut path
121 if not os
.path
.isabs(file):
122 file = os
.path
.normpath(self
.path
+ "/" + file)
124 # We need to check if file is a valid file or dir
125 if not (os
.path
.isdir(file) or os
.path
.isfile(file)):
126 raise TestException("'{}' is not a valid file nor a valid directory".format(file))
128 self
.log
.debug("'{}' will be copied into all images".format(file))
131 self
.settings
["copy_from"] = tmp
135 def virtual_environ_setup(self
):
136 self
.virtual_environ
= virtual_environ
.Virtual_environ(self
.settings
["virtual_environ_path"])
138 self
.virtual_networks
= self
.virtual_environ
.get_networks()
140 self
.virtual_machines
= self
.virtual_environ
.get_machines()
142 def virtual_environ_start(self
):
143 for name
in self
.virtual_environ
.network_names
:
144 self
.virtual_networks
[name
].define()
145 self
.virtual_networks
[name
].start()
147 for name
in self
.virtual_environ
.machine_names
:
148 self
.virtual_machines
[name
].define()
149 self
.virtual_machines
[name
].create_snapshot()
150 # We can only copy files when we know which and to which dir
151 if self
.settings
["copy_from"] and self
.settings
["copy_to"]:
152 self
.virtual_machines
[name
].copy_in(self
.settings
["copy_from"], self
.settings
["copy_to"])
153 self
.virtual_machines
[name
].start()
155 # Time to which all serial output log entries are relativ
156 log_start_time
= time
.time()
158 # Number of chars of the longest machine name
159 longest_machine_name
= self
.virtual_environ
.longest_machine_name
161 self
.log
.info("Try to login on all machines")
162 for name
in self
.virtual_environ
.machine_names
:
163 self
.log
.info("Try to login on {}".format(name
))
164 self
.virtual_machines
[name
].login("{}/test.log".format(self
.log_path
),
165 log_start_time
=log_start_time
,
166 longest_machine_name
=longest_machine_name
)
168 def load_recipe(self
):
169 self
.log
.info("Going to load the recipe")
171 self
.recipe
= recipe
.Recipe(self
.recipe_file
, machines
=self
.virtual_environ
.machine_names
)
172 for line
in self
.recipe
.recipe
:
175 self
.log
.debug("This was the recipe")
176 except BaseException
as e
:
177 self
.log
.error("Failed to load recipe")
180 def run_recipe(self
):
181 for line
in self
.recipe
.recipe
:
182 return_value
= self
.virtual_machines
[line
[0]].cmd(line
[2])
183 self
.log
.debug("Return value is: {}".format(return_value
))
184 if return_value
!= "0" and line
[1] == "":
185 raise TestException("Failed to execute command '{}' on {}, return code: {}".format(line
[2],line
[0], return_value
))
186 elif return_value
== "0" and line
[1] == "!":
187 raise TestException("Succeded to execute command '{}' on {}, return code: {}".format(line
[2],line
[0],return_value
))
189 self
.log
.debug("Command '{}' on {} returned with: {}".format(line
[2],line
[0],return_value
))
191 def virtual_environ_stop(self
):
192 for name
in self
.virtual_environ
.machine_names
:
193 # We just catch exception here to avoid
194 # that we stop the cleanup process if only one command fails
196 self
.virtual_machines
[name
].shutdown()
197 self
.virtual_machines
[name
].revert_snapshot()
198 self
.virtual_machines
[name
].undefine()
199 except BaseException
as e
:
200 self
.log
.exception(e
)
202 for name
in self
.virtual_environ
.network_names
:
204 self
.virtual_networks
[name
].undefine()
205 except BaseException
as e
:
206 self
.log
.exception(e
)