]>
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
, path
, log_path
, settings
=None):
25 self
.path
= os
.path
.abspath(path
)
26 self
.log
= logger
.getChild(os
.path
.basename(self
.path
))
27 except BaseException
as e
:
28 logger
.error("Could not get absolute path")
31 self
.log
.debug("Path of this test is: {}".format(self
.path
))
33 self
.log_path
= log_path
35 self
.settings_file
= "{}/settings".format(self
.path
)
36 if not os
.path
.isfile(self
.settings_file
):
37 self
.log
.error("No such file: {}".format(self
.settings_file
))
38 raise TestException("No settings file found")
40 self
.recipe_file
= "{}/recipe".format(self
.path
)
41 if not os
.path
.isfile(self
.recipe_file
):
42 self
.log
.error("No such file: {}".format(self
.recipe_file
))
43 raise TestException("No recipe file found")
45 self
.cmd_settings
= settings
47 def read_settings(self
):
48 self
.log
.debug("Going to read all settings from the ini file")
50 self
.config
= configparser
.ConfigParser()
51 self
.config
.read(self
.settings_file
)
52 except BaseException
as e
:
53 self
.log
.error("Failed to parse the config")
56 self
.settings
["name"] = self
.config
.get("GENERAL","name", fallback
="")
57 self
.settings
["description"] = self
.config
.get("GENERAL", "description", fallback
="")
58 self
.settings
["copy_to"] = self
.config
.get("GENERAL", "copy_to", fallback
=None)
59 self
.settings
["copy_from"] = self
.config
.get("GENERAL", "copy_from", fallback
=None)
60 self
.settings
["virtual_environ_path"] = self
.config
.get("VIRTUAL_ENVIRONMENT", "path", fallback
=None)
62 if not self
.settings
["virtual_environ_path"]:
63 self
.log
.error("No path for virtual environment found.")
64 raise TestException("No path for virtual environment found.")
66 self
.settings
["virtual_environ_path"] = os
.path
.normpath(self
.path
+ "/" + self
.settings
["virtual_environ_path"])
68 # Parse copy_from setting
69 if self
.settings
["copy_from"]:
70 self
.log
.debug("Going to parse the copy_from setting.")
71 self
.settings
["copy_from"] = self
.settings
["copy_from"].split(",")
74 for file in self
.settings
["copy_from"]:
76 # If file is empty we do not want to add it to the list
78 # If we get an absolut path we do nothing
79 # If not we add self.path to get an absolut path
80 if not os
.path
.isabs(file):
81 file = os
.path
.normpath(self
.path
+ "/" + file)
83 # We need to check if file is a valid file or dir
84 if not (os
.path
.isdir(file) or os
.path
.isfile(file)):
85 raise TestException("'{}' is not a valid file nor a valid directory".format(file))
87 self
.log
.debug("'{}' will be copied into all images".format(file))
90 self
.settings
["copy_from"] = tmp
94 def virtual_environ_setup(self
):
95 self
.virtual_environ
= virtual_environ
.Virtual_environ(self
.settings
["virtual_environ_path"])
97 self
.virtual_networks
= self
.virtual_environ
.get_networks()
99 self
.virtual_machines
= self
.virtual_environ
.get_machines()
101 def virtual_environ_start(self
):
102 for name
in self
.virtual_environ
.network_names
:
103 self
.virtual_networks
[name
].define()
104 self
.virtual_networks
[name
].start()
106 for name
in self
.virtual_environ
.machine_names
:
107 self
.virtual_machines
[name
].define()
108 self
.virtual_machines
[name
].create_snapshot()
109 # We can only copy files when we know which and to which dir
110 if self
.settings
["copy_from"] and self
.settings
["copy_to"]:
111 self
.virtual_machines
[name
].copy_in(self
.settings
["copy_from"], self
.settings
["copy_to"])
112 self
.virtual_machines
[name
].start()
114 # Time to which all serial output log entries are relativ
115 log_start_time
= time
.time()
117 # Number of chars of the longest machine name
118 longest_machine_name
= self
.virtual_environ
.longest_machine_name
120 self
.log
.info("Try to login on all machines")
121 for name
in self
.virtual_environ
.machine_names
:
122 self
.log
.info("Try to login on {}".format(name
))
123 self
.virtual_machines
[name
].login("{}/test.log".format(self
.log_path
),
124 log_start_time
=log_start_time
,
125 longest_machine_name
=longest_machine_name
)
127 def load_recipe(self
):
128 self
.log
.info("Going to load the recipe")
130 self
.recipe
= recipe
.Recipe(self
.recipe_file
, machines
=self
.virtual_environ
.machine_names
)
131 for line
in self
.recipe
.recipe
:
134 self
.log
.debug("This was the recipe")
135 except BaseException
as e
:
136 self
.log
.error("Failed to load recipe")
139 def run_recipe(self
):
140 for line
in self
.recipe
.recipe
:
141 return_value
= self
.virtual_machines
[line
[0]].cmd(line
[2])
142 self
.log
.debug("Return value is: {}".format(return_value
))
143 if return_value
!= "0" and line
[1] == "":
144 raise TestException("Failed to execute command '{}' on {}, return code: {}".format(line
[2],line
[0], return_value
))
145 elif return_value
== "0" and line
[1] == "!":
146 raise TestException("Succeded to execute command '{}' on {}, return code: {}".format(line
[2],line
[0],return_value
))
148 self
.log
.debug("Command '{}' on {} returned with: {}".format(line
[2],line
[0],return_value
))
150 def virtual_environ_stop(self
):
151 for name
in self
.virtual_environ
.machine_names
:
152 # We just catch exception here to avoid
153 # that we stop the cleanup process if only one command fails
155 self
.virtual_machines
[name
].shutdown()
156 self
.virtual_machines
[name
].revert_snapshot()
157 self
.virtual_machines
[name
].undefine()
158 except BaseException
as e
:
159 self
.log
.exception(e
)
161 for name
in self
.virtual_environ
.network_names
:
163 self
.virtual_networks
[name
].undefine()
164 except BaseException
as e
:
165 self
.log
.exception(e
)