Update python3 to python3.6
[functest-kubernetes.git] / functest_kubernetes / k8stest.py
index ce343a2..59eb999 100644 (file)
@@ -16,18 +16,20 @@ from __future__ import division
 
 import logging
 import os
+import re
 import subprocess
 import time
 
-from functest.core import testcase
-
-
-LOGGER = logging.getLogger(__name__)
+from xtesting.core import testcase
 
 
 class K8sTesting(testcase.TestCase):
     """Kubernetes test runner"""
 
+    __logger = logging.getLogger(__name__)
+
+    config = '/root/.kube/config'
+
     def __init__(self, **kwargs):
         super(K8sTesting, self).__init__(**kwargs)
         self.cmd = []
@@ -35,54 +37,80 @@ class K8sTesting(testcase.TestCase):
         self.start_time = 0
         self.stop_time = 0
 
-    def run_kubetest(self):
+    def run_kubetest(self):  # pylint: disable=too-many-branches
         """Run the test suites"""
         cmd_line = self.cmd
-        LOGGER.info("Starting k8s test: '%s'.", cmd_line)
+        self.__logger.info("Starting k8s test: '%s'.", cmd_line)
 
         process = subprocess.Popen(cmd_line, stdout=subprocess.PIPE,
                                    stderr=subprocess.STDOUT)
-        remark = []
-        lines = process.stdout.readlines()
-        for i in range(len(lines) - 1, -1, -1):
-            new_line = str(lines[i])
-
-            if 'SUCCESS!' in new_line or 'FAIL!' in new_line:
-                remark = new_line.replace('--', '|').split('|')
+        output = process.stdout.read()
+        if ('Error loading client' in output or
+                'Unexpected error' in output):
+            raise Exception(output)
+
+        remarks = []
+        lines = output.split('\n')
+        success = False
+        failure = False
+        i = 0
+        while i < len(lines):
+            if '[Fail]' in lines[i] or 'Failures:' in lines[i]:
+                self.__logger.error(lines[i])
+            if re.search(r'\[(.)*[0-9]+ seconds\]', lines[i]):
+                self.__logger.debug(lines[i])
+                i = i + 1
+                while i < len(lines) and lines[i] != '-' * len(lines[i]):
+                    if lines[i].startswith('STEP:') or ('INFO:' in lines[i]):
+                        break
+                    self.__logger.debug(lines[i])
+                    i = i + 1
+            if i >= len(lines):
                 break
-
-        if remark and 'SUCCESS!' in remark[0]:
+            success = 'SUCCESS!' in lines[i]
+            failure = 'FAIL!' in lines[i]
+            if success or failure:
+                if i != 0 and 'seconds' in lines[i - 1]:
+                    remarks.append(lines[i - 1])
+                remarks = remarks + lines[i].replace('--', '|').split('|')
+                break
+            i = i + 1
+
+        self.__logger.debug('-' * 10)
+        self.__logger.info("Remarks:")
+        for remark in remarks:
+            if 'seconds' in remark:
+                self.__logger.debug(remark)
+            elif 'Passed' in remark:
+                self.__logger.info("Passed: %s", remark.split()[0])
+            elif 'Skipped' in remark:
+                self.__logger.info("Skipped: %s", remark.split()[0])
+            elif 'Failed' in remark:
+                self.__logger.info("Failed: %s", remark.split()[0])
+
+        if success:
             self.result = 100
+        elif failure:
+            self.result = 0
 
-    def run(self):
+    def run(self, **kwargs):
 
-        if not os.path.isfile(os.getenv('KUBECONFIG')):
-            LOGGER.error("Cannot run k8s testcases. Config file not found ")
+        if not os.path.isfile(self.config):
+            self.__logger.error(
+                "Cannot run k8s testcases. Config file not found")
             return self.EX_RUN_ERROR
 
         self.start_time = time.time()
         try:
             self.run_kubetest()
             res = self.EX_OK
-        except Exception as ex:  # pylint: disable=broad-except
-            LOGGER.error("Error with running %s", str(ex))
+        except Exception:  # pylint: disable=broad-except
+            self.__logger.exception("Error with running kubetest:")
             res = self.EX_RUN_ERROR
 
         self.stop_time = time.time()
         return res
 
-    def check_envs(self):  # pylint: disable=no-self-use
-        """Check if required environment variables are set"""
-        try:
-            assert 'DEPLOY_SCENARIO' in os.environ
-            assert 'KUBECONFIG' in os.environ
-            assert 'KUBE_MASTER_IP' in os.environ
-            assert 'KUBERNETES_PROVIDER' in os.environ
-            assert 'KUBE_MASTER_URL' in os.environ
-        except Exception as ex:
-            raise Exception("Cannot run k8s testcases. "
-                            "Please check env var: %s" % str(ex))
-
 
 class K8sSmokeTest(K8sTesting):
     """Kubernetes smoke test suite"""
@@ -90,6 +118,17 @@ class K8sSmokeTest(K8sTesting):
         if "case_name" not in kwargs:
             kwargs.get("case_name", 'k8s_smoke')
         super(K8sSmokeTest, self).__init__(**kwargs)
-        self.check_envs()
-        self.cmd = ['/src/k8s.io/kubernetes/cluster/test-smoke.sh', '--host',
-                    os.getenv('KUBE_MASTER_URL')]
+        self.cmd = ['e2e.test', '-ginkgo.focus', 'Guestbook.application',
+                    '-ginkgo.noColor', '-kubeconfig', self.config,
+                    '--provider', 'local']
+
+
+class K8sConformanceTest(K8sTesting):
+    """Kubernetes conformance test suite"""
+    def __init__(self, **kwargs):
+        if "case_name" not in kwargs:
+            kwargs.get("case_name", 'k8s_conformance')
+        super(K8sConformanceTest, self).__init__(**kwargs)
+        self.cmd = ['e2e.test', '-ginkgo.focus', 'Conformance',
+                    '-ginkgo.noColor', '-kubeconfig', self.config,
+                    '--provider', 'local']