From d0c9fc59e53cace276093f8785fbdcd16fb1d2aa Mon Sep 17 00:00:00 2001 From: John O Loughlin Date: Fri, 13 Jul 2018 10:41:15 +0100 Subject: Check for network already created k8 When creating a network the host should be checked to see if the network is already created. If the required network is already there it should be used. JIRA: YARDSTICK-1315 Change-Id: I09b114a728364ee56397af70cc48f1b7904f06cc Signed-off-by: John O Loughlin --- yardstick/common/kubernetes_utils.py | 22 +++++- yardstick/orchestrator/kubernetes.py | 2 +- .../tests/unit/common/test_kubernetes_utils.py | 78 ++++++++++++++++++++-- .../tests/unit/orchestrator/test_kubernetes.py | 3 +- 4 files changed, 97 insertions(+), 8 deletions(-) (limited to 'yardstick') diff --git a/yardstick/common/kubernetes_utils.py b/yardstick/common/kubernetes_utils.py index 42267fc41..d1dd42173 100644 --- a/yardstick/common/kubernetes_utils.py +++ b/yardstick/common/kubernetes_utils.py @@ -241,8 +241,28 @@ def get_custom_resource_definition(kind): action='delete', resource='CustomResourceDefinition') -def create_network(scope, group, version, plural, body, namespace='default'): +def get_network(scope, group, version, plural, name, namespace='default'): api = get_custom_objects_api() + try: + if scope == consts.SCOPE_CLUSTER: + network = api.get_cluster_custom_object(group, version, plural, name) + else: + network = api.get_namespaced_custom_object( + group, version, namespace, plural, name) + except ApiException as e: + if e.status in [404]: + return + else: + raise exceptions.KubernetesApiException( + action='get', resource='Custom Object: Network') + return network + + +def create_network(scope, group, version, plural, body, name, namespace='default'): + api = get_custom_objects_api() + if get_network(scope, group, version, plural, name, namespace): + logging.info('Network %s already exists', name) + return try: if scope == consts.SCOPE_CLUSTER: api.create_cluster_custom_object(group, version, plural, body) diff --git a/yardstick/orchestrator/kubernetes.py b/yardstick/orchestrator/kubernetes.py index bb01b33fa..afff5cf58 100644 --- a/yardstick/orchestrator/kubernetes.py +++ b/yardstick/orchestrator/kubernetes.py @@ -399,7 +399,7 @@ class NetworkObject(object): def create(self): k8s_utils.create_network(self.scope, self.group, self.version, - self.plural, self.template) + self.plural, self.template, self._name) def delete(self): k8s_utils.delete_network(self.scope, self.group, self.version, diff --git a/yardstick/tests/unit/common/test_kubernetes_utils.py b/yardstick/tests/unit/common/test_kubernetes_utils.py index bf9992b57..5da32a7cc 100644 --- a/yardstick/tests/unit/common/test_kubernetes_utils.py +++ b/yardstick/tests/unit/common/test_kubernetes_utils.py @@ -159,37 +159,105 @@ class GetCustomResourceDefinitionTestCase(base.BaseUnitTestCase): kubernetes_utils.get_custom_resource_definition('kind') -class CreateNetworkTestCase(base.BaseUnitTestCase): +class GetNetworkTestCase(base.BaseUnitTestCase): @mock.patch.object(kubernetes_utils, 'get_custom_objects_api') def test_execute_correct(self, mock_get_api): + mock_api = mock.Mock() + mock_get_api.return_value = mock_api + group = 'group.com' + version = mock.Mock() + plural = 'networks' + name = 'net_one' + + kubernetes_utils.get_network( + constants.SCOPE_CLUSTER, group, version, plural, name) + mock_api.get_cluster_custom_object.assert_called_once_with( + group, version, plural, name) + + mock_api.reset_mock() + kubernetes_utils.get_network( + constants.SCOPE_NAMESPACED, group, version, plural, name) + mock_api.get_namespaced_custom_object.assert_called_once_with( + group, version, 'default', plural, name) + + @mock.patch.object(kubernetes_utils, 'get_custom_objects_api') + def test_execute_exception(self, mock_get_api): + mock_api = mock.Mock() + mock_api.get_cluster_custom_object.side_effect = rest.ApiException(404) + mock_api.get_namespaced_custom_object.side_effect = rest.ApiException(404) + mock_get_api.return_value = mock_api + group = 'group.com' + version = mock.Mock() + plural = 'networks' + name = 'net_one' + + network_obj = kubernetes_utils.get_network( + constants.SCOPE_CLUSTER, group, version, plural, name) + self.assertIsNone(network_obj) + + mock_api.reset_mock() + network_obj = kubernetes_utils.get_network( + constants.SCOPE_NAMESPACED, group, version, plural, name) + self.assertIsNone(network_obj) + + +class CreateNetworkTestCase(base.BaseUnitTestCase): + @mock.patch.object(kubernetes_utils, 'get_custom_objects_api') + @mock.patch.object(kubernetes_utils, 'get_network') + def test_execute_correct(self, mock_get_net, mock_get_api): + mock_get_net.return_value = None mock_api = mock.Mock() mock_get_api.return_value = mock_api group = 'group.com' version = mock.Mock() plural = 'networks' body = mock.Mock() + name = 'net_one' kubernetes_utils.create_network( - constants.SCOPE_CLUSTER, group, version, plural, body) + constants.SCOPE_CLUSTER, group, version, plural, body, name) mock_api.create_cluster_custom_object.assert_called_once_with( group, version, plural, body) mock_api.reset_mock() kubernetes_utils.create_network( - constants.SCOPE_NAMESPACED, group, version, plural, body) + constants.SCOPE_NAMESPACED, group, version, plural, body, name) mock_api.create_namespaced_custom_object.assert_called_once_with( group, version, 'default', plural, body) + @mock.patch.object(kubernetes_utils, 'get_custom_objects_api') + @mock.patch.object(kubernetes_utils, 'get_network') + def test_network_already_created(self, mock_get_net, mock_get_api): + mock_get_net.return_value = mock.Mock + mock_api = mock.Mock() + mock_get_api.return_value = mock_api + group = 'group.com' + version = mock.Mock() + plural = 'networks' + body = mock.Mock() + name = 'net_one' + + mock_api.reset_mock() + kubernetes_utils.create_network( + constants.SCOPE_CLUSTER, group, version, plural, body, name) + mock_api.create_cluster_custom_object.assert_not_called() + + mock_api.reset_mock() + kubernetes_utils.create_network( + constants.SCOPE_NAMESPACED, group, version, plural, body, name) + mock_api.create_namespaced_custom_object.assert_not_called() @mock.patch.object(kubernetes_utils, 'get_custom_objects_api') - def test_execute_exception(self, mock_get_api): + @mock.patch.object(kubernetes_utils, 'get_network') + def test_execute_exception(self, mock_get_net, mock_get_api): + mock_get_net.return_value = None mock_api = mock.Mock() mock_api.create_cluster_custom_object.side_effect = rest.ApiException mock_get_api.return_value = mock_api with self.assertRaises(exceptions.KubernetesApiException): kubernetes_utils.create_network( constants.SCOPE_CLUSTER, mock.ANY, mock.ANY, mock.ANY, - mock.ANY) + mock.ANY, mock.ANY) class DeleteNetworkTestCase(base.BaseUnitTestCase): diff --git a/yardstick/tests/unit/orchestrator/test_kubernetes.py b/yardstick/tests/unit/orchestrator/test_kubernetes.py index f248338ee..5a48fe573 100644 --- a/yardstick/tests/unit/orchestrator/test_kubernetes.py +++ b/yardstick/tests/unit/orchestrator/test_kubernetes.py @@ -491,9 +491,10 @@ class NetworkObjectTestCase(base.BaseUnitTestCase): net_obj._version = 'version' net_obj._plural = 'plural' net_obj._template = 'template' + net_obj._name = 'fake_name' net_obj.create() mock_create_network.assert_called_once_with( - 'scope', 'group', 'version', 'plural', 'template') + 'scope', 'group', 'version', 'plural', 'template', 'fake_name') @mock.patch.object(kubernetes_utils, 'delete_network') def test_delete(self, mock_delete_network): -- cgit 1.2.3-korg