Add the tests command to MoonClient. 13/1613/1
authorasteroide <thomas.duval@orange.com>
Wed, 16 Sep 2015 08:41:36 +0000 (10:41 +0200)
committerasteroide <thomas.duval@orange.com>
Wed, 16 Sep 2015 08:41:36 +0000 (10:41 +0200)
Change-Id: I7f294e7f388af25473aba686958d4d9207580baf

moonclient/moonclient/tests.py
moonclient/moonclient/tests/tests_tenants.json
moonclient/setup.py

index e058fd9..b259feb 100644 (file)
@@ -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)
 
index 76e2b10..ca2d228 100644 (file)
@@ -1,24 +1,32 @@
 {
-  "command_prefix": "moon",
+  "command_options": "-f value",
   "tests_group": {
     "group1": [
       {
         "name": "list tenant",
         "command": "tenant list",
-        "result": "^$",
+        "result": "(?!alt_demo)",
         "description": "List all tenants (must be empty)"
       },
       {
-        "name": "add tenant demo",
-        "command": "tenant add demo",
-        "result": "Tenant created: (?P<uuid>\\w+)",
-        "description": "Add a new tenant"
+        "name": "add tenant alt_demo",
+        "command": "tenant add alt_demo",
+        "result": "^$",
+        "description": "Add a new tenant",
+        "command_options": ""
+      },
+      {
+        "name": "check tenant alt_demo",
+        "command": "tenant list",
+        "result": "(?P<uuid>\\w+)\\s+alt_demo",
+        "description": "Check that tenant alt_demo has been correctly added"
       },
       {
         "name": "create_intraextension_admin",
-        "command": "intraextension create --policy_model policy_admin func_test",
-        "result": "%uuid_admin%",
-        "description": "Create an admin intra extension"
+        "command": "intraextension create --policy_model policy_admin admin_test",
+        "result": "IntraExtension created: (?P<uuid_admin>\\w+)",
+        "description": "Create an admin intra extension",
+        "command_options": ""
       },
       {
         "name": "list_intraextension_admin",
         "result": "$uuid_admin",
         "description": "Check the existence of that admin intra extension"
       },
+      {
+        "name": "create_intraextension_authz",
+        "command": "intraextension create --policy_model policy_authz authz_test",
+        "result": "IntraExtension created: (?P<uuid_authz>\\w+)",
+        "description": "Create an authz intra extension",
+        "command_options": ""
+      },
+      {
+        "name": "list_intraextension_authz",
+        "command": "intraextension list",
+        "result": "$uuid_authz",
+        "description": "Check the existence of that authz intra extension"
+      },
       {
         "name": "set_tenant_authz",
-        "command": "intraextension tenant set authz $uuid_authz demo",
+        "command": "tenant set --authz $uuid_authz demo",
         "result": "",
-        "description": "Connect the authz intra extension to the tenant demo"
+        "description": "Connect the authz intra extension to the tenant demo",
+        "command_options": ""
       },
       {
         "name": "set_tenant_admin",
-        "command": "intraextension tenant set authz $uuid_authz demo",
+        "command": "tenant set --admin $uuid_admin demo",
+        "result": "",
+        "description": "Connect the admin intra extension to the tenant demo",
+        "command_options": ""
+      },
+      {
+        "name": "delete_admin_intra_extension",
+        "command": "intraextension delete $uuid_admin",
+        "result": "",
+        "description": "Delete the admin intra extension",
+        "command_options": ""
+      },
+      {
+        "name": "delete_authz_intra_extension",
+        "command": "intraextension delete $uuid_authz",
+        "result": "",
+        "description": "Delete the authz intra extension",
+        "command_options": ""
+      },
+      {
+        "name": "delete_tenant",
+        "command": "tenant delete $uuid",
         "result": "",
-        "description": "Connect the admin intra extension to the tenant demo"
+        "description": "Delete the tenant alt_demo",
+        "command_options": ""
       }
     ]
   }
index 2da0ab1..39afdf8 100644 (file)
@@ -120,6 +120,8 @@ setup(
             'rule_add = moonclient.rules:RuleAdd',
             'rule_delete = moonclient.rules:RuleDelete',
             'log = moonclient.logs:LogsList',
+
+            'tests = moonclient.tests:TestsLaunch',
         ],
     },