]>
git.ipfire.org Git - nitsi.git/blob - src/nitsi/test.py
12 from nitsi
.virtual_environ
import virtual_environ
13 from nitsi
.recipe
import recipe
17 logger
= logging
.getLogger("nitsi.test")
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")
27 self
.log
.debug("Path of this test is: {}".format(self
.path
))
29 self
.log_path
= log_path
31 self
.settings_file
= "{}/settings".format(self
.path
)
32 if not os
.path
.isfile(self
.settings_file
):
33 self
.log
.error("No such file: {}".format(self
.settings_file
))
35 self
.recipe_file
= "{}/recipe".format(self
.path
)
36 if not os
.path
.isfile(self
.recipe_file
):
37 self
.log
.error("No such file: {}".format(self
.recipe_file
))
39 def read_settings(self
):
40 self
.config
= configparser
.ConfigParser()
41 self
.config
.read(self
.settings_file
)
42 self
.name
= self
.config
["DEFAULT"]["Name"]
43 self
.description
= self
.config
["DEFAULT"]["Description"]
44 self
.copy_to
= self
.config
["DEFAULT"]["Copy_to"]
45 self
.copy_from
= self
.config
["DEFAULT"]["Copy_from"]
46 self
.copy_from
= self
.copy_from
.split(",")
49 for file in self
.copy_from
:
51 file = os
.path
.normpath(self
.path
+ "/" + file)
56 self
.virtual_environ_name
= self
.config
["VIRTUAL_ENVIRONMENT"]["Name"]
57 self
.virtual_environ_path
= self
.config
["VIRTUAL_ENVIRONMENT"]["Path"]
58 self
.virtual_environ_path
= os
.path
.normpath(self
.path
+ "/" + self
.virtual_environ_path
)
60 def virtual_environ_setup(self
):
61 self
.virtual_environ
= virtual_environ(self
.virtual_environ_path
)
63 self
.virtual_networks
= self
.virtual_environ
.get_networks()
65 self
.virtual_machines
= self
.virtual_environ
.get_machines()
67 def virtual_environ_start(self
):
68 for name
in self
.virtual_environ
.network_names
:
69 self
.virtual_networks
[name
].define()
70 self
.virtual_networks
[name
].start()
72 for name
in self
.virtual_environ
.machine_names
:
73 self
.virtual_machines
[name
].define()
74 self
.virtual_machines
[name
].create_snapshot()
75 self
.virtual_machines
[name
].copy_in(self
.copy_from
, self
.copy_to
)
76 self
.virtual_machines
[name
].start()
78 # Time to which all serial output log entries are relativ
79 log_start_time
= time
.time()
81 # Number of chars of the longest machine name
82 longest_machine_name
= self
.virtual_environ
.longest_machine_name
84 self
.log
.debug("Try to login on all machines")
85 for name
in self
.virtual_environ
.machine_names
:
86 self
.log
.debug("Try to login on {}".format(name
))
87 self
.virtual_machines
[name
].login("{}/test.log".format(self
.log_path
),
88 log_start_time
=log_start_time
,
89 longest_machine_name
=longest_machine_name
)
91 def load_recipe(self
):
92 self
.log
.info("Going to load the recipe")
94 self
.recipe
= recipe(self
.recipe_file
)
95 for line
in self
.recipe
.recipe
:
98 self
.log
.debug("This was the recipe")
99 except BaseException
as e
:
100 self
.log
.error("Failed to load recipe")
103 def run_recipe(self
):
104 for line
in self
.recipe
.recipe
:
105 return_value
= self
.virtual_machines
[line
[0]].cmd(line
[2])
106 self
.log
.debug("Return value is: {}".format(return_value
))
107 if return_value
!= "0" and line
[1] == "":
108 self
.log
.error("Failed to execute command '{}' on {}, return code: {}".format(line
[2],line
[0], return_value
))
110 elif return_value
== "0" and line
[1] == "!":
111 self
.log
.error("Succeded to execute command '{}' on {}, return code: {}".format(line
[2],line
[0],return_value
))
114 self
.log
.debug("Command '{}' on {} returned with: {}".format(line
[2],line
[0],return_value
))
116 def virtual_environ_stop(self
):
117 for name
in self
.virtual_environ
.machine_names
:
118 self
.virtual_machines
[name
].shutdown()
119 self
.virtual_machines
[name
].revert_snapshot()
120 self
.virtual_machines
[name
].undefine()
122 for name
in self
.virtual_environ
.network_names
:
123 self
.virtual_networks
[name
].undefine()