Fix black issues
[osm/N2VC.git] / n2vc / tests / unit / test_k8s_helm3_conn.py
index c175b7a..7a76e7e 100644 (file)
 import asynctest
 import logging
 
-from asynctest.mock import Mock
+from asynctest.mock import Mock, patch
 from osm_common.dbmemory import DbMemory
 from osm_common.fslocal import FsLocal
-from n2vc.k8s_helm3_conn import K8sHelm3Connector
+from n2vc.k8s_helm3_conn import K8sHelm3Connector, K8sException
 
 __author__ = "Isabel Lloret <illoret@indra.es>"
 
@@ -31,7 +31,9 @@ class TestK8sHelm3Conn(asynctest.TestCase):
     logger = logging.getLogger(__name__)
     logger.setLevel(logging.DEBUG)
 
-    async def setUp(self):
+    @patch("n2vc.k8s_helm_base_conn.EnvironConfig")
+    async def setUp(self, mock_env):
+        mock_env.return_value = {"stablerepourl": "https://charts.helm.sh/stable"}
         self.db = Mock(DbMemory())
         self.fs = asynctest.Mock(FsLocal())
         self.fs.path = "./tmp/"
@@ -84,7 +86,7 @@ class TestK8sHelm3Conn(asynctest.TestCase):
     async def test_repo_add(self):
         repo_name = "bitnami"
         repo_url = "https://charts.bitnami.com/bitnami"
-        self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
+        self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=(0, ""))
 
         await self.helm_conn.repo_add(self.cluster_uuid, repo_name, repo_url)
 
@@ -100,35 +102,38 @@ class TestK8sHelm3Conn(asynctest.TestCase):
             ),
         )
 
-        repo_update_command = "/usr/bin/helm3 repo update"
-        repo_add_command = "/usr/bin/helm3 repo add {} {}".format(repo_name, repo_url)
+        repo_update_command = (
+            "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo update {}"
+        ).format(repo_name)
+        repo_add_command = (
+            "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo add {} {}"
+        ).format(repo_name, repo_url)
         calls = self.helm_conn._local_async_exec.call_args_list
         call0_kargs = calls[0][1]
         self.assertEqual(
             call0_kargs.get("command"),
-            repo_update_command,
-            "Invalid repo update command: {}".format(call0_kargs.get("command")),
+            repo_add_command,
+            "Invalid repo add command: {}".format(call0_kargs.get("command")),
         )
         self.assertEqual(
             call0_kargs.get("env"),
             self.env,
-            "Invalid env for update command: {}".format(call0_kargs.get("env")),
+            "Invalid env for add command: {}".format(call0_kargs.get("env")),
         )
         call1_kargs = calls[1][1]
         self.assertEqual(
             call1_kargs.get("command"),
-            repo_add_command,
-            "Invalid repo add command: {}".format(call1_kargs.get("command")),
+            repo_update_command,
+            "Invalid repo update command: {}".format(call1_kargs.get("command")),
         )
         self.assertEqual(
             call1_kargs.get("env"),
             self.env,
-            "Invalid env for add command: {}".format(call1_kargs.get("env")),
+            "Invalid env for update command: {}".format(call1_kargs.get("env")),
         )
 
     @asynctest.fail_on(active_handles=True)
     async def test_repo_list(self):
-
         self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
 
         await self.helm_conn.repo_list(self.cluster_uuid)
@@ -137,14 +142,13 @@ class TestK8sHelm3Conn(asynctest.TestCase):
         self.helm_conn.fs.reverse_sync.assert_called_once_with(
             from_path=self.cluster_id
         )
-        command = "/usr/bin/helm3 repo list --output yaml"
+        command = "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo list --output yaml"
         self.helm_conn._local_async_exec.assert_called_with(
             command=command, env=self.env, raise_exception_on_error=False
         )
 
     @asynctest.fail_on(active_handles=True)
     async def test_repo_remove(self):
-
         self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
         repo_name = "bitnami"
         await self.helm_conn.repo_remove(self.cluster_uuid, repo_name)
@@ -153,7 +157,9 @@ class TestK8sHelm3Conn(asynctest.TestCase):
         self.helm_conn.fs.reverse_sync.assert_called_once_with(
             from_path=self.cluster_id
         )
-        command = "/usr/bin/helm3 repo remove {}".format(repo_name)
+        command = "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 repo remove {}".format(
+            repo_name
+        )
         self.helm_conn._local_async_exec.assert_called_with(
             command=command, env=self.env, raise_exception_on_error=True
         )
