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