]> git.ipfire.org Git - nitsi.git/blame - src/nitsi/test.py
Support also a file as base for the log file dir
[nitsi.git] / src / nitsi / test.py
CommitLineData
5e7f6db7
JS
1#!/usr/bin/python3
2
6632e137 3import configparser
5e7f6db7 4import libvirt
6632e137 5import logging
5e7f6db7 6import os
6c352a80
JS
7import time
8
b560f31a
JS
9from . import recipe
10from . import virtual_environ
5e7f6db7 11
1ed8ca9f 12logger = logging.getLogger("nitsi.test")
5e7f6db7 13
61b44c10
JS
14
15class TestException(Exception):
16 def __init__(self, message):
17 self.message = message
18
ee227ea1 19class Test():
ab5b8849 20 def __init__(self, log_path, dir=None, recipe_file=None, settings_file=None, cmd_settings=None):
978854bf
JS
21 # init settings var
22 self.settings = {}
23
ab5b8849
JS
24 self.cmd_settings = cmd_settings
25 self.log_path = log_path
5e7f6db7 26
ab5b8849
JS
27 # Init all vars with None
28 self.settings_file = None
29 self.recipe_file = None
30 self.path = None
5e7f6db7 31
ab5b8849
JS
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")
35
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")
39
40 if dir:
41 try:
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")
46 raise e
47
48 logger.debug("Path of this test is: {}".format(self.path))
49
50 self.recipe_file = "{}/recipe".format(self.path)
51 self.settings_file = "{}/settings".format(self.path)
52
53 if recipe_file:
54 if not os.path.isabs(recipe_file):
55 self.recipe_file = os.path.abspath(recipe_file)
56 else:
57 self.recipe_file = recipe_file
58
59 if settings_file:
60 if not os.path.isabs(settings_file):
61 self.settings_file = os.path.abspath(settings_file)
62 else:
63 self.settings_file = settings_file
b4936764 64
5e7f6db7 65 if not os.path.isfile(self.settings_file):
ab5b8849 66 logger.error("No such file: {}".format(self.settings_file))
250db3b1 67 raise TestException("No settings file found")
5e7f6db7 68
5e7f6db7 69 if not os.path.isfile(self.recipe_file):
ab5b8849 70 logger.error("No such file: {}".format(self.recipe_file))
250db3b1 71 raise TestException("No recipe file found")
5e7f6db7 72
ab5b8849
JS
73 # Init logging
74 if dir:
75 self.log = logger.getChild(os.path.basename(self.path))
76
77 if recipe:
78 self.log = logger.getChild(os.path.basename(self.recipe_file))
978854bf 79
5e7f6db7 80 def read_settings(self):
ab5b8849
JS
81 if self.settings_file:
82 self.log.debug("Going to read all settings from the ini file")
83 try:
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")
88 raise e
89
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)
8428a452 95
978854bf 96 if not self.settings["virtual_environ_path"]:
8428a452
JS
97 self.log.error("No path for virtual environment found.")
98 raise TestException("No path for virtual environment found.")
99
978854bf 100 self.settings["virtual_environ_path"] = os.path.normpath(self.path + "/" + self.settings["virtual_environ_path"])
129c0b0f
JS
101
102 # Parse copy_from setting
978854bf 103 if self.settings["copy_from"]:
8428a452 104 self.log.debug("Going to parse the copy_from setting.")
978854bf 105 self.settings["copy_from"] = self.settings["copy_from"].split(",")
14cd493f 106
8428a452 107 tmp = []
978854bf 108 for file in self.settings["copy_from"]:
8428a452
JS
109 file = file.strip()
110 # If file is empty we do not want to add it to the list
111 if not file == "":
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)
f010720b 116
8428a452
JS
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))
f010720b 120
8428a452
JS
121 self.log.debug("'{}' will be copied into all images".format(file))
122 tmp.append(file)
14cd493f 123
978854bf 124 self.settings["copy_from"] = tmp
5e7f6db7 125
129c0b0f 126
5e7f6db7
JS
127
128 def virtual_environ_setup(self):
978854bf 129 self.virtual_environ = virtual_environ.Virtual_environ(self.settings["virtual_environ_path"])
5e7f6db7
JS
130
131 self.virtual_networks = self.virtual_environ.get_networks()
132
133 self.virtual_machines = self.virtual_environ.get_machines()
134
135 def virtual_environ_start(self):
3fa89b7c
JS
136 for name in self.virtual_environ.network_names:
137 self.virtual_networks[name].define()
138 self.virtual_networks[name].start()
5e7f6db7 139
3fa89b7c
JS
140 for name in self.virtual_environ.machine_names:
141 self.virtual_machines[name].define()
142 self.virtual_machines[name].create_snapshot()
8428a452 143 # We can only copy files when we know which and to which dir
978854bf
JS
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"])
3fa89b7c 146 self.virtual_machines[name].start()
5e7f6db7 147
6c352a80
JS
148 # Time to which all serial output log entries are relativ
149 log_start_time = time.time()
150
fc35cba1
JS
151 # Number of chars of the longest machine name
152 longest_machine_name = self.virtual_environ.longest_machine_name
153
5ff14f2d 154 self.log.info("Try to login on all machines")
3fa89b7c 155 for name in self.virtual_environ.machine_names:
5ff14f2d 156 self.log.info("Try to login on {}".format(name))
fc35cba1
JS
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)
5e7f6db7 160
3fa89b7c 161 def load_recipe(self):
2fa4467d 162 self.log.info("Going to load the recipe")
3fa89b7c 163 try:
ee227ea1 164 self.recipe = recipe.Recipe(self.recipe_file, machines=self.virtual_environ.machine_names)
4bc54b45
JS
165 for line in self.recipe.recipe:
166 self.log.debug(line)
2fa4467d
JS
167
168 self.log.debug("This was the recipe")
4bc54b45 169 except BaseException as e:
3fa89b7c 170 self.log.error("Failed to load recipe")
4bc54b45 171 raise e
3fa89b7c
JS
172
173 def run_recipe(self):
174 for line in self.recipe.recipe:
175 return_value = self.virtual_machines[line[0]].cmd(line[2])
bce7d520
JS
176 self.log.debug("Return value is: {}".format(return_value))
177 if return_value != "0" and line[1] == "":
61b44c10 178 raise TestException("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value))
bce7d520 179 elif return_value == "0" and line[1] == "!":
61b44c10 180 raise TestException("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value))
bce7d520
JS
181 else:
182 self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value))
3fa89b7c
JS
183
184 def virtual_environ_stop(self):
185 for name in self.virtual_environ.machine_names:
f9178ad3
JS
186 # We just catch exception here to avoid
187 # that we stop the cleanup process if only one command fails
188 try:
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)
3fa89b7c
JS
194
195 for name in self.virtual_environ.network_names:
f9178ad3
JS
196 try:
197 self.virtual_networks[name].undefine()
198 except BaseException as e:
199 self.log.exception(e)
200