diff options
Diffstat (limited to 'functest/ci')
-rw-r--r-- | functest/ci/check_deployment.py | 163 | ||||
-rw-r--r-- | functest/ci/check_os.sh | 123 | ||||
-rw-r--r-- | functest/ci/prepare_env.py | 38 |
3 files changed, 168 insertions, 156 deletions
diff --git a/functest/ci/check_deployment.py b/functest/ci/check_deployment.py new file mode 100644 index 00000000..fe20dc8f --- /dev/null +++ b/functest/ci/check_deployment.py @@ -0,0 +1,163 @@ +#!/usr/bin/env python + +# Copyright (c) 2017 Ericsson and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +""" +OpenStack deployment checker + +Verifies that: + - Credentials file is given and contains the right information + - OpenStack endpoints are reachable +""" + +import logging +import logging.config +import os +import pkg_resources +import socket +import time +from urlparse import urlparse + +from snaps.openstack.utils import glance_utils +from snaps.openstack.utils import keystone_utils +from snaps.openstack.utils import neutron_utils +from snaps.openstack.utils import nova_utils +from snaps.openstack.tests import openstack_tests + +__author__ = "Jose Lausuch <jose.lausuch@ericsson.com>" + +LOGGER = logging.getLogger(__name__) + + +def verify_connectivity(adress, port, timeout=10): + """ Returns true if an ip/port is reachable""" + connection = socket.socket() + count = 0 + while count < timeout: + try: + connection.connect((adress, port)) + LOGGER.debug('%s:%s is reachable!', adress, port) + return True + except socket.error: + count += 1 + time.sleep(1) + continue + LOGGER.error('%s:%s is not reachable.', adress, port) + return False + + +class CheckDeployment(object): + """ Check deployment class.""" + + def __init__(self, rc_file='/home/opnfv/functest/conf/openstack.creds'): + self.rc_file = rc_file + self.services = ('compute', 'network', 'image') + self.os_creds = None + + def check_rc(self): + """ Check if RC file exists and contains OS_AUTH_URL """ + if not os.path.isfile(self.rc_file): + raise IOError('RC file {} does not exist!'.format(self.rc_file)) + if 'OS_AUTH_URL' not in open(self.rc_file).read(): + raise SyntaxError('OS_AUTH_URL not defined in {}.'. + format(self.rc_file)) + + def check_auth_endpoint(self): + """ Verifies connectivity to the OS_AUTH_URL given in the RC file """ + rc_endpoint = self.os_creds.auth_url + if not (verify_connectivity(urlparse(rc_endpoint).hostname, + urlparse(rc_endpoint).port)): + raise Exception("OS_AUTH_URL {} is not reachable.". + format(rc_endpoint)) + LOGGER.info("Connectivity to OS_AUTH_URL %s ...OK", rc_endpoint) + + def check_public_endpoint(self): + """ Gets the public endpoint and verifies connectivity to it """ + public_endpoint = keystone_utils.get_endpoint(self.os_creds, + 'identity', + interface='public') + if not (verify_connectivity(urlparse(public_endpoint).hostname, + urlparse(public_endpoint).port)): + raise Exception("Public endpoint {} is not reachable.". + format(public_endpoint)) + LOGGER.info("Connectivity to the public endpoint %s ...OK", + public_endpoint) + + def check_service_endpoint(self, service): + """ Verifies connectivity to a given openstack service """ + endpoint = keystone_utils.get_endpoint(self.os_creds, + service, + interface='public') + if not (verify_connectivity(urlparse(endpoint).hostname, + urlparse(endpoint).port)): + raise Exception("{} endpoint {} is not reachable.". + format(service, endpoint)) + LOGGER.info("Connectivity to endpoint '%s' %s ...OK", + service, endpoint) + + def check_nova(self): + """ checks that a simple nova operation works """ + try: + client = nova_utils.nova_client(self.os_creds) + client.servers.list() + LOGGER.info("Nova service ...OK") + except Exception as error: + LOGGER.error("Nova service ...FAILED") + raise error + + def check_neutron(self): + """ checks that a simple neutron operation works """ + try: + client = neutron_utils.neutron_client(self.os_creds) + client.list_networks() + LOGGER.info("Neutron service ...OK") + except Exception as error: + LOGGER.error("Neutron service ...FAILED") + raise error + + def check_glance(self): + """ checks that a simple glance operation works """ + try: + client = glance_utils.glance_client(self.os_creds) + client.images.list() + LOGGER.info("Glance service ...OK") + except Exception as error: + LOGGER.error("Glance service ...FAILED") + raise error + + def check_all(self): + """ + Calls all the class functions and returns 0 if all of them succeed. + This is the method called by prepare_env or CLI + """ + self.check_rc() + try: + self.os_creds = openstack_tests.get_credentials( + os_env_file=self.rc_file, + proxy_settings_str=None, + ssh_proxy_cmd=None) + except: + raise Exception("Problem while getting credentials object.") + if self.os_creds is None: + raise Exception("Credentials is None.") + self.check_auth_endpoint() + self.check_public_endpoint() + for service in self.services: + self.check_service_endpoint(service) + self.check_nova() + self.check_neutron() + self.check_glance() + return 0 + + +def main(): + """Entry point""" + logging.config.fileConfig(pkg_resources.resource_filename( + 'functest', 'ci/logging.ini')) + deployment = CheckDeployment() + return deployment.check_all() diff --git a/functest/ci/check_os.sh b/functest/ci/check_os.sh deleted file mode 100644 index 7b66f3da..00000000 --- a/functest/ci/check_os.sh +++ /dev/null @@ -1,123 +0,0 @@ -#!/bin/bash -# -# Simple script to check the basic OpenStack clients -# -# Author: -# jose.lausuch@ericsson.com -# - -if [[ ${OS_INSECURE,,} == "true" ]]; then - options='--insecure' -else - options='' -fi - -declare -A service_cmd_array -service_cmd_array['nova']="openstack $options server list" -service_cmd_array['neutron']="openstack $options network list" -service_cmd_array['keystone']="openstack $options endpoint list" -service_cmd_array['cinder']="openstack $options volume list" -service_cmd_array['glance']="openstack $options image list" - -MANDATORY_SERVICES='nova neutron keystone glance' -OPTIONAL_SERVICES='cinder' - -verify_connectivity() { - for i in $(seq 0 9); do - if echo "test" | nc -v -w 10 $1 $2 &>/dev/null; then - return 0 - fi - sleep 1 - done - return 1 -} - -verify_SSL_connectivity() { - openssl s_client -connect $1:$2 &>/dev/null - return $? -} - -check_service() { - local service cmd - service=$1 - cmd=${service_cmd_array[$service]} - if [ -z "$2" ]; then - required='false' - else - required=$2 - fi - echo ">>Checking ${service} service..." - if ! openstack $options service list | grep -i ${service} > /dev/null; then - if [ "$required" == 'false' ]; then - echo "WARN: Optional Service ${service} is not enabled!" - return - else - echo "ERROR: Required Service ${service} is not enabled!" - exit 1 - fi - fi - $cmd &>/dev/null - result=$? - if [ $result -ne 0 ]; then - echo "ERROR: Failed execution $cmd. The $service does not seem to be working." - exit 1 - else - echo " ...OK" - fi -} - -if [ -z $OS_AUTH_URL ];then - echo "ERROR: OS_AUTH_URL environment variable missing... Have you sourced the OpenStack credentials?" - exit 1 -fi - - -echo "Checking OpenStack endpoints:" -publicURL=$(openstack $options catalog show identity |awk '/public/ {print $4}') -publicIP=$(echo $publicURL|sed 's/^.*http.*\:\/\///'|sed 's/.[^:]*$//') -publicPort=$(echo $publicURL|grep -Po '(?<=:)\d+') -https_enabled=$(echo $publicURL | grep 'https') -if [[ -n $https_enabled ]]; then - echo ">>Verifying SSL connectivity to the public endpoint $publicIP:$publicPort..." - verify_SSL_connectivity $publicIP $publicPort -else - echo ">>Verifying connectivity to the public endpoint $publicIP:$publicPort..." - verify_connectivity $publicIP $publicPort -fi -RETVAL=$? -if [ $RETVAL -ne 0 ]; then - echo "ERROR: Cannot talk to the public endpoint $publicIP:$publicPort ." - echo "OS_AUTH_URL=$OS_AUTH_URL" - exit 1 -fi -echo " ...OK" - - -echo "Checking Required OpenStack services:" -for service in $MANDATORY_SERVICES; do - check_service $service "true" -done -echo "Required OpenStack services are OK." - -echo "Checking Optional OpenStack services:" -for service in $OPTIONAL_SERVICES; do - check_service $service -done - -echo "Checking External network..." -networks=($(neutron $options net-list -F id | tail -n +4 | head -n -1 | awk '{print $2}')) -is_external=False -for net in "${networks[@]}" -do - is_external=$(neutron $options net-show $net|grep "router:external"|awk '{print $4}') - if [ $is_external == "True" ]; then - echo "External network found: $net" - break - fi -done -if [ $is_external == "False" ]; then - echo "ERROR: There are no external networks in the deployment." - exit 1 -fi - -exit 0 diff --git a/functest/ci/prepare_env.py b/functest/ci/prepare_env.py index da3e6245..c40e3266 100644 --- a/functest/ci/prepare_env.py +++ b/functest/ci/prepare_env.py @@ -19,6 +19,7 @@ import fileinput import yaml +from functest.ci import check_deployment import functest.utils.functest_utils as ft_utils import functest.utils.openstack_utils as os_utils from functest.utils.constants import CONST @@ -177,26 +178,6 @@ def create_directories(): def source_rc_file(): print_separator() - if CONST.__getattribute__('openstack_creds') is None: - logger.warning("The environment variable 'creds' must be set and" - "pointing to the local RC file. Using default: " - "/home/opnfv/functest/conf/openstack.creds ...") - os.path.join( - CONST.__getattribute__('dir_functest_conf'), 'openstack.creds') - - if not os.path.isfile(CONST.__getattribute__('openstack_creds')): - raise Exception( - "OpenStack credentials file not provided. " - "The OpenStack credentials must be in {}" - .format(CONST.__getattribute__('openstack_creds'))) - else: - logger.info("RC file provided in %s." - % CONST.__getattribute__('openstack_creds')) - if os.path.getsize(CONST.__getattribute__('openstack_creds')) == 0: - raise Exception( - "The OpenStack RC file {} is empty." - .format(CONST.__getattribute__('openstack_creds'))) - logger.info("Sourcing the OpenStack RC file...") os_utils.source_credentials(CONST.__getattribute__('openstack_creds')) for key, value in os.environ.iteritems(): @@ -250,18 +231,9 @@ def update_db_url(): def verify_deployment(): print_separator() - logger.info("Verifying OpenStack services...") - cmd = "check_os.sh" - - logger.debug("Executing command: %s" % cmd) - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) - - while p.poll() is None: - line = p.stdout.readline().rstrip() - if "ERROR" in line: - logger.error(line) - raise Exception("Problem while running '{}'.".format(cmd)) - logger.info(line) + logger.info("Verifying OpenStack deployment...") + deployment = check_deployment.CheckDeployment() + deployment.check_all() def install_rally(): @@ -364,11 +336,11 @@ def prepare_env(**kwargs): return -1 elif kwargs['action'] == "start": logger.info("######### Preparing Functest environment #########\n") + verify_deployment() check_env_variables() create_directories() source_rc_file() update_config_file() - verify_deployment() install_rally() install_tempest() create_flavor() |