@@ -169,6 +175,9 @@ class TestK8sHelm3Conn(asynctest.TestCase):
         self.kdu_instance = "stable-openldap-0005399828"
         self.helm_conn.generate_kdu_instance_name = Mock(return_value=self.kdu_instance)
         self.helm_conn._get_namespaces = asynctest.CoroutineMock(return_value=[])
+        self.helm_conn._namespace_exists = asynctest.CoroutineMock(
+            side_effect=self.helm_conn._namespace_exists
+        )
         self.helm_conn._create_namespace = asynctest.CoroutineMock()
 
         await self.helm_conn.install(
@@ -180,13 +189,22 @@ class TestK8sHelm3Conn(asynctest.TestCase):
             db_dict=db_dict,
         )
 
+        self.helm_conn._namespace_exists.assert_called_once()
         self.helm_conn._get_namespaces.assert_called_once()
         self.helm_conn._create_namespace.assert_called_once_with(
             self.cluster_id, self.namespace
         )
-        self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
-        self.helm_conn.fs.reverse_sync.assert_called_once_with(
-            from_path=self.cluster_id
+        self.helm_conn.fs.sync.assert_has_calls(
+            [
+                asynctest.call(from_path=self.cluster_id),
+                asynctest.call(from_path=self.cluster_id),
+            ]
+        )
+        self.helm_conn.fs.reverse_sync.assert_has_calls(
+            [
+                asynctest.call(from_path=self.cluster_id),
+                asynctest.call(from_path=self.cluster_id),
+            ]
         )
         self.helm_conn._store_status.assert_called_with(
             cluster_id=self.cluster_id,
@@ -194,17 +212,46 @@ class TestK8sHelm3Conn(asynctest.TestCase):
             namespace=self.namespace,
             db_dict=db_dict,
             operation="install",
-            run_once=True,
-            check_every=0,
         )
         command = (
-            "/usr/bin/helm3 install stable-openldap-0005399828 --atomic --output yaml   "
+            "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 "
+            "install stable-openldap-0005399828 --atomic --output yaml   "
             "--timeout 300s --namespace testk8s stable/openldap --version 1.2.2"
         )
-        self.helm_conn._local_async_exec.assert_called_once_with(
+        self.helm_conn._local_async_exec.assert_called_with(
             command=command, env=self.env, raise_exception_on_error=False
         )
 
+        # Exception test if namespace could not being created for some reason
+        self.helm_conn._namespace_exists.return_value = False
+        self.helm_conn._create_namespace.side_effect = Exception()
+
+        with self.assertRaises(K8sException):
+            await self.helm_conn.install(
+                self.cluster_uuid,
+                kdu_model,
+                self.kdu_instance,
+                atomic=True,
+                namespace=self.namespace,
+                db_dict=db_dict,
+            )
+
+    @asynctest.fail_on(active_handles=True)
+    async def test_namespace_exists(self):
+        self.helm_conn._get_namespaces = asynctest.CoroutineMock()
+
+        self.helm_conn._get_namespaces.return_value = ["testk8s", "kube-system"]
+        result = await self.helm_conn._namespace_exists(self.cluster_id, self.namespace)
+        self.helm_conn._get_namespaces.assert_called_once()
+        self.assertEqual(result, True)
+
+        self.helm_conn._get_namespaces.reset_mock()
+        result = await self.helm_conn._namespace_exists(
+            self.cluster_id, "none-exists-namespace"
+        )
+        self.helm_conn._get_namespaces.assert_called_once()
+        self.assertEqual(result, False)
+
     @asynctest.fail_on(active_handles=True)
     async def test_upgrade(self):
         kdu_model = "stable/openldap:1.2.3"
@@ -226,9 +273,12 @@ class TestK8sHelm3Conn(asynctest.TestCase):
         await self.helm_conn.upgrade(
             self.cluster_uuid, kdu_instance, kdu_model, atomic=True, db_dict=db_dict
         )
-        self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
-        self.helm_conn.fs.reverse_sync.assert_called_once_with(
-            from_path=self.cluster_id
+        self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
+        self.helm_conn.fs.reverse_sync.assert_has_calls(
+            [
+                asynctest.call(from_path=self.cluster_id),
+                asynctest.call(from_path=self.cluster_id),
+            ]
         )
         self.helm_conn._store_status.assert_called_with(
             cluster_id=self.cluster_id,
@@ -236,15 +286,14 @@ class TestK8sHelm3Conn(asynctest.TestCase):
             namespace=self.namespace,
             db_dict=db_dict,
             operation="upgrade",
-            run_once=True,
-            check_every=0,
         )
         command = (
+            "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config "
             "/usr/bin/helm3 upgrade stable-openldap-0005399828 stable/openldap "
-            "--namespace testk8s --atomic --output yaml  --timeout 300s  "
+            "--namespace testk8s --atomic --output yaml  --timeout 300s "
             "--version 1.2.3"
         )
-        self.helm_conn._local_async_exec.assert_called_once_with(
+        self.helm_conn._local_async_exec.assert_called_with(
             command=command, env=self.env, raise_exception_on_error=False
         )
 
@@ -268,7 +317,7 @@ class TestK8sHelm3Conn(asynctest.TestCase):
         await self.helm_conn.rollback(
             self.cluster_uuid, kdu_instance=kdu_instance, revision=1, db_dict=db_dict
         )
-        self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
+        self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
         self.helm_conn.fs.reverse_sync.assert_called_once_with(
             from_path=self.cluster_id
         )
@@ -278,10 +327,11 @@ class TestK8sHelm3Conn(asynctest.TestCase):
             namespace=self.namespace,
             db_dict=db_dict,
             operation="rollback",
-            run_once=True,
-            check_every=0,
         )
-        command = "/usr/bin/helm3 rollback stable-openldap-0005399828 1 --namespace=testk8s --wait"
+        command = (
+            "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 "
+            "rollback stable-openldap-0005399828 1 --namespace=testk8s --wait"
+        )
         self.helm_conn._local_async_exec.assert_called_once_with(
             command=command, env=self.env, raise_exception_on_error=False
         )
@@ -303,13 +353,13 @@ class TestK8sHelm3Conn(asynctest.TestCase):
         )
 
         await self.helm_conn.uninstall(self.cluster_uuid, kdu_instance)
-        self.helm_conn.fs.sync.assert_called_once_with(from_path=self.cluster_id)
+        self.helm_conn.fs.sync.assert_called_with(from_path=self.cluster_id)
         self.helm_conn.fs.reverse_sync.assert_called_once_with(
             from_path=self.cluster_id
         )
-        command = "/usr/bin/helm3 uninstall {} --namespace={}".format(
-            kdu_instance, self.namespace
-        )
+        command = (
+            "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 uninstall {} --namespace={}"
+        ).format(kdu_instance, self.namespace)
         self.helm_conn._local_async_exec.assert_called_once_with(
             command=command, env=self.env, raise_exception_on_error=True
         )
@@ -332,9 +382,9 @@ class TestK8sHelm3Conn(asynctest.TestCase):
             from_path=self.cluster_id
         )
         self.helm_conn._parse_services.assert_called_once()
-        command1 = "/usr/bin/helm3 get manifest {} --namespace=testk8s".format(
-            kdu_instance
-        )
+        command1 = (
+            "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 get manifest {} --namespace=testk8s"
+        ).format(kdu_instance)
         command2 = "/usr/bin/kubectl get --namespace={} -f -".format(self.namespace)
         self.helm_conn._local_async_exec_pipe.assert_called_once_with(
             command1, command2, env=self.env, raise_exception_on_error=True
@@ -435,11 +485,11 @@ class TestK8sHelm3Conn(asynctest.TestCase):
         self.helm_conn._local_async_exec = asynctest.CoroutineMock(return_value=("", 0))
 
         await self.helm_conn._status_kdu(
-            self.cluster_id, kdu_instance, self.namespace, return_text=True
-        )
-        command = "/usr/bin/helm3 status {} --namespace={} --output yaml".format(
-            kdu_instance, self.namespace
+            self.cluster_id, kdu_instance, self.namespace, yaml_format=True
         )
+        command = (
+            "env KUBECONFIG=./tmp/helm3_cluster_id/.kube/config /usr/bin/helm3 status {} --namespace={} --output yaml"
+        ).format(kdu_instance, self.namespace)
         self.helm_conn._local_async_exec.assert_called_once_with(
             command=command,
             env=self.env,
@@ -471,14 +521,12 @@ class TestK8sHelm3Conn(asynctest.TestCase):
             namespace=self.namespace,
             db_dict=db_dict,
             operation="install",
-            run_once=True,
-            check_every=0,
         )
         self.helm_conn._status_kdu.assert_called_once_with(
             cluster_id=self.cluster_id,
             kdu_instance=kdu_instance,
             namespace=self.namespace,
-            return_text=False,
+            yaml_format=False,
         )
         self.helm_conn.write_app_status_to_db.assert_called_once_with(
             db_dict=db_dict,