]> git.ipfire.org Git - nitsi.git/blobdiff - test.py
Add recipe parsing functionality
[nitsi.git] / test.py
diff --git a/test.py b/test.py
index 37876551acde3cce786b0360fd642a26a8e080f2..0507d04570a0eabe390c5551f29c46c0f78103c3 100755 (executable)
--- a/test.py
+++ b/test.py
@@ -72,7 +72,7 @@ class vm():
         self.image = image
 
         if not os.path.isfile(self.image):
-            self.log.error("No such file: {}".format(self.settings_file))
+            self.log.error("No such file: {}".format(self.image))
 
         self.root_uid = root_uid
 
@@ -109,7 +109,7 @@ class vm():
     def revert_snapshot(self):
         print(inspect.getmembers(self.dom, predicate=inspect.ismethod))
         self.dom.revertToSnapshot(self.snapshot)
-        #self.dom.SnapshotDelete(self.snapshot)
+        self.snapshot.delete()
 
     def is_running(self):
 
@@ -353,9 +353,33 @@ class connection():
 
 # A class which define and undefine a virtual network based on an xml file
 class network():
-    def __init__(self, path):
+    def __init__(self, network_xml_file):
         self.log = log(4)
-        self.log.debug("Path is: {}".format(path))
+        self.con = libvirt_con("qemu:///system")
+        try:
+            with open(network_xml_file) as fobj:
+                self.network_xml = fobj.read()
+        except FileNotFoundError as error:
+            self.log.error("No such file: {}".format(vm_xml_file))
+
+    def define(self):
+        self.network = self.con.con.networkDefineXML(self.network_xml)
+
+        if network == None:
+            self.log.error("Failed to define virtual network")
+
+    def start(self):
+        self.network.create()
+
+    def undefine(self):
+        self.network.destroy()
+
+
+
+class RecipeExeption(Exception):
+    pass
+
+
 
 # Should read the test, check if the syntax are valid
 # and return tuples with the ( host, command ) structure
@@ -363,6 +387,8 @@ class recipe():
     def __init__(self, path):
         self.log = log(4)
         self.recipe_file = path
+        self._recipe = None
+
         if not os.path.isfile(self.recipe_file):
             self.log.error("No such file: {}".format(self.recipe_file))
 
@@ -372,8 +398,38 @@ class recipe():
         except FileNotFoundError as error:
             self.log.error("No such file: {}".format(vm_xml_file))
 
+    @property
+    def recipe(self):
+        if not self._recipe:
+            self.parse()
+
+        return self._recipe
+
+    def parse(self):
+        self._recipe = []
+        i = 1
         for line in self.raw_recipe:
-            print(line)
+            raw_line = line.split(":")
+            if len(raw_line) < 2:
+                self.log.error("Error parsing the recipe in line {}".format(i))
+                raise RecipeExeption
+            cmd = raw_line[1]
+            raw_line = raw_line[0].strip().split(" ")
+            if len(raw_line) == 0:
+                self.log.error("Failed to parse the recipe in line {}".format(i))
+                raise RecipeExeption
+            elif len(raw_line) == 1:
+                if raw_line[0] == "":
+                    self.log.error("Failed to parse the recipe in line {}".format(i))
+                    raise RecipeExeption
+                machine = raw_line[0]
+                extra = ""
+            elif len(raw_line) == 2:
+                machine = raw_line[0]
+                extra = raw_line[1]
+
+            self._recipe.append((machine.strip(), extra.strip(), cmd.strip()))
+            i = i + 1
 
 
 class test():