]>
Commit | Line | Data |
---|---|---|
5e7f6db7 JS |
1 | #!/usr/bin/python3 |
2 | ||
6632e137 | 3 | import configparser |
5e7f6db7 | 4 | import libvirt |
6632e137 | 5 | import logging |
5e7f6db7 | 6 | import os |
6c352a80 JS |
7 | import time |
8 | ||
b560f31a JS |
9 | from . import recipe |
10 | from . import virtual_environ | |
5e7f6db7 | 11 | |
1ed8ca9f | 12 | logger = logging.getLogger("nitsi.test") |
5e7f6db7 | 13 | |
61b44c10 JS |
14 | |
15 | class TestException(Exception): | |
16 | def __init__(self, message): | |
17 | self.message = message | |
18 | ||
5e7f6db7 | 19 | class 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") |
5addee87 | 26 | raise e |
5e7f6db7 | 27 | |
2fa4467d | 28 | self.log.debug("Path of this test is: {}".format(self.path)) |
5e7f6db7 | 29 | |
b4936764 JS |
30 | self.log_path = log_path |
31 | ||
5e7f6db7 JS |
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"] | |
14cd493f JS |
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 | file = os.path.normpath(self.path + "/" + file) | |
53 | tmp.append(file) | |
54 | ||
55 | self.copy_from = tmp | |
5e7f6db7 JS |
56 | |
57 | self.virtual_environ_name = self.config["VIRTUAL_ENVIRONMENT"]["Name"] | |
58 | self.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["Path"] | |
59 | self.virtual_environ_path = os.path.normpath(self.path + "/" + self.virtual_environ_path) | |
60 | ||
61 | def virtual_environ_setup(self): | |
b560f31a | 62 | self.virtual_environ = virtual_environ.virtual_environ(self.virtual_environ_path) |
5e7f6db7 JS |
63 | |
64 | self.virtual_networks = self.virtual_environ.get_networks() | |
65 | ||
66 | self.virtual_machines = self.virtual_environ.get_machines() | |
67 | ||
68 | def virtual_environ_start(self): | |
3fa89b7c JS |
69 | for name in self.virtual_environ.network_names: |
70 | self.virtual_networks[name].define() | |
71 | self.virtual_networks[name].start() | |
5e7f6db7 | 72 | |
3fa89b7c JS |
73 | for name in self.virtual_environ.machine_names: |
74 | self.virtual_machines[name].define() | |
75 | self.virtual_machines[name].create_snapshot() | |
14cd493f | 76 | self.virtual_machines[name].copy_in(self.copy_from, self.copy_to) |
3fa89b7c | 77 | self.virtual_machines[name].start() |
5e7f6db7 | 78 | |
6c352a80 JS |
79 | # Time to which all serial output log entries are relativ |
80 | log_start_time = time.time() | |
81 | ||
fc35cba1 JS |
82 | # Number of chars of the longest machine name |
83 | longest_machine_name = self.virtual_environ.longest_machine_name | |
84 | ||
3fa89b7c JS |
85 | self.log.debug("Try to login on all machines") |
86 | for name in self.virtual_environ.machine_names: | |
1ed8ca9f | 87 | self.log.debug("Try to login on {}".format(name)) |
fc35cba1 JS |
88 | self.virtual_machines[name].login("{}/test.log".format(self.log_path), |
89 | log_start_time=log_start_time, | |
90 | longest_machine_name=longest_machine_name) | |
5e7f6db7 | 91 | |
3fa89b7c | 92 | def load_recipe(self): |
2fa4467d | 93 | self.log.info("Going to load the recipe") |
3fa89b7c | 94 | try: |
b560f31a | 95 | self.recipe = recipe.recipe(self.recipe_file) |
4bc54b45 JS |
96 | for line in self.recipe.recipe: |
97 | self.log.debug(line) | |
2fa4467d JS |
98 | |
99 | self.log.debug("This was the recipe") | |
4bc54b45 | 100 | except BaseException as e: |
3fa89b7c | 101 | self.log.error("Failed to load recipe") |
4bc54b45 | 102 | raise e |
3fa89b7c JS |
103 | |
104 | def run_recipe(self): | |
105 | for line in self.recipe.recipe: | |
106 | return_value = self.virtual_machines[line[0]].cmd(line[2]) | |
bce7d520 JS |
107 | self.log.debug("Return value is: {}".format(return_value)) |
108 | if return_value != "0" and line[1] == "": | |
61b44c10 | 109 | raise TestException("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value)) |
bce7d520 | 110 | elif return_value == "0" and line[1] == "!": |
61b44c10 | 111 | raise TestException("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value)) |
bce7d520 JS |
112 | else: |
113 | self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value)) | |
3fa89b7c JS |
114 | |
115 | def virtual_environ_stop(self): | |
116 | for name in self.virtual_environ.machine_names: | |
f9178ad3 JS |
117 | # We just catch exception here to avoid |
118 | # that we stop the cleanup process if only one command fails | |
119 | try: | |
120 | self.virtual_machines[name].shutdown() | |
121 | self.virtual_machines[name].revert_snapshot() | |
122 | self.virtual_machines[name].undefine() | |
123 | except BaseException as e: | |
124 | self.log.exception(e) | |
3fa89b7c JS |
125 | |
126 | for name in self.virtual_environ.network_names: | |
f9178ad3 JS |
127 | try: |
128 | self.virtual_networks[name].undefine() | |
129 | except BaseException as e: | |
130 | self.log.exception(e) | |
131 |