diff options
Diffstat (limited to 'functest/opnfv_tests')
27 files changed, 595 insertions, 627 deletions
diff --git a/functest/opnfv_tests/features/barometer.py b/functest/opnfv_tests/features/barometer.py index 8a409406..cbfe7d9a 100644 --- a/functest/opnfv_tests/features/barometer.py +++ b/functest/opnfv_tests/features/barometer.py @@ -6,6 +6,8 @@ # # http://www.apache.org/licenses/LICENSE-2.0 +import logging + from baro_tests import collectd import functest.core.feature as base @@ -16,5 +18,7 @@ class BarometerCollectd(base.Feature): Class for executing barometercollectd testcase. ''' + __logger = logging.getLogger(__name__) + def execute(self): - return collectd.main(self.logger) + return collectd.main(self.__logger) diff --git a/functest/opnfv_tests/openstack/examples/create_instance_and_ip.py b/functest/opnfv_tests/openstack/examples/create_instance_and_ip.py deleted file mode 100755 index b4400864..00000000 --- a/functest/opnfv_tests/openstack/examples/create_instance_and_ip.py +++ /dev/null @@ -1,128 +0,0 @@ -#!/usr/bin/python -# -# Copyright (c) 2015 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 -# -# This script boots an instance and assigns a floating ip -# - -import argparse -import os -import sys - -from functest.utils.constants import CONST -import functest.utils.functest_logger as ft_logger -import functest.utils.openstack_utils as os_utils - -parser = argparse.ArgumentParser() - -parser.add_argument("-r", "--report", - help="Create json result file", - action="store_true") - -args = parser.parse_args() - -""" logging configuration """ -logger = ft_logger.Logger("create_instance_and_ip").getLogger() - -HOME = CONST.dir_home + "/" - -VM_BOOT_TIMEOUT = 180 - -EXAMPLE_INSTANCE_NAME = CONST.example_vm_name -EXAMPLE_FLAVOR = CONST.example_flavor -EXAMPLE_IMAGE_NAME = CONST.example_image_name -IMAGE_FILENAME = CONST.openstack_image_file_name -IMAGE_FORMAT = CONST.openstack_image_disk_format -IMAGE_PATH = os.path.join(CONST.dir_functest_data, IMAGE_FILENAME) - -# NEUTRON Private Network parameters - -EXAMPLE_PRIVATE_NET_NAME = CONST.example_private_net_name -EXAMPLE_PRIVATE_SUBNET_NAME = CONST.example_private_subnet_name -EXAMPLE_PRIVATE_SUBNET_CIDR = CONST.example_private_subnet_cidr -EXAMPLE_ROUTER_NAME = CONST.example_router_name - -EXAMPLE_SECGROUP_NAME = CONST.example_sg_name -EXAMPLE_SECGROUP_DESCR = CONST.example_sg_desc - - -def main(): - - nova_client = os_utils.get_nova_client() - neutron_client = os_utils.get_neutron_client() - glance_client = os_utils.get_glance_client() - - image_id = os_utils.create_glance_image(glance_client, - EXAMPLE_IMAGE_NAME, - IMAGE_PATH, - disk=IMAGE_FORMAT, - container="bare", - public=True) - - network_dic = os_utils.create_network_full( - neutron_client, - EXAMPLE_PRIVATE_NET_NAME, - EXAMPLE_PRIVATE_SUBNET_NAME, - EXAMPLE_ROUTER_NAME, - EXAMPLE_PRIVATE_SUBNET_CIDR) - if not network_dic: - logger.error( - "There has been a problem when creating the neutron network") - sys.exit(-1) - - network_id = network_dic["net_id"] - - sg_id = os_utils.create_security_group_full(neutron_client, - EXAMPLE_SECGROUP_NAME, - EXAMPLE_SECGROUP_DESCR) - - # boot INTANCE - logger.info("Creating instance '%s'..." % EXAMPLE_INSTANCE_NAME) - logger.debug( - "Configuration:\n name=%s \n flavor=%s \n image=%s \n " - "network=%s \n" - % (EXAMPLE_INSTANCE_NAME, EXAMPLE_FLAVOR, image_id, network_id)) - instance = os_utils.create_instance_and_wait_for_active( - EXAMPLE_FLAVOR, - image_id, - network_id, - EXAMPLE_INSTANCE_NAME) - - if instance is None: - logger.error("Error while booting instance.") - sys.exit(-1) - # Retrieve IP of INSTANCE - instance_ip = instance.networks.get(EXAMPLE_PRIVATE_NET_NAME)[0] - logger.debug("Instance '%s' got private ip '%s'." % - (EXAMPLE_INSTANCE_NAME, instance_ip)) - - logger.info("Adding '%s' to security group '%s'..." - % (EXAMPLE_INSTANCE_NAME, EXAMPLE_SECGROUP_NAME)) - os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id) - - logger.info("Creating floating IP for VM '%s'..." % EXAMPLE_INSTANCE_NAME) - floatip_dic = os_utils.create_floating_ip(neutron_client) - floatip = floatip_dic['fip_addr'] - # floatip_id = floatip_dic['fip_id'] - - if floatip is None: - logger.error("Cannot create floating IP.") - sys.exit(-1) - logger.info("Floating IP created: '%s'" % floatip) - - logger.info("Associating floating ip: '%s' to VM '%s' " - % (floatip, EXAMPLE_INSTANCE_NAME)) - if not os_utils.add_floating_ip(nova_client, instance.id, floatip): - logger.error("Cannot associate floating IP to VM.") - sys.exit(-1) - - sys.exit(0) - - -if __name__ == '__main__': - main() diff --git a/functest/opnfv_tests/openstack/rally/rally.py b/functest/opnfv_tests/openstack/rally/rally.py index e07e2a8d..40f8386c 100644 --- a/functest/opnfv_tests/openstack/rally/rally.py +++ b/functest/opnfv_tests/openstack/rally/rally.py @@ -8,7 +8,10 @@ # http://www.apache.org/licenses/LICENSE-2.0 # +from __future__ import division + import json +import logging import os import re import subprocess @@ -19,11 +22,9 @@ import yaml from functest.core import testcase from functest.utils.constants import CONST -import functest.utils.functest_logger as ft_logger -import functest.utils.functest_utils as ft_utils import functest.utils.openstack_utils as os_utils -logger = ft_logger.Logger('Rally').getLogger() +logger = logging.getLogger(__name__) class RallyBase(testcase.TestCase): @@ -31,7 +32,7 @@ class RallyBase(testcase.TestCase): 'neutron', 'nova', 'quotas', 'requests', 'vm', 'all'] GLANCE_IMAGE_NAME = CONST.openstack_image_name GLANCE_IMAGE_FILENAME = CONST.openstack_image_file_name - GLANCE_IMAGE_PATH = os.path.join(CONST.dir_functest_data, + GLANCE_IMAGE_PATH = os.path.join(CONST.dir_functest_images, GLANCE_IMAGE_FILENAME) GLANCE_IMAGE_FORMAT = CONST.openstack_image_disk_format FLAVOR_NAME = "m1.tiny" @@ -480,11 +481,12 @@ class RallyBase(testcase.TestCase): total_duration_str2 = "{0:<10}".format(total_duration_str) total_nb_tests_str = "{0:<13}".format(total_nb_tests) - if len(self.summary): - success_rate = total_success / len(self.summary) - else: - success_rate = 100 - success_rate = "{:0.2f}".format(success_rate) + try: + self.result = total_success / len(self.summary) + except ZeroDivisionError: + self.result = 100 + + success_rate = "{:0.2f}".format(self.result) success_rate_str = "{0:<10}".format(str(success_rate) + '%') report += ("+===================+============" "+===============+===========+") @@ -500,12 +502,10 @@ class RallyBase(testcase.TestCase): 'nb tests': total_nb_tests, 'nb success': success_rate}}) - self.result = ft_utils.check_success_rate( - self.case_name, success_rate) self.details = payload - logger.info("Rally '%s' success_rate is %s%%, is marked as %s" - % (self.case_name, success_rate, self.result)) + logger.info("Rally '%s' success_rate is %s%%" + % (self.case_name, success_rate)) def _clean_up(self): if self.volume_type: diff --git a/functest/opnfv_tests/openstack/refstack_client/defcore.txt b/functest/opnfv_tests/openstack/refstack_client/defcore.txt index be8fd899..0a1787ef 100644 --- a/functest/opnfv_tests/openstack/refstack_client/defcore.txt +++ b/functest/opnfv_tests/openstack/refstack_client/defcore.txt @@ -1,4 +1,11 @@ -# Set of DefCore tempest test cases not flagged and required. It only contains OpenStack core (no object storage) +# Set of DefCore tempest test cases not flagged and required. +# According to https://github.com/openstack/interop/blob/master/2016.08/procedure.rst, +# some tests are still flagged due to outstanding bugs in the Tempest library, +# particularly tests that require SSH. Refstack developers +# are working on correcting these bugs upstream. Please note that although some tests +# are flagged because of bugs, there is still an expectation that the capabilities +# covered by the tests are available. +# It only contains Openstack core compute (no object storage) # The approved guidelines (2016.08) are valid for Kilo, Liberty, Mitaka and Newton releases of OpenStack # The list can be generated using the Rest API from RefStack project: # https://refstack.openstack.org/api/v1/guidelines/2016.08/tests?target=compute&type=required&alias=true&flag=false diff --git a/functest/opnfv_tests/openstack/refstack_client/refstack_client.py b/functest/opnfv_tests/openstack/refstack_client/refstack_client.py index 2f2fc00f..2a2718dd 100755 --- a/functest/opnfv_tests/openstack/refstack_client/refstack_client.py +++ b/functest/opnfv_tests/openstack/refstack_client/refstack_client.py @@ -5,7 +5,12 @@ # 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 + +from __future__ import division + + import argparse +import logging import os import re import sys @@ -15,12 +20,11 @@ import time from functest.core import testcase from functest.opnfv_tests.openstack.tempest import conf_utils from functest.utils.constants import CONST -import functest.utils.functest_logger as ft_logger import functest.utils.functest_utils as ft_utils from tempest_conf import TempestConf """ logging configuration """ -logger = ft_logger.Logger("refstack_defcore").getLogger() +logger = logging.getLogger(__name__) class RefstackClient(testcase.TestCase): @@ -123,7 +127,11 @@ class RefstackClient(testcase.TestCase): skipped_testcases += match + ", " num_executed = int(num_tests) - int(num_skipped) - success_rate = 100 * int(num_success) / int(num_executed) + + try: + self.result = 100 * int(num_success) / int(num_executed) + except ZeroDivisionError: + logger.error("No test has been executed") self.details = {"tests": int(num_tests), "failures": int(num_failures), @@ -131,12 +139,10 @@ class RefstackClient(testcase.TestCase): "errors": failed_testcases, "skipped": skipped_testcases} except Exception: - success_rate = 0 + self.result = 0 - self.result = ft_utils.check_success_rate( - self.case_name, success_rate) - logger.info("Testcase %s success_rate is %s%%, is marked as %s" - % (self.case_name, success_rate, self.result)) + logger.info("Testcase %s success_rate is %s%%" + % (self.case_name, self.result)) def run(self): '''used for functest command line, @@ -220,6 +226,7 @@ class RefstackClientParser(object): if __name__ == '__main__': + logging.basicConfig() refstackclient = RefstackClient() parser = RefstackClientParser() args = parser.parse_args(sys.argv[1:]) diff --git a/functest/opnfv_tests/openstack/refstack_client/tempest_conf.py b/functest/opnfv_tests/openstack/refstack_client/tempest_conf.py index d01f0872..5c04253c 100755 --- a/functest/opnfv_tests/openstack/refstack_client/tempest_conf.py +++ b/functest/opnfv_tests/openstack/refstack_client/tempest_conf.py @@ -5,15 +5,15 @@ # 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 +import logging import os from functest.opnfv_tests.openstack.tempest import conf_utils from functest.utils import openstack_utils from functest.utils.constants import CONST -import functest.utils.functest_logger as ft_logger """ logging configuration """ -logger = ft_logger.Logger("refstack_defcore").getLogger() +logger = logging.getLogger(__name__) class TempestConf(object): @@ -48,5 +48,6 @@ class TempestConf(object): if __name__ == '__main__': + logging.basicConfig() tempestconf = TempestConf() tempestconf.main() diff --git a/functest/opnfv_tests/openstack/snaps/health_check.py b/functest/opnfv_tests/openstack/snaps/health_check.py index c057eb2b..0daddcdd 100644 --- a/functest/opnfv_tests/openstack/snaps/health_check.py +++ b/functest/opnfv_tests/openstack/snaps/health_check.py @@ -23,14 +23,15 @@ class HealthCheck(SnapsTestRunner): """ def __init__(self, **kwargs): if "case_name" not in kwargs: - kwargs["case_name"] = "snaps_health_check" + kwargs["case_name"] = "snaps_images_cirros" super(HealthCheck, self).__init__(**kwargs) self.suite = unittest.TestSuite() image_custom_config = None - if hasattr(CONST, 'snaps_health_check'): - image_custom_config = CONST.__getattribute__('snaps_health_check') + + if hasattr(CONST, 'snaps_images_cirros'): + image_custom_config = CONST.__getattribute__('snaps_images_cirros') self.suite.addTest( OSIntegrationTestCase.parameterize( SimpleHealthCheck, os_creds=self.os_creds, diff --git a/functest/opnfv_tests/openstack/snaps/smoke.py b/functest/opnfv_tests/openstack/snaps/smoke.py index 2c6fc255..d9f95e90 100644 --- a/functest/opnfv_tests/openstack/snaps/smoke.py +++ b/functest/opnfv_tests/openstack/snaps/smoke.py @@ -28,11 +28,9 @@ class SnapsSmoke(SnapsTestRunner): self.suite = unittest.TestSuite() - # The snaps smoke test uses the same config as the - # snaps_health_check suite, so reuse it here - image_custom_config = None - if hasattr(CONST, 'snaps_health_check'): - image_custom_config = CONST.__getattribute__('snaps_health_check') + image_config = None + if hasattr(CONST, 'snaps_images_cirros'): + image_config = CONST.__getattribute__('snaps_images_cirros') # Tests requiring floating IPs leverage files contained within the # SNAPS repository and are found relative to that path @@ -47,5 +45,5 @@ class SnapsSmoke(SnapsTestRunner): ext_net_name=self.ext_net_name, use_keystone=self.use_keystone, flavor_metadata=self.flavor_metadata, - image_metadata=image_custom_config, + image_metadata=image_config, use_floating_ips=self.use_fip) diff --git a/functest/opnfv_tests/openstack/snaps/snaps_test_runner.py b/functest/opnfv_tests/openstack/snaps/snaps_test_runner.py index 8a68cad9..94b97551 100644 --- a/functest/opnfv_tests/openstack/snaps/snaps_test_runner.py +++ b/functest/opnfv_tests/openstack/snaps/snaps_test_runner.py @@ -5,6 +5,8 @@ # # http://www.apache.org/licenses/LICENSE-2.0 +import logging + from functest.core.pytest_suite_runner import PyTestSuiteRunner from functest.opnfv_tests.openstack.snaps import snaps_utils from functest.utils import functest_utils @@ -16,12 +18,11 @@ from snaps.openstack.tests import openstack_tests class SnapsTestRunner(PyTestSuiteRunner): """ - This test executes the SNAPS Python Test case SimpleHealthCheck which - creates a VM with a single port with an IPv4 address that is assigned by - DHCP. This test then validates the expected IP with the actual + This test executes the SNAPS Python Tests """ def __init__(self, **kwargs): super(SnapsTestRunner, self).__init__(**kwargs) + self.logger = logging.getLogger(__name__) self.os_creds = openstack_tests.get_credentials( os_env_file=CONST.__getattribute__('openstack_creds'), diff --git a/functest/opnfv_tests/openstack/tempest/conf_utils.py b/functest/opnfv_tests/openstack/tempest/conf_utils.py index f4a94f65..cd6a2a8c 100644 --- a/functest/opnfv_tests/openstack/tempest/conf_utils.py +++ b/functest/opnfv_tests/openstack/tempest/conf_utils.py @@ -8,13 +8,13 @@ # http://www.apache.org/licenses/LICENSE-2.0 # import ConfigParser +import logging import os import re import shutil import subprocess from functest.utils.constants import CONST -import functest.utils.functest_logger as ft_logger import functest.utils.functest_utils as ft_utils import functest.utils.openstack_utils as os_utils @@ -22,7 +22,7 @@ import functest.utils.openstack_utils as os_utils IMAGE_ID_ALT = None FLAVOR_ID_ALT = None REPO_PATH = CONST.dir_repo_functest -GLANCE_IMAGE_PATH = os.path.join(CONST.dir_functest_data, +GLANCE_IMAGE_PATH = os.path.join(CONST.dir_functest_images, CONST.openstack_image_file_name) TEMPEST_TEST_LIST_DIR = CONST.dir_tempest_cases TEMPEST_RESULTS_DIR = os.path.join(CONST.dir_results, @@ -42,7 +42,7 @@ CI_INSTALLER_TYPE = CONST.INSTALLER_TYPE CI_INSTALLER_IP = CONST.INSTALLER_IP """ logging configuration """ -logger = ft_logger.Logger("Tempest").getLogger() +logger = logging.getLogger(__name__) def create_tempest_resources(use_custom_images=False, @@ -279,12 +279,20 @@ def configure_tempest_update_params(tempest_conf_file, config.set('identity', 'tenant_name', CONST.tempest_identity_tenant_name) config.set('identity', 'username', CONST.tempest_identity_user_name) config.set('identity', 'password', CONST.tempest_identity_user_password) + config.set('identity', 'region', 'RegionOne') config.set( 'validation', 'ssh_timeout', CONST.tempest_validation_ssh_timeout) config.set('object-storage', 'operator_role', CONST.tempest_object_storage_operator_role) if CONST.OS_ENDPOINT_TYPE is not None: + sections = config.sections() + if os_utils.is_keystone_v3(): + config.set('identity', 'v3_endpoint_type', CONST.OS_ENDPOINT_TYPE) + if 'identity-feature-enabled' not in sections: + config.add_section('identity-feature-enabled') + config.set('identity-feature-enabled', 'api_v2', False) + config.set('identity-feature-enabled', 'api_v2_admin', False) services_list = ['compute', 'volume', 'image', @@ -292,7 +300,6 @@ def configure_tempest_update_params(tempest_conf_file, 'data-processing', 'object-storage', 'orchestration'] - sections = config.sections() for service in services_list: if service not in sections: config.add_section(service) diff --git a/functest/opnfv_tests/openstack/tempest/tempest.py b/functest/opnfv_tests/openstack/tempest/tempest.py index e6c6b44f..a41d07c7 100644 --- a/functest/opnfv_tests/openstack/tempest/tempest.py +++ b/functest/opnfv_tests/openstack/tempest/tempest.py @@ -8,6 +8,9 @@ # http://www.apache.org/licenses/LICENSE-2.0 # +from __future__ import division + +import logging import os import re import shutil @@ -19,11 +22,10 @@ import yaml from functest.core import testcase from functest.opnfv_tests.openstack.tempest import conf_utils from functest.utils.constants import CONST -import functest.utils.functest_logger as ft_logger import functest.utils.functest_utils as ft_utils """ logging configuration """ -logger = ft_logger.Logger("Tempest").getLogger() +logger = logging.getLogger(__name__) class TempestCommon(testcase.TestCase): @@ -181,7 +183,13 @@ class TempestCommon(testcase.TestCase): try: num_executed = int(num_tests) - int(num_skipped) - success_rate = 100 * int(num_success) / int(num_executed) + try: + self.result = 100 * int(num_success) / int(num_executed) + except ZeroDivisionError: + logger.error("No test has been executed") + self.result = 0 + return + with open(os.path.join(conf_utils.TEMPEST_RESULTS_DIR, "tempest.log"), 'r') as logfile: output = logfile.read() @@ -198,12 +206,10 @@ class TempestCommon(testcase.TestCase): "errors": error_logs, "skipped": skipped_testcase} except Exception: - success_rate = 0 + self.result = 0 - self.result = ft_utils.check_success_rate( - self.case_name, success_rate) - logger.info("Tempest %s success_rate is %s%%, is marked as %s" - % (self.case_name, success_rate, self.result)) + logger.info("Tempest %s success_rate is %s%%" + % (self.case_name, self.result)) def run(self): diff --git a/functest/opnfv_tests/openstack/vping/vping_base.py b/functest/opnfv_tests/openstack/vping/vping_base.py index 8bf263eb..8e71bf82 100644 --- a/functest/opnfv_tests/openstack/vping/vping_base.py +++ b/functest/opnfv_tests/openstack/vping/vping_base.py @@ -7,281 +7,194 @@ # # http://www.apache.org/licenses/LICENSE-2.0 +from datetime import datetime +import logging import os -import pprint import time -from datetime import datetime +import uuid -import functest.core.testcase as testcase -import functest.utils.openstack_utils as os_utils +from functest.core.testcase import TestCase +from functest.utils import functest_utils from functest.utils.constants import CONST +from snaps.openstack import create_flavor +from snaps.openstack.create_flavor import FlavorSettings, OpenStackFlavor +from snaps.openstack.create_network import NetworkSettings, SubnetSettings +from snaps.openstack.tests import openstack_tests +from snaps.openstack.utils import deploy_utils, nova_utils + + +class VPingBase(TestCase): + + """ + Base class for vPing tests that check connectivity between two VMs shared + internal network. + This class is responsible for creating the image, internal network. + """ -class VPingBase(testcase.TestCase): def __init__(self, **kwargs): super(VPingBase, self).__init__(**kwargs) - self.logger = None - self.functest_repo = CONST.dir_repo_functest - self.repo = CONST.dir_vping - self.vm1_name = CONST.vping_vm_name_1 - self.vm2_name = CONST.vping_vm_name_2 - self.vm_boot_timeout = 180 - self.vm_delete_timeout = 100 - self.ping_timeout = CONST.vping_ping_timeout - - self.image_name = CONST.vping_image_name - self.image_filename = CONST.openstack_image_file_name - self.image_format = CONST.openstack_image_disk_format - self.image_username = CONST.openstack_image_username - self.image_password = CONST.openstack_image_password - self.image_path = os.path.join(CONST.dir_functest_data, - self.image_filename) - - self.flavor_name = CONST.vping_vm_flavor + + self.logger = logging.getLogger(self.__class__.__name__) + + self.functest_repo = CONST.__getattribute__('dir_repo_functest') + self.guid = '' + if CONST.__getattribute__('vping_unique_names'): + self.guid = '-' + str(uuid.uuid4()) + + self.os_creds = openstack_tests.get_credentials( + os_env_file=CONST.__getattribute__('openstack_creds')) + + self.repo = CONST.__getattribute__('dir_vping') + + self.creators = list() + self.image_creator = None + self.network_creator = None + self.vm1_creator = None + self.vm2_creator = None + + self.self_cleanup = CONST.__getattribute__('vping_cleanup_objects') + + # Image constants + self.image_name =\ + CONST.__getattribute__('vping_image_name') + self.guid + + # VM constants + self.vm1_name = CONST.__getattribute__('vping_vm_name_1') + self.guid + self.vm2_name = CONST.__getattribute__('vping_vm_name_2') + self.guid + self.vm_boot_timeout = CONST.__getattribute__('vping_vm_boot_timeout') + self.vm_delete_timeout =\ + CONST.__getattribute__('vping_vm_delete_timeout') + self.vm_ssh_connect_timeout = CONST.vping_vm_ssh_connect_timeout + self.ping_timeout = CONST.__getattribute__('vping_ping_timeout') + self.flavor_name = 'vping-flavor' + self.guid # NEUTRON Private Network parameters - self.private_net_name = CONST.vping_private_net_name - self.private_subnet_name = CONST.vping_private_subnet_name - self.private_subnet_cidr = CONST.vping_private_subnet_cidr - self.router_name = CONST.vping_router_name - self.sg_name = CONST.vping_sg_name - self.sg_desc = CONST.vping_sg_desc - self.neutron_client = os_utils.get_neutron_client() - self.glance_client = os_utils.get_glance_client() - self.nova_client = os_utils.get_nova_client() - - def run(self, **kwargs): - if not self.check_repo_exist(): - return testcase.TestCase.EX_RUN_ERROR - - image_id = self.create_image() - if not image_id: - return testcase.TestCase.EX_RUN_ERROR - - flavor = self.get_flavor() - if not flavor: - return testcase.TestCase.EX_RUN_ERROR - - network_id = self.create_network_full() - if not network_id: - return testcase.TestCase.EX_RUN_ERROR - - sg_id = self.create_security_group() - if not sg_id: - return testcase.TestCase.EX_RUN_ERROR - - self.delete_exist_vms() + self.private_net_name =\ + CONST.__getattribute__('vping_private_net_name') + self.guid + self.private_subnet_name =\ + CONST.__getattribute__('vping_private_subnet_name') + self.guid + self.private_subnet_cidr =\ + CONST.__getattribute__('vping_private_subnet_cidr') - self.start_time = time.time() - self.logger.info("vPing Start Time:'%s'" % ( - datetime.fromtimestamp(self.start_time).strftime( - '%Y-%m-%d %H:%M:%S'))) + scenario = functest_utils.get_scenario() - vm1 = self.boot_vm(self.vm1_name, - image_id, - flavor, - network_id, - None, - sg_id) - if not vm1: - return testcase.TestCase.EX_RUN_ERROR - - test_ip = self.get_test_ip(vm1) - vm2 = self.boot_vm(self.vm2_name, - image_id, - flavor, - network_id, - test_ip, - sg_id) - if not vm2: - return testcase.TestCase.EX_RUN_ERROR - - EXIT_CODE = self.do_vping(vm2, test_ip) - if EXIT_CODE == testcase.TestCase.EX_RUN_ERROR: - return EXIT_CODE + self.flavor_metadata = create_flavor.MEM_PAGE_SIZE_ANY + if 'ovs' in scenario or 'fdio' in scenario: + self.flavor_metadata = create_flavor.MEM_PAGE_SIZE_LARGE - self.stop_time = time.time() - self.parse_result(EXIT_CODE, - self.start_time, - self.stop_time) - return testcase.TestCase.EX_OK + self.cirros_image_config = None - def boot_vm_preparation(self, config, vmname, test_ip): - pass + # Move this configuration option up for all tests to leverage + if hasattr(CONST, 'snaps_images_cirros'): + self.cirros_image_config = CONST.__getattribute__( + 'snaps_images_cirros') - def do_vping(self, vm, test_ip): - raise NotImplementedError('vping execution is not implemented') + def run(self): + """ + Begins the test execution which should originate from the subclass + """ - def check_repo_exist(self): if not os.path.exists(self.functest_repo): - self.logger.error("Functest repository not found '%s'" - % self.functest_repo) - return False - return True + raise Exception( + "Functest repository not found '%s'" % self.functest_repo) - def create_image(self): - _, image_id = os_utils.get_or_create_image(self.image_name, - self.image_path, - self.image_format) - if not image_id: - return None + self.logger.info('Begin virtual environment setup') - return image_id + self.start_time = time.time() + self.logger.info("vPing Start Time:'%s'" % ( + datetime.fromtimestamp(self.start_time).strftime( + '%Y-%m-%d %H:%M:%S'))) - def get_flavor(self): - try: - flavor = self.nova_client.flavors.find(name=self.flavor_name) - self.logger.info("Using existing Flavor '%s'..." - % self.flavor_name) - return flavor - except: - self.logger.error("Flavor '%s' not found." % self.flavor_name) - self.logger.info("Available flavors are: ") - self.pMsg(self.nova_client.flavor.list()) - return None - - def create_network_full(self): - network_dic = os_utils.create_network_full(self.neutron_client, - self.private_net_name, - self.private_subnet_name, - self.router_name, - self.private_subnet_cidr) - - if not network_dic: - self.logger.error( - "There has been a problem when creating the neutron network") - return None - network_id = network_dic["net_id"] - return network_id - - def create_security_group(self): - sg_id = os_utils.get_security_group_id(self.neutron_client, - self.sg_name) - if sg_id != '': - self.logger.info("Using existing security group '%s'..." - % self.sg_name) + self.__delete_exist_vms() + + image_base_name = self.image_name + '-' + str(self.guid) + os_image_settings = openstack_tests.cirros_image_settings( + image_base_name, image_metadata=self.cirros_image_config) + self.logger.info("Creating image with name: '%s'" % self.image_name) + + self.image_creator = deploy_utils.create_image( + self.os_creds, os_image_settings) + self.creators.append(self.image_creator) + + self.logger.info( + "Creating network with name: '%s'" % self.private_net_name) + self.network_creator = deploy_utils.create_network( + self.os_creds, + NetworkSettings(name=self.private_net_name, + subnet_settings=[SubnetSettings( + name=self.private_subnet_name, + cidr=self.private_subnet_cidr)])) + self.creators.append(self.network_creator) + + self.logger.info( + "Creating flavor with name: '%s'" % self.flavor_name) + flavor_creator = OpenStackFlavor( + self.os_creds, + FlavorSettings(name=self.flavor_name, ram=512, disk=1, vcpus=1, + metadata=self.flavor_metadata)) + flavor_creator.create() + self.creators.append(flavor_creator) + + def _execute(self): + """ + Method called by subclasses after environment has been setup + :return: the exit code + """ + self.logger.info('Begin test execution') + + test_ip = self.vm1_creator.get_port_ip( + self.vm1_creator.instance_settings.port_settings[0].name) + + if self.vm1_creator.vm_active( + block=True) and self.vm2_creator.vm_active(block=True): + result = self._do_vping(self.vm2_creator, test_ip) else: - self.logger.info("Creating security group '%s'..." - % self.sg_name) - SECGROUP = os_utils.create_security_group(self.neutron_client, - self.sg_name, - self.sg_desc) - if not SECGROUP: - self.logger.error("Failed to create the security group...") - return None - - sg_id = SECGROUP['id'] - - self.logger.debug("Security group '%s' with ID=%s created " - "successfully." % (SECGROUP['name'], sg_id)) - - self.logger.debug("Adding ICMP rules in security group '%s'..." - % self.sg_name) - if not os_utils.create_secgroup_rule(self.neutron_client, sg_id, - 'ingress', 'icmp'): - self.logger.error("Failed to create security group rule...") - return None - - self.logger.debug("Adding SSH rules in security group '%s'..." - % self.sg_name) - if not os_utils.create_secgroup_rule(self.neutron_client, sg_id, - 'ingress', 'tcp', - '22', '22'): - self.logger.error("Failed to create security group rule...") - return None - - if not os_utils.create_secgroup_rule( - self.neutron_client, sg_id, 'egress', 'tcp', '22', '22'): - self.logger.error("Failed to create security group rule...") - return None - return sg_id - - def delete_exist_vms(self): - servers = self.nova_client.servers.list() + raise Exception('VMs never became active') + + if result == TestCase.EX_RUN_ERROR: + return TestCase.EX_RUN_ERROR + + self.stop_time = time.time() + self.result = 100 + return TestCase.EX_OK + + def _cleanup(self): + """ + Cleanup all OpenStack objects. Should be called on completion + :return: + """ + if self.self_cleanup: + for creator in reversed(self.creators): + try: + creator.clean() + except Exception as e: + self.logger.error('Unexpected error cleaning - %s', e) + + def _do_vping(self, vm_creator, test_ip): + """ + Method to be implemented by subclasses + Begins the real test after the OpenStack environment has been setup + :param vm_creator: the SNAPS VM instance creator object + :param test_ip: the IP to which the VM needs to issue the ping + :return: T/F + """ + raise NotImplementedError('vping execution is not implemented') + + def __delete_exist_vms(self): + """ + Cleans any existing VMs using the same name. + """ + nova_client = nova_utils.nova_client(self.os_creds) + servers = nova_client.servers.list() for server in servers: if server.name == self.vm1_name or server.name == self.vm2_name: self.logger.info("Deleting instance %s..." % server.name) server.delete() - def boot_vm(self, vmname, image_id, flavor, network_id, test_ip, sg_id): - config = dict() - config['name'] = vmname - config['flavor'] = flavor - config['image'] = image_id - config['nics'] = [{"net-id": network_id}] - self.boot_vm_preparation(config, vmname, test_ip) - self.logger.info("Creating instance '%s'..." % vmname) - self.logger.debug("Configuration: %s" % config) - vm = self.nova_client.servers.create(**config) - - # wait until VM status is active - if not self.waitVmActive(self.nova_client, vm): - vm_status = os_utils.get_instance_status(self.nova_client, vm) - self.logger.error("Instance '%s' cannot be booted. Status is '%s'" - % (vmname, vm_status)) - return None - else: - self.logger.info("Instance '%s' is ACTIVE." % vmname) - - self.add_secgroup(vmname, vm.id, sg_id) - - return vm - - def waitVmActive(self, nova, vm): - # sleep and wait for VM status change - sleep_time = 3 - count = self.vm_boot_timeout / sleep_time - while True: - status = os_utils.get_instance_status(nova, vm) - self.logger.debug("Status: %s" % status) - if status == "ACTIVE": - return True - if status == "ERROR" or status == "error": - return False - if count == 0: - self.logger.debug("Booting a VM timed out...") - return False - count -= 1 - time.sleep(sleep_time) - return False - - def add_secgroup(self, vmname, vm_id, sg_id): - self.logger.info("Adding '%s' to security group '%s'..." % - (vmname, self.sg_name)) - os_utils.add_secgroup_to_instance(self.nova_client, vm_id, sg_id) - - def get_test_ip(self, vm): - test_ip = vm.networks.get(self.private_net_name)[0] - self.logger.debug("Instance '%s' got %s" % (vm.name, test_ip)) - return test_ip - - def parse_result(self, code, start_time, stop_time): - test_status = "FAIL" - if code == 0: - self.logger.info("vPing OK") - duration = round(stop_time - start_time, 1) - self.logger.info("vPing duration:'%s'" % duration) - test_status = "PASS" - elif code == -2: - duration = 0 - self.logger.info("Userdata is not supported in nova boot. " - "Aborting test...") - else: - duration = 0 - self.logger.error("vPing FAILED") - - self.details = {'timestart': start_time, - 'duration': duration, - 'status': test_status} - self.result = test_status - - @staticmethod - def pMsg(msg): - """pretty printing""" - pprint.PrettyPrinter(indent=4).pprint(msg) - class VPingMain(object): + def __init__(self, vping_cls): self.vping = vping_cls() @@ -291,6 +204,6 @@ class VPingMain(object): if result != VPingBase.EX_OK: return result if kwargs['report']: - return self.vping.push_to_db() - except Exception: + return self.vping.publish_report() + except: return VPingBase.EX_RUN_ERROR diff --git a/functest/opnfv_tests/openstack/vping/vping_ssh.py b/functest/opnfv_tests/openstack/vping/vping_ssh.py index c26c4e0c..0ad77402 100755 --- a/functest/opnfv_tests/openstack/vping/vping_ssh.py +++ b/functest/opnfv_tests/openstack/vping/vping_ssh.py @@ -7,124 +7,161 @@ # # http://www.apache.org/licenses/LICENSE-2.0 +import argparse import os -import re +from scp import SCPClient import sys import time -import argparse -import paramiko -from scp import SCPClient - -import functest.utils.functest_logger as ft_logger -import functest.utils.openstack_utils as os_utils +from snaps.openstack.create_instance import FloatingIpSettings, \ + VmInstanceSettings +from snaps.openstack.create_keypairs import KeypairSettings +from snaps.openstack.create_network import PortSettings +from snaps.openstack.create_router import RouterSettings +from snaps.openstack.create_security_group import Direction, Protocol, \ + SecurityGroupSettings, SecurityGroupRuleSettings +from snaps.openstack.utils import deploy_utils + +from functest.core.testcase import TestCase +from functest.opnfv_tests.openstack.snaps import snaps_utils +from functest.utils.constants import CONST import vping_base -import functest.core.testcase as testcase class VPingSSH(vping_base.VPingBase): + """ + Class to execute the vPing test using a Floating IP to connect to one VM + to issue the ping command to the second + """ def __init__(self, **kwargs): if "case_name" not in kwargs: kwargs["case_name"] = "vping_ssh" super(VPingSSH, self).__init__(**kwargs) - self.logger = ft_logger.Logger(self.case_name).getLogger() - - def do_vping(self, vm, test_ip): - floatip = self.add_float_ip(vm) - if not floatip: - return testcase.TestCase.EX_RUN_ERROR - ssh = self.establish_ssh(vm, floatip) - if not ssh: - return testcase.TestCase.EX_RUN_ERROR - if not self.transfer_ping_script(ssh, floatip): - return testcase.TestCase.EX_RUN_ERROR - return self.do_vping_ssh(ssh, test_ip) - - def add_float_ip(self, vm): - self.logger.info("Creating floating IP for VM '%s'..." % self.vm2_name) - floatip_dic = os_utils.create_floating_ip(self.neutron_client) - floatip = floatip_dic['fip_addr'] - - if floatip is None: - self.logger.error("Cannot create floating IP.") - return None - self.logger.info("Floating IP created: '%s'" % floatip) - - self.logger.info("Associating floating ip: '%s' to VM '%s' " - % (floatip, self.vm2_name)) - if not os_utils.add_floating_ip(self.nova_client, vm.id, floatip): - self.logger.error("Cannot associate floating IP to VM.") - return None - - return floatip - - def establish_ssh(self, vm, floatip): - self.logger.info("Trying to establish SSH connection to %s..." - % floatip) - ssh = paramiko.SSHClient() - ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - - timeout = 50 - nolease = False - got_ip = False - discover_count = 0 - cidr_first_octet = self.private_subnet_cidr.split('.')[0] - while timeout > 0: - try: - ssh.connect(floatip, username=self.image_username, - password=self.image_password, timeout=2) - self.logger.debug("SSH connection established to %s." - % floatip) - break - except: - self.logger.debug("Waiting for %s..." % floatip) - time.sleep(6) - timeout -= 1 - - console_log = vm.get_console_output() - - # print each "Sending discover" captured on the console log - if (len(re.findall("Sending discover", console_log)) > - discover_count and not got_ip): - discover_count += 1 - self.logger.debug("Console-log '%s': Sending discover..." - % self.vm2_name) - - # check if eth0 got an ip,the line looks like this: - # "inet addr:192.168.".... - # if the dhcp agent fails to assing ip, this line will not appear - if "inet addr:" + cidr_first_octet in console_log and not got_ip: - got_ip = True - self.logger.debug("The instance '%s' succeeded to get the IP " - "from the dhcp agent." % self.vm2_name) - - # if dhcp not work,it shows "No lease, failing".The test will fail - if ("No lease, failing" in console_log and - not nolease and - not got_ip): - nolease = True - self.logger.debug("Console-log '%s': No lease, failing..." - % self.vm2_name) - self.logger.info("The instance failed to get an IP from DHCP " - "agent. The test will probably timeout...") - - if timeout == 0: # 300 sec timeout (5 min) - self.logger.error("Cannot establish connection to IP '%s'. " - "Aborting" % floatip) - return None - return ssh - - def transfer_ping_script(self, ssh, floatip): - self.logger.info("Trying to transfer ping.sh to %s..." % floatip) + + self.kp_name = CONST.__getattribute__('vping_keypair_name') + self.guid + self.kp_priv_file = CONST.__getattribute__('vping_keypair_priv_file') + self.kp_pub_file = CONST.__getattribute__('vping_keypair_pub_file') + self.router_name =\ + CONST.__getattribute__('vping_router_name') + self.guid + self.sg_name = CONST.__getattribute__('vping_sg_name') + self.guid + self.sg_desc = CONST.__getattribute__('vping_sg_desc') + + self.ext_net_name = snaps_utils.get_ext_net_name(self.os_creds) + + def run(self): + """ + Sets up the OpenStack keypair, router, security group, and VM instance + objects then validates the ping. + :return: the exit code from the super.execute() method + """ + try: + super(VPingSSH, self).run() + + self.logger.info("Creating keypair with name: '%s'" % self.kp_name) + kp_creator = deploy_utils.create_keypair( + self.os_creds, + KeypairSettings(name=self.kp_name, + private_filepath=self.kp_priv_file, + public_filepath=self.kp_pub_file)) + self.creators.append(kp_creator) + + # Creating router to external network + self.logger.info("Creating router with name: '%s'" + % self.router_name) + net_set = self.network_creator.network_settings + sub_set = [net_set.subnet_settings[0].name] + router_creator = deploy_utils.create_router( + self.os_creds, + RouterSettings( + name=self.router_name, + external_gateway=self.ext_net_name, + internal_subnets=sub_set)) + self.creators.append(router_creator) + + # Creating Instance 1 + port1_settings = PortSettings( + name=self.vm1_name + '-vPingPort', + network_name=self.network_creator.network_settings.name) + instance1_settings = VmInstanceSettings( + name=self.vm1_name, flavor=self.flavor_name, + vm_boot_timeout=self.vm_boot_timeout, + vm_delete_timeout=self.vm_delete_timeout, + ssh_connect_timeout=self.vm_ssh_connect_timeout, + port_settings=[port1_settings]) + + self.logger.info( + "Creating VM 1 instance with name: '%s'" + % instance1_settings.name) + self.vm1_creator = deploy_utils.create_vm_instance( + self.os_creds, + instance1_settings, + self.image_creator.image_settings, + keypair_creator=kp_creator) + self.creators.append(self.vm1_creator) + + # Creating Instance 2 + sg_creator = self.__create_security_group() + self.creators.append(sg_creator) + + port2_settings = PortSettings( + name=self.vm2_name + '-vPingPort', + network_name=self.network_creator.network_settings.name) + instance2_settings = VmInstanceSettings( + name=self.vm2_name, flavor=self.flavor_name, + vm_boot_timeout=self.vm_boot_timeout, + vm_delete_timeout=self.vm_delete_timeout, + ssh_connect_timeout=self.vm_ssh_connect_timeout, + port_settings=[port2_settings], + security_group_names=[sg_creator.sec_grp_settings.name], + floating_ip_settings=[FloatingIpSettings( + name=self.vm2_name + '-FIPName', + port_name=port2_settings.name, + router_name=router_creator.router_settings.name)]) + + self.logger.info( + "Creating VM 2 instance with name: '%s'" + % instance2_settings.name) + self.vm2_creator = deploy_utils.create_vm_instance( + self.os_creds, + instance2_settings, + self.image_creator.image_settings, + keypair_creator=kp_creator) + self.creators.append(self.vm2_creator) + + return self._execute() + except Exception as e: + self.logger.error('Unexpected error running test - ' + e.message) + return TestCase.EX_RUN_ERROR + finally: + self._cleanup() + + def _do_vping(self, vm_creator, test_ip): + """ + Override from super + """ + if vm_creator.vm_ssh_active(block=True): + ssh = vm_creator.ssh_client() + if not self.__transfer_ping_script(ssh): + return TestCase.EX_RUN_ERROR + return self.__do_vping_ssh(ssh, test_ip) + else: + return -1 + + def __transfer_ping_script(self, ssh): + """ + Uses SCP to copy the ping script via the SSH client + :param ssh: the SSH client + :return: + """ + self.logger.info("Trying to transfer ping.sh") scp = SCPClient(ssh.get_transport()) local_path = self.functest_repo + "/" + self.repo ping_script = os.path.join(local_path, "ping.sh") try: scp.put(ping_script, "~/") except: - self.logger.error("Cannot SCP the file '%s' to VM '%s'" - % (ping_script, floatip)) + self.logger.error("Cannot SCP the file '%s'" % ping_script) return False cmd = 'chmod 755 ~/ping.sh' @@ -134,8 +171,14 @@ class VPingSSH(vping_base.VPingBase): return True - def do_vping_ssh(self, ssh, test_ip): - EXIT_CODE = -1 + def __do_vping_ssh(self, ssh, test_ip): + """ + Pings the test_ip via the SSH client + :param ssh: the SSH client used to issue the ping command + :param test_ip: the IP for the ping command to use + :return: exit_code (int) + """ + exit_code = TestCase.EX_TESTCASE_FAILED self.logger.info("Waiting for ping...") sec = 0 @@ -150,7 +193,7 @@ class VPingSSH(vping_base.VPingBase): for line in output: if "vPing OK" in line: self.logger.info("vPing detected!") - EXIT_CODE = 0 + exit_code = TestCase.EX_OK flag = True break @@ -162,7 +205,35 @@ class VPingSSH(vping_base.VPingBase): break self.logger.debug("Pinging %s. Waiting for response..." % test_ip) sec += 1 - return EXIT_CODE + return exit_code + + def __create_security_group(self): + """ + Configures and deploys an OpenStack security group object + :return: the creator object + """ + sg_rules = list() + sg_rules.append( + SecurityGroupRuleSettings(sec_grp_name=self.sg_name, + direction=Direction.ingress, + protocol=Protocol.icmp)) + sg_rules.append( + SecurityGroupRuleSettings(sec_grp_name=self.sg_name, + direction=Direction.ingress, + protocol=Protocol.tcp, port_range_min=22, + port_range_max=22)) + sg_rules.append( + SecurityGroupRuleSettings(sec_grp_name=self.sg_name, + direction=Direction.egress, + protocol=Protocol.tcp, port_range_min=22, + port_range_max=22)) + + self.logger.info("Security group with name: '%s'" % self.sg_name) + return deploy_utils.create_security_group(self.os_creds, + SecurityGroupSettings( + name=self.sg_name, + description=self.sg_desc, + rule_settings=sg_rules)) if __name__ == '__main__': diff --git a/functest/opnfv_tests/openstack/vping/vping_userdata.py b/functest/opnfv_tests/openstack/vping/vping_userdata.py index 1b00ca23..8ea9be84 100755 --- a/functest/opnfv_tests/openstack/vping/vping_userdata.py +++ b/functest/opnfv_tests/openstack/vping/vping_userdata.py @@ -7,70 +7,141 @@ # # http://www.apache.org/licenses/LICENSE-2.0 +import argparse import sys import time -import argparse +from functest.core.testcase import TestCase + +from snaps.openstack.utils import deploy_utils +from snaps.openstack.create_instance import VmInstanceSettings +from snaps.openstack.create_network import PortSettings -import functest.utils.functest_logger as ft_logger import vping_base class VPingUserdata(vping_base.VPingBase): + """ + Class to execute the vPing test using userdata and the VM's console + """ def __init__(self, **kwargs): if "case_name" not in kwargs: kwargs["case_name"] = "vping_userdata" super(VPingUserdata, self).__init__(**kwargs) - self.logger = ft_logger.Logger(self.case_name).getLogger() - - def boot_vm_preparation(self, config, vmname, test_ip): - config['config_drive'] = True - if vmname == self.vm2_name: - u = ("#!/bin/sh\n\n" - "while true; do\n" - " ping -c 1 %s 2>&1 >/dev/null\n" - " RES=$?\n" - " if [ \"Z$RES\" = \"Z0\" ] ; then\n" - " echo 'vPing OK'\n" - " break\n" - " else\n" - " echo 'vPing KO'\n" - " fi\n" - " sleep 1\n" - "done\n" % test_ip) - config['userdata'] = u - - def do_vping(self, vm, test_ip): + + def run(self): + """ + Sets up the OpenStack VM instance objects then executes the ping and + validates. + :return: the exit code from the super.execute() method + """ + try: + super(VPingUserdata, self).run() + + # Creating Instance 1 + port1_settings = PortSettings( + name=self.vm1_name + '-vPingPort', + network_name=self.network_creator.network_settings.name) + instance1_settings = VmInstanceSettings( + name=self.vm1_name, + flavor=self.flavor_name, + vm_boot_timeout=self.vm_boot_timeout, + port_settings=[port1_settings]) + + self.logger.info( + "Creating VM 1 instance with name: '%s'" + % instance1_settings.name) + self.vm1_creator = deploy_utils.create_vm_instance( + self.os_creds, instance1_settings, + self.image_creator.image_settings) + self.creators.append(self.vm1_creator) + + userdata = _get_userdata( + self.vm1_creator.get_port_ip(port1_settings.name)) + if userdata: + # Creating Instance 2 + port2_settings = PortSettings( + name=self.vm2_name + '-vPingPort', + network_name=self.network_creator.network_settings.name) + instance2_settings = VmInstanceSettings( + name=self.vm2_name, + flavor=self.flavor_name, + vm_boot_timeout=self.vm_boot_timeout, + port_settings=[port2_settings], + userdata=userdata) + + self.logger.info( + "Creating VM 2 instance with name: '%s'" + % instance2_settings.name) + self.vm2_creator = deploy_utils.create_vm_instance( + self.os_creds, instance2_settings, + self.image_creator.image_settings) + self.creators.append(self.vm2_creator) + else: + raise Exception('Userdata is None') + + return self._execute() + + finally: + self._cleanup() + + def _do_vping(self, vm_creator, test_ip): + """ + Override from super + """ self.logger.info("Waiting for ping...") - EXIT_CODE = -1 + exit_code = -1 sec = 0 tries = 0 while True: time.sleep(1) - p_console = vm.get_console_output() + p_console = vm_creator.get_vm_inst().get_console_output() if "vPing OK" in p_console: self.logger.info("vPing detected!") - EXIT_CODE = 0 + exit_code = TestCase.EX_OK break elif "failed to read iid from metadata" in p_console or tries > 5: - EXIT_CODE = -2 + exit_code = TestCase.EX_TESTCASE_FAILED break elif sec == self.ping_timeout: self.logger.info("Timeout reached.") break elif sec % 10 == 0: if "request failed" in p_console: - self.logger.debug("It seems userdata is not supported " - "in nova boot. Waiting a bit...") + self.logger.debug( + "It seems userdata is not supported in nova boot. " + + "Waiting a bit...") tries += 1 else: - self.logger.debug("Pinging %s. Waiting for response..." - % test_ip) + self.logger.debug( + "Pinging %s. Waiting for response..." % test_ip) sec += 1 - return EXIT_CODE + return exit_code + + +def _get_userdata(test_ip): + """ + Returns the post VM creation script to be added into the VM's userdata + :param test_ip: the IP value to substitute into the script + :return: the bash script contents + """ + if test_ip: + return ("#!/bin/sh\n\n" + "while true; do\n" + " ping -c 1 %s 2>&1 >/dev/null\n" + " RES=$?\n" + " if [ \"Z$RES\" = \"Z0\" ] ; then\n" + " echo 'vPing OK'\n" + " break\n" + " else\n" + " echo 'vPing KO'\n" + " fi\n" + " sleep 1\n" + "done\n" % test_ip) + return None if __name__ == '__main__': diff --git a/functest/opnfv_tests/sdn/odl/odl.py b/functest/opnfv_tests/sdn/odl/odl.py index 6f4acf6d..6f586b7a 100755 --- a/functest/opnfv_tests/sdn/odl/odl.py +++ b/functest/opnfv_tests/sdn/odl/odl.py @@ -16,21 +16,23 @@ Example: $ python odl.py """ +from __future__ import division + import argparse import errno import fileinput +import logging import os import re import sys -import urlparse import robot.api from robot.errors import RobotError import robot.run from robot.utils.robottime import timestamp_to_secs +from six.moves import urllib from functest.core import testcase -import functest.utils.functest_logger as ft_logger import functest.utils.openstack_utils as op_utils __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>" @@ -70,7 +72,7 @@ class ODLTests(testcase.TestCase): "csit/suites/integration/basic") default_suites = [basic_suite_dir, neutron_suite_dir] res_dir = '/home/opnfv/functest/results/odl/' - logger = ft_logger.Logger("opendaylight").getLogger() + __logger = logging.getLogger(__name__) @classmethod def set_robotframework_vars(cls, odlusername="admin", odlpassword="admin"): @@ -85,13 +87,13 @@ class ODLTests(testcase.TestCase): try: for line in fileinput.input(odl_variables_files, inplace=True): - print re.sub("AUTH = .*", + print(re.sub("AUTH = .*", ("AUTH = [u'" + odlusername + "', u'" + odlpassword + "']"), - line.rstrip()) + line.rstrip())) return True except Exception as ex: # pylint: disable=broad-except - cls.logger.error("Cannot set ODL creds: %s", str(ex)) + cls.__logger.error("Cannot set ODL creds: %s", str(ex)) return False def parse_results(self): @@ -100,7 +102,12 @@ class ODLTests(testcase.TestCase): result = robot.api.ExecutionResult(xml_file) visitor = ODLResultVisitor() result.visit(visitor) - self.result = result.suite.status + try: + self.result = 100 * ( + result.suite.statistics.critical.passed / + result.suite.statistics.critical.total) + except ZeroDivisionError: + self.__logger.error("No test has been ran") self.start_time = timestamp_to_secs(result.suite.starttime) self.stop_time = timestamp_to_secs(result.suite.endtime) self.details = {} @@ -143,7 +150,7 @@ class ODLTests(testcase.TestCase): odlusername = kwargs['odlusername'] odlpassword = kwargs['odlpassword'] osauthurl = kwargs['osauthurl'] - keystoneip = urlparse.urlparse(osauthurl).hostname + keystoneip = urllib.parse.urlparse(osauthurl).hostname variables = ['KEYSTONE:' + keystoneip, 'NEUTRON:' + kwargs['neutronip'], 'OS_AUTH_URL:"' + osauthurl + '"', @@ -154,15 +161,15 @@ class ODLTests(testcase.TestCase): 'PORT:' + kwargs['odlwebport'], 'RESTCONFPORT:' + kwargs['odlrestconfport']] except KeyError as ex: - self.logger.error("Cannot run ODL testcases. Please check " - "%s", str(ex)) + self.__logger.error("Cannot run ODL testcases. Please check " + "%s", str(ex)) return self.EX_RUN_ERROR if self.set_robotframework_vars(odlusername, odlpassword): try: os.makedirs(self.res_dir) except OSError as ex: if ex.errno != errno.EEXIST: - self.logger.exception( + self.__logger.exception( "Cannot create %s", self.res_dir) return self.EX_RUN_ERROR stdout_file = os.path.join(self.res_dir, 'stdout.txt') @@ -174,19 +181,19 @@ class ODLTests(testcase.TestCase): report='NONE', stdout=stdout) stdout.seek(0, 0) - self.logger.info("\n" + stdout.read()) - self.logger.info("ODL results were successfully generated") + self.__logger.info("\n" + stdout.read()) + self.__logger.info("ODL results were successfully generated") try: self.parse_results() - self.logger.info("ODL results were successfully parsed") + self.__logger.info("ODL results were successfully parsed") except RobotError as ex: - self.logger.error("Run tests before publishing: %s", - ex.message) + self.__logger.error("Run tests before publishing: %s", + ex.message) return self.EX_RUN_ERROR try: os.remove(stdout_file) except OSError: - self.logger.warning("Cannot remove %s", stdout_file) + self.__logger.warning("Cannot remove %s", stdout_file) return self.EX_OK else: return self.EX_RUN_ERROR @@ -211,7 +218,7 @@ class ODLTests(testcase.TestCase): except KeyError: pass neutron_url = op_utils.get_endpoint(service_type='network') - kwargs = {'neutronip': urlparse.urlparse(neutron_url).hostname} + kwargs = {'neutronip': urllib.parse.urlparse(neutron_url).hostname} kwargs['odlip'] = kwargs['neutronip'] kwargs['odlwebport'] = '8080' kwargs['odlrestconfport'] = '8181' @@ -237,12 +244,12 @@ class ODLTests(testcase.TestCase): else: kwargs['odlip'] = os.environ['SDN_CONTROLLER_IP'] except KeyError as ex: - self.logger.error("Cannot run ODL testcases. " - "Please check env var: " - "%s", str(ex)) + self.__logger.error("Cannot run ODL testcases. " + "Please check env var: " + "%s", str(ex)) return self.EX_RUN_ERROR except Exception: # pylint: disable=broad-except - self.logger.exception("Cannot run ODL testcases.") + self.__logger.exception("Cannot run ODL testcases.") return self.EX_RUN_ERROR return self.main(suites, **kwargs) @@ -301,6 +308,7 @@ class ODLParser(object): # pylint: disable=too-few-public-methods if __name__ == '__main__': + logging.basicConfig() ODL = ODLTests() PARSER = ODLParser() ARGS = PARSER.parse_args(sys.argv[1:]) diff --git a/functest/opnfv_tests/sdn/onos/onos.py b/functest/opnfv_tests/sdn/onos/onos.py index 4d489d67..5dfff036 100644 --- a/functest/opnfv_tests/sdn/onos/onos.py +++ b/functest/opnfv_tests/sdn/onos/onos.py @@ -7,6 +7,7 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 +import logging import os import re import subprocess @@ -16,7 +17,6 @@ import urlparse from functest.core import testcase from functest.utils.constants import CONST -import functest.utils.functest_logger as ft_logger import functest.utils.functest_utils as ft_utils import functest.utils.openstack_utils as openstack_utils @@ -25,12 +25,12 @@ class OnosBase(testcase.TestCase): onos_repo_path = CONST.__getattribute__('dir_repo_onos') onos_sfc_image_name = CONST.__getattribute__('onos_sfc_image_name') onos_sfc_image_path = os.path.join( - CONST.__getattribute__('dir_functest_data'), + CONST.__getattribute__('dir_functest_images'), CONST.__getattribute__('onos_sfc_image_file_name')) onos_sfc_path = os.path.join(CONST.__getattribute__('dir_repo_functest'), CONST.__getattribute__('dir_onos_sfc')) installer_type = CONST.__getattribute__('INSTALLER_TYPE') - logger = ft_logger.Logger(__name__).getLogger() + logger = logging.getLogger(__name__) def __init__(self, **kwargs): if "case_name" not in kwargs: diff --git a/functest/opnfv_tests/sdn/onos/sfc/sfc.py b/functest/opnfv_tests/sdn/onos/sfc/sfc.py index 22412270..0155d24d 100755 --- a/functest/opnfv_tests/sdn/onos/sfc/sfc.py +++ b/functest/opnfv_tests/sdn/onos/sfc/sfc.py @@ -1,4 +1,3 @@ -"""Script to Test the SFC scenarios in ONOS.""" # !/usr/bin/python # # Copyright (c) CREATED5 All rights reserved @@ -22,13 +21,14 @@ # Testcase 7 : Cleanup # ########################################################################### # +"""Script to Test the SFC scenarios in ONOS.""" +import logging import time -import functest.utils.functest_logger as ft_logger import functest.utils.functest_utils as ft_utils from sfc_onos import SfcOnos -logger = ft_logger.Logger("sfc").getLogger() +logger = logging.getLogger(__name__) Sfc_obj = SfcOnos() OK = 200 @@ -174,4 +174,5 @@ def main(): if __name__ == '__main__': + logging.basicConfig() main() diff --git a/functest/opnfv_tests/sdn/onos/sfc/sfc_onos.py b/functest/opnfv_tests/sdn/onos/sfc/sfc_onos.py index c2198690..1101f239 100644 --- a/functest/opnfv_tests/sdn/onos/sfc/sfc_onos.py +++ b/functest/opnfv_tests/sdn/onos/sfc/sfc_onos.py @@ -1,3 +1,4 @@ +import logging import os import re import time @@ -8,8 +9,6 @@ from multiprocessing import Process from multiprocessing import Queue from pexpect import pxssh -import functest.utils.functest_logger as ft_logger - from functest.utils.constants import CONST OK = 200 @@ -23,7 +22,7 @@ class SfcOnos(object): def __init__(self): """Initialization of variables.""" - self.logger = ft_logger.Logger("sfc_fun").getLogger() + self.logger = logging.getLogger(__name__) self.osver = "v2.0" self.token_id = 0 self.net_id = 0 diff --git a/functest/opnfv_tests/sdn/onos/teston/adapters/client.py b/functest/opnfv_tests/sdn/onos/teston/adapters/client.py index 81d5f7d7..a88d2f06 100644 --- a/functest/opnfv_tests/sdn/onos/teston/adapters/client.py +++ b/functest/opnfv_tests/sdn/onos/teston/adapters/client.py @@ -11,17 +11,17 @@ Description: # """ import json +import logging import pexpect import requests import time from environment import Environment -import functest.utils.functest_logger as ft_logger class Client(Environment): - logger = ft_logger.Logger("client").getLogger() + logger = logging.getLogger(__name__) def __init__(self): Environment.__init__(self) diff --git a/functest/opnfv_tests/sdn/onos/teston/adapters/connection.py b/functest/opnfv_tests/sdn/onos/teston/adapters/connection.py index 3786945d..dfaa5cc1 100644 --- a/functest/opnfv_tests/sdn/onos/teston/adapters/connection.py +++ b/functest/opnfv_tests/sdn/onos/teston/adapters/connection.py @@ -13,17 +13,17 @@ Description: # http://www.apache.org/licenses/LICENSE-2.0 # """ +import logging import os import pexpect import re from foundation import Foundation -import functest.utils.functest_logger as ft_logger class Connection(Foundation): - logger = ft_logger.Logger("connection").getLogger() + logger = logging.getLogger(__name__) def __init__(self): Foundation.__init__(self) diff --git a/functest/opnfv_tests/sdn/onos/teston/adapters/environment.py b/functest/opnfv_tests/sdn/onos/teston/adapters/environment.py index 046a821d..cb75b5c3 100644 --- a/functest/opnfv_tests/sdn/onos/teston/adapters/environment.py +++ b/functest/opnfv_tests/sdn/onos/teston/adapters/environment.py @@ -15,6 +15,7 @@ Description: # """ +import logging import pexpect import pxssh import re @@ -23,12 +24,11 @@ import sys import time from connection import Connection -import functest.utils.functest_logger as ft_logger class Environment(Connection): - logger = ft_logger.Logger("environment").getLogger() + logger = logging.getLogger(__name__) def __init__(self): Connection.__init__(self) diff --git a/functest/opnfv_tests/vnf/aaa/aaa.py b/functest/opnfv_tests/vnf/aaa/aaa.py index 9c94cfb1..0030256c 100755 --- a/functest/opnfv_tests/vnf/aaa/aaa.py +++ b/functest/opnfv_tests/vnf/aaa/aaa.py @@ -7,18 +7,18 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 +import logging import sys import argparse import functest.core.testcase as testcase -import functest.core.vnf_base as vnf_base -import functest.utils.functest_logger as ft_logger +import functest.core.vnf as vnf -class AaaVnf(vnf_base.VnfOnBoardingBase): +class AaaVnf(vnf.VnfOnBoarding): - logger = ft_logger.Logger("VNF AAA").getLogger() + logger = logging.getLogger(__name__) def __init__(self, **kwargs): if "case_name" not in kwargs: @@ -60,6 +60,7 @@ class AaaVnf(vnf_base.VnfOnBoardingBase): if __name__ == '__main__': + logging.basicConfig() parser = argparse.ArgumentParser() args = vars(parser.parse_args()) aaa_vnf = AaaVnf() diff --git a/functest/opnfv_tests/vnf/ims/clearwater_ims_base.py b/functest/opnfv_tests/vnf/ims/clearwater_ims_base.py index 494633f4..42d31e3b 100644 --- a/functest/opnfv_tests/vnf/ims/clearwater_ims_base.py +++ b/functest/opnfv_tests/vnf/ims/clearwater_ims_base.py @@ -7,21 +7,21 @@ # # http://www.apache.org/licenses/LICENSE-2.0 import json +import logging import os import shutil import requests -import functest.core.vnf_base as vnf_base +import functest.core.vnf as vnf from functest.utils.constants import CONST -import functest.utils.functest_logger as ft_logger import functest.utils.functest_utils as ft_utils -class ClearwaterOnBoardingBase(vnf_base.VnfOnBoardingBase): +class ClearwaterOnBoardingBase(vnf.VnfOnBoarding): def __init__(self, **kwargs): - self.logger = ft_logger.Logger(__name__).getLogger() + self.logger = logging.getLogger(__name__) super(ClearwaterOnBoardingBase, self).__init__(**kwargs) self.case_dir = os.path.join(CONST.dir_functest_test, 'vnf', 'ims') self.data_dir = CONST.dir_ims_data diff --git a/functest/opnfv_tests/vnf/ims/cloudify_ims.py b/functest/opnfv_tests/vnf/ims/cloudify_ims.py index 0e6d4797..ba4c5790 100644 --- a/functest/opnfv_tests/vnf/ims/cloudify_ims.py +++ b/functest/opnfv_tests/vnf/ims/cloudify_ims.py @@ -7,6 +7,7 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 +import logging import os import sys import time @@ -18,7 +19,6 @@ from functest.opnfv_tests.vnf.ims.clearwater import Clearwater import functest.opnfv_tests.vnf.ims.clearwater_ims_base as clearwater_ims_base from functest.opnfv_tests.vnf.ims.orchestrator_cloudify import Orchestrator from functest.utils.constants import CONST -import functest.utils.functest_logger as ft_logger import functest.utils.functest_utils as ft_utils import functest.utils.openstack_utils as os_utils @@ -29,7 +29,7 @@ class CloudifyIms(clearwater_ims_base.ClearwaterOnBoardingBase): if "case_name" not in kwargs: kwargs["case_name"] = "cloudify_ims" super(CloudifyIms, self).__init__(**kwargs) - self.logger = ft_logger.Logger(__name__).getLogger() + self.logger = logging.getLogger(__name__) # Retrieve the configuration try: diff --git a/functest/opnfv_tests/vnf/ims/opera_ims.py b/functest/opnfv_tests/vnf/ims/opera_ims.py index a46f9d71..8c33d16e 100644 --- a/functest/opnfv_tests/vnf/ims/opera_ims.py +++ b/functest/opnfv_tests/vnf/ims/opera_ims.py @@ -8,6 +8,7 @@ # http://www.apache.org/licenses/LICENSE-2.0 import json +import logging import os import time @@ -15,16 +16,15 @@ from opera import openo_connect import requests import functest.opnfv_tests.vnf.ims.clearwater_ims_base as clearwater_ims_base -from functest.utils.constants import CONST -import functest.utils.functest_logger as ft_logger class OperaIms(clearwater_ims_base.ClearwaterOnBoardingBase): - def __init__(self, project='functest', case_name='opera_ims', - repo=CONST.dir_repo_opera, cmd=''): - super(OperaIms, self).__init__(project, case_name, repo, cmd) - self.logger = ft_logger.Logger(__name__).getLogger() + def __init__(self, **kwargs): + if "case_name" not in kwargs: + kwargs["case_name"] = "opera_ims" + super(OperaIms, self).__init__(**kwargs) + self.logger = logging.getLogger(__name__) self.ellis_file = os.path.join(self.result_dir, 'ellis.info') self.live_test_file = os.path.join(self.result_dir, 'live_test_report.json') @@ -64,27 +64,27 @@ class OperaIms(clearwater_ims_base.ClearwaterOnBoardingBase): self.logger.info('VNFM IP: %s', vnfm_ip) vnf_status_url = 'http://{0}:5000/api/v1/model/status'.format(vnfm_ip) vnf_alive = False - retry = 15 + retry = 40 self.logger.info('Check the VNF status') while retry > 0: - rq = requests.get(vnf_status_url) + rq = requests.get(vnf_status_url, timeout=90) response = rq.json() vnf_alive = response['vnf_alive'] msg = response['msg'] self.logger.info(msg) if vnf_alive: break - self.logger.info('check again in one minute...') + self.logger.info('check again in one and half a minute...') retry = retry - 1 - time.sleep(60) + time.sleep(90) if not vnf_alive: raise Exception('VNF failed to start: {0}'.format(msg)) ellis_config_url = ('http://{0}:5000/api/v1/model/ellis/configure' .format(vnfm_ip)) - rq = requests.get(ellis_config_url, timeout=60) + rq = requests.get(ellis_config_url, timeout=90) if rq.json() and not rq.json()['ellis_ok']: self.logger.error(rq.json()['data']) raise Exception('Failed to configure Ellis') @@ -92,7 +92,7 @@ class OperaIms(clearwater_ims_base.ClearwaterOnBoardingBase): self.logger.info('Get Clearwater deployment detail') vnf_info_url = ('http://{0}:5000/api/v1/model/output' .format(vnfm_ip)) - rq = requests.get(vnf_info_url, timeout=60) + rq = requests.get(vnf_info_url, timeout=90) data = rq.json()['data'] self.logger.info(data) bono_ip = data['bono_ip'] diff --git a/functest/opnfv_tests/vnf/ims/orchestra_ims.py b/functest/opnfv_tests/vnf/ims/orchestra_ims.py index 351c5fbe..6f341970 100755 --- a/functest/opnfv_tests/vnf/ims/orchestra_ims.py +++ b/functest/opnfv_tests/vnf/ims/orchestra_ims.py @@ -8,16 +8,16 @@ # http://www.apache.org/licenses/LICENSE-2.0 import json +import logging +import os import socket import sys import time import yaml -import functest.core.vnf_base as vnf_base -import functest.utils.functest_logger as ft_logger +import functest.core.vnf as vnf import functest.utils.functest_utils as ft_utils import functest.utils.openstack_utils as os_utils -import os from functest.utils.constants import CONST from org.openbaton.cli.agents.agents import MainAgent @@ -76,7 +76,7 @@ def servertest(host, port): return True -class ImsVnf(vnf_base.VnfOnBoardingBase): +class ImsVnf(vnf.VnfOnBoarding): def __init__(self, project='functest', case_name='orchestra_ims', repo='', cmd=''): @@ -87,7 +87,7 @@ class ImsVnf(vnf_base.VnfOnBoardingBase): self.ob_port = "8080" self.ob_ip = "localhost" self.ob_instance_id = "" - self.logger = ft_logger.Logger("orchestra_ims").getLogger() + self.logger = logging.getLogger(__name__) self.case_dir = os.path.join(CONST.dir_functest_test, 'vnf/ims/') self.data_dir = CONST.dir_ims_data self.test_dir = CONST.dir_repo_vims_test @@ -495,6 +495,7 @@ class ImsVnf(vnf_base.VnfOnBoardingBase): if __name__ == '__main__': + logging.basicConfig() test = ImsVnf() test.deploy_orchestrator() test.deploy_vnf() diff --git a/functest/opnfv_tests/vnf/ims/orchestrator_cloudify.py b/functest/opnfv_tests/vnf/ims/orchestrator_cloudify.py index 82a9dca0..4ceeb25f 100644 --- a/functest/opnfv_tests/vnf/ims/orchestrator_cloudify.py +++ b/functest/opnfv_tests/vnf/ims/orchestrator_cloudify.py @@ -11,6 +11,7 @@ # http://www.apache.org/licenses/LICENSE-2.0 ######################################################################## +import logging import os import shutil import subprocess32 as subprocess @@ -18,8 +19,6 @@ import yaml from git import Repo -import functest.utils.functest_logger as ft_logger - class Orchestrator(object): @@ -29,7 +28,7 @@ class Orchestrator(object): self.input_file = 'inputs.yaml' self.manager_blueprint = False self.config = inputs - self.logger = ft_logger.Logger("Orchestrator").getLogger() + self.logger = logging.getLogger(__name__) self.manager_up = False def set_credentials(self, username, password, tenant_name, auth_url): |