Merge "Update unit test related to vyos_vrouter"
[functest.git] / functest / opnfv_tests / openstack / refstack_client / refstack_client.py
index 6ac7217..42befe2 100644 (file)
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
+
 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
-# matthew.lijun@huawei.com wangwulin@huawei.com
+#
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Apache License, Version 2.0
 # which accompanies this distribution, and is available at
@@ -10,7 +11,6 @@
 
 from __future__ import division
 
-
 import argparse
 import logging
 import os
@@ -29,11 +29,14 @@ from functest.opnfv_tests.openstack.tempest import conf_utils
 from functest.utils.constants import CONST
 import functest.utils.functest_utils as ft_utils
 
+__author__ = ("Matthew Li <matthew.lijun@huawei.com>,"
+              "Linda Wang <wangwulin@huawei.com>")
+
 # logging configuration """
 LOGGER = logging.getLogger(__name__)
 
 
-class RefstackClient(testcase.OSGCTestCase):
+class RefstackClient(testcase.TestCase):
     """RefstackClient testcase implementation class."""
 
     def __init__(self, **kwargs):
@@ -41,6 +44,7 @@ class RefstackClient(testcase.OSGCTestCase):
         if "case_name" not in kwargs:
             kwargs["case_name"] = "refstack_defcore"
         super(RefstackClient, self).__init__(**kwargs)
+        self.tempestconf = None
         self.conf_path = pkg_resources.resource_filename(
             'functest',
             'opnfv_tests/openstack/refstack_client/refstack_tempest.conf')
@@ -57,6 +61,13 @@ class RefstackClient(testcase.OSGCTestCase):
                 CONST.__getattribute__('OS_INSECURE').lower() == 'true'):
             self.insecure = '-k'
 
+    def generate_conf(self):
+        if not os.path.exists(conf_utils.REFSTACK_RESULTS_DIR):
+            os.makedirs(conf_utils.REFSTACK_RESULTS_DIR)
+
+        self.tempestconf = TempestConf()
+        self.tempestconf.generate_tempestconf()
+
     def run_defcore(self, conf, testlist):
         """Run defcore sys command."""
         cmd = ("refstack-client test {0} -c {1} -v --test-list {2}"
@@ -65,43 +76,38 @@ class RefstackClient(testcase.OSGCTestCase):
         ft_utils.execute_command(cmd)
 
     def run_defcore_default(self):
-        """Run default defcare sys command."""
-        cmd = ("refstack-client test {0} -c {1} -v --test-list {2}"
-               .format(self.insecure, self.confpath, self.defcorelist))
+        """Run default defcore sys command."""
+        options = ["-v"] if not self.insecure else ["-v", self.insecure]
+        cmd = (["refstack-client", "test", "-c", self.confpath] +
+               options + ["--test-list", self.defcorelist])
         LOGGER.info("Starting Refstack_defcore test case: '%s'.", cmd)
 
-        header = ("Refstack environment:\n"
-                  "  SUT: %s\n  Scenario: %s\n  Node: %s\n  Date: %s\n" %
-                  (CONST.__getattribute__('INSTALLER_TYPE'),
-                   CONST.__getattribute__('DEPLOY_SCENARIO'),
-                   CONST.__getattribute__('NODE_NAME'),
-                   time.strftime("%a %b %d %H:%M:%S %Z %Y")))
-
-        f_stdout = open(
-            os.path.join(conf_utils.REFSTACK_RESULTS_DIR,
-                         "refstack.log"), 'w+')
-        f_env = open(os.path.join(conf_utils.REFSTACK_RESULTS_DIR,
-                                  "environment.log"), 'w+')
-        f_env.write(header)
-
-        process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
-                                   stderr=subprocess.STDOUT, bufsize=1)
-
-        with process.stdout:
-            for line in iter(process.stdout.readline, b''):
-                if 'Tests' in line:
-                    break
-                if re.search(r"\} tempest\.", line):
-                    LOGGER.info(line.replace('\n', ''))
-                f_stdout.write(line)
-        process.wait()
-
-        f_stdout.close()
-        f_env.close()
+        with open(os.path.join(conf_utils.REFSTACK_RESULTS_DIR,
+                               "environment.log"), 'w+') as f_env:
+            f_env.write(
+                ("Refstack environment:\n"
+                 "  SUT: {}\n  Scenario: {}\n  Node: {}\n  Date: {}\n").format(
+                    CONST.__getattribute__('INSTALLER_TYPE'),
+                    CONST.__getattribute__('DEPLOY_SCENARIO'),
+                    CONST.__getattribute__('NODE_NAME'),
+                    time.strftime("%a %b %d %H:%M:%S %Z %Y")))
+
+        with open(os.path.join(conf_utils.REFSTACK_RESULTS_DIR,
+                               "refstack.log"), 'w+') as f_stdout:
+            subprocess.call(cmd, shell=False, stdout=f_stdout,
+                            stderr=subprocess.STDOUT)
 
     def parse_refstack_result(self):
-        """Parse Refstact results."""
+        """Parse Refstack results."""
         try:
+            with open(os.path.join(conf_utils.REFSTACK_RESULTS_DIR,
+                                   "refstack.log"), 'r') as logfile:
+                for line in logfile.readlines():
+                    if 'Tests' in line:
+                        break
+                    if re.search(r"\} tempest\.", line):
+                        LOGGER.info(line.replace('\n', ''))
+
             with open(os.path.join(conf_utils.REFSTACK_RESULTS_DIR,
                                    "refstack.log"), 'r') as logfile:
                 output = logfile.read()
@@ -119,15 +125,15 @@ class RefstackClient(testcase.OSGCTestCase):
             for match in re.findall(r"(- Failed: )(\d+)", output):
                 num_failures = match[1]
                 LOGGER.info("".join(match))
-            success_testcases = ""
-            for match in re.findall(r"\{0\}(.*?)[. ]*ok", output):
-                success_testcases += match + ", "
-            failed_testcases = ""
-            for match in re.findall(r"\{0\}(.*?)[. ]*FAILED", output):
-                failed_testcases += match + ", "
-            skipped_testcases = ""
-            for match in re.findall(r"\{0\}(.*?)[. ]*SKIPPED:", output):
-                skipped_testcases += match + ", "
+            success_testcases = []
+            for match in re.findall(r"\{0\} (.*?)[. ]*ok", output):
+                success_testcases.append(match)
+            failed_testcases = []
+            for match in re.findall(r"\{0\} (.*?)[. ]*FAILED", output):
+                failed_testcases.append(match)
+            skipped_testcases = []
+            for match in re.findall(r"\{0\} (.*?)[. ]*SKIPPED:", output):
+                skipped_testcases.append(match)
 
             num_executed = int(num_tests) - int(num_skipped)
 
@@ -157,18 +163,18 @@ class RefstackClient(testcase.OSGCTestCase):
         """
         self.start_time = time.time()
 
-        if not os.path.exists(conf_utils.REFSTACK_RESULTS_DIR):
-            os.makedirs(conf_utils.REFSTACK_RESULTS_DIR)
-
         try:
-            tempestconf = TempestConf()
-            tempestconf.generate_tempestconf()
+            # Make sure that Tempest is configured
+            if not self.tempestconf:
+                self.generate_conf()
             self.run_defcore_default()
             self.parse_refstack_result()
             res = testcase.TestCase.EX_OK
         except Exception:
             LOGGER.exception("Error with run")
             res = testcase.TestCase.EX_RUN_ERROR
+        finally:
+            self.tempestconf.clean()
 
         self.stop_time = time.time()
         return res