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