]>
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 | raise TestException("No settings file found") | |
36 | ||
37 | self.recipe_file = "{}/recipe".format(self.path) | |
38 | if not os.path.isfile(self.recipe_file): | |
39 | self.log.error("No such file: {}".format(self.recipe_file)) | |
40 | raise TestException("No recipe file found") | |
41 | ||
42 | def read_settings(self): | |
43 | try: | |
44 | self.config = configparser.ConfigParser() | |
45 | self.config.read(self.settings_file) | |
46 | except BaseException as e: | |
47 | self.log.error("Failed to parse the config") | |
48 | raise e | |
49 | ||
50 | self.name = self.config["GENERAL"]["name"] | |
51 | self.description = self.config["GENERAL"]["description"] | |
52 | self.copy_to = self.config["GENERAL"]["copy_to"] | |
53 | self.copy_from = self.config["GENERAL"]["copy_from"] | |
54 | self.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["path"] | |
55 | self.virtual_environ_path = os.path.normpath(self.path + "/" + self.virtual_environ_path) | |
56 | ||
57 | # Parse copy_from setting | |
58 | self.copy_from = self.copy_from.split(",") | |
59 | ||
60 | tmp = [] | |
61 | for file in self.copy_from: | |
62 | file = file.strip() | |
63 | # If file is empty we do not want to add it to the list | |
64 | if not file == "": | |
65 | # If we get an absolut path we do nothing | |
66 | # If not we add self.path to get an absolut path | |
67 | if not os.path.isabs(file): | |
68 | file = os.path.normpath(self.path + "/" + file) | |
69 | ||
70 | # We need to check if file is a valid file or dir | |
71 | if not (os.path.isdir(file) or os.path.isfile(file)): | |
72 | raise TestException("'{}' is not a valid file nor a valid directory".format(file)) | |
73 | ||
74 | self.log.debug("'{}' will be copied into all images".format(file)) | |
75 | tmp.append(file) | |
76 | ||
77 | self.copy_from = tmp | |
78 | ||
79 | ||
80 | ||
81 | def virtual_environ_setup(self): | |
82 | self.virtual_environ = virtual_environ.Virtual_environ(self.virtual_environ_path) | |
83 | ||
84 | self.virtual_networks = self.virtual_environ.get_networks() | |
85 | ||
86 | self.virtual_machines = self.virtual_environ.get_machines() | |
87 | ||
88 | def virtual_environ_start(self): | |
89 | for name in self.virtual_environ.network_names: | |
90 | self.virtual_networks[name].define() | |
91 | self.virtual_networks[name].start() | |
92 | ||
93 | for name in self.virtual_environ.machine_names: | |
94 | self.virtual_machines[name].define() | |
95 | self.virtual_machines[name].create_snapshot() | |
96 | self.virtual_machines[name].copy_in(self.copy_from, self.copy_to) | |
97 | self.virtual_machines[name].start() | |
98 | ||
99 | # Time to which all serial output log entries are relativ | |
100 | log_start_time = time.time() | |
101 | ||
102 | # Number of chars of the longest machine name | |
103 | longest_machine_name = self.virtual_environ.longest_machine_name | |
104 | ||
105 | self.log.info("Try to login on all machines") | |
106 | for name in self.virtual_environ.machine_names: | |
107 | self.log.info("Try to login on {}".format(name)) | |
108 | self.virtual_machines[name].login("{}/test.log".format(self.log_path), | |
109 | log_start_time=log_start_time, | |
110 | longest_machine_name=longest_machine_name) | |
111 | ||
112 | def load_recipe(self): | |
113 | self.log.info("Going to load the recipe") | |
114 | try: | |
115 | self.recipe = recipe.Recipe(self.recipe_file, machines=self.virtual_environ.machine_names) | |
116 | for line in self.recipe.recipe: | |
117 | self.log.debug(line) | |
118 | ||
119 | self.log.debug("This was the recipe") | |
120 | except BaseException as e: | |
121 | self.log.error("Failed to load recipe") | |
122 | raise e | |
123 | ||
124 | def run_recipe(self): | |
125 | for line in self.recipe.recipe: | |
126 | return_value = self.virtual_machines[line[0]].cmd(line[2]) | |
127 | self.log.debug("Return value is: {}".format(return_value)) | |
128 | if return_value != "0" and line[1] == "": | |
129 | raise TestException("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value)) | |
130 | elif return_value == "0" and line[1] == "!": | |
131 | raise TestException("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value)) | |
132 | else: | |
133 | self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value)) | |
134 | ||
135 | def virtual_environ_stop(self): | |
136 | for name in self.virtual_environ.machine_names: | |
137 | # We just catch exception here to avoid | |
138 | # that we stop the cleanup process if only one command fails | |
139 | try: | |
140 | self.virtual_machines[name].shutdown() | |
141 | self.virtual_machines[name].revert_snapshot() | |
142 | self.virtual_machines[name].undefine() | |
143 | except BaseException as e: | |
144 | self.log.exception(e) | |
145 | ||
146 | for name in self.virtual_environ.network_names: | |
147 | try: | |
148 | self.virtual_networks[name].undefine() | |
149 | except BaseException as e: | |
150 | self.log.exception(e) | |
151 |