From: David Garcia Date: Mon, 26 Jul 2021 14:04:59 +0000 (+0200) Subject: Unit tests improvements X-Git-Tag: release-v11.0-start~13 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=4ae527ee394a9794c25e7a90fe895aa446c6c797;p=osm%2FN2VC.git Unit tests improvements - From now on, black must pass. Jenkins will give a -1 if it doesn't - Ignore E203 because it is not pep8 compliant, and conflicts with black https://github.com/psf/black/issues/315#issuecomment-395457972 - Black fixes and minor fix in unit test - Fix devops-stages/stage-test.sh: jenkins was not giving a -1 when tox was failing Change-Id: Ic7318dcb6d4006a271607cd064a1f36d0936b7b9 Signed-off-by: David Garcia --- diff --git a/devops-stages/stage-test.sh b/devops-stages/stage-test.sh index 50f588a..00ed439 100755 --- a/devops-stages/stage-test.sh +++ b/devops-stages/stage-test.sh @@ -12,5 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -OUTPUT=$(TOX_PARALLEL_NO_SPINNER=1 tox --parallel=auto) -printf "$OUTPUT" +tox --parallel=auto diff --git a/n2vc/k8s_helm3_conn.py b/n2vc/k8s_helm3_conn.py index 5bbd39b..edefc86 100644 --- a/n2vc/k8s_helm3_conn.py +++ b/n2vc/k8s_helm3_conn.py @@ -117,9 +117,7 @@ class K8sHelm3Connector(K8sHelmBaseConnector): if not await self._namespace_exists(cluster_id, namespace): err_msg = ( "namespace {} does not exist in cluster_id {} " - "error message: ".format( - namespace, e - ) + "error message: ".format(namespace, e) ) self.log.error(err_msg) raise K8sException(err_msg) diff --git a/n2vc/k8s_helm_base_conn.py b/n2vc/k8s_helm_base_conn.py index ad59e8b..4a43ee2 100644 --- a/n2vc/k8s_helm_base_conn.py +++ b/n2vc/k8s_helm_base_conn.py @@ -1471,11 +1471,11 @@ class K8sHelmBaseConnector(K8sConnector): # check embeded chart (file or dir) if chart_name.startswith("/"): # extract file or directory name - chart_name = chart_name[chart_name.rfind("/") + 1:] + chart_name = chart_name[chart_name.rfind("/") + 1 :] # check URL elif "://" in chart_name: # extract last portion of URL - chart_name = chart_name[chart_name.rfind("/") + 1:] + chart_name = chart_name[chart_name.rfind("/") + 1 :] name = "" for c in chart_name: diff --git a/n2vc/kubectl.py b/n2vc/kubectl.py index ff48eda..a56b6cd 100644 --- a/n2vc/kubectl.py +++ b/n2vc/kubectl.py @@ -254,10 +254,15 @@ class Kubectl: delay=1, fallback=Exception("Failed getting the secret from service account"), ) - async def get_secret_data(self, name: str, namespace: str = "kube-system") -> (str, str): + async def get_secret_data( + self, name: str, namespace: str = "kube-system" + ) -> (str, str): """ Get secret data + :param: name: Name of the secret data + :param: namespace: Name of the namespace where the secret is stored + :return: Tuple with the token and client certificate """ v1_core = self.clients[CORE_CLIENT] diff --git a/n2vc/loggable.py b/n2vc/loggable.py index b3b98da..d588a1d 100644 --- a/n2vc/loggable.py +++ b/n2vc/loggable.py @@ -115,7 +115,7 @@ class Loggable: if not include_path: i = filename.rfind("/") if i > 0: - filename = filename[i + 1:] + filename = filename[i + 1 :] # datetime dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") diff --git a/n2vc/tests/unit/test_k8s_helm3_conn.py b/n2vc/tests/unit/test_k8s_helm3_conn.py index b16ec76..93d0c4c 100644 --- a/n2vc/tests/unit/test_k8s_helm3_conn.py +++ b/n2vc/tests/unit/test_k8s_helm3_conn.py @@ -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,7 +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._namespace_exists = asynctest.CoroutineMock( + side_effect=self.helm_conn._namespace_exists + ) self.helm_conn._create_namespace = asynctest.CoroutineMock() await self.helm_conn.install( @@ -229,13 +229,15 @@ class TestK8sHelm3Conn(asynctest.TestCase): async def test_namespace_exists(self): self.helm_conn._get_namespaces = asynctest.CoroutineMock() - self.helm_conn._get_namespaces.return_value = ['testk8s', 'kube-system'] + 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') + 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) diff --git a/n2vc/tests/unit/test_libjuju.py b/n2vc/tests/unit/test_libjuju.py index 3447340..724b5d7 100644 --- a/n2vc/tests/unit/test_libjuju.py +++ b/n2vc/tests/unit/test_libjuju.py @@ -103,7 +103,7 @@ class GetControllerTest(LibjujuTestCase): with self.assertRaises(JujuControllerFailedConnecting): controller = self.loop.run_until_complete(self.libjuju.get_controller()) self.assertIsNone(controller) - mock_disconnect_controller.assert_called_once() + mock_disconnect_controller.assert_called() def test_same_endpoint_get_controller(self, mock_api_endpoints, mock_connect): self.libjuju.endpoints = ["127.0.0.1:17070"] diff --git a/tox.ini b/tox.ini index 628b15e..0e3f69f 100644 --- a/tox.ini +++ b/tox.ini @@ -34,8 +34,8 @@ deps = -r{toxinidir}/requirements.txt deps = black skip_install = true commands = - - black --check --diff n2vc/ - - black --check --diff setup.py + black --check --diff n2vc/ + black --check --diff setup.py ####################################################################################### @@ -114,6 +114,7 @@ ignore = W503, E123, E125, + E203, E226, E241 exclude =