diff options
-rw-r--r-- | yardstick/common/kubernetes_utils.py | 22 | ||||
-rw-r--r-- | yardstick/orchestrator/kubernetes.py | 2 | ||||
-rw-r--r-- | yardstick/tests/unit/common/test_kubernetes_utils.py | 78 | ||||
-rw-r--r-- | yardstick/tests/unit/orchestrator/test_kubernetes.py | 3 |
4 files changed, 97 insertions, 8 deletions
diff --git a/yardstick/common/kubernetes_utils.py b/yardstick/common/kubernetes_utils.py index e708dc503..323f13abb 100644 --- a/yardstick/common/kubernetes_utils.py +++ b/yardstick/common/kubernetes_utils.py @@ -263,10 +263,30 @@ 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) else: api.create_namespaced_custom_object( diff --git a/yardstick/orchestrator/kubernetes.py b/yardstick/orchestrator/kubernetes.py index 594f679c0..b0b93a3c2 100644 --- a/yardstick/orchestrator/kubernetes.py +++ b/yardstick/orchestrator/kubernetes.py @@ -437,7 +437,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 30c1c1ffb..ba6b5f388 100644 --- a/yardstick/tests/unit/common/test_kubernetes_utils.py +++ b/yardstick/tests/unit/common/test_kubernetes_utils.py @@ -176,7 +176,7 @@ 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() @@ -184,29 +184,97 @@ class CreateNetworkTestCase(base.BaseUnitTestCase): 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 4bf678390..2d5c4a26f 100644 --- a/yardstick/tests/unit/orchestrator/test_kubernetes.py +++ b/yardstick/tests/unit/orchestrator/test_kubernetes.py @@ -529,9 +529,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): |