X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=testcases%2Fconfig_functest.py;h=76020046e9ca1d02a50364befeb1081cf623d435;hb=522e7a093b0c52f2ddca5f5334489bc1773a2ec3;hp=1d189a78c40751b8f180a2ade7f82bac10dbfdee;hpb=b0262cccffca4d2eb51f36cf991d5997eee28f71;p=functest.git diff --git a/testcases/config_functest.py b/testcases/config_functest.py index 1d189a78c..76020046e 100644 --- a/testcases/config_functest.py +++ b/testcases/config_functest.py @@ -8,21 +8,12 @@ # http://www.apache.org/licenses/LICENSE-2.0 # -import re, json, os, urllib2, argparse, logging, shutil, subprocess +import re, json, os, urllib2, argparse, logging, shutil, subprocess, yaml from git import Repo -actions = ['start', 'check', 'clean'] +from neutronclient.v2_0 import client -""" global variables """ -functest_dir = os.environ['HOME'] + '/.functest/' -image_url = 'https://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img' -#image_url = 'http://download.cirros-cloud.net/0.3.0/cirros-0.3.0-i386-disk.img' -image_disk_format = 'raw' -image_name = image_url.rsplit('/')[-1] -image_path = functest_dir + image_name -rally_repo_dir = functest_dir + "Rally_repo/" -rally_test_dir = functest_dir + "Rally_test/" -rally_installation_dir = os.environ['HOME'] + "/.rally" +actions = ['start', 'check', 'clean'] @@ -48,29 +39,95 @@ logger.addHandler(ch) +yaml_url = 'https://git.opnfv.org/cgit/functest/plain/testcases/functest.yaml' +name = yaml_url.rsplit('/')[-1] +dest = "./" + name +if not os.path.exists(dest): + logger.info("Downloading functest.yaml...") + try: + response = urllib2.urlopen(yaml_url) + except (urllib2.HTTPError, urllib2.URLError): + logger.error("Error in fetching %s" %yaml_url) + exit(-1) + with open(dest, 'wb') as f: + f.write(response.read()) + logger.info("functest.yaml stored in %s" % dest) +else: + logger.info("functest.yaml found in %s" % dest) + + +with open('./functest.yaml') as f: + functest_yaml = yaml.safe_load(f) +f.close() + + +""" global variables """ +# Directories +HOME = os.environ['HOME']+"/" +FUNCTEST_BASE_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_functest") +RALLY_REPO_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_rally_repo") +RALLY_TEST_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_rally") +RALLY_INSTALLATION_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_rally_inst") +BENCH_TESTS_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_rally_scn") +VPING_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_vping") +ODL_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_odl") + +# NEUTRON Public Network parameters +NEUTRON_PUBLIC_NET_NAME = functest_yaml.get("general").get("openstack").get("neutron_public_net_name") +NEUTRON_PUBLIC_SUBNET_NAME = functest_yaml.get("general").get("openstack").get("neutron_public_subnet_name") +NEUTRON_PUBLIC_SUBNET_CIDR = functest_yaml.get("general").get("openstack").get("neutron_public_subnet_cidr") +NEUTRON_PUBLIC_SUBNET_START = functest_yaml.get("general").get("openstack").get("neutron_public_subnet_start") +NEUTRON_PUBLIC_SUBNET_END = functest_yaml.get("general").get("openstack").get("neutron_public_subnet_end") + +# NEUTRON Private Network parameters +NEUTRON_PRIVATE_NET_NAME = functest_yaml.get("general").get("openstack").get("neutron_private_net_name") +NEUTRON_PRIVATE_SUBNET_NAME = functest_yaml.get("general").get("openstack").get("neutron_private_subnet_name") +NEUTRON_PRIVATE_SUBNET_CIDR = functest_yaml.get("general").get("openstack").get("neutron_private_subnet_cidr") +ROUTER_NAME = functest_yaml.get("general").get("openstack").get("neutron_router_name") + +#GLANCE image parameters +IMAGE_URL = functest_yaml.get("general").get("openstack").get("image_url") +IMAGE_DISK_FORMAT = functest_yaml.get("general").get("openstack").get("image_disk_format") +IMAGE_NAME = functest_yaml.get("general").get("openstack").get("image_name") +IMAGE_FILE_NAME = IMAGE_URL.rsplit('/')[-1] +IMAGE_DOWNLOAD_PATH = FUNCTEST_BASE_DIR + IMAGE_FILE_NAME + +credentials = None +neutron_client = None + def config_functest_start(): """ Start the functest environment installation """ + if config_functest_check(): - logger.info("Functest environment already installed in %s. Nothing to do." %functest_dir) + logger.info("Functest environment already installed in %s. Nothing to do." %FUNCTEST_BASE_DIR) exit(0) - elif not check_internet_connectivity(): + + if not check_internet_connectivity(): logger.error("There is no Internet connectivity. Please check the network configuration.") exit(-1) elif not check_credentials(): logger.error("Please source the openrc credentials and run the script again.") #TODO: source the credentials in this script exit(-1) + else: + # Clean in case there are left overs + config_functest_clean() - logger.info("Starting installationg of functest environment in %s" %functest_dir) - os.makedirs(functest_dir) - if not os.path.exists(functest_dir): + logger.info("Starting installationg of functest environment in %s" % FUNCTEST_BASE_DIR) + os.makedirs(FUNCTEST_BASE_DIR) + if not os.path.exists(FUNCTEST_BASE_DIR): logger.error("There has been a problem while creating the environment directory.") exit(-1) + logger.info("Donwloading test scripts and scenarios...") + if not download_tests(): + logger.error("There has been a problem while downloading the test scripts and scenarios.") + config_functest_clean() + exit(-1) logger.info("Installing Rally...") if not install_rally(): @@ -78,45 +135,146 @@ def config_functest_start(): config_functest_clean() exit(-1) - logger.info("Installing Robot...") - if not install_robot(): + logger.info("Installing ODL environment...") + if not install_odl(): logger.error("There has been a problem while installing Robot.") config_functest_clean() exit(-1) - logger.info("Donwloading test scripts and scenarios...") - if not download_tests(): - logger.error("There has been a problem while downloading the test scripts and scenarios.") - config_functest_clean() - exit(-1) + credentials = get_credentials() + neutron_client = client.Client(**credentials) + + logger.info("Configuring Neutron...") + logger.info("Checking if public network '%s' exists..." % NEUTRON_PUBLIC_NET_NAME) + #Now: if exists we don't create it again (the clean command does not clean the neutron networks yet) + if check_neutron_net(neutron_client, NEUTRON_PUBLIC_NET_NAME): + logger.info("Public network '%s' found. No need to create another one." % NEUTRON_PUBLIC_NET_NAME) + else: + logger.info("Public network '%s' not found. Creating..." % NEUTRON_PUBLIC_NET_NAME) + if not create_public_neutron_net(neutron_client): + logger.error("There has been a problem while creating the Neutron network.") + #config_functest_clean() + exit(-1) + + + logger.info("Checking if private network '%s' exists..." % NEUTRON_PRIVATE_NET_NAME) + #Now: if exists we don't create it again (the clean command does not clean the neutron networks yet) + if check_neutron_net(neutron_client, NEUTRON_PRIVATE_NET_NAME): + logger.info("Private network '%s' found. No need to create another one." % NEUTRON_PRIVATE_NET_NAME) + else: + logger.info("Private network '%s' not found. Creating..." % NEUTRON_PRIVATE_NET_NAME) + if not create_private_neutron_net(neutron_client): + logger.error("There has been a problem while creating the Neutron network.") + #config_functest_clean() + exit(-1) + logger.info("Donwloading image...") - if not download_url_with_progress(image_url, functest_dir): + if not download_url_with_progress(IMAGE_URL, FUNCTEST_BASE_DIR): logger.error("There has been a problem while downloading the image.") config_functest_clean() exit(-1) - logger.info("Creating Glance image: %s ..." %image_name) - if not create_glance_image(image_path,image_name,image_disk_format): + logger.info("Creating Glance image: %s ..." %IMAGE_NAME) + if not create_glance_image(IMAGE_DOWNLOAD_PATH,IMAGE_NAME,IMAGE_DISK_FORMAT): logger.error("There has been a problem while creating the Glance image.") config_functest_clean() exit(-1) - + exit(0) def config_functest_check(): """ - Check if the functest environment is installed + Check if the functest environment is properly installed """ + errors_all = False + logger.info("Checking current functest configuration...") - if os.path.exists(functest_dir) and os.path.exists(rally_installation_dir): - logger.info("Functest environment directory found in %s" %functest_dir) - return True + credentials = get_credentials() + neutron_client = client.Client(**credentials) + + logger.debug("Checking directories...") + errors = False + dirs = [FUNCTEST_BASE_DIR, RALLY_INSTALLATION_DIR, RALLY_REPO_DIR, RALLY_TEST_DIR, BENCH_TESTS_DIR, VPING_DIR, ODL_DIR] + for dir in dirs: + if not os.path.exists(dir): + logger.debug("The directory '%s' does NOT exist." % dir) + errors = True + errors_all = True + else: + logger.debug(" %s found" % dir) + if not errors: + logger.debug("...OK") + else: + logger.debug("...FAIL") + + + logger.debug("Checking Rally deployment...") + if not check_rally(): + logger.debug("Rally deployment NOT found.") + errors_all = True + logger.debug("...FAIL") else: - logger.info("Functest environment directory not found") + logger.debug("...OK") + + + logger.debug("Checking Neutron...") + errors = False + if not check_neutron_net(neutron_client, NEUTRON_PRIVATE_NET_NAME): + logger.debug(" Private network '%s' NOT found." % NEUTRON_PRIVATE_NET_NAME) + errors = True + errors_all = True + else: + logger.debug(" Private network '%s' found." % NEUTRON_PRIVATE_NET_NAME) + + if not check_neutron_net(neutron_client, NEUTRON_PUBLIC_NET_NAME): + logger.debug(" Public network '%s' NOT found." % NEUTRON_PUBLIC_NET_NAME) + errors = True + errors_all = True + else: + logger.debug(" Public network '%s' found." % NEUTRON_PUBLIC_NET_NAME) + + if not errors: + logger.debug("...OK") + else: + logger.debug("...FAIL") + + + logger.debug("Checking Image...") + errors = False + if not os.path.isfile(IMAGE_DOWNLOAD_PATH): + logger.debug(" Image file '%s' NOT found." % IMAGE_DOWNLOAD_PATH) + errors = True + errors_all = True + else: + logger.debug(" Image file found in %s" % IMAGE_DOWNLOAD_PATH) + + cmd="glance image-list | grep " + IMAGE_NAME + FNULL = open(os.devnull, 'w'); + logger.debug(' Executing command : {}'.format(cmd)) + p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=FNULL); + #if the command does not exist or there is no glance image + line = p.stdout.readline() + if line == "": + logger.debug(" Glance image NOT found.") + errors = True + errors_all = True + else: + logger.debug(" Glance image found.") + + if not errors: + logger.debug("...OK") + else: + logger.debug("...FAIL") + + #TODO: check OLD environment setup + if errors_all: return False + else: + return True + @@ -125,37 +283,44 @@ def config_functest_clean(): Clean the existing functest environment """ logger.info("Removing current functest environment...") - if os.path.exists(rally_installation_dir): - logger.debug("Removing rally installation directory %s" % rally_installation_dir) - shutil.rmtree(rally_installation_dir,ignore_errors=True) - - if os.path.exists(functest_dir): - logger.debug("Removing functest directory %s" % functest_dir) - cmd = "sudo rm -rf " + functest_dir #need to be sudo, not possible with rmtree + if os.path.exists(RALLY_INSTALLATION_DIR): + logger.debug("Removing rally installation directory %s" % RALLY_INSTALLATION_DIR) + shutil.rmtree(RALLY_INSTALLATION_DIR,ignore_errors=True) + + if os.path.exists(FUNCTEST_BASE_DIR): + logger.debug("Removing functest directory %s" % FUNCTEST_BASE_DIR) + cmd = "sudo rm -rf " + FUNCTEST_BASE_DIR #need to be sudo, not possible with rmtree execute_command(cmd) - + + #logger.debug("Deleting Neutron network %s" % NEUTRON_PRIVATE_NET_NAME) + #if not delete_neutron_net() : + # logger.error("Error deleting the network. Remove it manually.") + logger.debug("Deleting glance images") - cmd = "glance image-list | grep "+image_name+" | cut -c3-38" + cmd = "glance image-list | grep "+IMAGE_NAME+" | cut -c3-38" p = os.popen(cmd,"r") - - #while image_id = p.readline() + + #while image_id = p.readline() for image_id in p.readlines(): cmd = "glance image-delete " + image_id execute_command(cmd) - + return True + + + def install_rally(): if check_rally(): logger.info("Rally is already installed.") else: logger.debug("Cloning repository...") url = "https://git.openstack.org/openstack/rally" - Repo.clone_from(url, rally_repo_dir) + Repo.clone_from(url, RALLY_REPO_DIR) - logger.debug("Executing %s./install_rally.sh..." %rally_repo_dir) - install_script = rally_repo_dir + "install_rally.sh" + logger.debug("Executing %s./install_rally.sh..." %RALLY_REPO_DIR) + install_script = RALLY_REPO_DIR + "install_rally.sh" cmd = 'sudo ' + install_script execute_command(cmd) #subprocess.call(['sudo', install_script]) @@ -186,24 +351,29 @@ def check_rally(): """ Check if Rally is installed and properly configured """ - if os.path.exists(os.environ['HOME']+"/.rally/"): - #TODO: do a more consistent check, for example running the comand rally deployment check + if os.path.exists(RALLY_INSTALLATION_DIR): + logger.debug(" Rally installation directory found in %s" % RALLY_INSTALLATION_DIR) + FNULL = open(os.devnull, 'w'); + cmd="rally deployment list | grep opnfv"; + logger.debug(' Executing command : {}'.format(cmd)) + p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=FNULL); + #if the command does not exist or there is no deployment + line = p.stdout.readline() + if line == "": + logger.debug(" Rally deployment not found") + return False + logger.debug(" Rally deployment found") return True else: + logger.debug(" Rally installation directory not found") return False -def install_robot(): - cmd = "sudo pip install requests" - execute_command(cmd) - cmd = "sudo pip install robotframework" +def install_odl(): + cmd = "chmod +x " + ODL_DIR + "create_venv.sh" execute_command(cmd) - cmd = "sudo pip install robotframework-sshlibrary" + cmd = ODL_DIR + "create_venv.sh" execute_command(cmd) - cmd = "sudo pip install robotframework-requests" - execute_command(cmd) - #cmd = "mkvirtualenv robot" - #execute_command(cmd) return True @@ -235,48 +405,164 @@ def check_credentials(): return True +def get_credentials(): + d = {} + d['username'] = os.environ['OS_USERNAME'] + d['password'] = os.environ['OS_PASSWORD'] + d['auth_url'] = os.environ['OS_AUTH_URL'] + d['tenant_name'] = os.environ['OS_TENANT_NAME'] + return d + +def get_nova_credentials(): + d = {} + d['username'] = os.environ['OS_USERNAME'] + d['api_key'] = os.environ['OS_PASSWORD'] + d['auth_url'] = os.environ['OS_AUTH_URL'] + d['project_id'] = os.environ['OS_TENANT_NAME'] + return d + + def download_tests(): - vPing_dir = functest_dir + "vPing/" - odl_dir = functest_dir + "ODL/" - bench_tests_dir = rally_test_dir + "scenarios/" + os.makedirs(VPING_DIR) + os.makedirs(ODL_DIR) + os.makedirs(BENCH_TESTS_DIR) - os.makedirs(vPing_dir) - os.makedirs(odl_dir) - os.makedirs(bench_tests_dir) + logger.info("Copying functest.yaml to functest environment...") + try: + shutil.copy("./functest.yaml", FUNCTEST_BASE_DIR+"functest.yaml") + except: + print "Error copying the file:", sys.exc_info()[0] + return False logger.info("Downloading vPing test...") vPing_url = 'https://git.opnfv.org/cgit/functest/plain/testcases/vPing/CI/libraries/vPing.py' - if not download_url(vPing_url,vPing_dir): - return False - - vPing_url = 'https://git.opnfv.org/cgit/functest/plain/testcases/vPing/CI/libraries/vPing.py' - if not download_url(vPing_url,vPing_dir): + if not download_url(vPing_url,VPING_DIR): return False + logger.info("Downloading Rally bench tests...") run_rally_url = 'https://git.opnfv.org/cgit/functest/plain/testcases/VIM/OpenStack/CI/libraries/run_rally.py' - if not download_url(run_rally_url,rally_test_dir ): + if not download_url(run_rally_url,RALLY_TEST_DIR): return False - + rally_bench_base_url = 'https://git.opnfv.org/cgit/functest/plain/testcases/VIM/OpenStack/CI/suites/' bench_tests = ['authenticate', 'cinder', 'glance', 'heat', 'keystone', 'neutron', 'nova', 'quotas', 'requests', 'tempest', 'vm'] for i in bench_tests: rally_bench_url = rally_bench_base_url + "opnfv-" + i + ".json" logger.debug("Downloading %s" %rally_bench_url) - if not download_url(rally_bench_url,bench_tests_dir): + if not download_url(rally_bench_url,BENCH_TESTS_DIR): return False logger.info("Downloading OLD tests...") odl_base_url = 'https://git.opnfv.org/cgit/functest/plain/testcases/Controllers/ODL/CI/' - odl_tests = ['start_tests.sh', 'test_list.txt'] + odl_tests = ['create_venv.sh', 'requirements.pip', 'start_tests.sh', 'test_list.txt'] for i in odl_tests: odl_url = odl_base_url + i logger.debug("Downloading %s" %odl_url) - if not download_url(odl_url,odl_dir): + if not download_url(odl_url,ODL_DIR): return False - + + return True + +def create_public_neutron_net(neutron): + try: + neutron.format = 'json' + logger.debug('Creating Neutron network %s...' % NEUTRON_PUBLIC_NET_NAME) + json_body = {'network': {'name': NEUTRON_PUBLIC_NET_NAME, 'admin_state_up': True, 'router:external': True}} + netw = neutron.create_network(body=json_body) + net_dict = netw['network'] + network_id = net_dict['id'] + logger.debug("Network '%s' created successfully" % network_id) + + logger.debug('Creating Subnet....') + json_body = {'subnets': [{'cidr': NEUTRON_PUBLIC_SUBNET_CIDR, + 'ip_version': 4, 'network_id': network_id}]} + subnet = neutron.create_subnet(body=json_body) + logger.debug("Subnet '%s' created successfully" % subnet) + except: + print "Error:", sys.exc_info()[0] + return False + + logger.info("Public Neutron network created successfully.") return True - #TODO: complete + +def create_private_neutron_net(neutron): + try: + neutron.format = 'json' + logger.debug('Creating Neutron network %s...' % NEUTRON_PRIVATE_NET_NAME) + json_body = {'network': {'name': NEUTRON_PRIVATE_NET_NAME, + 'admin_state_up': True}} + netw = neutron.create_network(body=json_body) + net_dict = netw['network'] + network_id = net_dict['id'] + logger.debug("Network '%s' created successfully" % network_id) + + logger.debug('Creating Subnet....') + json_body = {'subnets': [{'cidr': NEUTRON_PRIVATE_SUBNET_CIDR, + 'ip_version': 4, 'network_id': network_id}]} + subnet = neutron.create_subnet(body=json_body) + logger.debug("Subnet '%s' created successfully" % subnet) + + logger.debug('Creating Router...') + json_body = {'router': {'name': ROUTER_NAME, 'admin_state_up': True}} + router = neutron.create_router(json_body) + logger.debug("Router '%s' created successfully" % router) + router_id = router['router']['id'] + + logger.debug('Creating Port') + json_body = {'port': { + 'admin_state_up': True, + 'device_id': router_id, + 'name': 'port1', + 'network_id': network_id, + }} + response = neutron.create_port(body=json_body) + logger.debug("Port created successfully.") + + logger.debug('Setting up gateway...') + public_network_id = get_network_id(neutron,NEUTRON_PUBLIC_NET_NAME) + json_body = {'network_id': public_network_id, 'enable_snat' : True} + gateway = neutron.add_gateway_router(router_id,body=json_body) + logger.debug("Gateway '%s' added successfully" % gateway) + except: + print "Error:", sys.exc_info()[0] + return False + + logger.info("Private Neutron network created successfully.") + return True + +def get_network_id(neutron, network_name): + networks = neutron.list_networks()['networks'] + id = '' + for n in networks: + if n['name'] == network_name: + id = n['id'] + break + return id + +def check_neutron_net(neutron, net_name): + for network in neutron.list_networks()['networks']: + if network['name'] == net_name : + for subnet in network['subnets']: + return True + return False + +def delete_neutron_net(neutron): + #TODO: remove router, ports + try: + #https://github.com/isginf/openstack_tools/blob/master/openstack_remove_tenant.py + for network in neutron.list_networks()['networks']: + if network['name'] == NEUTRON_PRIVATE_NET_NAME : + for subnet in network['subnets']: + print "Deleting subnet " + subnet + neutron.delete_subnet(subnet) + print "Deleting network " + network['name'] + neutron.delete_neutron_net(network['id']) + finally: + return True + return False + + @@ -289,6 +575,9 @@ def create_glance_image(path,name,disk_format): return True + + + def download_url(url, dest_path): """ Download a file to a destination path given a URL @@ -383,7 +672,10 @@ def main(): config_functest_start() if args.action == "check": - config_functest_check() + if config_functest_check(): + logger.info("Functest environment correctly installed") + else: + logger.info("Functest environment not found or faulty") if args.action == "clean": while True: