Add the possibility to launch external command like 'nova list', 'nova pause ...... 81/2981/1
authorasteroide <thomas.duval@orange.com>
Mon, 2 Nov 2015 16:45:55 +0000 (17:45 +0100)
committerasteroide <thomas.duval@orange.com>
Mon, 2 Nov 2015 16:45:55 +0000 (17:45 +0100)
Change-Id: Ie563df0161693ea5e1c84cae3d6a3b23712ad3fc

moonclient/moonclient/tests.py
moonclient/moonclient/tests/tests_external_commands.json [new file with mode: 0644]

index 5badf4b..dd0dd7d 100644 (file)
@@ -12,6 +12,7 @@ from cliff.command import Command
 from uuid import uuid4
 import os
 import time
+import subprocess
 
 
 class TestsLaunch(Lister):
@@ -19,7 +20,8 @@ class TestsLaunch(Lister):
 
     log = logging.getLogger(__name__)
     result_vars = dict()
-    logfile = open("/tmp/moonclient_test_{}.log".format(time.strftime("%Y%m%d-%H%M%S")), "w")
+    logfile_name = "/tmp/moonclient_test_{}.log".format(time.strftime("%Y%m%d-%H%M%S"))
+    logfile = open(logfile_name, "w")
 
     def get_parser(self, prog_name):
         parser = super(TestsLaunch, self).get_parser(prog_name)
@@ -31,9 +33,12 @@ class TestsLaunch(Lister):
         return parser
 
     def __replace_var_in_str(self, data_str):
+        self.log.debug("__replace_var_in_str " + data_str)
         for exp in re.findall("\$\w+", data_str):
+            self.log.debug("--->" + exp + str(self.result_vars))
             if exp.replace("$", "") in self.result_vars:
                 data_str = re.sub(exp.replace("$", "\$") + "(?!\w)", self.result_vars[exp.replace("$", "")], data_str)
+        self.log.debug("__replace_var_in_str " + data_str)
         return data_str
 
     def __compare_results(self, expected, observed):
@@ -44,7 +49,7 @@ class TestsLaunch(Lister):
         return False
 
     def take_action(self, parsed_args):
-        self.log.info("Write tests output to {}".format(self.logfile))
+        self.log.info("Write tests output to {}".format(self.logfile_name))
         stdout_back = self.app.stdout
         if not parsed_args.testfile:
             self.log.error("You don't give a test filename.")
@@ -56,37 +61,67 @@ class TestsLaunch(Lister):
             global_command_options = tests_dict["command_options"]
         data = list()
         for group_name, tests_list in tests_dict["tests_group"].iteritems():
+            overall_result = True
             self.log.info("\n\033[1mgroup {}\033[0m".format(group_name))
             self.logfile.write("{}:\n\n".format(group_name))
+            test_count = len(tests_list)
             for test in tests_list:
+                result_str = ""
+                error_str = ""
                 data_tmp = list()
                 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))
+                if "command" not in test:
+                    ext_command = test["external_command"]
+                    ext_command = self.__replace_var_in_str(ext_command)
+                    self.logfile.write("-----> {}\n".format(ext_command))
+                    self.log.info("    \\-executing external \"{}\"".format(ext_command))
+                    pipe = subprocess.Popen(shlex.split(ext_command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+                    com = pipe.communicate()
+                    result_str = com[0]
+                    error_str = com[1]
+                    self.logfile.write("stdout: {}\n".format(result_str))
+                    self.logfile.write("stderr: {}\n".format(error_str))
+                if "command" in 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(group_name)
                 data_tmp.append(test["name"])
                 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:
+                if error_str:
                     compare = "\033[1m\033[31mFalse\033[m"
+                    overall_result = False
+                else:
+                    overall_result = overall_result and compare
+                    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)
+            data_tmp = list()
+            data_tmp.append("\033[1m" + group_name + "\033[m")
+            data_tmp.append("\033[1mOverall results ({})\033[m".format(test_count))
+            if overall_result:
+                data_tmp.append("\033[1m\033[32mTrue\033[m")
+            else:
+                data_tmp.append("\033[1m\033[31mFalse\033[m")
+            data_tmp.append(self.logfile_name)
+            data.append(data_tmp)
 
         return (
             ("group_name", "test_name", "result", "description"),
diff --git a/moonclient/moonclient/tests/tests_external_commands.json b/moonclient/moonclient/tests/tests_external_commands.json
new file mode 100644 (file)
index 0000000..0aa6f70
--- /dev/null
@@ -0,0 +1,109 @@
+{
+  "command_options": "-f value",
+  "tests_group": {
+    "main": [
+      {
+        "name": "list tenant",
+        "command": "tenant list",
+        "result": "(?!demo)",
+        "description": "List all tenants (must be empty)"
+      },
+      {
+        "name": "add tenant demo",
+        "command": "tenant add demo",
+        "result": "^$",
+        "description": "Add a new tenant",
+        "command_options": ""
+      },
+      {
+        "name": "check tenant demo",
+        "command": "tenant list",
+        "result": "(?P<uuid>\\w+)\\s+demo",
+        "description": "Check that tenant demo has been correctly added"
+      },
+      {
+        "name": "create_intraextension_admin",
+        "command": "intraextension add --policy_model policy_rbac_admin admin_test",
+        "result": "IntraExtension created: (?P<uuid_admin>\\w+)",
+        "description": "Create an admin intra extension",
+        "command_options": ""
+      },
+      {
+        "name": "list_intraextension_admin",
+        "command": "intraextension list",
+        "result": "$uuid_admin",
+        "description": "Check the existence of that admin intra extension"
+      },
+      {
+        "name": "create_intraextension_authz",
+        "command": "intraextension add --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": "tenant set --authz $uuid_authz $uuid",
+        "result": "",
+        "description": "Connect the authz intra extension to the tenant demo",
+        "command_options": ""
+      },
+      {
+        "name": "check authz ie for tenant demo",
+        "command": "tenant list",
+        "result": "demo $uuid_authz",
+        "description": "Check that authz ie has been correctly added for tenant demo ",
+        "command_options": "-c name -c intra_authz_extension_id -f value"
+      },
+      {
+        "name": "set_tenant_admin",
+        "command": "tenant set --admin $uuid_admin $uuid",
+        "result": "",
+        "description": "Connect the admin intra extension to the tenant demo",
+        "command_options": ""
+      },
+      {
+        "name": "check admin ie for tenant demo",
+        "command": "tenant list",
+        "result": "demo $uuid_admin",
+        "description": "Check that admin ie has been correctly added for tenant demo ",
+        "command_options": "-c name -c intra_admin_extension_id -f value"
+      },
+
+      {
+        "name": "check nova command",
+        "external_command": "nova list",
+        "result": "\\| (?P<uuid_server>[\\w\\-]+)\\s+\\| (?P<name_server>\\w+)\\s+\\| ACTIVE\\s+\\| [\\w\\-]+\\s+\\| Running",
+        "description": "Check that nova is running and get the ID of one running server"
+      },
+
+      {
+        "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": "Delete the tenant demo",
+        "command_options": ""
+      }
+    ]
+  }
+}
\ No newline at end of file