diff options
author | SerenaFeng <feng.xiaowei@zte.com.cn> | 2016-12-22 10:53:36 +0800 |
---|---|---|
committer | SerenaFeng <feng.xiaowei@zte.com.cn> | 2016-12-22 19:28:12 +0800 |
commit | 2632ba86dd920267455ee10154b9e2ce4695a889 (patch) | |
tree | 32a9c96d6b5bb5db69d750976938faaa68695635 /functest/cli/commands | |
parent | 62ffd8a7e6790ab0377550740d3cfe8eea298392 (diff) |
refactor cli module using new constants provider
JIRA: FUNCTEST-673
Change-Id: I643fb16c694a8d7df45a13237f34b19a02906881
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'functest/cli/commands')
-rw-r--r-- | functest/cli/commands/cli_env.py | 66 | ||||
-rw-r--r-- | functest/cli/commands/cli_os.py | 45 | ||||
-rw-r--r-- | functest/cli/commands/cli_testcase.py | 19 | ||||
-rw-r--r-- | functest/cli/commands/cli_tier.py | 18 |
4 files changed, 67 insertions, 81 deletions
diff --git a/functest/cli/commands/cli_env.py b/functest/cli/commands/cli_env.py index 9f793e71..5228c3e8 100644 --- a/functest/cli/commands/cli_env.py +++ b/functest/cli/commands/cli_env.py @@ -12,8 +12,8 @@ import os import click import git +from functest.utils.constants import CONST import functest.utils.functest_utils as ft_utils -import functest.utils.functest_constants as ft_constants class CliEnv: @@ -28,7 +28,7 @@ class CliEnv: "it again? [y|n]\n") while True: if answer.lower() in ["y", "yes"]: - os.remove(ft_constants.ENV_FILE) + os.remove(CONST.env_active) break elif answer.lower() in ["n", "no"]: return @@ -36,40 +36,28 @@ class CliEnv: answer = raw_input("Invalid answer. Please type [y|n]\n") cmd = ("python %s/functest/ci/prepare_env.py start" % - ft_constants.FUNCTEST_REPO_DIR) + CONST.dir_repo_functest) ft_utils.execute_command(cmd) def show(self): - CI_INSTALLER_TYPE = ft_constants.CI_INSTALLER_TYPE - if CI_INSTALLER_TYPE is None: - CI_INSTALLER_TYPE = "Unknown" - CI_INSTALLER_IP = ft_constants.CI_INSTALLER_IP - if CI_INSTALLER_IP is None: - CI_INSTALLER_IP = "Unknown" - CI_INSTALLER = ("%s, %s" % (CI_INSTALLER_TYPE, CI_INSTALLER_IP)) - - CI_SCENARIO = ft_constants.CI_SCENARIO - if CI_SCENARIO is None: - CI_SCENARIO = "Unknown" - - CI_NODE = ft_constants.CI_NODE - if CI_NODE is None: - CI_NODE = "Unknown" - - repo = git.Repo(ft_constants.FUNCTEST_REPO_DIR) + def _get_value(attr, default='Unknown'): + return attr if attr else default + + install_type = _get_value(CONST.INSTALLER_TYPE) + installer_ip = _get_value(CONST.INSTALLER_IP) + installer_info = ("%s, %s" % (install_type, installer_ip)) + scenario = _get_value(CONST.DEPLOY_SCENARIO) + node = _get_value(CONST.NODE_NAME) + repo = git.Repo(CONST.dir_repo_functest) branch = repo.head.reference - GIT_BRANCH = branch.name - GIT_HASH = branch.commit.hexsha - - CI_BUILD_TAG = ft_constants.CI_BUILD_TAG - if CI_BUILD_TAG is not None: - CI_BUILD_TAG = CI_BUILD_TAG.lstrip( + git_branch = branch.name + git_hash = branch.commit.hexsha + is_debug = _get_value(CONST.CI_DEBUG, 'false') + build_tag = CONST.BUILD_TAG + if build_tag is not None: + build_tag = build_tag.lstrip( "jenkins-").lstrip("functest").lstrip("-") - CI_DEBUG = ft_constants.CI_DEBUG - if CI_DEBUG is None: - CI_DEBUG = "false" - STATUS = "not ready" if self.status(verbose=False) == 0: STATUS = "ready" @@ -77,14 +65,14 @@ class CliEnv: 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("| INSTALLER: %s|" % installer_info.ljust(41)) + click.echo("| SCENARIO: %s|" % scenario.ljust(41)) + click.echo("| POD: %s|" % node.ljust(41)) + click.echo("| GIT BRACNH: %s|" % git_branch.ljust(41)) + click.echo("| GIT HASH: %s|" % git_hash.ljust(41)) + if build_tag: + click.echo("| BUILD TAG: %s|" % build_tag.ljust(41)) + click.echo("| DEBUG FLAG: %s|" % is_debug.ljust(41)) click.echo("+------------------------------------------------------+") click.echo("| STATUS: %s|" % STATUS.ljust(41)) click.echo("+------------------------------------------------------+") @@ -92,7 +80,7 @@ class CliEnv: def status(self, verbose=True): ret_val = 0 - if not os.path.isfile(ft_constants.ENV_FILE): + if not os.path.isfile(CONST.env_active): if verbose: click.echo("Functest environment is not installed.\n") ret_val = 1 diff --git a/functest/cli/commands/cli_os.py b/functest/cli/commands/cli_os.py index bb859219..aeb34974 100644 --- a/functest/cli/commands/cli_os.py +++ b/functest/cli/commands/cli_os.py @@ -12,23 +12,21 @@ import os import click +from functest.utils.constants import CONST import functest.utils.functest_utils as ft_utils import functest.utils.openstack_clean as os_clean import functest.utils.openstack_snapshot as os_snapshot -import functest.utils.functest_constants as ft_constants - - -OPENSTACK_RC_FILE = ft_constants.OPENSTACK_CREDS -OPENSTACK_SNAPSHOT_FILE = ft_constants.OPENSTACK_SNAPSHOT_FILE class CliOpenStack: def __init__(self): - self.os_auth_url = ft_constants.OS_AUTH_URL + self.os_auth_url = CONST.OS_AUTH_URL self.endpoint_ip = None self.endpoint_port = None - if self.os_auth_url is not None: + self.openstack_creds = CONST.openstack_creds + self.snapshot_file = CONST.openstack_snapshot_file + if self.os_auth_url: self.endpoint_ip = self.os_auth_url.rsplit("/")[2].rsplit(":")[0] self.endpoint_port = self.os_auth_url.rsplit("/")[2].rsplit(":")[1] @@ -43,13 +41,14 @@ class CliOpenStack: click.echo("Cannot talk to the endpoint %s\n" % self.endpoint_ip) exit(0) - def show_credentials(self): + @staticmethod + def show_credentials(): for key, value in os.environ.items(): if key.startswith('OS_'): click.echo("{}={}".format(key, value)) def fetch_credentials(self): - if os.path.isfile(OPENSTACK_RC_FILE): + if os.path.isfile(self.openstack_creds): answer = raw_input("It seems the RC file is already present. " "Do you want to overwrite it? [y|n]\n") while True: @@ -60,31 +59,31 @@ class CliOpenStack: else: answer = raw_input("Invalid answer. Please type [y|n]\n") - CI_INSTALLER_TYPE = ft_constants.CI_INSTALLER_TYPE - if CI_INSTALLER_TYPE is None: + installer_type = CONST.INSTALLER_TYPE + if installer_type is None: click.echo("The environment variable 'INSTALLER_TYPE' is not" "defined. Please export it") - CI_INSTALLER_IP = ft_constants.CI_INSTALLER_IP - if CI_INSTALLER_IP is None: + installer_ip = CONST.INSTALLER_IP + if installer_ip is None: click.echo("The environment variable 'INSTALLER_IP' is not" "defined. Please export it") cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s" - % (ft_constants.REPOS_DIR, - OPENSTACK_RC_FILE, - CI_INSTALLER_TYPE, - CI_INSTALLER_IP)) + % (CONST.dir_repos, + self.openstack_creds, + installer_type, + installer_ip)) click.echo("Fetching credentials from installer node '%s' with IP=%s.." - % (CI_INSTALLER_TYPE, CI_INSTALLER_IP)) + % (installer_type, installer_ip)) ft_utils.execute_command(cmd, verbose=False) def check(self): self.ping_endpoint() - cmd = ft_constants.FUNCTEST_REPO_DIR + "/functest/ci/check_os.sh" + cmd = CONST.dir_repo_functest + "/functest/ci/check_os.sh" ft_utils.execute_command(cmd, verbose=False) def snapshot_create(self): self.ping_endpoint() - if os.path.isfile(OPENSTACK_SNAPSHOT_FILE): + if os.path.isfile(self.snapshot_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") @@ -100,18 +99,18 @@ class CliOpenStack: os_snapshot.main() def snapshot_show(self): - if not os.path.isfile(OPENSTACK_SNAPSHOT_FILE): + if not os.path.isfile(self.snapshot_file): click.echo("There is no OpenStack snapshot created. To create " "one run the command " "'functest openstack snapshot-create'") return - with open(OPENSTACK_SNAPSHOT_FILE, 'r') as yaml_file: + with open(self.snapshot_file, 'r') as yaml_file: click.echo("\n%s" % yaml_file.read()) def clean(self): self.ping_endpoint() - if not os.path.isfile(OPENSTACK_SNAPSHOT_FILE): + if not os.path.isfile(self.snapshot_file): click.echo("Not possible to clean OpenStack without a snapshot. " "This could cause problems. " "Run first the command " diff --git a/functest/cli/commands/cli_testcase.py b/functest/cli/commands/cli_testcase.py index 70a77a14..d019632a 100644 --- a/functest/cli/commands/cli_testcase.py +++ b/functest/cli/commands/cli_testcase.py @@ -14,19 +14,17 @@ import os import click import functest.ci.tier_builder as tb +from functest.utils.constants import CONST import functest.utils.functest_utils as ft_utils import functest.utils.functest_vacation as vacation -import functest.utils.functest_constants as ft_constants class CliTestcase: def __init__(self): - CI_INSTALLER_TYPE = ft_constants.CI_INSTALLER_TYPE - CI_SCENARIO = ft_constants.CI_SCENARIO - testcases = ft_constants.FUNCTEST_TESTCASES_YAML - - self.tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, testcases) + self.tiers = tb.TierBuilder(CONST.INSTALLER_TYPE, + CONST.DEPLOY_SCENARIO, + CONST.functest_testcases_yaml) def list(self): summary = "" @@ -43,10 +41,11 @@ class CliTestcase: click.echo(description) - def run(self, testname, noclean=False): + @staticmethod + def run(testname, noclean=False): if testname == 'vacation': vacation.main() - elif not os.path.isfile(ft_constants.ENV_FILE): + elif not os.path.isfile(CONST.env_active): click.echo("Functest environment is not ready. " "Run first 'functest env prepare'") else: @@ -54,8 +53,8 @@ class CliTestcase: for test in tests: if noclean: cmd = ("python %s/functest/ci/run_tests.py " - "-n -t %s" % (ft_constants.FUNCTEST_REPO_DIR, test)) + "-n -t %s" % (CONST.dir_repo_functest, test)) else: cmd = ("python %s/functest/ci/run_tests.py " - "-t %s" % (ft_constants.FUNCTEST_REPO_DIR, test)) + "-t %s" % (CONST.dir_repo_functest, test)) ft_utils.execute_command(cmd) diff --git a/functest/cli/commands/cli_tier.py b/functest/cli/commands/cli_tier.py index 9da51072..01208498 100644 --- a/functest/cli/commands/cli_tier.py +++ b/functest/cli/commands/cli_tier.py @@ -14,17 +14,16 @@ import os import click import functest.ci.tier_builder as tb +from functest.utils.constants import CONST import functest.utils.functest_utils as ft_utils -import functest.utils.functest_constants as ft_constants class CliTier: def __init__(self): - CI_INSTALLER_TYPE = ft_constants.CI_INSTALLER_TYPE - CI_SCENARIO = ft_constants.CI_SCENARIO - testcases = ft_constants.FUNCTEST_TESTCASES_YAML - self.tiers = tb.TierBuilder(CI_INSTALLER_TYPE, CI_SCENARIO, testcases) + self.tiers = tb.TierBuilder(CONST.INSTALLER_TYPE, + CONST.DEPLOY_SCENARIO, + CONST.functest_testcases_yaml) def list(self): summary = "" @@ -54,15 +53,16 @@ class CliTier: tests = tier.get_test_names() click.echo("Test cases in tier '%s':\n %s\n" % (tiername, tests)) - def run(self, tiername, noclean=False): - if not os.path.isfile(ft_constants.ENV_FILE): + @staticmethod + def run(tiername, noclean=False): + if not os.path.isfile(CONST.env_active): click.echo("Functest environment is not ready. " "Run first 'functest env prepare'") else: if noclean: cmd = ("python %s/functest/ci/run_tests.py " - "-n -t %s" % (ft_constants.FUNCTEST_REPO_DIR, tiername)) + "-n -t %s" % (CONST.dir_repo_functest, tiername)) else: cmd = ("python %s/functest/ci/run_tests.py " - "-t %s" % (ft_constants.FUNCTEST_REPO_DIR, tiername)) + "-t %s" % (CONST.dir_repo_functest, tiername)) ft_utils.execute_command(cmd) |