Stop hardcoding KUBECONFIG
[functest-kubernetes.git] / functest_kubernetes / k8stest.py
index 7e629ec..7bf2e7a 100644 (file)
@@ -30,12 +30,12 @@ class E2ETesting(testcase.TestCase):
 
     __logger = logging.getLogger(__name__)
 
-    config = '/root/.kube/config'
+    config = '~/.kube/config'
     gcr_repo = os.getenv("MIRROR_REPO", "gcr.io")
     k8s_gcr_repo = os.getenv("MIRROR_REPO", "k8s.gcr.io")
 
     def __init__(self, **kwargs):
-        super(E2ETesting, self).__init__(**kwargs)
+        super().__init__(**kwargs)
         self.cmd = []
         self.dir_results = "/home/opnfv/functest/results"
         self.res_dir = os.path.join(self.dir_results, self.case_name)
@@ -45,12 +45,28 @@ class E2ETesting(testcase.TestCase):
         self.output_log_name = 'functest-kubernetes.log'
         self.output_debug_log_name = 'functest-kubernetes.debug.log'
 
+    @staticmethod
+    def convert_ini_to_dict(value):
+        "Convert oslo.conf input to dict"
+        assert isinstance(value, str)
+        try:
+            return dict((x.rsplit(':', 1) for x in value.split(',')))
+        except ValueError:
+            return {}
+
     def run_kubetest(self, **kwargs):  # pylint: disable=too-many-branches
         """Run the test suites"""
-        cmd_line = ['e2e.test', '-ginkgo.noColor', '-kubeconfig', self.config,
-                    '-provider', 'skeleton', '-report-dir', self.res_dir]
-        for arg in kwargs:
-            cmd_line.extend(['-ginkgo.{}'.format(arg), kwargs.get(arg)])
+        cmd_line = [
+            'ginkgo', f'--nodes={kwargs.get("nodes", 1)}',
+            '--no-color', '/usr/local/bin/e2e.test', '--',
+            '-kubeconfig', self.config,
+            '-provider', kwargs.get('provider', 'local'),
+            '-report-dir', self.res_dir]
+        for arg in kwargs.get("ginkgo", {}):
+            cmd_line.extend([f'-ginkgo.{arg}', kwargs["ginkgo"][arg]])
+        for key, value in self.convert_ini_to_dict(
+                os.environ.get("E2E_TEST_OPTS", "")).items():
+            cmd_line.extend([f'-{key}', value])
         if "NON_BLOCKING_TAINTS" in os.environ:
             cmd_line.extend(
                 ['-non-blocking-taints', os.environ["NON_BLOCKING_TAINTS"]])
@@ -58,13 +74,14 @@ class E2ETesting(testcase.TestCase):
         self._generate_repo_list_file()
         self.__logger.info("Starting k8s test: '%s'.", cmd_line)
         env = os.environ.copy()
-        env["GINKGO_PARALLEL"] = 'y'
-        env["KUBE_TEST_REPO_LIST"] = "{}/repositories.yml".format(self.res_dir)
-        process = subprocess.Popen(cmd_line, stdout=subprocess.PIPE,
-                                   stderr=subprocess.STDOUT, env=env)
-        boutput = process.stdout.read()
-        with open(os.path.join(self.res_dir, 'e2e.log'), 'wb') as foutput:
-            foutput.write(boutput)
+        env["KUBE_TEST_REPO_LIST"] = f"{self.res_dir}/repositories.yml"
+        with subprocess.Popen(
+                cmd_line, stdout=subprocess.PIPE,
+                stderr=subprocess.STDOUT, env=env) as process:
+            boutput = process.stdout.read()
+        with open(os.path.join(
+                self.res_dir, 'e2e.log'), 'w', encoding='utf-8') as foutput:
+            foutput.write(boutput.decode("utf-8"))
         grp = re.search(
             r'^(FAIL|SUCCESS)!.* ([0-9]+) Passed \| ([0-9]+) Failed \|'
             r' ([0-9]+) Pending \| ([0-9]+) Skipped',
@@ -113,22 +130,21 @@ class E2ETesting(testcase.TestCase):
         gcr_repo = os.getenv("GCR_REPO", self.gcr_repo)
         k8s_gcr_repo = os.getenv("K8S_GCR_REPO", self.k8s_gcr_repo)
         repo_list = {
-            "GcAuthenticatedRegistry": "{}/authenticated-image-pulling".format(
-                gcr_repo),
-            "E2eRegistry":             "{}/kubernetes-e2e-test-images".format(
-                gcr_repo),
-            "PromoterE2eRegistry":     "{}/e2e-test-images".format(
-                k8s_gcr_repo),
-            "BuildImageRegistry":      "{}/build-image".format(k8s_gcr_repo),
-            "InvalidRegistry":         "invalid.com/invalid",
-            "GcEtcdRegistry":          "{}".format(k8s_gcr_repo),
-            "GcRegistry":              "{}".format(k8s_gcr_repo),
-            "SigStorageRegistry":      "{}/sig-storage".format(k8s_gcr_repo),
-            "PrivateRegistry":         "{}/k8s-authenticated-test".format(
-                gcr_repo),
-            "SampleRegistry":          "{}/google-samples".format(gcr_repo),
-            "GcrReleaseRegistry":      "{}/gke-release".format(gcr_repo),
-            "MicrosoftRegistry":       "mcr.microsoft.com",
+            "GcAuthenticatedRegistry":
+                f"{gcr_repo}/authenticated-image-pulling",
+            "E2eRegistry": f"{gcr_repo}/kubernetes-e2e-test-images",
+            "PromoterE2eRegistry": f"{k8s_gcr_repo}/e2e-test-images",
+            "BuildImageRegistry": f"{k8s_gcr_repo}/build-image",
+            "InvalidRegistry": "invalid.com/invalid",
+            "GcEtcdRegistry": k8s_gcr_repo,
+            "GcRegistry": k8s_gcr_repo,
+            "SigStorageRegistry": f"{k8s_gcr_repo}/sig-storage",
+            "PrivateRegistry": f"{gcr_repo}/k8s-authenticated-test",
+            "SampleRegistry": f"{gcr_repo}/google-samples",
+            "GcrReleaseRegistry": f"{gcr_repo}/gke-release",
+            "MicrosoftRegistry": "mcr.microsoft.com"
         }
-        with open("{}/repositories.yml".format(self.res_dir), 'w') as file:
+        with open(
+                f"{self.res_dir}/repositories.yml", 'w',
+                encoding='utf-8') as file:
             yaml.dump(repo_list, file)