Add 'special' test operation allowing to change authentication (name, url, ...) durin... 55/3155/1
authorasteroide <thomas.duval@orange.com>
Mon, 9 Nov 2015 15:07:16 +0000 (16:07 +0100)
committerasteroide <thomas.duval@orange.com>
Mon, 9 Nov 2015 15:07:16 +0000 (16:07 +0100)
Change-Id: I44f91b19d438dc92183d58f99047228ea5dc257a

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

index ce59b3c..49422a4 100644 (file)
@@ -154,6 +154,24 @@ class MoonClient(App):
         except ValueError:
             return {"content": content}
 
+    def auth_keystone(self, username=None, password=None, host=None, port=None):
+        """Send a new authentication request to Keystone
+
+        :param username: user identification name
+        :return:
+        """
+        if username:
+            self.post["auth"]["identity"]["password"]["user"]["name"] = username
+        if password:
+            self.post["auth"]["identity"]["password"]["user"]["password"] = password
+        if host:
+            self.host = host
+        if port:
+            self.port = port
+        data = self.get_url("/v3/auth/tokens", post_data=self.post)
+        if "token" not in data:
+            raise Exception("Authentication problem ({})".format(data))
+
     def initialize_app(self, argv):
         self.log.debug('initialize_app: {}'.format(argv))
         if self.options.username:
index a97ba61..ea72295 100644 (file)
@@ -68,16 +68,57 @@ class TestsLaunch(Lister):
             for test in tests_list:
                 result_str = ""
                 error_str = ""
+                if "auth_name" in test or "auth_password" in test or "auth_url" in test:
+                    username = None
+                    password = None
+                    host = None
+                    port = None
+                    description = ""
+                    if "auth_name" in test:
+                        username = test["auth_name"]
+                    if "auth_password" in test:
+                        password = test["auth_password"]
+                    if "auth_host" in test:
+                        host = test["auth_host"]
+                    if "auth_port" in test:
+                        port = test["auth_port"]
+                    if "description" in test:
+                        description = test["description"]
+                    self.app.auth_keystone(username, password, host, port)
+                    title = "Change auth to "
+                    if username:
+                        title += username
+                    if host:
+                        title += "@" + host
+                    if port:
+                        title += ":" + port
+                    title += "\n"
+                    self.logfile.write(title)
+                    self.log.info(title)
+                    data_tmp = list()
+                    data_tmp.append("")
+                    data_tmp.append(title.strip())
+                    data_tmp.append("\033[32mOK\033[m")
+                    data_tmp.append(description.strip())
+                    data.append(data_tmp)
+                    continue
                 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" not in test:
-                    ext_command = test["external_command"]
+                    if "external_command" in test:
+                        ext_command = test["external_command"]
+                    else:
+                        ext_command = test["shell_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)
+                    if "external_command" in test:
+                        pipe = subprocess.Popen(shlex.split(ext_command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+                    else:
+                        # Note (asteroide): security hazard! Must reduce the possible commands here.
+                        pipe = subprocess.Popen(ext_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                     com = pipe.communicate()
                     result_str = com[0]
                     error_str = com[1]
@@ -104,14 +145,17 @@ class TestsLaunch(Lister):
                 if error_str:
                     if compare:
                         compare = "\033[33mTrue\033[m"
-                        overall_result = True
+                        overall_result = overall_result and True
                     else:
                         compare = "\033[1m\033[31mFalse\033[m"
-                        overall_result = False
+                        overall_result = overall_result and False
                 else:
                     overall_result = overall_result and compare
                     if compare:
-                        compare = "\033[32mTrue\033[m"
+                        if overall_result:
+                            compare = "\033[32mTrue\033[m"
+                        else:
+                            compare = "\033[mTrue\033[m"
                     else:
                         compare = "\033[1m\033[31mFalse\033[m"
                 data_tmp.append(compare)
diff --git a/moonclient/moonclient/tests/tests_change_auth.json b/moonclient/moonclient/tests/tests_change_auth.json
new file mode 100644 (file)
index 0000000..38d1d13
--- /dev/null
@@ -0,0 +1,32 @@
+{
+  "command_options": "-f value",
+  "tests_group": {
+    "authz": [
+
+      {
+        "auth_name": "demo",
+        "description": "Change user to demo"
+      },
+
+      {
+        "name": "list tenant",
+        "command": "tenant list",
+        "result": "^$",
+        "description": "Check if user demo cannot read the list of all tenants."
+      },
+
+      {
+        "auth_name": "admin",
+        "description": "Change user to admin"
+      },
+
+      {
+        "name": "list tenant",
+        "command": "tenant list",
+        "result": "admin",
+        "description": "Check if user admin can read the list of all tenants."
+      }
+
+    ]
+  }
+}
\ No newline at end of file