]>
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 self
.cmd_settings
= cmd_settings
25 self
.log_path
= log_path
27 # Init all vars with None
28 self
.settings_file
= None
29 self
.recipe_file
= None
32 # We need at least a path to a recipe file or a dir to a test
33 if not dir and not recipe
:
34 raise TestException("Did not get a path to a test or to a recipe file")
36 # We cannot decide which to use when we get both
37 if (dir and recipe_file
) or (dir and settings_file
):
38 raise TestException("Get dir and path to recipe or settings file")
42 if not os
.path
.isabs(dir):
43 self
.path
= os
.path
.abspath(dir)
44 except BaseException
as e
:
45 logger
.error("Could not get absolute path")
48 logger
.debug("Path of this test is: {}".format(self
.path
))
50 self
.recipe_file
= "{}/recipe".format(self
.path
)
51 self
.settings_file
= "{}/settings".format(self
.path
)
54 if not os
.path
.isabs(recipe_file
):
55 self
.recipe_file
= os
.path
.abspath(recipe_file
)
57 self
.recipe_file
= recipe_file
60 if not os
.path
.isabs(settings_file
):
61 self
.settings_file
= os
.path
.abspath(settings_file
)
63 self
.settings_file
= settings_file
65 if not os
.path
.isfile(self
.settings_file
):
66 logger
.error("No such file: {}".format(self
.settings_file
))
67 raise TestException("No settings file found")
69 if not os
.path
.isfile(self
.recipe_file
):
70 logger
.error("No such file: {}".format(self
.recipe_file
))
71 raise TestException("No recipe file found")
75 self
.log
= logger
.getChild(os
.path
.basename(self
.path
))
78 self
.log
= logger
.getChild(os
.path
.basename(self
.recipe_file
))
80 def read_settings(self
):
81 if self
.settings_file
:
82 self
.log
.debug("Going to read all settings from the ini file")
84 self
.config
= configparser
.ConfigParser()
85 self
.config
.read(self
.settings_file
)
86 except BaseException
as e
:
87 self
.log
.error("Failed to parse the config")
90 self
.settings
["name"] = self
.config
.get("GENERAL","name", fallback
="")
91 self
.settings
["description"] = self
.config
.get("GENERAL", "description", fallback
="")
92 self
.settings
["copy_to"] = self
.config
.get("GENERAL", "copy_to", fallback
=None)
93 self
.settings
["copy_from"] = self
.config
.get("GENERAL", "copy_from", fallback
=None)
94 self
.settings
["virtual_environ_path"] = self
.config
.get("VIRTUAL_ENVIRONMENT", "path", fallback
=None)
96 if not self
.settings
["virtual_environ_path"]:
97 self
.log
.error("No path for virtual environment found.")
98 raise TestException("No path for virtual environment found.")
100 self
.settings
["virtual_environ_path"] = os
.path
.normpath(self
.path
+ "/" + self
.settings
["virtual_environ_path"])
102 # Parse copy_from setting
103 if self
.settings
["copy_from"]:
104 self
.log
.debug("Going to parse the copy_from setting.")
105 self
.settings
["copy_from"] = self
.settings
["copy_from"].split(",")
108 for file in self
.settings
["copy_from"]:
110 # If file is empty we do not want to add it to the list
112 # If we get an absolut path we do nothing
113 # If not we add self.path to get an absolut path
114 if not os
.path
.isabs(file):
115 file = os
.path
.normpath(self
.path
+ "/" + file)
117 # We need to check if file is a valid file or dir
118 if not (os
.path
.isdir(file) or os
.path
.isfile(file)):
119 raise TestException("'{}' is not a valid file nor a valid directory".format(file))
121 self
.log
.debug("'{}' will be copied into all images".format(file))
124 self
.settings
["copy_from"] = tmp
128 def virtual_environ_setup(self
):
129 self
.virtual_environ
= virtual_environ
.Virtual_environ(self
.settings
["virtual_environ_path"])
131 self
.virtual_networks
= self
.virtual_environ
.get_networks()
133 self
.virtual_machines
= self
.virtual_environ
.get_machines()
135 def virtual_environ_start(self
):
136 for name
in self
.virtual_environ
.network_names
:
137 self
.virtual_networks
[name
].define()
138 self
.virtual_networks
[name
].start()
140 for name
in self
.virtual_environ
.machine_names
:
141 self
.virtual_machines
[name
].define()
142 self
.virtual_machines
[name
].create_snapshot()
143 # We can only copy files when we know which and to which dir
144 if self
.settings
["copy_from"] and self
.settings
["copy_to"]:
145 self
.virtual_machines
[name
].copy_in(self
.settings
["copy_from"], self
.settings
["copy_to"])
146 self
.virtual_machines
[name
].start()
148 # Time to which all serial output log entries are relativ
149 log_start_time
= time
.time()
151 # Number of chars of the longest machine name
152 longest_machine_name
= self
.virtual_environ
.longest_machine_name
154 self
.log
.info("Try to login on all machines")
155 for name
in self
.virtual_environ
.machine_names
:
156 self
.log
.info("Try to login on {}".format(name
))
157 self
.virtual_machines
[name
].login("{}/test.log".format(self
.log_path
),
158 log_start_time
=log_start_time
,
159 longest_machine_name
=longest_machine_name
)
161 def load_recipe(self
):
162 self
.log
.info("Going to load the recipe")
164 self
.recipe
= recipe
.Recipe(self
.recipe_file
, machines
=self
.virtual_environ
.machine_names
)
165 for line
in self
.recipe
.recipe
:
168 self
.log
.debug("This was the recipe")
169 except BaseException
as e
:
170 self
.log
.error("Failed to load recipe")
173 def run_recipe(self
):
174 for line
in self
.recipe
.recipe
:
175 return_value
= self
.virtual_machines
[line
[0]].cmd(line
[2])
176 self
.log
.debug("Return value is: {}".format(return_value
))
177 if return_value
!= "0" and line
[1] == "":
178 raise TestException("Failed to execute command '{}' on {}, return code: {}".format(line
[2],line
[0], return_value
))
179 elif return_value
== "0" and line
[1] == "!":
180 raise TestException("Succeded to execute command '{}' on {}, return code: {}".format(line
[2],line
[0],return_value
))
182 self
.log
.debug("Command '{}' on {} returned with: {}".format(line
[2],line
[0],return_value
))
184 def virtual_environ_stop(self
):
185 for name
in self
.virtual_environ
.machine_names
:
186 # We just catch exception here to avoid
187 # that we stop the cleanup process if only one command fails
189 self
.virtual_machines
[name
].shutdown()
190 self
.virtual_machines
[name
].revert_snapshot()
191 self
.virtual_machines
[name
].undefine()
192 except BaseException
as e
:
193 self
.log
.exception(e
)
195 for name
in self
.virtual_environ
.network_names
:
197 self
.virtual_networks
[name
].undefine()
198 except BaseException
as e
:
199 self
.log
.exception(e
)