]> git.ipfire.org Git - people/ms/nitsi.git/blame - src/nitsi/test.py
Improve return codes
[people/ms/nitsi.git] / src / nitsi / test.py
CommitLineData
5e7f6db7
JS
1#!/usr/bin/python3
2
5e7f6db7
JS
3
4import libvirt
5
5e7f6db7
JS
6import os
7
8import configparser
9
6c352a80
JS
10import time
11
2fa4467d
JS
12from nitsi.virtual_environ import virtual_environ
13from nitsi.recipe import recipe
14cd493f 14
1ed8ca9f 15import logging
5e7f6db7 16
1ed8ca9f 17logger = logging.getLogger("nitsi.test")
5e7f6db7 18
61b44c10
JS
19
20class TestException(Exception):
21 def __init__(self, message):
22 self.message = message
23
5e7f6db7 24class test():
d7036f7b 25 def __init__(self, path, log_path):
5e7f6db7
JS
26 try:
27 self.path = os.path.abspath(path)
1ed8ca9f 28 self.log = logger.getChild(os.path.basename(self.path))
5e7f6db7 29 except BaseException as e:
1ed8ca9f 30 logger.error("Could not get absolute path")
5e7f6db7 31
2fa4467d 32 self.log.debug("Path of this test is: {}".format(self.path))
5e7f6db7 33
b4936764
JS
34 self.log_path = log_path
35
5e7f6db7
JS
36 self.settings_file = "{}/settings".format(self.path)
37 if not os.path.isfile(self.settings_file):
38 self.log.error("No such file: {}".format(self.settings_file))
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
44 def read_settings(self):
45 self.config = configparser.ConfigParser()
46 self.config.read(self.settings_file)
47 self.name = self.config["DEFAULT"]["Name"]
48 self.description = self.config["DEFAULT"]["Description"]
14cd493f
JS
49 self.copy_to = self.config["DEFAULT"]["Copy_to"]
50 self.copy_from = self.config["DEFAULT"]["Copy_from"]
51 self.copy_from = self.copy_from.split(",")
52
53 tmp = []
54 for file in self.copy_from:
55 file = file.strip()
56 file = os.path.normpath(self.path + "/" + file)
57 tmp.append(file)
58
59 self.copy_from = tmp
5e7f6db7
JS
60
61 self.virtual_environ_name = self.config["VIRTUAL_ENVIRONMENT"]["Name"]
62 self.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["Path"]
63 self.virtual_environ_path = os.path.normpath(self.path + "/" + self.virtual_environ_path)
64
65 def virtual_environ_setup(self):
66 self.virtual_environ = virtual_environ(self.virtual_environ_path)
67
68 self.virtual_networks = self.virtual_environ.get_networks()
69
70 self.virtual_machines = self.virtual_environ.get_machines()
71
72 def virtual_environ_start(self):
3fa89b7c
JS
73 for name in self.virtual_environ.network_names:
74 self.virtual_networks[name].define()
75 self.virtual_networks[name].start()
5e7f6db7 76
3fa89b7c
JS
77 for name in self.virtual_environ.machine_names:
78 self.virtual_machines[name].define()
79 self.virtual_machines[name].create_snapshot()
14cd493f 80 self.virtual_machines[name].copy_in(self.copy_from, self.copy_to)
3fa89b7c 81 self.virtual_machines[name].start()
5e7f6db7 82
6c352a80
JS
83 # Time to which all serial output log entries are relativ
84 log_start_time = time.time()
85
fc35cba1
JS
86 # Number of chars of the longest machine name
87 longest_machine_name = self.virtual_environ.longest_machine_name
88
3fa89b7c
JS
89 self.log.debug("Try to login on all machines")
90 for name in self.virtual_environ.machine_names:
1ed8ca9f 91 self.log.debug("Try to login on {}".format(name))
fc35cba1
JS
92 self.virtual_machines[name].login("{}/test.log".format(self.log_path),
93 log_start_time=log_start_time,
94 longest_machine_name=longest_machine_name)
5e7f6db7 95
3fa89b7c 96 def load_recipe(self):
2fa4467d 97 self.log.info("Going to load the recipe")
3fa89b7c
JS
98 try:
99 self.recipe = recipe(self.recipe_file)
4bc54b45
JS
100 for line in self.recipe.recipe:
101 self.log.debug(line)
2fa4467d
JS
102
103 self.log.debug("This was the recipe")
4bc54b45 104 except BaseException as e:
3fa89b7c 105 self.log.error("Failed to load recipe")
4bc54b45 106 raise e
3fa89b7c
JS
107
108 def run_recipe(self):
109 for line in self.recipe.recipe:
110 return_value = self.virtual_machines[line[0]].cmd(line[2])
bce7d520
JS
111 self.log.debug("Return value is: {}".format(return_value))
112 if return_value != "0" and line[1] == "":
61b44c10 113 raise TestException("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value))
bce7d520 114 elif return_value == "0" and line[1] == "!":
61b44c10 115 raise TestException("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value))
bce7d520
JS
116 else:
117 self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value))
3fa89b7c
JS
118
119 def virtual_environ_stop(self):
120 for name in self.virtual_environ.machine_names:
121 self.virtual_machines[name].shutdown()
122 self.virtual_machines[name].revert_snapshot()
123 self.virtual_machines[name].undefine()
124
125 for name in self.virtual_environ.network_names:
126 self.virtual_networks[name].undefine()
5e7f6db7
JS
127
128
0a4b6cfb 129