X-Git-Url: http://git.ipfire.org/?p=nitsi.git;a=blobdiff_plain;f=test.py;h=9ea57981e5b4a47acad742d593c40fe1e965e44f;hp=a3d6a0dca10db9b0fa0562810a8f2c1f2ef36c7b;hb=1ed8ca9f2ddd8d9cd0cf1a47ed1ebb4376a0db53;hpb=ab759374e636cd1f88adf002d664285fedee8833 diff --git a/test.py b/test.py index a3d6a0d..9ea5798 100755 --- a/test.py +++ b/test.py @@ -7,187 +7,20 @@ import os import configparser -from disk import disk - -class log(): - def __init__(self, log_level): - self.log_level = log_level - - def debug(self, string): - if self.log_level >= 4: - print("DEBUG: {}".format(string)) - - def error(self, string): - print("ERROR: {}".format(string)) - -class libvirt_con(): - def __init__(self, uri): - self.log = log(4) - self.uri = uri - self.connection = None - - def get_domain_from_name(self, name): - dom = self.con.lookupByName(name) - - if dom == None: - raise BaseException - return dom - - @property - def con(self): - if self.connection == None: - try: - self.connection = libvirt.open(self.uri) - except BaseException as error: - self.log.error("Could not connect to: {}".format(self.uri)) - - self.log.debug("Connected to: {}".format(self.uri)) - return self.connection - - return self.connection - - - -# A class which define and undefine a virtual network based on an xml file -class network(): - def __init__(self, network_xml_file): - self.log = log(4) - 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 -class recipe(): - def __init__(self, path, circle=[]): - self.log = log(4) - self.recipe_file = path - self.path = os.path.dirname(self.recipe_file) - self.log.debug("Path of recipe is: {}".format(self.recipe_file)) - self._recipe = None - self._machines = None - - self.in_recursion = True - if len(circle) == 0: - self.in_recursion = False - - self.circle = circle - self.log.debug(circle) - self.log.debug(self.circle) - - if not os.path.isfile(self.recipe_file): - self.log.error("No such file: {}".format(self.recipe_file)) - - try: - with open(self.recipe_file) as fobj: - self.raw_recipe = fobj.readlines() - 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 - - @property - def machines(self): - if not self._machines: - self._machines = [] - for line in self._recipe: - if line[0] != "all" and line[0] not in self._machines: - self._machines.append(line[0]) - - return self._machines - - def parse(self): - self._recipe = [] - i = 1 - for line in self.raw_recipe: - 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].strip() - 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 - - if raw_line[0].strip() == "": - self.log.error("Failed to parse the recipe in line {}".format(i)) - raise RecipeExeption - - machine = raw_line[0].strip() - - if len(raw_line) == 2: - extra = raw_line[1].strip() - else: - extra = "" - - # We could get a machine here or a include statement - if machine == "include": - path = cmd.strip() - path = os.path.normpath(self.path + "/" + path) - path = path + "/recipe" - if path in self.circle: - self.log.error("Detect import loop!") - raise RecipeExeption - self.circle.append(path) - recipe_to_include = recipe(path, circle=self.circle) - - if machine == "include": - self._recipe.extend(recipe_to_include.recipe) - else: - # Support also something like 'alice,bob: echo' - machines = machine.split(",") - for machine in machines: - self._recipe.append((machine.strip(), extra.strip(), cmd.strip())) - i = i + 1 - - if not self.in_recursion: - tmp_recipe = [] - for line in self._recipe: - if line[0] != "all": - tmp_recipe.append(line) - else: - for machine in self.machines: - tmp_recipe.append((machine.strip(), line[1], line[2])) - - self._recipe = tmp_recipe +from virtual_environ import virtual_environ +from recipe import recipe +import logging +logger = logging.getLogger("nitsi.test") class test(): def __init__(self, path): - self.log = log(4) try: self.path = os.path.abspath(path) + self.log = logger.getChild(os.path.basename(self.path)) except BaseException as e: - self.log.error("Could not get absolute path") + logger.error("Could not get absolute path") self.log.debug(self.path) @@ -240,14 +73,17 @@ class test(): self.log.debug("Try to login on all machines") for name in self.virtual_environ.machine_names: + self.log.debug("Try to login on {}".format(name)) self.virtual_machines[name].login() def load_recipe(self): try: self.recipe = recipe(self.recipe_file) - except BaseException: + for line in self.recipe.recipe: + self.log.debug(line) + except BaseException as e: self.log.error("Failed to load recipe") - exit(1) + raise e def run_recipe(self): for line in self.recipe.recipe: @@ -272,24 +108,4 @@ class test(): self.virtual_networks[name].undefine() -if __name__ == "__main__": - import argparse - - parser = argparse.ArgumentParser() - - parser.add_argument("-d", "--directory", dest="dir") - - args = parser.parse_args() - - currenttest = test(args.dir) - currenttest.read_settings() - currenttest.virtual_environ_setup() - currenttest.load_recipe() - try: - currenttest.virtual_environ_start() - currenttest.run_recipe() - except BaseException as e: - print(e) - finally: - currenttest.virtual_environ_stop()