]> git.ipfire.org Git - nitsi.git/blobdiff - src/nitsi/test.py
Make config parsing more robust against errors
[nitsi.git] / src / nitsi / test.py
index e1ae24cbd3cab719db7ffab0c3eceac06f790d2e..8d29e3047cb57209580d62325b51b6b1376ba319 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,18 +32,29 @@ 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"]
+        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["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.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["path"]
+        self.virtual_environ_path = os.path.normpath(self.path + "/" + self.virtual_environ_path)
+
+        # Parse copy_from setting
         self.copy_from = self.copy_from.split(",")
 
         tmp = []
@@ -65,12 +76,10 @@ class test():
 
         self.copy_from = tmp
 
-        self.virtual_environ_name = self.config["VIRTUAL_ENVIRONMENT"]["Name"]
-        self.virtual_environ_path = self.config["VIRTUAL_ENVIRONMENT"]["Path"]
-        self.virtual_environ_path = os.path.normpath(self.path + "/" + self.virtual_environ_path)
+
 
     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()
 
@@ -103,7 +112,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)