Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Feb 20, 2024
1 parent ec90789 commit fd757c3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
4 changes: 2 additions & 2 deletions IM/connectors/Kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 7 additions & 7 deletions test/files/tosca_k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/unit/Tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
47 changes: 24 additions & 23 deletions test/unit/connectors/Kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
Expand All @@ -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]
Expand All @@ -162,22 +162,22 @@ 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 = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "test",
"namespace": "some_namespace",
"namespace": "somenamespace",
"labels": {"name": "test", "IM_INFRA_ID": "infid", "key": "invalid_"},
},
"spec": {
Expand All @@ -202,15 +202,15 @@ 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 = {
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": "test",
"namespace": "some_namespace",
"namespace": "somenamespace",
"labels": {"name": "test"},
},
"spec": {
Expand All @@ -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 = {
Expand All @@ -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": [
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand All @@ -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())

Expand Down

0 comments on commit fd757c3

Please sign in to comment.