From 61b44c10759477593d6706f5e4ea4acdc7ba393a Mon Sep 17 00:00:00 2001 From: Jonatan Schlag Date: Sun, 13 May 2018 17:18:59 +0200 Subject: [PATCH] Improve return codes This commit adds different return codes for erros in recipe parsing, test errors and other errors. We now return 1 when the test fails, 2 when the recipe parsing fails, 3 on other erros and 0 when everything works fine. Signed-off-by: Jonatan Schlag --- nitsi.in | 26 ++++++++++++++++++++------ src/nitsi/recipe.py | 14 ++++++++------ src/nitsi/test.py | 11 +++++++---- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/nitsi.in b/nitsi.in index 2090e0d..51d5d86 100755 --- a/nitsi.in +++ b/nitsi.in @@ -4,6 +4,10 @@ from nitsi.test import test from nitsi.logger import init_logging import logging +from nitsi.recipe import RecipeExeption + +from nitsi.test import TestException + logger = logging.getLogger("nitsi") logger.setLevel(logging.DEBUG) # create console handler with a higher log level @@ -36,15 +40,25 @@ if __name__ == "__main__": fh.setLevel(logging.DEBUG) logger.addHandler(fh) logger.debug("We now logging everything to {}/general.log".format(log_dir)) + try: + currenttest = test(args.dir, log_dir) + currenttest.read_settings() + currenttest.virtual_environ_setup() + currenttest.load_recipe() + except RecipeExeption as e: + logger.exception(e) + exit(2) - currenttest = test(args.dir, log_dir) - currenttest.read_settings() - currenttest.virtual_environ_setup() - currenttest.load_recipe() try: currenttest.virtual_environ_start() currenttest.run_recipe() + except TestException as e: + logger.exception(e) + exit(1) except BaseException as e: - print(e) + logger.exception(e) + exit(3) finally: - currenttest.virtual_environ_stop() \ No newline at end of file + currenttest.virtual_environ_stop() + + exit(0) \ No newline at end of file diff --git a/src/nitsi/recipe.py b/src/nitsi/recipe.py index b4d3b44..394f50b 100644 --- a/src/nitsi/recipe.py +++ b/src/nitsi/recipe.py @@ -9,7 +9,8 @@ logger = logging.getLogger("nitsi.recipe") class RecipeExeption(Exception): - pass + def __init__(self, message): + self.message = message @@ -39,13 +40,14 @@ class recipe(): if not os.path.isfile(self.recipe_file): self.log.error("{} is not a file".format(self.recipe_file)) - raise RecipeExeption + raise RecipeExeption("{} is not a 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)) + raise error @property def recipe(self): @@ -71,16 +73,16 @@ class recipe(): raw_line = line.split(":", 1) if len(raw_line) < 2: self.log.error("Error parsing the recipe in line {}".format(i)) - raise RecipeExeption + raise RecipeExeption("Error parsing the recipe in line {}".format(i)) 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 + raise RecipeExeption("Failed to parse the recipe in line {}".format(i)) if raw_line[0].strip() == "": self.log.error("Failed to parse the recipe in line {}".format(i)) - raise RecipeExeption + raise RecipeExeption("Failed to parse the recipe in line {}".format(i)) machine = raw_line[0].strip() @@ -96,7 +98,7 @@ class recipe(): path = path + "/recipe" if path in self.circle: self.log.error("Detect import loop!") - raise RecipeExeption + raise RecipeExeption("Detect import loop!") self.circle.append(path) recipe_to_include = recipe(path, circle=self.circle) diff --git a/src/nitsi/test.py b/src/nitsi/test.py index ecbe5f0..1f7728f 100755 --- a/src/nitsi/test.py +++ b/src/nitsi/test.py @@ -16,6 +16,11 @@ import logging logger = logging.getLogger("nitsi.test") + +class TestException(Exception): + def __init__(self, message): + self.message = message + class test(): def __init__(self, path, log_path): try: @@ -105,11 +110,9 @@ class test(): return_value = self.virtual_machines[line[0]].cmd(line[2]) self.log.debug("Return value is: {}".format(return_value)) if return_value != "0" and line[1] == "": - self.log.error("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value)) - return False + raise TestException("Failed to execute command '{}' on {}, return code: {}".format(line[2],line[0], return_value)) elif return_value == "0" and line[1] == "!": - self.log.error("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value)) - return False + raise TestException("Succeded to execute command '{}' on {}, return code: {}".format(line[2],line[0],return_value)) else: self.log.debug("Command '{}' on {} returned with: {}".format(line[2],line[0],return_value)) -- 2.39.2