From ec66e9ac6d550af4dcef27d786adc1a41bc30bbc Mon Sep 17 00:00:00 2001 From: tierno Date: Wed, 17 May 2017 15:28:10 +0200 Subject: [PATCH] added set of tests to test_RO.py Change-Id: I484cb4ca4856bac3636f1ee5e6a3d1ce0f57cae3 Signed-off-by: tierno --- test/test_RO.py | 542 ++++++++++++++++++++++++++++++------------------ 1 file changed, 341 insertions(+), 201 deletions(-) diff --git a/test/test_RO.py b/test/test_RO.py index 36588b0f..26a7c94e 100755 --- a/test/test_RO.py +++ b/test/test_RO.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- ## -# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. +# Copyright 2017 # This file is part of openmano # All Rights Reserved. # @@ -18,8 +18,6 @@ # License for the specific language governing permissions and limitations # under the License. # -# For those usages not covered by the Apache License, Version 2.0 please -# contact with: nfvlabs@tid.es ## ''' @@ -27,13 +25,13 @@ Module for testing openmano functionality. It uses openmanoclient.py for invokin ''' __author__="Pablo Montes, Alfonso Tierno" __date__ ="$16-Feb-2017 17:08:16$" -__version__="0.0.2" +__version__="0.0.3" version_date="May 2017" import logging -import imp import os -from optparse import OptionParser +from argparse import ArgumentParser +import argcomplete import unittest import string import inspect @@ -44,16 +42,12 @@ import yaml import sys import time -global test_number -global test_directory -global scenario_test_folder -global test_image_name -global timeout -global management_network -global manual +global test_config # used for global variables with the test configuration +test_config = {} + def check_instance_scenario_active(uuid): - instance = client.get_instance(uuid=uuid) + instance = test_config["client"].get_instance(uuid=uuid) for net in instance['nets']: status = net['status'] @@ -68,22 +62,23 @@ def check_instance_scenario_active(uuid): return (True, None) + ''' IMPORTANT NOTE All unittest classes for code based tests must have prefix 'test_' in order to be taken into account for tests ''' -class test_tenant_operations(unittest.TestCase): +class test_VIM_datacenter_tenant_operations(unittest.TestCase): test_index = 1 tenant_name = None test_text = None @classmethod def setUpClass(cls): - logger.info("{}. {}".format(test_number, cls.__name__)) + logger.info("{}. {}".format(test_config["test_number"], cls.__name__)) @classmethod def tearDownClass(cls): - globals().__setitem__('test_number', globals().__getitem__('test_number') + 1) + test_config["test_number"] += 1 def tearDown(self): exec_info = sys.exc_info() @@ -99,41 +94,43 @@ class test_tenant_operations(unittest.TestCase): def test_000_create_RO_tenant(self): self.__class__.tenant_name = _get_random_string(20) - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - tenant = client.create_tenant(name=self.__class__.tenant_name, description=self.__class__.tenant_name) + tenant = test_config["client"].create_tenant(name=self.__class__.tenant_name, + description=self.__class__.tenant_name) logger.debug("{}".format(tenant)) self.assertEqual(tenant.get('tenant', {}).get('name', ''), self.__class__.tenant_name) def test_010_list_RO_tenant(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - tenant = client.get_tenant(name=self.__class__.tenant_name) + tenant = test_config["client"].get_tenant(name=self.__class__.tenant_name) logger.debug("{}".format(tenant)) self.assertEqual(tenant.get('tenant', {}).get('name', ''), self.__class__.tenant_name) def test_020_delete_RO_tenant(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - tenant = client.delete_tenant(name=self.__class__.tenant_name) + tenant = test_config["client"].delete_tenant(name=self.__class__.tenant_name) logger.debug("{}".format(tenant)) assert('deleted' in tenant.get('result',"")) -class test_datacenter_operations(unittest.TestCase): + +class test_VIM_datacenter_operations(unittest.TestCase): test_index = 1 datacenter_name = None test_text = None @classmethod def setUpClass(cls): - logger.info("{}. {}".format(test_number, cls.__name__)) + logger.info("{}. {}".format(test_config["test_number"], cls.__name__)) @classmethod def tearDownClass(cls): - globals().__setitem__('test_number', globals().__getitem__('test_number') + 1) + test_config["test_number"] += 1 def tearDown(self): exec_info = sys.exc_info() @@ -148,59 +145,62 @@ class test_datacenter_operations(unittest.TestCase): logger.critical("{}".format(msg)) def test_000_create_datacenter(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.datacenter_name = _get_random_string(20) self.__class__.test_index += 1 - self.datacenter = client.create_datacenter(name=self.__class__.datacenter_name, vim_url="http://fakeurl/fake") + self.datacenter = test_config["client"].create_datacenter(name=self.__class__.datacenter_name, + vim_url="http://fakeurl/fake") logger.debug("{}".format(self.datacenter)) self.assertEqual (self.datacenter.get('datacenter', {}).get('name',''), self.__class__.datacenter_name) def test_010_list_datacenter(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - self.datacenter = client.get_datacenter(all_tenants=True, name=self.__class__.datacenter_name) + self.datacenter = test_config["client"].get_datacenter(all_tenants=True, name=self.__class__.datacenter_name) logger.debug("{}".format(self.datacenter)) self.assertEqual (self.datacenter.get('datacenter', {}).get('name', ''), self.__class__.datacenter_name) def test_020_attach_datacenter(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - self.datacenter = client.attach_datacenter(name=self.__class__.datacenter_name, vim_tenant_name='fake') + self.datacenter = test_config["client"].attach_datacenter(name=self.__class__.datacenter_name, + vim_tenant_name='fake') logger.debug("{}".format(self.datacenter)) assert ('vim_tenants' in self.datacenter.get('datacenter', {})) def test_030_list_attached_datacenter(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - self.datacenter = client.get_datacenter(all_tenants=False, name=self.__class__.datacenter_name) + self.datacenter = test_config["client"].get_datacenter(all_tenants=False, name=self.__class__.datacenter_name) logger.debug("{}".format(self.datacenter)) self.assertEqual (self.datacenter.get('datacenter', {}).get('name', ''), self.__class__.datacenter_name) def test_040_detach_datacenter(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - self.datacenter = client.detach_datacenter(name=self.__class__.datacenter_name) + self.datacenter = test_config["client"].detach_datacenter(name=self.__class__.datacenter_name) logger.debug("{}".format(self.datacenter)) assert ('detached' in self.datacenter.get('result', "")) def test_050_delete_datacenter(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - self.datacenter = client.delete_datacenter(name=self.__class__.datacenter_name) + self.datacenter = test_config["client"].delete_datacenter(name=self.__class__.datacenter_name) logger.debug("{}".format(self.datacenter)) assert('deleted' in self.datacenter.get('result',"")) + class test_VIM_network_operations(unittest.TestCase): test_index = 1 vim_network_name = None @@ -209,11 +209,11 @@ class test_VIM_network_operations(unittest.TestCase): @classmethod def setUpClass(cls): - logger.info("{}. {}".format(test_number, cls.__name__)) + logger.info("{}. {}".format(test_config["test_number"], cls.__name__)) @classmethod def tearDownClass(cls): - globals().__setitem__('test_number', globals().__getitem__('test_number') + 1) + test_config["test_number"] += 1 def tearDown(self): exec_info = sys.exc_info() @@ -228,51 +228,52 @@ class test_VIM_network_operations(unittest.TestCase): logger.critical("{}".format(msg)) def test_000_create_VIM_network(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.vim_network_name = _get_random_string(20) self.__class__.test_index += 1 - network = client.vim_action("create", "networks", name=self.__class__.vim_network_name) + network = test_config["client"].vim_action("create", "networks", name=self.__class__.vim_network_name) logger.debug("{}".format(network)) self.__class__.vim_network_uuid = network["network"]["id"] self.assertEqual(network.get('network', {}).get('name', ''), self.__class__.vim_network_name) def test_010_list_VIM_networks(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - networks = client.vim_action("list", "networks") + networks = test_config["client"].vim_action("list", "networks") logger.debug("{}".format(networks)) def test_020_get_VIM_network_by_uuid(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - network = client.vim_action("show", "networks", uuid=self.__class__.vim_network_uuid) + network = test_config["client"].vim_action("show", "networks", uuid=self.__class__.vim_network_uuid) logger.debug("{}".format(network)) self.assertEqual(network.get('network', {}).get('name', ''), self.__class__.vim_network_name) def test_030_delete_VIM_network_by_uuid(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - network = client.vim_action("delete", "networks", uuid=self.__class__.vim_network_uuid) + network = test_config["client"].vim_action("delete", "networks", uuid=self.__class__.vim_network_uuid) logger.debug("{}".format(network)) assert ('deleted' in network.get('result', "")) + class test_VIM_image_operations(unittest.TestCase): test_index = 1 test_text = None @classmethod def setUpClass(cls): - logger.info("{}. {}".format(test_number, cls.__name__)) + logger.info("{}. {}".format(test_config["test_number"], cls.__name__)) @classmethod def tearDownClass(cls): - globals().__setitem__('test_number', globals().__getitem__('test_number') + 1) + test_config["test_number"] += 1 def tearDown(self): exec_info = sys.exc_info() @@ -287,10 +288,10 @@ class test_VIM_image_operations(unittest.TestCase): logger.critical("{}".format(msg)) def test_000_list_VIM_images(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - images = client.vim_action("list", "images") + images = test_config["client"].vim_action("list", "images") logger.debug("{}".format(images)) ''' @@ -306,13 +307,13 @@ class test_VIM_tenant_operations(unittest.TestCase): @classmethod def setUpClass(cls): - logger.info("{}. {}".format(test_number, cls.__name__)) + logger.info("{}. {}".format(test_config["test_number"], cls.__name__)) logger.warning("In case of OpenStack datacenter these tests will only success " "if RO has access to the admin endpoint") @classmethod def tearDownClass(cls): - globals().__setitem__('test_number', globals().__getitem__('test_number') + 1) + test_config["test_number"] += 1 def tearDown(self): exec_info = sys.exc_info() @@ -327,37 +328,37 @@ class test_VIM_tenant_operations(unittest.TestCase): logger.critical("{}".format(msg)) def test_000_create_VIM_tenant(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.vim_tenant_name = _get_random_string(20) self.__class__.test_index += 1 - tenant = client.vim_action("create", "tenants", name=self.__class__.vim_tenant_name) + tenant = test_config["client"].vim_action("create", "tenants", name=self.__class__.vim_tenant_name) logger.debug("{}".format(tenant)) self.__class__.vim_tenant_uuid = tenant["tenant"]["id"] self.assertEqual(tenant.get('tenant', {}).get('name', ''), self.__class__.vim_tenant_name) def test_010_list_VIM_tenants(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - tenants = client.vim_action("list", "tenants") + tenants = test_config["client"].vim_action("list", "tenants") logger.debug("{}".format(tenants)) def test_020_get_VIM_tenant_by_uuid(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - tenant = client.vim_action("show", "tenants", uuid=self.__class__.vim_tenant_uuid) + tenant = test_config["client"].vim_action("show", "tenants", uuid=self.__class__.vim_tenant_uuid) logger.debug("{}".format(tenant)) self.assertEqual(tenant.get('tenant', {}).get('name', ''), self.__class__.vim_tenant_name) def test_030_delete_VIM_tenant_by_uuid(self): - self.__class__.test_text = "{}.{}. TEST {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name) self.__class__.test_index += 1 - tenant = client.vim_action("delete", "tenants", uuid=self.__class__.vim_tenant_uuid) + tenant = test_config["client"].vim_action("delete", "tenants", uuid=self.__class__.vim_tenant_uuid) logger.debug("{}".format(tenant)) assert ('deleted' in tenant.get('result', "")) @@ -378,12 +379,12 @@ class descriptor_based_scenario_test(unittest.TestCase): def setUpClass(cls): cls.test_index = 1 cls.to_delete_list = [] - cls.scenario_test_path = test_directory + '/' + scenario_test_folder - logger.info("{}. {} {}".format(test_number, cls.__name__, scenario_test_folder)) + cls.scenario_test_path = test_config["test_directory"] + '/' + test_config["test_folder"] + logger.info("{}. {} {}".format(test_config["test_number"], cls.__name__, test_config["test_folder"])) @classmethod def tearDownClass(cls): - globals().__setitem__('test_number', globals().__getitem__('test_number') + 1) + test_config["test_number"] += 1 def tearDown(self): exec_info = sys.exc_info() @@ -399,15 +400,15 @@ class descriptor_based_scenario_test(unittest.TestCase): def test_000_load_scenario(self): - self.__class__.test_text = "{}.{}. TEST {} {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {} {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name, - scenario_test_folder) + test_config["test_folder"]) self.__class__.test_index += 1 vnfd_files = glob.glob(self.__class__.scenario_test_path+'/vnfd_*.yaml') scenario_file = glob.glob(self.__class__.scenario_test_path + '/scenario_*.yaml') if len(vnfd_files) == 0 or len(scenario_file) > 1: - raise Exception('Test '+scenario_test_folder+' not valid. It must contain an scenario file and at least one' - ' vnfd file') + raise Exception("Test '{}' not valid. It must contain an scenario file and at least one vnfd file'".format( + test_config["test_folder"])) #load all vnfd for vnfd in vnfd_files: @@ -416,53 +417,54 @@ class descriptor_based_scenario_test(unittest.TestCase): vnfc_list = vnf_descriptor['vnf']['VNFC'] for vnfc in vnfc_list: - vnfc['image name'] = test_image_name + vnfc['image name'] = test_config["image_name"] devices = vnfc.get('devices',[]) for device in devices: if device['type'] == 'disk' and 'image name' in device: - device['image name'] = test_image_name + device['image name'] = test_config["image_name"] logger.debug("VNF descriptor: {}".format(vnf_descriptor)) - vnf = client.create_vnf(descriptor=vnf_descriptor) + vnf = test_config["client"].create_vnf(descriptor=vnf_descriptor) logger.debug(vnf) - self.__class__.to_delete_list.insert(0, {"item": "vnf", "function": client.delete_vnf, + self.__class__.to_delete_list.insert(0, {"item": "vnf", "function": test_config["client"].delete_vnf, "params": {"uuid": vnf['vnf']['uuid']}}) #load the scenario definition with open(scenario_file[0], 'r') as stream: scenario_descriptor = yaml.load(stream) networks = scenario_descriptor['scenario']['networks'] - networks[management_network] = networks.pop('mgmt') + networks[test_config["mgmt_net"]] = networks.pop('mgmt') logger.debug("Scenario descriptor: {}".format(scenario_descriptor)) - scenario = client.create_scenario(descriptor=scenario_descriptor) + scenario = test_config["client"].create_scenario(descriptor=scenario_descriptor) logger.debug(scenario) - self.__class__.to_delete_list.insert(0,{"item": "scenario", "function": client.delete_scenario, + self.__class__.to_delete_list.insert(0,{"item": "scenario", "function": test_config["client"].delete_scenario, "params":{"uuid": scenario['scenario']['uuid']} }) self.__class__.scenario_uuid = scenario['scenario']['uuid'] def test_010_instantiate_scenario(self): - self.__class__.test_text = "{}.{}. TEST {} {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {} {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name, - scenario_test_folder) + test_config["test_folder"]) self.__class__.test_index += 1 - instance = client.create_instance(scenario_id=self.__class__.scenario_uuid, name=self.__class__.test_text) + instance = test_config["client"].create_instance(scenario_id=self.__class__.scenario_uuid, + name=self.__class__.test_text) self.__class__.instance_scenario_uuid = instance['uuid'] logger.debug(instance) - self.__class__.to_delete_list.insert(0, {"item": "instance", "function": client.delete_instance, + self.__class__.to_delete_list.insert(0, {"item": "instance", "function": test_config["client"].delete_instance, "params": {"uuid": instance['uuid']}}) def test_020_check_deployent(self): - self.__class__.test_text = "{}.{}. TEST {} {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {} {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name, - scenario_test_folder) + test_config["test_folder"]) self.__class__.test_index += 1 - if manual: + if test_config["manual"]: raw_input('Scenario has been deployed. Perform manual check and press any key to resume') return - keep_waiting = timeout + keep_waiting = test_config["timeout"] instance_active = False while True: result = check_instance_scenario_active(self.__class__.instance_scenario_uuid) @@ -485,9 +487,9 @@ class descriptor_based_scenario_test(unittest.TestCase): raise Exception(msg) def test_030_clean_deployment(self): - self.__class__.test_text = "{}.{}. TEST {} {}".format(test_number, self.__class__.test_index, + self.__class__.test_text = "{}.{}. TEST {} {}".format(test_config["test_number"], self.__class__.test_index, inspect.currentframe().f_code.co_name, - scenario_test_folder) + test_config["test_folder"]) self.__class__.test_index += 1 #At the moment if you delete an scenario right after creating it, in openstack datacenters #sometimes scenario ports get orphaned. This sleep is just a dirty workaround @@ -496,6 +498,7 @@ class descriptor_based_scenario_test(unittest.TestCase): response = item["function"](**item["params"]) logger.debug(response) + def _get_random_string(maxLength): '''generates a string with random characters string.letters and string.digits with a random length up to maxLength characters. If maxLength is <15 it will be changed automatically to 15 @@ -508,149 +511,125 @@ def _get_random_string(maxLength): length = random.randint(minLength,maxLength) return 'testing_'+"".join([random.choice(string.letters+string.digits) for i in xrange(length)]) -if __name__=="__main__": - sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "/osm_ro") - import openmanoclient - - parser = OptionParser() - #Mandatory arguments - parser.add_option('-d', '--datacenter', dest='datacenter_name', help='MANDATORY, Set the datacenter name to test') - parser.add_option("-i", '--image-name', dest='image-name', help='MANDATORY. Image name of an Ubuntu 16.04 image ' - 'that will be used for testing available in the ' - 'datacenter.') - parser.add_option("-n", '--mgmt-net-name', dest='mgmt-net', help='MANDATORY. Set the vim management network to use') - parser.add_option("-t", '--tenant', dest='tenant_name', help='MANDATORY. Set the tenant name to test') +def test_vimconnector(args): + global test_config + sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "/osm_ro") + if args.vimtype == "vmware": + import vimconn_vmware as vim + elif args.vimtype == "aws": + import vimconn_aws as vim + elif args.vimtype == "openstack": + import vimconn_openstack as vim + elif args.vimtype == "openvim": + import vimconn_openvim as vim + else: + logger.critical("vimtype '{}' not supported".format(args.vimtype)) + sys.exit(1) + executed = 0 + failed = 0 + clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass) + # If only want to obtain a tests list print it and exit + if args.list_tests: + tests_names = [] + for cls in clsmembers: + if cls[0].startswith('test_vimconnector'): + tests_names.append(cls[0]) - #Optional arguments - parser.add_option('--debug', help='Set logs to debug level', dest='debug', action="store_true", default=False) - parser.add_option('--failfast', help='Stop when a test fails rather than execute all tests', - dest='failfast', action="store_true", default=False) - parser.add_option('--failed', help='Set logs to show only failed tests. --debug disables this option', - dest='failed', action="store_true", default=False) - default_logger_file = os.path.dirname(__file__)+'/'+os.path.splitext(os.path.basename(__file__))[0]+'.log' - parser.add_option('--list-tests', help='List all available tests', dest='list-tests', action="store_true", - default=False) - parser.add_option('--logger_file', dest='logger_file', help='Set the logger file. By default '+default_logger_file, - default=default_logger_file) - parser.add_option('-m', '--manual-check', help='Pause execution once deployed to allow manual checking of the ' - 'deployed instance scenario', - dest='manual', action="store_true", default=False) - parser.add_option('--timeout', help='Specify the instantiation timeout in seconds. By default 300', - dest='timeout', type='int', default=300) - parser.add_option('--test', '--tests', help='Specify the tests to run', dest='tests', default=None) - parser.add_option('-u', '--url', dest='endpoint_url', help='Set the openmano server url. By default ' - 'http://localhost:9090/openmano', - default='http://localhost:9090/openmano') - parser.add_option("-v",'--version', help='Show current version', dest='version', action="store_true", default=False) - - (options, args) = parser.parse_args() + msg = "The 'vim' set tests are:\n\t" + ', '.join(sorted(tests_names)) + print(msg) + logger.info(msg) + sys.exit(0) - # default logger level is INFO. Options --debug and --failed override this, being --debug prioritary - logger_level = 'INFO' - if options.__dict__['debug']: - logger_level = 'DEBUG' - elif options.__dict__['failed']: - logger_level = 'WARNING' - logger_name = os.path.basename(__file__) - logger = logging.getLogger(logger_name) - logger.setLevel(logger_level) - failfast = options.__dict__['failfast'] + # Create the list of tests to be run + code_based_tests = [] + if args.tests: + for test in args.tests: + for t in test.split(','): + matches_code_based_tests = [item for item in clsmembers if item[0] == t] + if len(matches_code_based_tests) > 0: + code_based_tests.append(matches_code_based_tests[0][1]) + else: + logger.critical("Test '{}' is not among the possible ones".format(t)) + sys.exit(1) + if not code_based_tests: + # include all tests + for cls in clsmembers: + # We exclude 'test_VIM_tenant_operations' unless it is specifically requested by the user + if cls[0].startswith('test_vimconnector'): + code_based_tests.append(cls[1]) - # Configure a logging handler to store in a logging file - fileHandler = logging.FileHandler(options.__dict__['logger_file']) - formatter_fileHandler = logging.Formatter('%(asctime)s %(name)s %(levelname)s: %(message)s') - fileHandler.setFormatter(formatter_fileHandler) - logger.addHandler(fileHandler) + logger.debug("tests to be executed: {}".format(code_based_tests)) - # Configure a handler to print to stdout - consoleHandler = logging.StreamHandler(sys.stdout) - formatter_consoleHandler = logging.Formatter('%(message)s') - consoleHandler.setFormatter(formatter_consoleHandler) - logger.addHandler(consoleHandler) + # TextTestRunner stream is set to /dev/null in order to avoid the method to directly print the result of tests. + # This is handled in the tests using logging. + stream = open('/dev/null', 'w') - logger.debug('Program started with the following arguments: ' + str(options.__dict__)) + # Run code based tests + basic_tests_suite = unittest.TestSuite() + for test in code_based_tests: + basic_tests_suite.addTest(unittest.makeSuite(test)) + result = unittest.TextTestRunner(stream=stream, failfast=failfast).run(basic_tests_suite) + executed += result.testsRun + failed += len(result.failures) + len(result.errors) + if failfast and failed: + sys.exit(1) + if len(result.failures) > 0: + logger.debug("failures : {}".format(result.failures)) + if len(result.errors) > 0: + logger.debug("errors : {}".format(result.errors)) + return executed, failed - # If version is required print it and exit - if options.__dict__['version']: - logger.info("{}".format((sys.argv[0], __version__+" version", version_date))) - logger.info("(c) Copyright Telefonica") - sys.exit(0) - test_directory = os.path.dirname(__file__) + "/RO_tests" - test_directory_content = os.listdir(test_directory) +def test_vim(args): + global test_config + sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "/osm_ro") + import openmanoclient + executed = 0 + failed = 0 + test_config["client"] = openmanoclient.openmanoclient( + endpoint_url=args.endpoint_url, + tenant_name=args.tenant_name, + datacenter_name=args.datacenter, + debug=args.debug, logger=test_config["logger_name"]) clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass) - # If only want to obtain a tests list print it and exit - if options.__dict__['list-tests']: + if args.list_tests: tests_names = [] for cls in clsmembers: - if cls[0].startswith('test_'): + if cls[0].startswith('test_VIM'): tests_names.append(cls[0]) - msg = "The code based tests are:\n\t" + ', '.join(sorted(tests_names)) + \ - "\nThe descriptor based tests are:\n\t" + ', '.join(sorted(test_directory_content)) + \ + msg = "The 'vim' set tests are:\n\t" + ', '.join(sorted(tests_names)) +\ "\nNOTE: The test test_VIM_tenant_operations will fail in case the used datacenter is type OpenStack " \ "unless RO has access to the admin endpoint. Therefore this test is excluded by default" - + print(msg) logger.info(msg) sys.exit(0) - # Make sure required arguments are present - required = "tenant_name datacenter_name image-name mgmt-net".split() - error = False - for r in required: - if options.__dict__[r] is None: - print ("ERROR: parameter "+r+" is required") - error = True - if error: - parser.print_help() - sys.exit(1) - - # set test image name and management network - test_image_name = options.__dict__['image-name'] - management_network = options.__dict__['mgmt-net'] - manual = options.__dict__['manual'] - timeout = options.__dict__['timeout'] - # Create the list of tests to be run - descriptor_based_tests = [] code_based_tests = [] - if options.__dict__['tests'] is not None: - tests = sorted(options.__dict__['tests'].split(',')) - for test in tests: - matches_code_based_tests = [item for item in clsmembers if item[0] == test] - if test in test_directory_content: - descriptor_based_tests.append(test) - elif len(matches_code_based_tests) > 0: - code_based_tests.append(matches_code_based_tests[0][1]) - else: - logger.critical("Test {} is not among the possible ones".format(test)) - sys.exit(1) - else: + if args.tests: + for test in args.tests: + for t in test.split(','): + matches_code_based_tests = [item for item in clsmembers if item[0] == t] + if len(matches_code_based_tests) > 0: + code_based_tests.append(matches_code_based_tests[0][1]) + else: + logger.critical("Test '{}' is not among the possible ones".format(t)) + sys.exit(1) + if not code_based_tests: # include all tests - descriptor_based_tests = test_directory_content for cls in clsmembers: # We exclude 'test_VIM_tenant_operations' unless it is specifically requested by the user - if cls[0].startswith('test_') and cls[0] != 'test_VIM_tenant_operations': + if cls[0].startswith('test_VIM') and cls[0] != 'test_VIM_tenant_operations': code_based_tests.append(cls[1]) - logger.debug("descriptor_based_tests to be executed: {}".format(descriptor_based_tests)) - logger.debug("code_based_tests to be executed: {}".format(code_based_tests)) - - # import openmanoclient from relative path - client = openmanoclient.openmanoclient( - endpoint_url=options.__dict__['endpoint_url'], - tenant_name=options.__dict__['tenant_name'], - datacenter_name=options.__dict__['datacenter_name'], - debug=options.__dict__['debug'], logger=logger_name) + logger.debug("tests to be executed: {}".format(code_based_tests)) # TextTestRunner stream is set to /dev/null in order to avoid the method to directly print the result of tests. # This is handled in the tests using logging. stream = open('/dev/null', 'w') - test_number = 1 - executed = 0 - failed = 0 # Run code based tests basic_tests_suite = unittest.TestSuite() @@ -665,11 +644,57 @@ if __name__=="__main__": logger.debug("failures : {}".format(result.failures)) if len(result.errors) > 0: logger.debug("errors : {}".format(result.errors)) + return executed, failed + - # Additionally to the previous tests, scenario based tests will be executed. +def test_deploy(args): + global test_config + sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "/osm_ro") + import openmanoclient + executed = 0 + failed = 0 + test_config["test_directory"] = os.path.dirname(__file__) + "/RO_tests" + test_config["image_name"] = args.image_name + test_config["mgmt_net"] = args.mgmt_net + test_config["manual"] = args.manual + test_directory_content = os.listdir(test_config["test_directory"]) + # If only want to obtain a tests list print it and exit + if args.list_tests: + msg = "he 'deploy' set tests are:\n\t" + ', '.join(sorted(test_directory_content)) + print(msg) + logger.info(msg) + sys.exit(0) + + descriptor_based_tests = [] + # Create the list of tests to be run + code_based_tests = [] + if args.tests: + for test in args.tests: + for t in test.split(','): + if t in test_directory_content: + descriptor_based_tests.append(t) + else: + logger.critical("Test '{}' is not among the possible ones".format(t)) + sys.exit(1) + if not descriptor_based_tests: + # include all tests + descriptor_based_tests = test_directory_content + + logger.debug("tests to be executed: {}".format(code_based_tests)) + + # import openmanoclient from relative path + test_config["client"] = openmanoclient.openmanoclient( + endpoint_url=args.endpoint_url, + tenant_name=args.tenant_name, + datacenter_name=args.datacenter, + debug=args.debug, logger=test_config["logger_name"]) + + # TextTestRunner stream is set to /dev/null in order to avoid the method to directly print the result of tests. + # This is handled in the tests using logging. + stream = open('/dev/null', 'w') # This scenario based tests are defined as directories inside the directory defined in 'test_directory' for test in descriptor_based_tests: - scenario_test_folder = test + test_config["test_folder"] = test test_suite = unittest.TestSuite() test_suite.addTest(unittest.makeSuite(descriptor_based_scenario_test)) result = unittest.TextTestRunner(stream=stream, failfast=False).run(test_suite) @@ -682,6 +707,121 @@ if __name__=="__main__": if len(result.errors) > 0: logger.debug("errors : {}".format(result.errors)) + return executed, failed + +if __name__=="__main__": + + parser = ArgumentParser(description='Test RO module') + parser.add_argument('-v','--version', action='version', help="Show current version", + version='%(prog)s version ' + __version__ + ' ' + version_date) + + # Common parameters + parent_parser = ArgumentParser(add_help=False) + parent_parser.add_argument('--failfast', help='Stop when a test fails rather than execute all tests', + dest='failfast', action="store_true", default=False) + parent_parser.add_argument('--failed', help='Set logs to show only failed tests. --debug disables this option', + dest='failed', action="store_true", default=False) + default_logger_file = os.path.dirname(__file__)+'/'+os.path.splitext(os.path.basename(__file__))[0]+'.log' + parent_parser.add_argument('--list-tests', help='List all available tests', dest='list_tests', action="store_true", + default=False) + parent_parser.add_argument('--logger_file', dest='logger_file', default=default_logger_file, + help='Set the logger file. By default '+default_logger_file) + parent_parser.add_argument("-t", '--tenant', dest='tenant_name', default="osm", + help="Set the openmano tenant to use for the test. By default 'osm'") + parent_parser.add_argument('--debug', help='Set logs to debug level', dest='debug', action="store_true") + parent_parser.add_argument('--timeout', help='Specify the instantiation timeout in seconds. By default 300', + dest='timeout', type=int, default=300) + parent_parser.add_argument('--test', '--tests', help='Specify the tests to run', dest='tests', action="append") + + subparsers = parser.add_subparsers(help='test sets') + + # Deployment test set + # ------------------- + deploy_parser = subparsers.add_parser('deploy', parents=[parent_parser], + help="test deployment using descriptors at RO_test folder ") + deploy_parser.set_defaults(func=test_deploy) + + # Mandatory arguments + mandatory_arguments = deploy_parser.add_argument_group('mandatory arguments') + mandatory_arguments.add_argument('-d', '--datacenter', required=True, help='Set the datacenter to test') + mandatory_arguments.add_argument("-i", '--image-name', required=True, dest="image_name", + help='Image name available at datacenter used for the tests') + mandatory_arguments.add_argument("-n", '--mgmt-net-name', required=True, dest='mgmt_net', + help='Set the vim management network to use for tests') + + # Optional arguments + deploy_parser.add_argument('-m', '--manual-check', dest='manual', action="store_true", default=False, + help='Pause execution once deployed to allow manual checking of the ' + 'deployed instance scenario') + deploy_parser.add_argument('-u', '--url', dest='endpoint_url', default='http://localhost:9090/openmano', + help="Set the openmano server url. By default 'http://localhost:9090/openmano'") + + # Vimconn test set + # ------------------- + vimconn_parser = subparsers.add_parser('vimconn', parents=[parent_parser], help="test vimconnector plugin") + vimconn_parser.set_defaults(func=test_vimconnector) + # Mandatory arguments + mandatory_arguments = vimconn_parser.add_argument_group('mandatory arguments') + mandatory_arguments.add_argument('--vimtype', choices=['vmware', 'aws', 'openstack', 'openvim'], required=True, + help='Set the vimconnector type to test') + # TODO add mandatory arguments for vimconn test + # mandatory_arguments.add_argument('-c', '--config', dest='config_param', required=True, help='') + + # Optional arguments + # TODO add optional arguments for vimconn tests + # vimconn_parser.add_argument("-i", '--image-name', dest='image_name', help='')) + + # Datacenter test set + # ------------------- + vimconn_parser = subparsers.add_parser('vim', parents=[parent_parser], help="test vim") + vimconn_parser.set_defaults(func=test_vim) + + # Mandatory arguments + mandatory_arguments = vimconn_parser.add_argument_group('mandatory arguments') + mandatory_arguments.add_argument('-d', '--datacenter', required=True, help='Set the datacenter to test') + + # Optional arguments + vimconn_parser.add_argument('-u', '--url', dest='endpoint_url', default='http://localhost:9090/openmano', + help="Set the openmano server url. By default 'http://localhost:9090/openmano'") + + argcomplete.autocomplete(parser) + args = parser.parse_args() + # print str(args) + test_config = {} + + # default logger level is INFO. Options --debug and --failed override this, being --debug prioritary + logger_level = 'INFO' + if args.debug: + logger_level = 'DEBUG' + elif args.failed: + logger_level = 'WARNING' + logger_name = os.path.basename(__file__) + test_config["logger_name"] = logger_name + logger = logging.getLogger(logger_name) + logger.setLevel(logger_level) + failfast = args.failfast + + # Configure a logging handler to store in a logging file + if args.logger_file: + fileHandler = logging.FileHandler(args.logger_file) + formatter_fileHandler = logging.Formatter('%(asctime)s %(name)s %(levelname)s: %(message)s') + fileHandler.setFormatter(formatter_fileHandler) + logger.addHandler(fileHandler) + + # Configure a handler to print to stdout + consoleHandler = logging.StreamHandler(sys.stdout) + formatter_consoleHandler = logging.Formatter('%(asctime)s %(name)s %(levelname)s: %(message)s') + consoleHandler.setFormatter(formatter_consoleHandler) + logger.addHandler(consoleHandler) + + logger.debug('Program started with the following arguments: ' + str(args)) + + # set test config parameters + test_config["timeout"] = args.timeout + test_config["test_number"] = 1 + + executed, failed = args.func(args) + # Log summary logger.warning("Total number of tests: {}; Total number of failures/errors: {}".format(executed, failed)) sys.exit(1 if failed else 0) -- 2.25.1