]> git.ipfire.org Git - nitsi.git/blobdiff - src/nitsi/test.py
Fix check if we get correct files for settings and recipe
[nitsi.git] / src / nitsi / test.py
index 6ddf96766eb8cff46004fe5b407b3f6277b08662..9cc1188c2b1bdb09144af37055b0eecb1758104e 100755 (executable)
@@ -17,56 +17,109 @@ class TestException(Exception):
         self.message = message
 
 class Test():
-    def __init__(self, path, log_path):
-        try:
-            self.path = os.path.abspath(path)
-            self.log = logger.getChild(os.path.basename(self.path))
-        except BaseException as e:
-            logger.error("Could not get absolute path")
-            raise e
+    def __init__(self, log_path, dir=None, recipe_file=None, settings_file=None, cmd_settings=None):
+        # init settings var
+        self.settings = {}
+
+        # Set default values for the settings dict
+        self.settings["name"] = ""
+        self.settings["description"] = ""
+        self.settings["copy_from"] = None
+        self.settings["copy_to"] = None
+        self.settings["virtual_environ_path"] = None
+
+        self.cmd_settings = cmd_settings
+        self.log_path = log_path
 
-        self.log.debug("Path of this test is: {}".format(self.path))
+        # Init all vars with None
+        self.settings_file = None
+        self.recipe_file = None
+        self.path = None
 
-        self.log_path = log_path
+        # We need at least a path to a recipe file or a dir to a test
+        if not dir and not recipe:
+            raise TestException("Did not get a path to a test or to a recipe file")
+
+        # We cannot decide which to use when we get both
+        if (dir and recipe_file) or (dir and settings_file):
+            raise TestException("Get dir and path to recipe or settings file")
 
-        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")
+        if dir:
+            try:
+                if not os.path.isabs(dir):
+                    self.path = os.path.abspath(dir)
+            except BaseException as e:
+                logger.error("Could not get absolute path")
+                raise e
 
-        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")
+            logger.debug("Path of this test is: {}".format(self.path))
 
-    def read_settings(self):
-        self.log.debug("Going to read all settings from the ini file")
+            self.recipe_file = "{}/recipe".format(self.path)
+            self.settings_file = "{}/settings".format(self.path)
+
+        if recipe_file:
+            if not os.path.isabs(recipe_file):
+                self.recipe_file = os.path.abspath(recipe_file)
+            else:
+                self.recipe_file = recipe_file
+
+        if settings_file:
+            if not os.path.isabs(settings_file):
+                self.settings_file = os.path.abspath(settings_file)
+            else:
+                self.settings_file = settings_file
+
+        # We can also go on without a settings file
+        if self.settings_file:
+            if not os.path.isfile(self.settings_file):
+                logger.error("No such file: {}".format(self.settings_file))
+                raise TestException("No settings file found")
+
+        # os.path.isfile fails if self.recipe_file is None so we need to catch exceptions here
         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
+            if not (self.recipe_file or os.path.isfile(self.recipe_file)):
+                logger.error("No such file: {}".format(self.recipe_file))
+                raise TestException("No recipe file found")
+        except BaseException:
+            pass
+
+
+        # Init logging
+        if dir:
+             self.log = logger.getChild(os.path.basename(self.path))
+
+        if recipe:
+            self.log = logger.getChild(os.path.basename(self.recipe_file))
+
+    def read_settings(self):
+        if self.settings_file:
+            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)
+            self.settings["name"] = self.config.get("GENERAL","name", fallback="")
+            self.settings["description"] = self.config.get("GENERAL", "description",  fallback="")
+            self.settings["copy_to"] = self.config.get("GENERAL", "copy_to", fallback=None)
+            self.settings["copy_from"] = self.config.get("GENERAL", "copy_from", fallback=None)
+            self.settings["virtual_environ_path"] = self.config.get("VIRTUAL_ENVIRONMENT", "path", fallback=None)
 
-        if not self.virtual_environ_path:
+        if not self.settings["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)
+        self.settings["virtual_environ_path"] = os.path.normpath(self.path + "/" + self.settings["virtual_environ_path"])
 
         # Parse copy_from setting
-        if self.copy_from:
+        if self.settings["copy_from"]:
             self.log.debug("Going to parse the copy_from setting.")
-            self.copy_from = self.copy_from.split(",")
+            self.settings["copy_from"] = self.settings["copy_from"].split(",")
 
             tmp = []
-            for file in self.copy_from:
+            for file in self.settings["copy_from"]:
                 file = file.strip()
                 # If file is empty we do not want to add it to the list
                 if not file == "":
@@ -82,12 +135,12 @@ class Test():
                     self.log.debug("'{}' will be copied into all images".format(file))
                     tmp.append(file)
 
-            self.copy_from = tmp
+            self.settings["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.settings["virtual_environ_path"])
 
         self.virtual_networks = self.virtual_environ.get_networks()
 
@@ -102,8 +155,8 @@ class Test():
             self.virtual_machines[name].define()
             self.virtual_machines[name].create_snapshot()
             # 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)
+            if self.settings["copy_from"] and self.settings["copy_to"]:
+                self.virtual_machines[name].copy_in(self.settings["copy_from"], self.settings["copy_to"])
             self.virtual_machines[name].start()
 
         # Time to which all serial output log entries are relativ