]> git.ipfire.org Git - nitsi.git/blob - src/nitsi/test.py
Add self.settings and store all settings in a dictionary
[nitsi.git] / src / nitsi / test.py
1 #!/usr/bin/python3
2
3 import configparser
4 import libvirt
5 import logging
6 import os
7 import time
8
9 from . import recipe
10 from . import virtual_environ
11
12 logger = logging.getLogger("nitsi.test")
13
14
15 class TestException(Exception):
16 def __init__(self, message):
17 self.message = message
18
19 class Test():
20 def __init__(self, path, log_path, settings=None):
21 # init settings var
22 self.settings = {}
23
24 try:
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")
29 raise e
30
31 self.log.debug("Path of this test is: {}".format(self.path))
32
33 self.log_path = log_path
34
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")
39
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")
44
45 self.cmd_settings = settings
46
47 def read_settings(self):
48 self.log.debug("Going to read all settings from the ini file")
49 try:
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")
54 raise e
55
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)
61
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.")
65
66 self.settings["virtual_environ_path"] = os.path.normpath(self.path + "/" + self.settings["virtual_environ_path"])
67
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(",")
72
73 tmp = []
74 for file in self.settings["copy_from"]:
75 file = file.strip()
76 # If file is empty we do not want to add it to the list
77 if not file == "":
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)
82
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))
86
87 self.log.debug("'{}' will be copied into all images".format(file))
88 tmp.append(file)
89
90 self.settings["copy_from"] = tmp
91
92
93
94 def virtual_environ_setup(self):
95 self.virtual_environ = virtual_environ.Virtual_environ(self.settings["virtual_environ_path"])
96
97 self.virtual_networks = self.virtual_environ.get_networks()
98
99 self.virtual_machines = self.virtual_environ.get_machines()
100
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()
105
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()
113
114 # Time to which all serial output log entries are relativ
115 log_start_time = time.time()
116
117 # Number of chars of the longest machine name
118 longest_machine_name = self.virtual_environ.longest_machine_name
119
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)
126
127 def load_recipe(self):
128 self.log.info("Going to load the recipe")
129 try:
130 self.recipe = recipe.Recipe(self.recipe_file, machines=self.virtual_environ.machine_names)
131 for line in self.recipe.recipe:
132 self.log.debug(line)
133
134 self.log.debug("This was the recipe")
135 except BaseException as e:
136 self.log.error("Failed to load recipe")
137 raise e
138
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))
147 else:
148 self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value))
149
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
154 try:
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)
160
161 for name in self.virtual_environ.network_names:
162 try:
163 self.virtual_networks[name].undefine()
164 except BaseException as e:
165 self.log.exception(e)
166