]> git.ipfire.org Git - nitsi.git/blobdiff - src/nitsi/test.py
Provide fallback value if we fail to read the setting
[nitsi.git] / src / nitsi / test.py
index c46d7df680e8f8819c5c80322b4b38b89433fc7c..6ddf96766eb8cff46004fe5b407b3f6277b08662 100755 (executable)
@@ -16,7 +16,7 @@ class TestException(Exception):
     def __init__(self, message):
         self.message = message
 
-class test():
+class Test():
     def __init__(self, path, log_path):
         try:
             self.path = os.path.abspath(path)
@@ -32,34 +32,62 @@ class test():
         self.settings_file = "{}/settings".format(self.path)
         if not os.path.isfile(self.settings_file):
             self.log.error("No such file: {}".format(self.settings_file))
+            raise TestException("No settings file found")
 
         self.recipe_file = "{}/recipe".format(self.path)
         if not os.path.isfile(self.recipe_file):
             self.log.error("No such file: {}".format(self.recipe_file))
+            raise TestException("No recipe file found")
 
     def read_settings(self):
-        self.config = configparser.ConfigParser()
-        self.config.read(self.settings_file)
-        self.name = self.config["DEFAULT"]["Name"]
-        self.description = self.config["DEFAULT"]["Description"]
-        self.copy_to = self.config["DEFAULT"]["Copy_to"]
-        self.copy_from = self.config["DEFAULT"]["Copy_from"]
-        self.copy_from = self.copy_from.split(",")
-
-        tmp = []
-        for file in self.copy_from:
-            file = file.strip()
-            file = os.path.normpath(self.path + "/" + file)
-            tmp.append(file)
-
-        self.copy_from = tmp
-
-        self.virtual_environ_name = self.config["VIRTUAL_ENVIRONMENT"]["Name"]
-        self.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["Path"]
+        self.log.debug("Going to read all settings from the ini file")
+        try:
+            self.config = configparser.ConfigParser()
+            self.config.read(self.settings_file)
+        except BaseException as e:
+            self.log.error("Failed to parse the config")
+            raise e
+
+        self.name = self.config.get("GENERAL","name", fallback="")
+        self.description = self.config.get("GENERAL", "description",  fallback="")
+        self.copy_to = self.config.get("GENERAL", "copy_to", fallback=None)
+        self.copy_from = self.config.get("GENERAL", "copy_from", fallback=None)
+        self.virtual_environ_path = self.config.get("VIRTUAL_ENVIRONMENT", "path", fallback=None)
+
+        if not self.virtual_environ_path:
+            self.log.error("No path for virtual environment found.")
+            raise TestException("No path for virtual environment found.")
+
         self.virtual_environ_path = os.path.normpath(self.path + "/" + self.virtual_environ_path)
 
+        # Parse copy_from setting
+        if self.copy_from:
+            self.log.debug("Going to parse the copy_from setting.")
+            self.copy_from = self.copy_from.split(",")
+
+            tmp = []
+            for file in self.copy_from:
+                file = file.strip()
+                # If file is empty we do not want to add it to the list
+                if not file == "":
+                    # If we get an absolut path we do nothing
+                    # If not we add self.path to get an absolut path
+                    if not os.path.isabs(file):
+                        file = os.path.normpath(self.path + "/" + file)
+
+                    # We need to check if file is a valid file or dir
+                    if not (os.path.isdir(file) or os.path.isfile(file)):
+                        raise TestException("'{}' is not a valid file nor a valid directory".format(file))
+
+                    self.log.debug("'{}' will be copied into all images".format(file))
+                    tmp.append(file)
+
+            self.copy_from = tmp
+
+
+
     def virtual_environ_setup(self):
-        self.virtual_environ = virtual_environ.virtual_environ(self.virtual_environ_path)
+        self.virtual_environ = virtual_environ.Virtual_environ(self.virtual_environ_path)
 
         self.virtual_networks = self.virtual_environ.get_networks()
 
@@ -73,7 +101,9 @@ class test():
         for name in self.virtual_environ.machine_names:
             self.virtual_machines[name].define()
             self.virtual_machines[name].create_snapshot()
-            self.virtual_machines[name].copy_in(self.copy_from, self.copy_to)
+            # We can only copy files when we know which and to which dir
+            if self.copy_from and self.copy_to:
+                self.virtual_machines[name].copy_in(self.copy_from, self.copy_to)
             self.virtual_machines[name].start()
 
         # Time to which all serial output log entries are relativ
@@ -82,9 +112,9 @@ class test():
         # Number of chars of the longest machine name
         longest_machine_name = self.virtual_environ.longest_machine_name
 
-        self.log.debug("Try to login on all machines")
+        self.log.info("Try to login on all machines")
         for name in self.virtual_environ.machine_names:
-            self.log.debug("Try to login on {}".format(name))
+            self.log.info("Try to login on {}".format(name))
             self.virtual_machines[name].login("{}/test.log".format(self.log_path),
                                                 log_start_time=log_start_time,
                                                 longest_machine_name=longest_machine_name)
@@ -92,7 +122,7 @@ class test():
     def load_recipe(self):
         self.log.info("Going to load the recipe")
         try:
-            self.recipe = recipe.recipe(self.recipe_file)
+            self.recipe = recipe.Recipe(self.recipe_file, machines=self.virtual_environ.machine_names)
             for line in self.recipe.recipe:
                 self.log.debug(line)