From d8c3bf13ecccbf69078adac895b8d769efe41a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dulko?= Date: Tue, 17 Jul 2018 11:51:17 +0200 Subject: [PATCH] Get rid of cmp() usage and randomize pod labels cmp() builtin is not available in Python 3. This commit removes its usages. It also turns out we were comparing int to string in assertNotEqual() in Services tests, so that was always passing. Also tests were interfering with each other as labels added for pods in Services tests were hardcoded. That's fixed here as well. Change-Id: I37a5805f7670f905db24ddf86d3a4b4839453a6d --- kuryr_tempest_plugin/tests/scenario/base.py | 39 ++++++++++++++++++- .../tests/scenario/test_service.py | 32 ++++++++------- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/kuryr_tempest_plugin/tests/scenario/base.py b/kuryr_tempest_plugin/tests/scenario/base.py index c4d43fb1..b35d6b05 100644 --- a/kuryr_tempest_plugin/tests/scenario/base.py +++ b/kuryr_tempest_plugin/tests/scenario/base.py @@ -238,6 +238,7 @@ class BaseKuryrScenarioTest(manager.NetworkScenarioTest): try: time.sleep(5) session.get("http://{0}".format(service_ip), timeout=2) + return except Exception: LOG.warning('No initial traffic is passing through.') @@ -247,14 +248,17 @@ class BaseKuryrScenarioTest(manager.NetworkScenarioTest): raise lib_exc.ServerFault() @classmethod - def create_setup_for_service_test(cls, pod_num=2, spec_type="ClusterIP"): + def create_setup_for_service_test(cls, pod_num=2, spec_type="ClusterIP", + label=None): + label = label or data_utils.rand_name('kuryr-app') for i in range(pod_num): pod_name, pod = cls.create_pod( - labels={"app": 'pod-label'}, image='kuryr/demo') + labels={"app": label}, image='kuryr/demo') cls.addClassResourceCleanup(cls.delete_pod, pod_name) service_name, service_obj = cls.create_service( pod_label=pod.metadata.labels, spec_type=spec_type) cls.service_ip = cls.get_service_ip(service_name, spec_type=spec_type) + cls.verify_lbaas_endpoints_configured(service_name) cls.wait_service_status( cls.service_ip, CONF.kuryr_kubernetes.lb_build_timeout) @@ -297,3 +301,34 @@ class BaseKuryrScenarioTest(manager.NetworkScenarioTest): return [pod.metadata.name for pod in self.k8s_client.CoreV1Api().list_namespaced_pod( namespace=namespace).items] + + def _run_and_assert_fn(self, fn, repeats=10, responses_num=2): + cmd_outputs = set() + for i in range(repeats): + cmd_outputs.add(fn()) + self.assertEqual(responses_num, len(cmd_outputs), + 'Number of exclusive responses is incorrect. ' + 'Got %s.' % cmd_outputs) + + @classmethod + def verify_lbaas_endpoints_configured(cls, ep_name, namespace='default'): + cls._verify_endpoints_annotation( + ep_name=ep_name, ann_string=K8S_ANNOTATION_LBAAS_STATE, + poll_interval=10) + + @classmethod + def _verify_endpoints_annotation(cls, ep_name, ann_string, + poll_interval=1, namespace='default'): + # wait until endpoint annotation created + while True: + time.sleep(poll_interval) + ep = cls.k8s_client.CoreV1Api().read_namespaced_endpoints( + ep_name, namespace) + annotations = ep.metadata.annotations + try: + json.loads(annotations[ann_string]) + return + except KeyError: + LOG.info("Waiting till %s will appears " + "in ep=%s annotation ", ann_string, ep_name) + continue diff --git a/kuryr_tempest_plugin/tests/scenario/test_service.py b/kuryr_tempest_plugin/tests/scenario/test_service.py index 92782d7b..1ac89aa1 100644 --- a/kuryr_tempest_plugin/tests/scenario/test_service.py +++ b/kuryr_tempest_plugin/tests/scenario/test_service.py @@ -43,33 +43,35 @@ class TestServiceScenario(base.BaseKuryrScenarioTest): @decorators.idempotent_id('bddf5441-1244-449d-a125-b5fdcfc1a1a9') def test_service_curl(self): - cmd_output_list = list() LOG.info("Trying to curl the service IP %s" % self.service_ip) cmd = "curl {dst_ip}".format(dst_ip=self.service_ip) - for i in range(2): + + def curl(): try: - cmd_output_list.append( - subprocess.check_output(shlex.split(cmd))) + return subprocess.check_output(shlex.split(cmd)) except subprocess.CalledProcessError: LOG.error("Checking output of curl to the service IP %s " "failed" % self.service_ip) raise lib_exc.UnexpectedResponseCode() - self.assertNotEqual(cmp(cmd_output_list[0], cmd_output_list[1]), '0') + + self._run_and_assert_fn(curl) @decorators.idempotent_id('bddf5441-1244-449d-a125-b5fdcfa1a7a9') def test_pod_service_curl(self): - cmd_output_list = list() pod_name, pod = self.create_pod() self.addCleanup(self.delete_pod, pod_name) cmd = [ "/bin/sh", "-c", "curl {dst_ip}".format(dst_ip=self.service_ip)] - for i in range(2): - cmd_output_list.append(self.exec_command_in_pod(pod_name, cmd)) + + def curl(): + output = self.exec_command_in_pod(pod_name, cmd) # check if the curl command succeeded - if not cmd_output_list[i]: + if not output: LOG.error("Curl the service IP %s failed" % self.service_ip) raise lib_exc.UnexpectedResponseCode() - self.assertNotEqual(cmp(cmd_output_list[0], cmd_output_list[1]), '0') + return output + + self._run_and_assert_fn(curl) class TestLoadBalancerServiceScenario(base.BaseKuryrScenarioTest): @@ -89,15 +91,15 @@ class TestLoadBalancerServiceScenario(base.BaseKuryrScenarioTest): @decorators.idempotent_id('bddf5441-1244-449d-a175-b5fdcfc2a1a9') def test_lb_service_curl(self): - cmd_output_list = list() LOG.info("Trying to curl the service IP %s" % self.service_ip) cmd = "curl {dst_ip}".format(dst_ip=self.service_ip) - for i in range(2): + + def curl(): try: - cmd_output_list.append( - subprocess.check_output(shlex.split(cmd))) + return subprocess.check_output(shlex.split(cmd)) except subprocess.CalledProcessError: LOG.error("Checking output of curl to the service IP %s " "failed" % self.service_ip) raise lib_exc.UnexpectedResponseCode() - self.assertNotEqual(cmp(cmd_output_list[0], cmd_output_list[1]), '0') + + self._run_and_assert_fn(curl)