X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=n2vc%2Ftests%2Funit%2Ftest_k8s_helm3_conn.py;h=93d0c4ce4a6a3525c08223cbd7642c7bd07f924c;hb=fa02f8a90b7fe1e1b7a80feedef4132bef1ca3e4;hp=25749af39b003480017837e6287266a7dc151ee4;hpb=4395cfa6c7d0d80980c00d9f078440e0333fd826;p=osm%2FN2VC.git diff --git a/n2vc/tests/unit/test_k8s_helm3_conn.py b/n2vc/tests/unit/test_k8s_helm3_conn.py index 25749af..93d0c4c 100644 --- a/n2vc/tests/unit/test_k8s_helm3_conn.py +++ b/n2vc/tests/unit/test_k8s_helm3_conn.py @@ -21,7 +21,7 @@ import logging 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 " @@ -33,9 +33,7 @@ class TestK8sHelm3Conn(asynctest.TestCase): @patch("n2vc.k8s_helm_base_conn.EnvironConfig") async def setUp(self, mock_env): - mock_env.return_value = { - "stablerepourl": "https://charts.helm.sh/stable" - } + mock_env.return_value = {"stablerepourl": "https://charts.helm.sh/stable"} self.db = Mock(DbMemory()) self.fs = asynctest.Mock(FsLocal()) self.fs.path = "./tmp/" @@ -173,6 +171,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( @@ -184,6 +185,7 @@ 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 @@ -209,6 +211,36 @@ class TestK8sHelm3Conn(asynctest.TestCase): 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"