Adapt namespace test to the kuryrnetwork CRD object

Change-Id: If0aaf748d13027b3d660aa0f74c4f6653e911250
This commit is contained in:
Luis Tomas Bolivar 2020-03-09 09:35:45 +01:00
parent 6cd840bff2
commit fe61431318
4 changed files with 83 additions and 28 deletions

View File

@ -73,6 +73,8 @@ kuryr_k8s_opts = [
"cloud provider is set"),
cfg.BoolOpt("validate_crd", default=False, help="Whether or not kuryr "
"CRDs should be validated"),
cfg.BoolOpt("kuryrnetworks", default=False, help="Whether or not "
"kuryrnetworks CRDs are used"),
cfg.BoolOpt("configmap_modifiable", default=False, help="Whether config "
"map can be changed"),
cfg.StrOpt("controller_label", default="name=kuryr-controller",

View File

@ -41,6 +41,7 @@ LOG = logging.getLogger(__name__)
KURYR_CRD_GROUP = 'openstack.org'
KURYR_CRD_VERSION = 'v1'
KURYR_NET_CRD_PLURAL = 'kuryrnets'
KURYR_NETWORK_CRD_PLURAL = 'kuryrnetworks'
KURYR_NET_POLICY_CRD_PLURAL = 'kuryrnetpolicies'
K8S_ANNOTATION_PREFIX = 'openstack.org/kuryr'
K8S_ANNOTATION_LBAAS_STATE = K8S_ANNOTATION_PREFIX + '-lbaas-state'
@ -561,24 +562,37 @@ class BaseKuryrScenarioTest(manager.NetworkScenarioTest):
namespace=namespace)
@classmethod
def create_namespace(cls, name=None, wait_for_crd_annotation=True):
def create_namespace(cls, name=None, wait_for_crd=True):
if not name:
name = data_utils.rand_name(prefix='kuryr-namespace')
kuryr_crd_annotation = K8S_ANNOTATION_PREFIX + "-net-crd"
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 and
(not wait_for_crd_annotation or
ns.metadata.annotations.get(kuryr_crd_annotation))):
break
if not wait_for_crd:
return name, namespace_obj
if CONF.kuryr_kubernetes.kuryrnetworks:
# wait until kuryrnetwork CRD gets populated
while True:
time.sleep(1)
try:
kns_crd = cls.get_kuryr_network_crds(name)
except kubernetes.client.rest.ApiException:
continue
if (kns_crd.get('status') and
kns_crd['status'].get('routerId')):
break
else:
kuryr_crd_annotation = K8S_ANNOTATION_PREFIX + "-net-crd"
# wait until namespace gets created
while True:
time.sleep(1)
ns = cls.k8s_client.CoreV1Api().read_namespace_status(name)
if (ns.metadata.annotations and
ns.metadata.annotations.get(kuryr_crd_annotation)):
break
return name, namespace_obj
@ -587,6 +601,10 @@ class BaseKuryrScenarioTest(manager.NetworkScenarioTest):
body = cls.k8s_client.V1DeleteOptions(**kwargs)
cls.k8s_client.CoreV1Api().delete_namespace(name=name, body=body)
@classmethod
def get_namespace(cls, name):
cls.k8s_client.CoreV1Api().read_namespace(name=name)
@classmethod
def list_namespaces(cls, **kwargs):
return cls.k8s_client.CoreV1Api().list_namespace(**kwargs)
@ -612,6 +630,13 @@ class BaseKuryrScenarioTest(manager.NetworkScenarioTest):
group=KURYR_CRD_GROUP, version=KURYR_CRD_VERSION,
plural=KURYR_NET_CRD_PLURAL, name=name)
@classmethod
def get_kuryr_network_crds(cls, namespace):
return cls.k8s_client.CustomObjectsApi().get_namespaced_custom_object(
group=KURYR_CRD_GROUP, version=KURYR_CRD_VERSION,
namespace=namespace, plural=KURYR_NETWORK_CRD_PLURAL,
name=namespace)
@classmethod
def get_kuryr_netpolicy_crds(cls, name, namespace='default', **kwargs):
return cls.k8s_client.CustomObjectsApi().get_namespaced_custom_object(

View File

@ -28,6 +28,8 @@ from kuryr_tempest_plugin.tests.scenario import consts
LOG = logging.getLogger(__name__)
CONF = config.CONF
TIMEOUT_PERIOD = 120
class TestNamespaceScenario(base.BaseKuryrScenarioTest):
@ -66,20 +68,25 @@ class TestNamespaceScenario(base.BaseKuryrScenarioTest):
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)
if CONF.kuryr_kubernetes.kuryrnetworks:
kuryr_net_crd = self.get_kuryr_network_crds(namespace_name)
self.assertIn(namespace_name,
kuryr_net_crd['metadata']['name'])
self.assertIn(kuryr_net_crd['status']['subnetId'], subnet_id)
self.assertIn(kuryr_net_crd['status']['netId'], net_id)
else:
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)
# Check namespace pod connectivity
pod_name, pod = self.create_pod(labels={"app": 'pod-label'},
namespace=namespace_name)
svc_name, _ = self.create_service(pod_label=pod.metadata.labels,
spec_type='LoadBalancer',
namespace=namespace_name)
svc_service_ip = self.get_service_ip(service_name=svc_name,
spec_type='LoadBalancer',
namespace=namespace_name)
self.wait_service_status(svc_service_ip,
CONF.kuryr_kubernetes.lb_build_timeout)
@ -270,10 +277,22 @@ class TestNamespaceScenario(base.BaseKuryrScenarioTest):
# Check resources are deleted
self.delete_namespace(namespace)
while True:
start = time.time()
while time.time() - start < TIMEOUT_PERIOD:
time.sleep(1)
try:
self.get_kuryr_net_crds(net_crd['metadata']['name'])
if CONF.kuryr_kubernetes.kuryrnetworks:
self.get_kuryr_network_crds(namespace)
else:
self.get_kuryr_net_crds(net_crd['metadata']['name'])
except kubernetes.client.rest.ApiException:
break
# Also wait for the namespace removal
while time.time() - start < TIMEOUT_PERIOD:
time.sleep(1)
try:
self.get_namespace(namespace)
except kubernetes.client.rest.ApiException:
break
@ -285,16 +304,14 @@ class TestNamespaceScenario(base.BaseKuryrScenarioTest):
seen_subnet_names = [n['name'] for n in seen_subnets['subnets']]
self.assertNotIn(subnet, seen_subnet_names)
seen_sgs = self.list_security_groups()
seen_sg_ids = [sg['id'] for sg in seen_sgs]
if net_crd['spec'].get('sgId', None):
self.assertNotIn(net_crd['spec']['sgId'], seen_sg_ids)
@decorators.idempotent_id('90b7cb81-f80e-4ff3-9892-9e5fdcd08289')
def test_create_kuryrnet_crd_without_net_id(self):
if not CONF.kuryr_kubernetes.validate_crd:
raise self.skipException('CRD validation must be enabled to run '
'this test.')
if CONF.kuryr_kubernetes.kuryrnetworks:
raise self.skipException('Kuryrnetworks CRD should not be used '
'to run this test.')
kuryrnet = dict(self._get_kuryrnet_obj())
del kuryrnet['spec']['netId']
error_msg = 'spec.netId in body is required'
@ -306,6 +323,9 @@ class TestNamespaceScenario(base.BaseKuryrScenarioTest):
if not CONF.kuryr_kubernetes.validate_crd:
raise self.skipException('CRD validation must be enabled to run '
'this test.')
if CONF.kuryr_kubernetes.kuryrnetworks:
raise self.skipException('Kuryrnetworks CRD should not be used '
'to run this test.')
kuryrnet = dict(self._get_kuryrnet_obj())
kuryrnet['spec']['populated'] = 'False'
error_msg = 'spec.populated in body must be of type boolean'
@ -331,6 +351,9 @@ class TestNamespaceScenario(base.BaseKuryrScenarioTest):
}
def _create_kuryr_net_crd_obj(self, crd_manifest, error_msg, field):
if CONF.kuryr_kubernetes.kuryrnetworks:
raise self.skipException('Kuryrnetworks CRD should not be used '
'to run this test.')
version = 'v1'
group = 'openstack.org'
plural = 'kuryrnets'
@ -362,7 +385,9 @@ class TestNamespaceScenario(base.BaseKuryrScenarioTest):
ns_name = data_utils.rand_name(prefix='kuryr-ns')
ns_name, ns = self.create_namespace(
name=ns_name, wait_for_crd_annotation=False)
name=ns_name, wait_for_crd=False)
# Allow controller manager to create a token for the service account
time.sleep(1)
self.addCleanup(self.delete_namespace, ns_name)
pod_name, pod = self.create_pod(
namespace=ns_name, wait_for_status=False)
@ -372,18 +397,20 @@ class TestNamespaceScenario(base.BaseKuryrScenarioTest):
retries = 120
while True:
try:
time.sleep(1)
self.k8s_client.CoreV1Api().read_namespace(ns_name)
retries -= 1
self.assertNotEqual(0, retries,
"Timed out waiting for namespace %s to"
" be deleted" % ns_name)
time.sleep(1)
except kubernetes.client.rest.ApiException as e:
if e.status == 404:
break
ns_name, ns = self.create_namespace(
name=ns_name, wait_for_crd_annotation=False)
name=ns_name, wait_for_crd=False)
# Allow controller manager to create a token for the service account
time.sleep(1)
pod_name, pod = self.create_pod(
namespace=ns_name, wait_for_status=False)

View File

@ -328,6 +328,7 @@ class TestNetworkPolicyScenario(base.BaseKuryrScenarioTest):
@decorators.idempotent_id('24577a9b-1d46-419b-8b60-da3c49d777c3')
def test_create_delete_network_policy_non_default_ns(self):
ns_name, ns = self.create_namespace()
self.addCleanup(self.delete_namespace, ns_name)
match_labels = {'role': 'db'}
np = self.create_network_policy(match_labels=match_labels,
namespace=ns_name)