diff options
author | jose.lausuch <jose.lausuch@ericsson.com> | 2016-05-06 01:36:30 +0200 |
---|---|---|
committer | jose.lausuch <jose.lausuch@ericsson.com> | 2016-05-09 16:58:04 +0200 |
commit | bd00e6f289a69badf7beea827b20764ed3252c7a (patch) | |
tree | 77cfd39903e5ebf9ea492737a499ef57346cd3f1 /cli | |
parent | 073ca5525f38d7a7c9c67252d1b996e895ff9917 (diff) |
CLI implementation
JIRA: FUNCTEST-243
Change-Id: Ibf0ef9bcc5f3aeda96b050827b954ce060317613
Signed-off-by: jose.lausuch <jose.lausuch@ericsson.com>
Diffstat (limited to 'cli')
-rw-r--r-- | cli/cli_base.py | 94 | ||||
-rw-r--r-- | cli/commands/cli_env.py | 91 | ||||
-rw-r--r-- | cli/commands/cli_os.py | 120 | ||||
-rw-r--r-- | cli/commands/cli_testcase.py | 41 | ||||
-rw-r--r-- | cli/commands/cli_tier.py | 54 | ||||
-rw-r--r-- | cli/functest-complete.sh | 8 |
6 files changed, 360 insertions, 48 deletions
diff --git a/cli/cli_base.py b/cli/cli_base.py index e8c1d194..25696dbe 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() @@ -25,6 +36,12 @@ def env(ctx): @cli.group() @click.pass_context +def openstack(ctx): + pass + + +@cli.group() +@click.pass_context def testcase(ctx): pass @@ -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 b2570d37..9e6372d4 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 00000000..23b7beaa --- /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 71bfeddc..d8557b96 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 efb8ad5f..a872eb76 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 00000000..f0149071 --- /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; |