Merge "Testing service creation and connectivity"

This commit is contained in:
Zuul 2018-04-02 07:18:48 +00:00 committed by Gerrit Code Review
commit 30b44804c9
4 changed files with 135 additions and 4 deletions

View File

@ -29,3 +29,7 @@ port_pool_enabled = cfg.BoolOpt("port_pool_enabled",
default=False,
help="Whether or not port pool feature is "
"enabled")
lb_build_timeout = cfg.IntOpt("lb_build_timeout",
default=900,
help="The max time it should take to create LB")

View File

@ -36,8 +36,11 @@ class KuryrTempestPlugin(plugins.TempestPlugin):
group='vif_pool')
conf.register_opt(project_config.port_pool_enabled,
group='kuryr_kubernetes')
conf.register_opt(project_config.lb_build_timeout,
group='kuryr_kubernetes')
def get_opt_lists(self):
return [('service_available', [project_config.service_option]),
('kuryr_kubernetes', [project_config.port_pool_enabled]),
('vif_pool', [project_config.ports_pool_batch])]
('vif_pool', [project_config.ports_pool_batch,
project_config.lb_build_timeout])]

View File

@ -14,15 +14,21 @@
import time
from oslo_log import log as logging
import requests
from kubernetes import client as k8s_client
from kubernetes import config as k8s_config
from kubernetes.stream import stream
from tempest import config
from tempest.lib.common.utils import data_utils
from tempest.lib import exceptions as lib_exc
from tempest.scenario import manager
CONF = config.CONF
LOG = logging.getLogger(__name__)
class BaseKuryrScenarioTest(manager.NetworkScenarioTest):
@ -52,11 +58,12 @@ class BaseKuryrScenarioTest(manager.NetworkScenarioTest):
cls.os_admin.floating_ips_client.delete_floatingip(
fip['floatingip']['id'])
def create_pod(self, name=None, image='kuryr/demo',
def create_pod(self, name=None, labels=None, image='kuryr/demo',
namespace="default"):
name = data_utils.rand_name(prefix='kuryr-pod')
if not name:
name = data_utils.rand_name(prefix='kuryr-pod')
pod = self.k8s_client.V1Pod()
pod.metadata = self.k8s_client.V1ObjectMeta(name=name)
pod.metadata = self.k8s_client.V1ObjectMeta(name=name, labels=labels)
container = self.k8s_client.V1Container(name=name)
container.image = image
@ -137,3 +144,61 @@ class BaseKuryrScenarioTest(manager.NetworkScenarioTest):
for project in projects_list['projects']:
if project_name == project['name']:
return project['id']
def create_service(self, pod_label, service_name=None, api_version="v1",
kind=None, protocol="TCP", port=80, target_port=8080,
spec_type='ClusterIP', namespace="default"):
if not service_name:
service_name = data_utils.rand_name(prefix='kuryr-service')
service = self.k8s_client.V1Service()
service.api_version = api_version
service.kind = kind
service.metadata = self.k8s_client.V1ObjectMeta(name=service_name)
spec = self.k8s_client.V1ServiceSpec()
spec.ports = [self.k8s_client.V1ServicePort(
protocol=protocol,
port=port,
target_port=target_port)]
spec.selector = pod_label
spec.type = spec_type
service.spec = spec
service_obj = self.k8s_client.CoreV1Api().create_namespaced_service(
namespace=namespace, body=service)
self.addCleanup(self.delete_service, service_name)
return service_name, service_obj
def delete_service(self, service_name, namespace="default"):
self.k8s_client.CoreV1Api().delete_namespaced_service(
name=service_name,
namespace=namespace)
def get_service_ip(
self, service_name, spec_type="ClusterIP", namespace="default"):
api = self.k8s_client.CoreV1Api()
while True:
time.sleep(5)
service = api.read_namespaced_service(service_name, namespace)
if spec_type == "LoadBalancer":
if service.status.load_balancer.ingress:
return service.status.load_balancer.ingress[0].ip
elif spec_type == "ClusterIP":
return service.spec.cluster_ip
else:
raise lib_exc.NotImplemented()
def wait_service_status(self, service_ip, timeout_period):
session = requests.Session()
start = time.time()
while time.time() - start < timeout_period:
try:
session.get("http://{0}".format(service_ip), timeout=2)
time.sleep(1)
return
except Exception:
LOG.warning('No initial traffic is passing through.')
time.sleep(1)
LOG.error(
"Traffic didn't pass within the period of %s" % timeout_period)
raise lib_exc.ServerFault()

View File

@ -0,0 +1,59 @@
# 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.
import shlex
import subprocess
from oslo_log import log as logging
from tempest import config
from tempest.lib import decorators
from kuryr_tempest_plugin.tests.scenario import base
LOG = logging.getLogger(__name__)
CONF = config.CONF
class TestServiceScenario(base.BaseKuryrScenarioTest):
@classmethod
def skip_checks(cls):
super(TestServiceScenario, cls).skip_checks()
if not CONF.network_feature_enabled.floating_ips:
raise cls.skipException("Floating ips are not available")
@decorators.idempotent_id('bddf5441-1244-449d-a125-b5fdcfc1a1a9')
def test_service_curl(self):
pod = None
cmd_output_list = list()
for i in range(2):
pod_name, pod = self.create_pod(
labels={"app": 'pod-label'}, image='celebdor/kuryr-demo')
self.addCleanup(self.delete_pod, pod_name, pod)
service_name, service_obj = self.create_service(
pod_label=pod.metadata.labels)
service_ip = self.get_service_ip(service_name)
self.wait_service_status(
service_ip, CONF.kuryr_kubernetes.lb_build_timeout)
LOG.info("Trying to curl the service load balancer IP %s" % service_ip)
cmd = "curl {dst_ip}".format(dst_ip=service_ip)
for i in range(2):
try:
cmd_output_list.append(
subprocess.check_output(shlex.split(cmd)))
except subprocess.CalledProcessError:
LOG.error("Checking output of curl the service load balancer "
"IP %s failed" % service_ip)
raise