]>
git.ipfire.org Git - nitsi.git/blob - src/nitsi/test.py
5682752f7cb73537644f52acb89e7ebc9607a858
10 from . import virtual_environ
12 logger
= logging
.getLogger("nitsi.test")
15 class TestException(Exception):
16 def __init__(self
, message
):
17 self
.message
= message
20 def __init__(self
, path
, log_path
):
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")
28 self
.log
.debug("Path of this test is: {}".format(self
.path
))
30 self
.log_path
= log_path
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")
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")
42 def read_settings(self
):
43 self
.config
= configparser
.ConfigParser()
44 self
.config
.read(self
.settings_file
)
45 self
.name
= self
.config
["DEFAULT"]["name"]
46 self
.description
= self
.config
["DEFAULT"]["description"]
47 self
.copy_to
= self
.config
["DEFAULT"]["copy_to"]
48 self
.copy_from
= self
.config
["DEFAULT"]["copy_from"]
49 self
.copy_from
= self
.copy_from
.split(",")
52 for file in self
.copy_from
:
54 # If file is empty we do not want to add it to the list
56 # If we get an absolut path we do nothing
57 # If not we add self.path to get an absolut path
58 if not os
.path
.isabs(file):
59 file = os
.path
.normpath(self
.path
+ "/" + file)
61 # We need to check if file is a valid file or dir
62 if not (os
.path
.isdir(file) or os
.path
.isfile(file)):
63 raise TestException("'{}' is not a valid file nor a valid directory".format(file))
65 self
.log
.debug("'{}' will be copied into all images".format(file))
70 self
.virtual_environ_name
= self
.config
["VIRTUAL_ENVIRONMENT"]["name"]
71 self
.virtual_environ_path
= self
.config
["VIRTUAL_ENVIRONMENT"]["path"]
72 self
.virtual_environ_path
= os
.path
.normpath(self
.path
+ "/" + self
.virtual_environ_path
)
74 def virtual_environ_setup(self
):
75 self
.virtual_environ
= virtual_environ
.Virtual_environ(self
.virtual_environ_path
)
77 self
.virtual_networks
= self
.virtual_environ
.get_networks()
79 self
.virtual_machines
= self
.virtual_environ
.get_machines()
81 def virtual_environ_start(self
):
82 for name
in self
.virtual_environ
.network_names
:
83 self
.virtual_networks
[name
].define()
84 self
.virtual_networks
[name
].start()
86 for name
in self
.virtual_environ
.machine_names
:
87 self
.virtual_machines
[name
].define()
88 self
.virtual_machines
[name
].create_snapshot()
89 self
.virtual_machines
[name
].copy_in(self
.copy_from
, self
.copy_to
)
90 self
.virtual_machines
[name
].start()
92 # Time to which all serial output log entries are relativ
93 log_start_time
= time
.time()
95 # Number of chars of the longest machine name
96 longest_machine_name
= self
.virtual_environ
.longest_machine_name
98 self
.log
.info("Try to login on all machines")
99 for name
in self
.virtual_environ
.machine_names
:
100 self
.log
.info("Try to login on {}".format(name
))
101 self
.virtual_machines
[name
].login("{}/test.log".format(self
.log_path
),
102 log_start_time
=log_start_time
,
103 longest_machine_name
=longest_machine_name
)
105 def load_recipe(self
):
106 self
.log
.info("Going to load the recipe")
108 self
.recipe
= recipe
.Recipe(self
.recipe_file
, machines
=self
.virtual_environ
.machine_names
)
109 for line
in self
.recipe
.recipe
:
112 self
.log
.debug("This was the recipe")
113 except BaseException
as e
:
114 self
.log
.error("Failed to load recipe")
117 def run_recipe(self
):
118 for line
in self
.recipe
.recipe
:
119 return_value
= self
.virtual_machines
[line
[0]].cmd(line
[2])
120 self
.log
.debug("Return value is: {}".format(return_value
))
121 if return_value
!= "0" and line
[1] == "":
122 raise TestException("Failed to execute command '{}' on {}, return code: {}".format(line
[2],line
[0], return_value
))
123 elif return_value
== "0" and line
[1] == "!":
124 raise TestException("Succeded to execute command '{}' on {}, return code: {}".format(line
[2],line
[0],return_value
))
126 self
.log
.debug("Command '{}' on {} returned with: {}".format(line
[2],line
[0],return_value
))
128 def virtual_environ_stop(self
):
129 for name
in self
.virtual_environ
.machine_names
:
130 # We just catch exception here to avoid
131 # that we stop the cleanup process if only one command fails
133 self
.virtual_machines
[name
].shutdown()
134 self
.virtual_machines
[name
].revert_snapshot()
135 self
.virtual_machines
[name
].undefine()
136 except BaseException
as e
:
137 self
.log
.exception(e
)
139 for name
in self
.virtual_environ
.network_names
:
141 self
.virtual_networks
[name
].undefine()
142 except BaseException
as e
:
143 self
.log
.exception(e
)