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