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