From fd757c3ca88197452eb07589384f72dc9381d715 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 20 Feb 2024 06:31:27 +0100 Subject: [PATCH] Fix --- IM/connectors/Kubernetes.py | 4 +-- test/files/tosca_k8s.yml | 14 ++++----- test/unit/Tosca.py | 10 +++---- test/unit/connectors/Kubernetes.py | 47 +++++++++++++++--------------- 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/IM/connectors/Kubernetes.py b/IM/connectors/Kubernetes.py index 229c3299..5869cb32 100644 --- a/IM/connectors/Kubernetes.py +++ b/IM/connectors/Kubernetes.py @@ -370,8 +370,8 @@ def _generate_pod_data(self, namespace, name, outports, system, volumes, tags): @staticmethod def _get_namespace(inf): namespace = inf.id - if inf.description and inf.description.getValue('namespace'): - namespace = inf.description.getValue('namespace') + if inf.radl.description and inf.radl.description.getValue('namespace'): + namespace = inf.radl.description.getValue('namespace') return namespace def launch(self, inf, radl, requested_radl, num_vm, auth_data): diff --git a/test/files/tosca_k8s.yml b/test/files/tosca_k8s.yml index 17307b34..5c6ade8c 100644 --- a/test/files/tosca_k8s.yml +++ b/test/files/tosca_k8s.yml @@ -7,7 +7,7 @@ description: TOSCA test for K8s metadata: # Enable to set the K8s namespace for this deployment - namespace: some_namespace + namespace: somenamespace infra_name: some_infra repositories: @@ -21,7 +21,7 @@ topology_template: image: type: string description: The image to be used in the container - default: "mysql:5.7" + default: "mysql:8" mysql_root_password: type: string @@ -53,8 +53,8 @@ topology_template: capabilities: host: properties: - num_cpus: 1 - mem_size: 2 GB + num_cpus: 0.5 + mem_size: 1 GB publish_ports: - protocol: tcp target: 8800 @@ -80,13 +80,13 @@ topology_template: capabilities: host: properties: - num_cpus: 1 - mem_size: 2 GB + num_cpus: 0.5 + mem_size: 1 GB expose_ports: - protocol: tcp target: 3306 volumes: - - "some_vol:/some/path" + - "some_vol:/var/lib/mysql" some_vol: type: tosca.nodes.BlockStorage diff --git a/test/unit/Tosca.py b/test/unit/Tosca.py index 7639b322..0c5428b8 100755 --- a/test/unit/Tosca.py +++ b/test/unit/Tosca.py @@ -434,13 +434,13 @@ def test_tosca_k8s(self): radl = parse_radl(str(radl)) radl.check() - self.assertEqual(radl.description.getValue("namespace"), "some_namespace") + self.assertEqual(radl.description.getValue("namespace"), "somenamespace") node = radl.get_system_by_name('mysql_container') - self.assertEqual(node.getValue("disk.0.image.url"), "docker://docker.io/mysql:5.7") - self.assertEqual(node.getValue("cpu.count"), 1.0) - self.assertEqual(node.getValue("memory.size"), 2000000000) + self.assertEqual(node.getValue("disk.0.image.url"), "docker://docker.io/mysql:8") + self.assertEqual(node.getValue("cpu.count"), 0.5) + self.assertEqual(node.getValue("memory.size"), 1000000000) self.assertEqual(node.getValue("disk.1.size"), 10000000000) - self.assertEqual(node.getValue("disk.1.mount_path"), '/some/path') + self.assertEqual(node.getValue("disk.1.mount_path"), '/var/lib/mysql') self.assertEqual(node.getValue("environment.variables"), 'MYSQL_ROOT_PASSWORD=my-secret') net = radl.get_network_by_id('mysql_container_priv') self.assertEqual(net.getValue("outports"), '3306/tcp-3306/tcp') diff --git a/test/unit/connectors/Kubernetes.py b/test/unit/connectors/Kubernetes.py index 9fbe938c..48ac36d7 100755 --- a/test/unit/connectors/Kubernetes.py +++ b/test/unit/connectors/Kubernetes.py @@ -82,30 +82,30 @@ def get_response(self, method, url, verify, headers, data): resp.text = '{"versions": "v1"}' elif url.endswith("/pods/1"): resp.status_code = 200 - resp.text = ('{"metadata": {"namespace":"some_namespace", "name": "name"}, "status": ' + resp.text = ('{"metadata": {"namespace":"somenamespace", "name": "name"}, "status": ' '{"phase":"Running", "hostIP": "158.42.1.1", "podIP": "10.0.0.1"}, ' '"spec": {"containers": [{"image": "image:1.0"}], ' '"volumes": [{"persistentVolumeClaim": {"claimName" : "cname"}}]}}') - if url == "/api/v1/namespaces/some_namespace": + if url == "/api/v1/namespaces/somenamespace": resp.status_code = 200 elif method == "POST": if url.endswith("/pods"): resp.status_code = 201 - resp.text = '{"metadata": {"namespace":"some_namespace", "name": "name"}}' + resp.text = '{"metadata": {"namespace":"somenamespace", "name": "name"}}' elif url.endswith("/services"): resp.status_code = 201 elif url.endswith("/namespaces/"): resp.status_code = 201 elif url.endswith("/persistentvolumeclaims"): resp.status_code = 201 - elif url.endswith("/apis/networking.k8s.io/v1/namespaces/some_namespace/ingresses"): + elif url.endswith("/apis/networking.k8s.io/v1/namespaces/somenamespace/ingresses"): resp.status_code = 201 elif method == "DELETE": if url.endswith("/pods/1"): resp.status_code = 200 elif url.endswith("/services/1"): resp.status_code = 200 - elif url.endswith("/namespaces/some_namespace"): + elif url.endswith("/namespaces/somenamespace"): resp.status_code = 200 elif "persistentvolumeclaims" in url: resp.status_code = 200 @@ -126,7 +126,7 @@ def test_20_launch(self, save_data, requests): radl_data = """ description desc ( name = 'Infrastructure Name' and - namespace = 'some_namespace' + namespace = 'somenamespace' ) network net (outbound = 'yes' and outports = '38080-8080') system test ( @@ -152,8 +152,8 @@ def test_20_launch(self, save_data, requests): inf = MagicMock(["id", "_lock", "add_vm", "description"]) inf.id = "infid" - inf.description = MagicMock(["getValue"]) - inf.description.getValue.return_value = "some_namespace" + inf.radl = radl + inf.description.getValue.return_value = "somenamespace" inf.add_vm.side_effect = self.add_vm res = kube_cloud.launch(inf, radl, radl, 1, auth) success, _ = res[0] @@ -162,14 +162,14 @@ def test_20_launch(self, save_data, requests): exp_pvc = { "apiVersion": "v1", "kind": "PersistentVolumeClaim", - "metadata": {"name": "test-1", "namespace": "some_namespace"}, + "metadata": {"name": "test-1", "namespace": "somenamespace"}, "spec": { "accessModes": ["ReadWriteOnce"], "resources": {"requests": {"storage": 10737418240}}, }, } self.assertEqual(requests.call_args_list[1][0][1], - 'http://server.com:8080/api/v1/namespaces/some_namespace/persistentvolumeclaims') + 'http://server.com:8080/api/v1/namespaces/somenamespace/persistentvolumeclaims') self.assertEqual(json.loads(requests.call_args_list[1][1]['data']), exp_pvc) exp_pod = { @@ -177,7 +177,7 @@ def test_20_launch(self, save_data, requests): "kind": "Pod", "metadata": { "name": "test", - "namespace": "some_namespace", + "namespace": "somenamespace", "labels": {"name": "test", "IM_INFRA_ID": "infid", "key": "invalid_"}, }, "spec": { @@ -202,7 +202,7 @@ def test_20_launch(self, save_data, requests): }, } self.assertEqual(requests.call_args_list[2][0][1], - 'http://server.com:8080/api/v1/namespaces/some_namespace/pods') + 'http://server.com:8080/api/v1/namespaces/somenamespace/pods') self.assertEqual(json.loads(requests.call_args_list[2][1]['data']), exp_pod) exp_svc = { @@ -210,7 +210,7 @@ def test_20_launch(self, save_data, requests): "kind": "Service", "metadata": { "name": "test", - "namespace": "some_namespace", + "namespace": "somenamespace", "labels": {"name": "test"}, }, "spec": { @@ -228,7 +228,7 @@ def test_20_launch(self, save_data, requests): }, } self.assertEqual(requests.call_args_list[3][0][1], - 'http://server.com:8080/api/v1/namespaces/some_namespace/services') + 'http://server.com:8080/api/v1/namespaces/somenamespace/services') self.assertEqual(json.loads(requests.call_args_list[3][1]['data']), exp_svc) exp_ing = { @@ -237,7 +237,7 @@ def test_20_launch(self, save_data, requests): "metadata": { "labels": {"name": "test"}, "name": "test", - "namespace": "some_namespace", + "namespace": "somenamespace", }, "spec": { "rules": [ @@ -263,7 +263,7 @@ def test_20_launch(self, save_data, requests): } self.assertEqual(requests.call_args_list[5][0][1], - 'http://server.com:8080/apis/networking.k8s.io/v1/namespaces/some_namespace/ingresses') + 'http://server.com:8080/apis/networking.k8s.io/v1/namespaces/somenamespace/ingresses') self.assertEqual(json.loads(requests.call_args_list[5][1]['data']), exp_ing) self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue()) @@ -341,8 +341,9 @@ def test_60_finalize(self, requests): inf = MagicMock() inf.id = "infid" - inf.description = MagicMock(["getValue"]) - inf.description.getValue.return_value = "some_namespace" + inf.radl = MagicMock() + inf.radl.description = MagicMock(["getValue"]) + inf.radl.description.getValue.return_value = "somenamespace" vm = VirtualMachine(inf, "1", kube_cloud.cloud, "", "", kube_cloud, 1) requests.side_effect = self.get_response @@ -351,19 +352,19 @@ def test_60_finalize(self, requests): self.assertEqual(requests.call_args_list[1][0], ('DELETE', - 'http://server.com:8080/api/v1/namespaces/some_namespace/persistentvolumeclaims/cname')) + 'http://server.com:8080/api/v1/namespaces/somenamespace/persistentvolumeclaims/cname')) self.assertEqual(requests.call_args_list[2][0], ('DELETE', - 'http://server.com:8080/api/v1/namespaces/some_namespace/pods/1')) + 'http://server.com:8080/api/v1/namespaces/somenamespace/pods/1')) self.assertEqual(requests.call_args_list[3][0], ('DELETE', - 'http://server.com:8080/api/v1/namespaces/some_namespace/services/1')) + 'http://server.com:8080/api/v1/namespaces/somenamespace/services/1')) self.assertEqual(requests.call_args_list[4][0], ('DELETE', - 'http://server.com:8080/apis/networking.k8s.io/v1/namespaces/some_namespace/ingresses/1')) + 'http://server.com:8080/apis/networking.k8s.io/v1/namespaces/somenamespace/ingresses/1')) self.assertEqual(requests.call_args_list[5][0], ('DELETE', - 'http://server.com:8080/api/v1/namespaces/some_namespace')) + 'http://server.com:8080/api/v1/namespaces/somenamespace')) self.assertTrue(success, msg="ERROR: finalizing VM info.") self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue())