From bd00e6f289a69badf7beea827b20764ed3252c7a Mon Sep 17 00:00:00 2001 From: "jose.lausuch" Date: Fri, 6 May 2016 01:36:30 +0200 Subject: CLI implementation JIRA: FUNCTEST-243 Change-Id: Ibf0ef9bcc5f3aeda96b050827b954ce060317613 Signed-off-by: jose.lausuch --- ci/prepare_env.py | 2 +- ci/testcases.yaml | 4 +- ci/tier_builder.py | 6 +++ ci/tier_handler.py | 95 ++++++++++++++++------------------ cli/cli_base.py | 94 +++++++++++++++++++++++---------- cli/commands/cli_env.py | 91 +++++++++++++++++++++++++++----- cli/commands/cli_os.py | 120 +++++++++++++++++++++++++++++++++++++++++++ cli/commands/cli_testcase.py | 41 +++++++++++++-- cli/commands/cli_tier.py | 54 +++++++++++++++++-- cli/functest-complete.sh | 8 +++ docker/Dockerfile | 2 +- utils/clean_openstack.py | 75 +++++++++++++-------------- utils/functest_utils.py | 20 ++++---- 13 files changed, 461 insertions(+), 151 deletions(-) create mode 100644 cli/commands/cli_os.py create mode 100644 cli/functest-complete.sh diff --git a/ci/prepare_env.py b/ci/prepare_env.py index 1a3d495cf..61740cd2e 100644 --- a/ci/prepare_env.py +++ b/ci/prepare_env.py @@ -268,7 +268,7 @@ def main(): sys.exit() if args.action == "start": - logger.info("\n######### Preparing Functest environment #########\n") + logger.info("######### Preparing Functest environment #########\n") check_env_variables() create_directories() source_rc_file() diff --git a/ci/testcases.yaml b/ci/testcases.yaml index 51c43fcb1..7f701d1a3 100644 --- a/ci/testcases.yaml +++ b/ci/testcases.yaml @@ -135,7 +135,7 @@ tiers: scenario: '' - - name: sdnvpn + name: bgpvpn description: >- Test suite from SDNVPN project. dependencies: @@ -153,7 +153,7 @@ tiers: name: tempest description: >- The list of test cases is generated by - Tempest automatically and depend on the parameters of + Tempest automatically and depends on the parameters of the OpenStack deplopyment. dependencies: installer: '' diff --git a/ci/tier_builder.py b/ci/tier_builder.py index 82b58cd03..05bcc8f33 100644 --- a/ci/tier_builder.py +++ b/ci/tier_builder.py @@ -57,6 +57,12 @@ class TierBuilder: def get_tiers(self): return self.tier_objects + def get_tier_names(self): + tier_names = [] + for tier in self.tier_objects: + tier_names.append(tier.get_name()) + return tier_names + def get_tier(self, tier_name): for i in range(0, len(self.tier_objects)): if self.tier_objects[i].get_name() == tier_name: diff --git a/ci/tier_handler.py b/ci/tier_handler.py index 9b444b7ca..af6345f06 100644 --- a/ci/tier_handler.py +++ b/ci/tier_handler.py @@ -10,6 +10,23 @@ import re +LINE_LENGTH = 72 + + +def split_text(text, max_len): + words = text.split() + lines = [] + line = "" + for word in words: + if len(line) + len(word) < max_len - 1: + line += word + " " + else: + lines.append(line) + line = word + " " + if line != "": + lines.append(line) + return lines + class Tier: def __init__(self, name, order, ci, description=""): @@ -54,41 +71,28 @@ class Tier: return self.order def __str__(self): - lines = [] - line_max = 50 - line = "" - line_count = 0 - for i in range(len(self.description)): - line += self.description[i] - if line_count >= line_max - 1: - line_count = 0 - lines.append(line) - line = "" - else: - line_count += 1 - if line != "": - lines.append(line) + lines = split_text(self.description, LINE_LENGTH-6) out = "" - out += ("+=======================================================+\n") - out += ("| Tier: " + self.name.ljust(47) + "|\n") - out += ("+=======================================================+\n") - out += ("| Order: " + str(self.order).ljust(47) + "|\n") - out += ("| Description: |\n") - for i in range(len(lines)): - out += ("| " + lines[i].ljust(50) + " |\n") - out += ("| Test cases: |\n") + out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2))) + out += ("| Tier: " + self.name.ljust(LINE_LENGTH - 10) + "|\n") + out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2))) + out += ("| Order: " + str(self.order).ljust(LINE_LENGTH - 10) + "|\n") + out += ("| Description:".ljust(LINE_LENGTH - 1) + "|\n") + for line in lines: + out += ("| " + line.ljust(LINE_LENGTH - 7) + " |\n") + out += ("| Test cases:".ljust(LINE_LENGTH - 1) + "|\n") tests = self.get_test_names() if len(tests) > 0: for i in range(len(tests)): - out += ("| - %s |\n" % tests[i].ljust(48)) + out += ("| - %s |\n" % tests[i].ljust(LINE_LENGTH - 9)) else: out += ("| (There are no supported test cases " - .ljust(56) + "|\n") + .ljust(LINE_LENGTH - 1) + "|\n") out += ("| in this tier for the given scenario) " - .ljust(56) + "|\n") - out += ("|".ljust(56) + "|\n") - out += ("+-------------------------------------------------------+\n") + .ljust(LINE_LENGTH - 1) + "|\n") + out += ("|".ljust(LINE_LENGTH - 1) + "|\n") + out += ("+%s+\n" % ("-" * (LINE_LENGTH - 2))) return out @@ -111,35 +115,22 @@ class TestCase: return self.name def __str__(self): - lines = [] - line_max = 50 - line = "" - line_count = 0 - for i in range(len(self.description)): - line += self.description[i] - if line_count >= line_max - 1: - line_count = 0 - lines.append(line) - line = "" - else: - line_count += 1 - if line != "": - lines.append(line) + lines = split_text(self.description, LINE_LENGTH-6) out = "" - out += ("+=======================================================+\n") - out += ("| Testcase: " + self.name.ljust(43) + "|\n") - out += ("+=======================================================+\n") - out += ("| Description: |\n") - for i in range(len(lines)): - out += ("| " + lines[i].ljust(50) + " |\n") - out += ("| Dependencies: |\n") + out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2))) + out += ("| Testcase: " + self.name.ljust(LINE_LENGTH - 14) + "|\n") + out += ("+%s+\n" % ("=" * (LINE_LENGTH - 2))) + out += ("| Description:".ljust(LINE_LENGTH - 1) + "|\n") + for line in lines: + out += ("| " + line.ljust(LINE_LENGTH - 7) + " |\n") + out += ("| Dependencies:".ljust(LINE_LENGTH - 1) + "|\n") installer = self.dependency.get_installer() scenario = self.dependency.get_scenario() - out += ("| - Installer: " + installer.ljust(38) + "|\n") - out += ("| - Scenario : " + scenario.ljust(38) + "|\n") - out += ("|".ljust(56) + "|\n") - out += ("+-------------------------------------------------------+\n") + out += ("| - Installer:" + installer.ljust(LINE_LENGTH - 17) + "|\n") + out += ("| - Scenario :" + scenario.ljust(LINE_LENGTH - 17) + "|\n") + out += ("|".ljust(LINE_LENGTH - 1) + "|\n") + out += ("+%s+\n" % ("-" * (LINE_LENGTH - 2))) return out diff --git a/cli/cli_base.py b/cli/cli_base.py index e8c1d194a..25696dbe9 100644 --- a/cli/cli_base.py +++ b/cli/cli_base.py @@ -1,6 +1,16 @@ +#!/usr/bin/env python +# +# jose.lausuch@ericsson.com +# 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 +# + import click from functest.cli.commands.cli_env import CliEnv +from functest.cli.commands.cli_os import CliOpenStack from functest.cli.commands.cli_testcase import CliTestcase from functest.cli.commands.cli_tier import CliTier @@ -13,6 +23,7 @@ def cli(): pass _env = CliEnv() +_openstack = CliOpenStack() _testcase = CliTestcase() _tier = CliTier() @@ -23,6 +34,12 @@ def env(ctx): pass +@cli.group() +@click.pass_context +def openstack(ctx): + pass + + @cli.group() @click.pass_context def testcase(ctx): @@ -35,70 +52,93 @@ def tier(ctx): pass -@env.command('show', help="write the help here") -def env_show(): - _env.show() +@openstack.command('check', help="Checks connectivity and status " + "to the OpenStack deployment.") +def os_check(): + _openstack.check() -@env.command('status', help="write the help here") -def env_status(): - _env.status() +@openstack.command('snapshot-create', help="Generates a snapshot of the " + "current OpenStack resources.") +def os_snapshot_create(): + _openstack.snapshot_create() -@env.command('getrc', help="write the help here") -def env_getrc(): - _env.getrc() +@openstack.command('snapshot-show', help="Prints the OpenStack snapshot.") +def os_snapshot_show(): + _openstack.snapshot_show() -@env.command('sourcerc', help="write the help here") -def env_sourcerc(): - _env.sourcerc() +@openstack.command('clean', + help="Cleans the OpenStack resources except the snapshot.") +def os_clean(): + _openstack.clean() -@env.command('setdefaults', help="write the help here") -def env_setdefaults(): - _env.setdefaults() +@openstack.command('show-credentials', + help="Prints the OpenStack credentials.") +def os_show_credentials(): + _openstack.show_credentials() -@env.command('getdefaults', help="write the help here") -def env_getdefaults(): - _env.getdefaults() +@openstack.command('fetch-rc', help="Fetch the OpenStack RC file from " + "the installer") +def os_fetch_rc(): + _openstack.fetch_credentials() -@env.command('clean', help="write the help here") -def env_clean(): - _env.clean() +@env.command('prepare', help="Prepares the Functest environment. This step is " + "needed run the tests.") +def env_prepare(): + _env.prepare() -@testcase.command('list', help="write the help here") +@env.command('show', help="Shows information about the current environment.") +def env_show(): + _env.show() + + +@env.command('status', help="Checks if the Functest environment is ready to " + "run the tests.") +def env_status(): + _env.status() + + +@testcase.command('list', help="Lists the available testcases.") def testcase_list(): _testcase.list() -@testcase.command('show', help="write the help here") +@testcase.command('show', help="Shows information about a test case.") @click.argument('testname', type=click.STRING, required=True) def testcase_show(testname): _testcase.show(testname) -@testcase.command('run', help="write the help here") +@testcase.command('run', help="Executes a test case.") @click.argument('testname', type=click.STRING, required=True) def testcase_run(testname): _testcase.run(testname) -@tier.command('list', help="write the help here") +@tier.command('list', help="Lists the available tiers.") def tier_list(): _tier.list() -@tier.command('show', help="write the help here") +@tier.command('show', help="Shows information about a tier.") @click.argument('tiername', type=click.STRING, required=True) def tier_show(tiername): _tier.show(tiername) -@tier.command('run', help="write the help here") +@tier.command('get-tests', help="Prints the tests in a tier.") +@click.argument('tiername', type=click.STRING, required=True) +def tier_gettests(tiername): + _tier.gettests(tiername) + + +@tier.command('run', help="Executes all the tests within a tier.") @click.argument('tiername', type=click.STRING, required=True) def tier_run(tiername): _tier.run(tiername) diff --git a/cli/commands/cli_env.py b/cli/commands/cli_env.py index b2570d379..9e6372d4d 100644 --- a/cli/commands/cli_env.py +++ b/cli/commands/cli_env.py @@ -8,29 +8,94 @@ # import click +import git +import os + +import functest.utils.functest_utils as ft_utils + +ENV_FILE = "/home/opnfv/functest/conf/env_active" +FUNCTEST_REPO = "/home/opnfv/repos/functest/" class CliEnv: def __init__(self): pass + def prepare(self): + if self.status(verbose=False) == 0: + answer = raw_input("It seems that the environment has been " + "already prepared. Do you want to do " + "it again? [y|n]\n") + while True: + if answer.lower() in ["y", "yes"]: + os.remove(ENV_FILE) + break + elif answer.lower() in ["n", "no"]: + return + else: + answer = raw_input("Invalid answer. Please type [y|n]\n") + + cmd = ("python /home/opnfv/repos/functest/ci/prepare_env.py start") + ft_utils.execute_command(cmd) + def show(self): - click.echo("env show") + CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE') + if CI_INSTALLER_TYPE is None: + CI_INSTALLER_TYPE = "Unknown" + CI_INSTALLER_IP = os.getenv('INSTALLER_IP') + if CI_INSTALLER_IP is None: + CI_INSTALLER_IP = "Unknown" + CI_INSTALLER = ("%s, %s" % (CI_INSTALLER_TYPE, CI_INSTALLER_IP)) + + CI_SCENARIO = os.getenv('DEPLOY_SCENARIO') + if CI_SCENARIO is None: + CI_SCENARIO = "Unknown" + + CI_NODE = os.getenv('NODE_NAME') + if CI_NODE is None: + CI_NODE = "Unknown" + + repo = git.Repo(FUNCTEST_REPO) + branch = repo.head.reference + GIT_BRANCH = branch.name + GIT_HASH = branch.commit.hexsha - def status(self): - click.echo("env status") + CI_BUILD_TAG = os.getenv('BUILD_TAG') + if CI_BUILD_TAG is not None: + CI_BUILD_TAG = CI_BUILD_TAG.lstrip( + "jenkins-").lstrip("functest").lstrip("-") - def getrc(self): - click.echo("env getrc") + CI_DEBUG = os.getenv('CI_DEBUG') + if CI_DEBUG is None: + CI_DEBUG = "false" - def sourcerc(self): - click.echo("env sourcerc") + STATUS = "not ready" + if self.status(verbose=False) == 0: + STATUS = "ready" - def setdefaults(self): - click.echo("env setdefaults") + click.echo("+======================================================+") + click.echo("| Functest Environment info |") + click.echo("+======================================================+") + click.echo("| INSTALLER: %s|" % CI_INSTALLER.ljust(41)) + click.echo("| SCENARIO: %s|" % CI_SCENARIO.ljust(41)) + click.echo("| POD: %s|" % CI_NODE.ljust(41)) + click.echo("| GIT BRACNH: %s|" % GIT_BRANCH.ljust(41)) + click.echo("| GIT HASH: %s|" % GIT_HASH.ljust(41)) + if CI_BUILD_TAG: + click.echo("| BUILD TAG: %s|" % CI_BUILD_TAG.ljust(41)) + click.echo("| DEBUG FLAG: %s|" % CI_DEBUG.ljust(41)) + click.echo("+------------------------------------------------------+") + click.echo("| STATUS: %s|" % STATUS.ljust(41)) + click.echo("+------------------------------------------------------+") + click.echo("") - def getdefaults(self): - click.echo("env getdefaults") + def status(self, verbose=True): + ret_val = 0 + if not os.path.isfile(ENV_FILE): + if verbose: + click.echo("Functest environment is not installed.\n") + ret_val = 1 + elif verbose: + click.echo("Functest environment ready to run tests.\n") - def clean(self): - click.echo("env clean") + return ret_val diff --git a/cli/commands/cli_os.py b/cli/commands/cli_os.py new file mode 100644 index 000000000..23b7beaa3 --- /dev/null +++ b/cli/commands/cli_os.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python +# +# jose.lausuch@ericsson.com +# 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 +# + +import click +import os +import yaml + +import functest.utils.clean_openstack as clean_os +import functest.utils.functest_utils as ft_utils +import functest.utils.generate_defaults as gen_def + +""" global variables """ +with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f: + functest_yaml = yaml.safe_load(f) + +REPOS_DIR = os.getenv('repos_dir') +FUNCTEST_REPO = ("%s/functest/" % REPOS_DIR) +FUNCTEST_CONF_DIR = functest_yaml.get("general").get( + "directories").get("dir_functest_conf") +RC_FILE = os.getenv('creds') +OS_DEFAULTS_FILE = FUNCTEST_CONF_DIR + '/os_defaults.yaml' + + +class CliOpenStack: + def __init__(self): + self.os_auth_url = os.getenv('OS_AUTH_URL') + self.endpoint_ip = None + self.endpoint_port = None + if self.os_auth_url is not None: + self.endpoint_ip = self.os_auth_url.rsplit("/")[2].rsplit(":")[0] + self.endpoint_port = self.os_auth_url.rsplit("/")[2].rsplit(":")[1] + + def ping_endpoint(self): + if self.os_auth_url is None: + click.echo("Source the OpenStack credentials first '. $creds'") + exit(0) + response = os.system("ping -c 1 " + self.endpoint_ip + ">/dev/null") + if response == 0: + return 0 + else: + click.echo("Cannot talk to the endpoint %s\n" % self.endpoint_ip) + exit(0) + + def show_credentials(self): + cmd = "env|grep OS_" + ft_utils.execute_command(cmd, exit_on_error=False, verbose=False) + click.echo("") + + def fetch_credentials(self): + if os.path.isfile(RC_FILE): + answer = raw_input("It seems the RC file is already present. " + "Do you want to overwrite it? [y|n]\n") + while True: + if answer.lower() in ["y", "yes"]: + break + elif answer.lower() in ["n", "no"]: + return + else: + answer = raw_input("Invalid answer. Please type [y|n]\n") + + CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE') + if CI_INSTALLER_TYPE is None: + click.echo("The environment variable 'INSTALLER_TYPE' is not" + "defined. Please export it") + CI_INSTALLER_IP = os.getenv('INSTALLER_IP') + if CI_INSTALLER_IP is None: + click.echo("The environment variable 'INSTALLER_IP' is not" + "defined. Please export it") + cmd = ("/home/opnfv/repos/releng/utils/fetch_os_creds.sh " + "-d %s -i %s -a %s" + % (RC_FILE, CI_INSTALLER_TYPE, CI_INSTALLER_IP)) + click.echo("Fetching credentials from installer node '%s' with IP=%s.." + % (CI_INSTALLER_TYPE, CI_INSTALLER_IP)) + ft_utils.execute_command(cmd, verbose=False) + + def check(self): + self.ping_endpoint() + cmd = FUNCTEST_REPO + "ci/check_os.sh" + ft_utils.execute_command(cmd, verbose=False) + + def snapshot_create(self): + self.ping_endpoint() + if os.path.isfile(OS_DEFAULTS_FILE): + answer = raw_input("It seems there is already an OpenStack " + "snapshot. Do you want to overwrite it with " + "the current OpenStack status? [y|n]\n") + while True: + if answer.lower() in ["y", "yes"]: + break + elif answer.lower() in ["n", "no"]: + return + else: + answer = raw_input("Invalid answer. Please type [y|n]\n") + + click.echo("Generating Openstack snapshot...") + gen_def.main() + + def snapshot_show(self): + if not os.path.isfile(OS_DEFAULTS_FILE): + click.echo("There is no OpenStack snapshot created. To create " + "one run the command 'functest env os-create-snapshot'") + return + with open(OS_DEFAULTS_FILE, 'r') as yaml_file: + click.echo("\n%s" + % yaml_file.read()) + + def clean(self): + self.ping_endpoint() + if not os.path.isfile(OS_DEFAULTS_FILE): + click.echo("Not possible to clean OpenStack without a snapshot. " + "This could cause problems. " + "Run first the command 'os-create-shapshot'.") + return + clean_os.main() diff --git a/cli/commands/cli_testcase.py b/cli/commands/cli_testcase.py index 71bfeddcf..d8557b962 100644 --- a/cli/commands/cli_testcase.py +++ b/cli/commands/cli_testcase.py @@ -8,17 +8,50 @@ # import click +import os +import yaml + +import functest.ci.tier_builder as tb +import functest.utils.functest_utils as ft_utils + +""" global variables """ +with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f: + functest_yaml = yaml.safe_load(f) + +REPOS_DIR = os.getenv('repos_dir') +FUNCTEST_REPO = ("%s/functest/" % REPOS_DIR) +FUNCTEST_CONF_DIR = functest_yaml.get("general").get( + "directories").get("dir_functest_conf") +ENV_FILE = FUNCTEST_CONF_DIR + "/env_active" class CliTestcase: def __init__(self): - pass + CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE') + CI_SCENARIO = os.getenv('DEPLOY_SCENARIO') + testcases = FUNCTEST_REPO + "/ci/testcases.yaml" + self.tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, testcases) def list(self): - click.echo("testcase list") + summary = "" + for tier in self.tiers.get_tiers(): + for test in tier.get_tests(): + summary += (" %s\n" % test.get_name()) + click.echo(summary) def show(self, testname): - click.echo("testcase show %s" % testname) + description = self.tiers.get_test(testname) + if description is None: + click.echo("The test case '%s' does not exist or is not supported." + % testname) + + click.echo(description) def run(self, testname): - click.echo("testcase run %s" % testname) + if not os.path.isfile(ENV_FILE): + click.echo("Functest environment is not ready. " + "Run first 'functest env prepare'") + else: + cmd = ("python /home/opnfv/repos/functest/ci/run_tests.py -t %s" + % testname) + ft_utils.execute_command(cmd) diff --git a/cli/commands/cli_tier.py b/cli/commands/cli_tier.py index efb8ad5f2..a872eb76f 100644 --- a/cli/commands/cli_tier.py +++ b/cli/commands/cli_tier.py @@ -8,17 +8,63 @@ # import click +import os +import yaml + +import functest.ci.tier_builder as tb +import functest.utils.functest_utils as ft_utils + +""" global variables """ +with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f: + functest_yaml = yaml.safe_load(f) + +REPOS_DIR = os.getenv('repos_dir') +FUNCTEST_REPO = ("%s/functest/" % REPOS_DIR) +FUNCTEST_CONF_DIR = functest_yaml.get("general").get( + "directories").get("dir_functest_conf") +ENV_FILE = FUNCTEST_CONF_DIR + "/env_active" class CliTier: def __init__(self): - pass + CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE') + CI_SCENARIO = os.getenv('DEPLOY_SCENARIO') + testcases = FUNCTEST_REPO + "/ci/testcases.yaml" + self.tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, testcases) def list(self): - click.echo("tier list") + summary = "" + for tier in self.tiers.get_tiers(): + summary += (" - %s. %s:\n\t %s\n" + % (tier.get_order(), + tier.get_name(), + tier.get_test_names())) + click.echo(summary) def show(self, tiername): - click.echo("tier show %s" % tiername) + tier = self.tiers.get_tier(tiername) + if tier is None: + tier_names = self.tiers.get_tier_names() + click.echo("The tier with name '%s' does not exist. " + "Available tiers are:\n %s\n" % (tiername, tier_names)) + else: + click.echo(self.tiers.get_tier(tiername)) + + def gettests(self, tiername): + tier = self.tiers.get_tier(tiername) + if tier is None: + tier_names = self.tiers.get_tier_names() + click.echo("The tier with name '%s' does not exist. " + "Available tiers are:\n %s\n" % (tiername, tier_names)) + else: + tests = tier.get_test_names() + click.echo("Test cases in tier '%s':\n %s\n" % (tiername, tests)) def run(self, tiername): - click.echo("tier run %s" % tiername) + if not os.path.isfile(ENV_FILE): + click.echo("Functest environment is not ready. " + "Run first 'functest env prepare'") + else: + cmd = ("python /home/opnfv/repos/functest/ci/run_tests.py -t %s" + % tiername) + ft_utils.execute_command(cmd) diff --git a/cli/functest-complete.sh b/cli/functest-complete.sh new file mode 100644 index 000000000..f01490713 --- /dev/null +++ b/cli/functest-complete.sh @@ -0,0 +1,8 @@ +_functest_completion() { + COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \ + COMP_CWORD=$COMP_CWORD \ + _FUNCTEST_COMPLETE=complete $1 ) ) + return 0 +} + +complete -F _functest_completion -o default functest; diff --git a/docker/Dockerfile b/docker/Dockerfile index 2ea30d5b2..8f5758ce2 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -110,5 +110,5 @@ RUN cd ${repos_dir}/promise && npm install RUN /bin/echo -e "set nocompatible\nset backspace=2" >> /home/opnfv/.vimrc RUN echo "set nocompatible" >> /home/opnfv/.exrc -RUN echo "alias ll='ls -lh'" >> /home/opnfv/.bashrc +RUN echo -e "alias ll='ls -lh'\n. /home/opnfv/repos/functest/cli/functest-complete.sh" >> /home/opnfv/.bashrc RUN cd ${repos_dir}/functest/cli && pip install . diff --git a/utils/clean_openstack.py b/utils/clean_openstack.py index 4ad5d5bb0..b3c0d351d 100644 --- a/utils/clean_openstack.py +++ b/utils/clean_openstack.py @@ -37,34 +37,14 @@ import functest.utils.openstack_utils as os_utils logger = ft_logger.Logger("clean_openstack").getLogger() REPO_PATH = os.environ['repos_dir'] + '/functest/' - DEFAULTS_FILE = '/home/opnfv/functest/conf/os_defaults.yaml' -try: - with open(DEFAULTS_FILE) as f: - defaults_yaml = yaml.safe_load(f) -except Exception, e: - logger.info("The file %s does not exist. Please run generate_defaults.py " - "to create the OpenStack defaults. " - "Aborting cleanup..." % DEFAULTS_FILE) - exit(0) - -default_images = defaults_yaml.get('images') -default_instances = defaults_yaml.get('instances') -default_volumes = defaults_yaml.get('volumes') -default_networks = defaults_yaml.get('networks') -default_routers = defaults_yaml.get('routers') -default_security_groups = defaults_yaml.get('secgroups') -default_floatingips = defaults_yaml.get('floatingips') -default_users = defaults_yaml.get('users') -default_tenants = defaults_yaml.get('tenants') - def separator(): logger.info("-------------------------------------------") -def remove_instances(nova_client): +def remove_instances(nova_client, default_instances): logger.info("Removing Nova instances...") instances = os_utils.get_instances(nova_client) if instances is None or len(instances) == 0: @@ -93,7 +73,7 @@ def remove_instances(nova_client): time.sleep(1) -def remove_images(nova_client): +def remove_images(nova_client, default_images): logger.info("Removing Glance images...") images = os_utils.get_images(nova_client) if images is None or len(images) == 0: @@ -117,7 +97,7 @@ def remove_images(nova_client): "NOT be deleted.") -def remove_volumes(cinder_client): +def remove_volumes(cinder_client, default_volumes): logger.info("Removing Cinder volumes...") volumes = os_utils.get_volumes(cinder_client) if volumes is None or len(volumes) == 0: @@ -146,7 +126,7 @@ def remove_volumes(cinder_client): "NOT be deleted.") -def remove_floatingips(nova_client): +def remove_floatingips(nova_client, default_floatingips): logger.info("Removing floating IPs...") floatingips = os_utils.get_floating_ips(nova_client) if floatingips is None or len(floatingips) == 0: @@ -182,7 +162,7 @@ def remove_floatingips(nova_client): time.sleep(1) -def remove_networks(neutron_client): +def remove_networks(neutron_client, default_networks, default_routers): logger.info("Removing Neutron objects") network_ids = [] networks = os_utils.get_network_list(neutron_client) @@ -216,7 +196,7 @@ def remove_networks(neutron_client): if routers is None: logger.debug("There are no routers in the deployment. ") else: - remove_routers(neutron_client, routers) + remove_routers(neutron_client, routers, default_routers) # remove networks if network_ids is not None: @@ -277,7 +257,7 @@ def force_remove_port(neutron_client, port_id): % port_id) -def remove_routers(neutron_client, routers): +def remove_routers(neutron_client, routers, default_routers): for router in routers: router_id = router['id'] router_name = router['name'] @@ -304,7 +284,7 @@ def remove_routers(neutron_client, routers): "router '%s'(%s)..." % (router_name, router_id)) -def remove_security_groups(neutron_client): +def remove_security_groups(neutron_client, default_security_groups): logger.info("Removing Security groups...") secgroups = os_utils.get_security_groups(neutron_client) if secgroups is None or len(secgroups) == 0: @@ -327,7 +307,7 @@ def remove_security_groups(neutron_client): "be deleted.") -def remove_users(keystone_client): +def remove_users(keystone_client, default_users): logger.info("Removing Users...") users = os_utils.get_users(keystone_client) if users is None: @@ -350,7 +330,7 @@ def remove_users(keystone_client): "NOT be deleted.") -def remove_tenants(keystone_client): +def remove_tenants(keystone_client, default_tenants): logger.info("Removing Tenants...") tenants = os_utils.get_tenants(keystone_client) if tenants is None: @@ -377,6 +357,25 @@ def main(): logger.info("+++++++++++++++++++++++++++++++") logger.info("Cleaning OpenStack resources...") logger.info("+++++++++++++++++++++++++++++++") + + try: + with open(DEFAULTS_FILE) as f: + defaults_yaml = yaml.safe_load(f) + except Exception: + logger.info("The file %s does not exist. The OpenStack snapshot must" + " be created first. Aborting cleanup." % DEFAULTS_FILE) + exit(0) + + default_images = defaults_yaml.get('images') + default_instances = defaults_yaml.get('instances') + default_volumes = defaults_yaml.get('volumes') + default_networks = defaults_yaml.get('networks') + default_routers = defaults_yaml.get('routers') + default_security_groups = defaults_yaml.get('secgroups') + default_floatingips = defaults_yaml.get('floatingips') + default_users = defaults_yaml.get('users') + default_tenants = defaults_yaml.get('tenants') + creds_nova = os_utils.get_credentials("nova") nova_client = novaclient.Client('2', **creds_nova) @@ -399,21 +398,21 @@ def main(): "the script again.") exit(-1) - remove_instances(nova_client) + remove_instances(nova_client, default_instances) separator() - remove_images(nova_client) + remove_images(nova_client, default_images) separator() - remove_volumes(cinder_client) + remove_volumes(cinder_client, default_volumes) separator() - remove_floatingips(nova_client) + remove_floatingips(nova_client, default_floatingips) separator() - remove_networks(neutron_client) + remove_networks(neutron_client, default_networks, default_routers) separator() - remove_security_groups(neutron_client) + remove_security_groups(neutron_client, default_security_groups) separator() - remove_users(keystone_client) + remove_users(keystone_client, default_users) separator() - remove_tenants(keystone_client) + remove_tenants(keystone_client, default_tenants) separator() diff --git a/utils/functest_utils.py b/utils/functest_utils.py index 41925fc4a..2d871615a 100644 --- a/utils/functest_utils.py +++ b/utils/functest_utils.py @@ -213,16 +213,18 @@ def execute_command(cmd, logger=None, else: print(msg_exec) p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) - while p.poll() is None: - line = p.stdout.readline().rstrip() - if verbose: - if logger: - if info: - logger.info(line) - else: - logger.debug(line) + while True: + line = p.stdout.readline().replace('\n', '') + if not line: + break + if logger: + if info: + logger.info(line) else: - print line + logger.debug(line) + else: + print line + p.communicate() if p.returncode != 0: if verbose: if logger: -- cgit 1.2.3-korg