diff options
author | asteroide <thomas.duval@orange.com> | 2015-09-16 10:41:36 +0200 |
---|---|---|
committer | asteroide <thomas.duval@orange.com> | 2015-09-16 10:41:36 +0200 |
commit | 451454bed17c832740f32cfd8235661e3f5390c3 (patch) | |
tree | e15b4365e95428f18db84f079be8d9caa4533314 /moonclient/moonclient/tests.py | |
parent | abe028e7984be2d7f9dff7aabe461cc82a3b60c0 (diff) |
Add the tests command to MoonClient.
Change-Id: I7f294e7f388af25473aba686958d4d9207580baf
Diffstat (limited to 'moonclient/moonclient/tests.py')
-rw-r--r-- | moonclient/moonclient/tests.py | 54 |
1 files changed, 45 insertions, 9 deletions
diff --git a/moonclient/moonclient/tests.py b/moonclient/moonclient/tests.py index e058fd9b..b259feb6 100644 --- a/moonclient/moonclient/tests.py +++ b/moonclient/moonclient/tests.py @@ -9,44 +9,80 @@ import shlex import re from cliff.lister import Lister from cliff.command import Command +from uuid import uuid4 +import os +import time class TestsLaunch(Lister): - """List all Intra_Extensions.""" + """Tests launcher.""" log = logging.getLogger(__name__) result_vars = dict() + logfile = open("/tmp/moonclient_test_{}.log".format(time.strftime("%Y%m%d-%H%M%S")), "w") def get_parser(self, prog_name): parser = super(TestsLaunch, self).get_parser(prog_name) parser.add_argument( - '--testfile', + 'testfile', metavar='<filename>', help='Filename that contains tests to run', ) return parser def __replace_var_in_str(self, data_str): - for exp in re.findall("\$\w+"): + for exp in re.findall("\$\w+", data_str): if exp.replace("$", "") in self.result_vars: - data_str.replace(exp, self.result_vars[exp.replace("$", "")]) + data_str = data_str.replace(exp, self.result_vars[exp.replace("$", "")]) return data_str def __compare_results(self, expected, observed): - if expected in observed: + match = re.search(expected, observed) + if match: + self.result_vars.update(match.groupdict()) return True return False def take_action(self, parsed_args): + self.log.info("Write tests output to {}".format(self.logfile)) + stdout_back = self.app.stdout + if not parsed_args.testfile: + self.log.error("You don't give a test filename.") + raise Exception("Cannot execute tests.") tests_dict = json.load(open(parsed_args.testfile)) + self.log.debug("tests_dict = {}".format(tests_dict)) + global_command_options = "" + if "command_options" in tests_dict: + global_command_options = tests_dict["command_options"] data = list() - for group_name, tests_list in tests_dict.iteritems(): + for group_name, tests_list in tests_dict["tests_group"].iteritems(): + self.logfile.write("{}:\n\n".format(group_name)) for test in tests_list: data_tmp = list() - command = self.__replace_var_in_str(test["command"]) - result = self.app.run_subcommand(shlex.split(command)) + tmp_filename = os.path.join("/tmp", uuid4().hex) + tmp_filename_fd = open(tmp_filename, "w") + self.log.debug("test={}".format(test)) + if "command_options" in test: + command = test["command"] + " " + test["command_options"] + else: + command = test["command"] + " " + global_command_options + command = self.__replace_var_in_str(command) + self.logfile.write("-----> {}\n".format(command)) + self.log.info("executing {}".format(command)) + self.app.stdout = tmp_filename_fd + result_id = self.app.run_subcommand(shlex.split(command)) + tmp_filename_fd.close() + self.app.stdout = stdout_back + result_str = open(tmp_filename, "r").read() + self.logfile.write("{}".format(result_str)) data_tmp.append(test["name"]) - data_tmp.append(self.__compare_results(test["result"], result)) + compare = self.__compare_results(self.__replace_var_in_str(test["result"]), result_str) + self.logfile.write("----->{} ({})\n\n".format(compare, self.__replace_var_in_str(test["result"]))) + if compare: + compare = "\033[32mTrue\033[m" + else: + compare = "\033[1m\033[31mFalse\033[m" + data_tmp.append(compare) data_tmp.append(test["description"]) data.append(data_tmp) |