From 41990e9ce79121d343f803b1acef011efaad7f73 Mon Sep 17 00:00:00 2001 From: Luis Tomas Bolivar Date: Wed, 9 May 2018 09:52:47 +0000 Subject: [PATCH] Add tempest coverage for namespace creation It adds coverage to test namespace creation when the namespace handler and subnet driver are used. It checks the namespace gets created but also that the related resources get created too, in this case the Neutron Subnet, as well as the Kuryr Net CRD. In addition, it check the KuryrNet CRD has the right information about the network and subnet IDs created. Depends-On: I84580201f38c219f1943510bb493da0f07e07153 Change-Id: Iafc08ede300aecf1dc52135c6e51b89875e729d6 --- kuryr_tempest_plugin/config.py | 5 ++ kuryr_tempest_plugin/plugin.py | 5 +- kuryr_tempest_plugin/tests/scenario/base.py | 37 +++++++++++ .../tests/scenario/test_namespace.py | 63 +++++++++++++++++++ 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 kuryr_tempest_plugin/tests/scenario/test_namespace.py diff --git a/kuryr_tempest_plugin/config.py b/kuryr_tempest_plugin/config.py index 6adcb4b9..1e2f493c 100644 --- a/kuryr_tempest_plugin/config.py +++ b/kuryr_tempest_plugin/config.py @@ -33,3 +33,8 @@ port_pool_enabled = cfg.BoolOpt("port_pool_enabled", lb_build_timeout = cfg.IntOpt("lb_build_timeout", default=900, help="The max time it should take to create LB") + +namespace_enabled = cfg.BoolOpt("namespace_enabled", + default=False, + help="Whether or not namespace handler and " + "driver are enabled") diff --git a/kuryr_tempest_plugin/plugin.py b/kuryr_tempest_plugin/plugin.py index e10090d7..8d11d39b 100644 --- a/kuryr_tempest_plugin/plugin.py +++ b/kuryr_tempest_plugin/plugin.py @@ -38,9 +38,12 @@ class KuryrTempestPlugin(plugins.TempestPlugin): group='kuryr_kubernetes') conf.register_opt(project_config.lb_build_timeout, group='kuryr_kubernetes') + conf.register_opt(project_config.namespace_enabled, + group='kuryr_kubernetes') def get_opt_lists(self): return [('service_available', [project_config.service_option]), - ('kuryr_kubernetes', [project_config.port_pool_enabled]), + ('kuryr_kubernetes', [project_config.port_pool_enabled, + project_config.namespace_enabled]), ('vif_pool', [project_config.ports_pool_batch, project_config.lb_build_timeout])] diff --git a/kuryr_tempest_plugin/tests/scenario/base.py b/kuryr_tempest_plugin/tests/scenario/base.py index fec703f6..c4e47132 100644 --- a/kuryr_tempest_plugin/tests/scenario/base.py +++ b/kuryr_tempest_plugin/tests/scenario/base.py @@ -30,6 +30,10 @@ from tempest.scenario import manager CONF = config.CONF LOG = logging.getLogger(__name__) +KURYR_NET_CRD_GROUP = 'openstack.org' +KURYR_NET_CRD_VERSION = 'v1' +KURYR_NET_CRD_PLURAL = 'kuryrnets' + class BaseKuryrScenarioTest(manager.NetworkScenarioTest): @@ -225,3 +229,36 @@ class BaseKuryrScenarioTest(manager.NetworkScenarioTest): cls.service_ip, CONF.kuryr_kubernetes.lb_build_timeout) cls.addClassResourceCleanup(cls.delete_service, service_name) + + @classmethod + def create_namespace(cls, name=None): + if not name: + name = data_utils.rand_name(prefix='kuryr-namespace') + namespace = cls.k8s_client.V1Namespace() + namespace.metadata = cls.k8s_client.V1ObjectMeta(name=name) + namespace_obj = cls.k8s_client.CoreV1Api().create_namespace( + body=namespace) + + # wait until namespace gets created + while True: + time.sleep(1) + ns = cls.k8s_client.CoreV1Api().read_namespace_status(name) + if ns.metadata.annotations is not None: + break + + return name, namespace_obj + + @classmethod + def delete_namespace(cls, name, **kwargs): + body = cls.k8s_client.V1DeleteOptions(**kwargs) + cls.k8s_client.CoreV1Api().delete_namespace(name=name, body=body) + + @classmethod + def list_namespaces(cls, **kwargs): + return cls.k8s_client.CoreV1Api().list_namespace(**kwargs) + + @classmethod + def get_kuryr_net_crds(cls, name): + return cls.k8s_client.CustomObjectsApi().get_cluster_custom_object( + group=KURYR_NET_CRD_GROUP, version=KURYR_NET_CRD_VERSION, + plural=KURYR_NET_CRD_PLURAL, name=name) diff --git a/kuryr_tempest_plugin/tests/scenario/test_namespace.py b/kuryr_tempest_plugin/tests/scenario/test_namespace.py new file mode 100644 index 00000000..ea6aa095 --- /dev/null +++ b/kuryr_tempest_plugin/tests/scenario/test_namespace.py @@ -0,0 +1,63 @@ +# Copyright 2018 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from oslo_log import log as logging +from tempest import config + +from kuryr_tempest_plugin.tests.scenario import base + +LOG = logging.getLogger(__name__) +CONF = config.CONF + + +class TestNamespaceScenario(base.BaseKuryrScenarioTest): + + @classmethod + def skip_checks(cls): + super(TestNamespaceScenario, cls).skip_checks() + if not CONF.kuryr_kubernetes.namespace_enabled: + raise cls.skipException('Namespace driver and handler must be ' + 'enabled to run this tests') + + @classmethod + def setup_clients(cls): + super(TestNamespaceScenario, cls).setup_clients() + + def test_namespace(self): + namespace_name, namespace = self.create_namespace() + self.addCleanup(self.delete_namespace, namespace_name) + + existing_namespaces = [ns.metadata.name + for ns in self.list_namespaces().items] + + self.assertIn(namespace_name, existing_namespaces) + + subnet_name = 'ns/' + namespace_name + '-subnet' + kuryr_net_crd_name = 'ns-' + namespace_name + + seen_subnets = self.os_admin.subnets_client.list_subnets() + seen_subnet_names = [n['name'] for n in seen_subnets['subnets']] + + self.assertIn(subnet_name, seen_subnet_names) + + subnet_id = [n['id'] for n in seen_subnets['subnets'] + if n['name'] == subnet_name] + net_id = [n['network_id'] for n in seen_subnets['subnets'] + if n['name'] == subnet_name] + + kuryr_net_crd = self.get_kuryr_net_crds(kuryr_net_crd_name) + + self.assertIn(kuryr_net_crd_name, kuryr_net_crd['metadata']['name']) + self.assertIn(kuryr_net_crd['spec']['subnetId'], subnet_id) + self.assertIn(kuryr_net_crd['spec']['netId'], net_id)