From 36402266ffed001d182dcd425521bb2b8394fada Mon Sep 17 00:00:00 2001 From: Andrey Kurilin Date: Mon, 19 Feb 2018 19:14:12 +0200 Subject: [PATCH] Fix support for k8s-client 4.0 The new release of kubernetes python client improved the validation, so we need to ediot our test to create the right fake objects. As well, kubernetes.client.ConnectionObject was renamed to just `Connection` and we should handle this. Change-Id: I1a3275a8a5a3d729ce507689ab38e3c3d54d3c77 --- rally_openstack/scenarios/magnum/utils.py | 6 ++- requirements.txt | 2 +- tests/unit/scenarios/magnum/test_utils.py | 52 +++++++++++++++-------- upper-constraints.txt | 2 +- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/rally_openstack/scenarios/magnum/utils.py b/rally_openstack/scenarios/magnum/utils.py index c666fd46..db0dc0ee 100644 --- a/rally_openstack/scenarios/magnum/utils.py +++ b/rally_openstack/scenarios/magnum/utils.py @@ -163,7 +163,11 @@ class MagnumScenario(scenario.OpenStackScenario): cert_file = os.path.join(dir, cert_file) ca_certs = cluster_uuid + "_ca.crt" ca_certs = os.path.join(dir, ca_certs) - config = k8s_config.ConfigurationObject() + if hasattr(k8s_config, "ConfigurationObject"): + # k8sclient < 4.0.0 + config = k8s_config.ConfigurationObject() + else: + config = k8s_config.Configuration() config.host = cluster.api_address config.ssl_ca_cert = ca_certs config.cert_file = cert_file diff --git a/requirements.txt b/requirements.txt index decb9b58..a2f3d49e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -30,4 +30,4 @@ python-swiftclient>=3.2.0 # Apache Software License python-troveclient>=2.2.0 # Apache Software License python-watcherclient>=1.1.0 # Apache Software License python-zaqarclient>=1.0.0 # Apache Software License -kubernetes>1.0.0,<4.0.0 # Apache License Version 2.0 +kubernetes>1.0.0 # Apache License Version 2.0 diff --git a/tests/unit/scenarios/magnum/test_utils.py b/tests/unit/scenarios/magnum/test_utils.py index 10e2b6d8..18d38b53 100644 --- a/tests/unit/scenarios/magnum/test_utils.py +++ b/tests/unit/scenarios/magnum/test_utils.py @@ -16,6 +16,7 @@ import os import mock +from kubernetes import client as kubernetes_client from kubernetes.client import api_client from kubernetes.client.rest import ApiException from rally import exceptions @@ -135,9 +136,18 @@ class MagnumScenarioTestCase(test.ScenarioTestCase): @mock.patch("kubernetes.client.api_client.ApiClient") @mock.patch("kubernetes.client.apis.core_v1_api.CoreV1Api") - @mock.patch("kubernetes.client.ConfigurationObject") - def test_get_k8s_api_client_using_tls(self, mock_configuration_object, - mock_core_v1_api, mock_api_client): + def test_get_k8s_api_client_using_tls(self, mock_core_v1_api, + mock_api_client): + + if hasattr(kubernetes_client, "ConfigurationObject"): + # it is k8s-client < 4.0.0 + m = mock.patch("kubernetes.client.ConfigurationObject") + else: + m = mock.patch("kubernetes.client.Configuration") + + mock_configuration_object = m.start() + self.addCleanup(m.stop) + self.context.update({ "ca_certs_directory": "/home/stack", "tenant": { @@ -169,9 +179,17 @@ class MagnumScenarioTestCase(test.ScenarioTestCase): @mock.patch("kubernetes.client.api_client.ApiClient") @mock.patch("kubernetes.client.apis.core_v1_api.CoreV1Api") - @mock.patch("kubernetes.client.ConfigurationObject") - def test_get_k8s_api_client(self, mock_configuration_object, - mock_core_v1_api, mock_api_client): + def test_get_k8s_api_client(self, mock_core_v1_api, mock_api_client): + + if hasattr(kubernetes_client, "ConfigurationObject"): + # it is k8s-client < 4.0.0 + m = mock.patch("kubernetes.client.ConfigurationObject") + else: + m = mock.patch("kubernetes.client.Configuration") + + mock_configuration_object = m.start() + self.addCleanup(m.stop) + self.context.update({ "tenant": { "id": "rally_tenant_id", @@ -227,16 +245,17 @@ class MagnumScenarioTestCase(test.ScenarioTestCase): almost_ready_status.phase = "almost_ready" almost_ready_pod.status = almost_ready_status ready_pod = api_client.models.V1Pod() - ready_condition = api_client.models.V1PodCondition() - ready_condition.status = "True" - ready_condition.type = "Ready" + ready_condition = api_client.models.V1PodCondition(status="True", + type="Ready") ready_status = api_client.models.V1PodStatus() ready_status.phase = "Running" ready_status.conditions = [ready_condition] ready_pod_metadata = api_client.models.V1ObjectMeta() ready_pod_metadata.uid = "123456789" - ready_pod_spec = api_client.models.V1PodSpec() - ready_pod_spec.node_name = "host_abc" + ready_pod_spec = api_client.models.V1PodSpec( + node_name="host_abc", + containers=[] + ) ready_pod.status = ready_status ready_pod.metadata = ready_pod_metadata ready_pod.spec = ready_pod_spec @@ -309,12 +328,12 @@ class MagnumScenarioTestCase(test.ScenarioTestCase): k8s_api.create_namespaced_replication_controller.return_value = rc not_ready_rc = api_client.models.V1ReplicationController() not_ready_rc_status = ( - api_client.models.V1ReplicationControllerStatus()) - not_ready_rc_status.replicas = 0 + api_client.models.V1ReplicationControllerStatus(replicas=0)) not_ready_rc.status = not_ready_rc_status ready_rc = api_client.models.V1ReplicationController() - ready_rc_status = api_client.models.V1ReplicationControllerStatus() - ready_rc_status.replicas = manifest["spec"]["replicas"] + ready_rc_status = api_client.models.V1ReplicationControllerStatus( + replicas=manifest["spec"]["replicas"] + ) ready_rc_metadata = api_client.models.V1ObjectMeta() ready_rc_metadata.uid = "123456789" ready_rc_metadata.name = rcname @@ -352,8 +371,7 @@ class MagnumScenarioTestCase(test.ScenarioTestCase): k8s_api.create_namespaced_replication_controller.return_value = rc not_ready_rc = api_client.models.V1ReplicationController() not_ready_rc_status = ( - api_client.models.V1ReplicationControllerStatus()) - not_ready_rc_status.replicas = 0 + api_client.models.V1ReplicationControllerStatus(replicas=0)) not_ready_rc_metadata = api_client.models.V1ObjectMeta() not_ready_rc_metadata.uid = "123456789" not_ready_rc.status = not_ready_rc_status diff --git a/upper-constraints.txt b/upper-constraints.txt index 7d47a0f1..7d11fa0c 100644 --- a/upper-constraints.txt +++ b/upper-constraints.txt @@ -5,7 +5,7 @@ gnocchiclient===7.0.1 Jinja2===2.10 jsonschema===2.6.0 keystoneauth1===3.4.0 -kubernetes===3.0.0 +kubernetes===4.0.0 morph===0.1.2 netaddr===0.7.19 os-faults===0.1.17