]>
git.ipfire.org Git - nitsi.git/blob - src/nitsi/test.py
10 from nitsi
.virtual_environ
import virtual_environ
11 from nitsi
.recipe
import recipe
15 logger
= logging
.getLogger("nitsi.test")
18 def __init__(self
, path
, log_path
):
20 self
.path
= os
.path
.abspath(path
)
21 self
.log
= logger
.getChild(os
.path
.basename(self
.path
))
22 except BaseException
as e
:
23 logger
.error("Could not get absolute path")
25 self
.log
.debug("Path of this test is: {}".format(self
.path
))
27 self
.settings_file
= "{}/settings".format(self
.path
)
28 if not os
.path
.isfile(self
.settings_file
):
29 self
.log
.error("No such file: {}".format(self
.settings_file
))
31 self
.recipe_file
= "{}/recipe".format(self
.path
)
32 if not os
.path
.isfile(self
.recipe_file
):
33 self
.log
.error("No such file: {}".format(self
.recipe_file
))
35 def read_settings(self
):
36 self
.config
= configparser
.ConfigParser()
37 self
.config
.read(self
.settings_file
)
38 self
.name
= self
.config
["DEFAULT"]["Name"]
39 self
.description
= self
.config
["DEFAULT"]["Description"]
40 self
.copy_to
= self
.config
["DEFAULT"]["Copy_to"]
41 self
.copy_from
= self
.config
["DEFAULT"]["Copy_from"]
42 self
.copy_from
= self
.copy_from
.split(",")
45 for file in self
.copy_from
:
47 file = os
.path
.normpath(self
.path
+ "/" + file)
52 self
.virtual_environ_name
= self
.config
["VIRTUAL_ENVIRONMENT"]["Name"]
53 self
.virtual_environ_path
= self
.config
["VIRTUAL_ENVIRONMENT"]["Path"]
54 self
.virtual_environ_path
= os
.path
.normpath(self
.path
+ "/" + self
.virtual_environ_path
)
56 def virtual_environ_setup(self
):
57 self
.virtual_environ
= virtual_environ(self
.virtual_environ_path
)
59 self
.virtual_networks
= self
.virtual_environ
.get_networks()
61 self
.virtual_machines
= self
.virtual_environ
.get_machines()
63 def virtual_environ_start(self
):
64 for name
in self
.virtual_environ
.network_names
:
65 self
.virtual_networks
[name
].define()
66 self
.virtual_networks
[name
].start()
68 for name
in self
.virtual_environ
.machine_names
:
69 self
.virtual_machines
[name
].define()
70 self
.virtual_machines
[name
].create_snapshot()
71 self
.virtual_machines
[name
].copy_in(self
.copy_from
, self
.copy_to
)
72 self
.virtual_machines
[name
].start()
74 self
.log
.debug("Try to login on all machines")
75 for name
in self
.virtual_environ
.machine_names
:
76 self
.log
.debug("Try to login on {}".format(name
))
77 self
.virtual_machines
[name
].login()
79 def load_recipe(self
):
80 self
.log
.info("Going to load the recipe")
82 self
.recipe
= recipe(self
.recipe_file
)
83 for line
in self
.recipe
.recipe
:
86 self
.log
.debug("This was the recipe")
87 except BaseException
as e
:
88 self
.log
.error("Failed to load recipe")
92 for line
in self
.recipe
.recipe
:
93 return_value
= self
.virtual_machines
[line
[0]].cmd(line
[2])
94 self
.log
.debug("Return value is: {}".format(return_value
))
95 if return_value
!= "0" and line
[1] == "":
96 self
.log
.error("Failed to execute command '{}' on {}, return code: {}".format(line
[2],line
[0], return_value
))
98 elif return_value
== "0" and line
[1] == "!":
99 self
.log
.error("Succeded to execute command '{}' on {}, return code: {}".format(line
[2],line
[0],return_value
))
102 self
.log
.debug("Command '{}' on {} returned with: {}".format(line
[2],line
[0],return_value
))
104 def virtual_environ_stop(self
):
105 for name
in self
.virtual_environ
.machine_names
:
106 self
.virtual_machines
[name
].shutdown()
107 self
.virtual_machines
[name
].revert_snapshot()
108 self
.virtual_machines
[name
].undefine()
110 for name
in self
.virtual_environ
.network_names
:
111 self
.virtual_networks
[name
].undefine()