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