Use one time to which all serial connection output is relativ
[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
5e7f6db7 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")
5e7f6db7 26
2fa4467d 27 self.log.debug("Path of this test is: {}".format(self.path))
5e7f6db7 28
b4936764
JS
29 self.log_path = log_path
30
5e7f6db7
JS
31 self.settings_file = "{}/settings".format(self.path)
32 if not os.path.isfile(self.settings_file):
33 self.log.error("No such file: {}".format(self.settings_file))
34
35 self.recipe_file = "{}/recipe".format(self.path)
36 if not os.path.isfile(self.recipe_file):
37 self.log.error("No such file: {}".format(self.recipe_file))
38
39 def read_settings(self):
40 self.config = configparser.ConfigParser()
41 self.config.read(self.settings_file)
42 self.name = self.config["DEFAULT"]["Name"]
43 self.description = self.config["DEFAULT"]["Description"]
14cd493f
JS
44 self.copy_to = self.config["DEFAULT"]["Copy_to"]
45 self.copy_from = self.config["DEFAULT"]["Copy_from"]
46 self.copy_from = self.copy_from.split(",")
47
48 tmp = []
49 for file in self.copy_from:
50 file = file.strip()
51 file = os.path.normpath(self.path + "/" + file)
52 tmp.append(file)
53
54 self.copy_from = tmp
5e7f6db7
JS
55
56 self.virtual_environ_name = self.config["VIRTUAL_ENVIRONMENT"]["Name"]
57 self.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["Path"]
58 self.virtual_environ_path = os.path.normpath(self.path + "/" + self.virtual_environ_path)
59
60 def virtual_environ_setup(self):
61 self.virtual_environ = virtual_environ(self.virtual_environ_path)
62
63 self.virtual_networks = self.virtual_environ.get_networks()
64
65 self.virtual_machines = self.virtual_environ.get_machines()
66
67 def virtual_environ_start(self):
3fa89b7c
JS
68 for name in self.virtual_environ.network_names:
69 self.virtual_networks[name].define()
70 self.virtual_networks[name].start()
5e7f6db7 71
3fa89b7c
JS
72 for name in self.virtual_environ.machine_names:
73 self.virtual_machines[name].define()
74 self.virtual_machines[name].create_snapshot()
14cd493f 75 self.virtual_machines[name].copy_in(self.copy_from, self.copy_to)
3fa89b7c 76 self.virtual_machines[name].start()
5e7f6db7 77
6c352a80
JS
78 # Time to which all serial output log entries are relativ
79 log_start_time = time.time()
80
3fa89b7c
JS
81 self.log.debug("Try to login on all machines")
82 for name in self.virtual_environ.machine_names:
1ed8ca9f 83 self.log.debug("Try to login on {}".format(name))
6c352a80 84 self.virtual_machines[name].login("{}/test.log".format(self.log_path), log_start_time)
5e7f6db7 85
3fa89b7c 86 def load_recipe(self):
2fa4467d 87 self.log.info("Going to load the recipe")
3fa89b7c
JS
88 try:
89 self.recipe = recipe(self.recipe_file)
4bc54b45
JS
90 for line in self.recipe.recipe:
91 self.log.debug(line)
2fa4467d
JS
92
93 self.log.debug("This was the recipe")
4bc54b45 94 except BaseException as e:
3fa89b7c 95 self.log.error("Failed to load recipe")
4bc54b45 96 raise e
3fa89b7c
JS
97
98 def run_recipe(self):
99 for line in self.recipe.recipe:
100 return_value = self.virtual_machines[line[0]].cmd(line[2])
bce7d520
JS
101 self.log.debug("Return value is: {}".format(return_value))
102 if return_value != "0" and line[1] == "":
103 self.log.error("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value))
3fa89b7c 104 return False
bce7d520
JS
105 elif return_value == "0" and line[1] == "!":
106 self.log.error("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value))
3fa89b7c 107 return False
bce7d520
JS
108 else:
109 self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value))
3fa89b7c
JS
110
111 def virtual_environ_stop(self):
112 for name in self.virtual_environ.machine_names:
113 self.virtual_machines[name].shutdown()
114 self.virtual_machines[name].revert_snapshot()
115 self.virtual_machines[name].undefine()
116
117 for name in self.virtual_environ.network_names:
118 self.virtual_networks[name].undefine()
5e7f6db7
JS
119
120
0a4b6cfb 121