Merge "Re-Enable Promise testcases"
[functest.git] / functest / cli / commands / cli_env.py
1 #!/usr/bin/env python
2 #
3 # jose.lausuch@ericsson.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9
10 import os
11
12 import click
13 import git
14
15 from functest.utils.constants import CONST
16 import functest.utils.functest_utils as ft_utils
17
18
19 class CliEnv(object):
20
21     def __init__(self):
22         pass
23
24     def prepare(self):
25         if self.status(verbose=False) == 0:
26             answer = raw_input("It seems that the environment has been "
27                                "already prepared. Do you want to do "
28                                "it again? [y|n]\n")
29             while True:
30                 if answer.lower() in ["y", "yes"]:
31                     os.remove(CONST.__getattribute__('env_active'))
32                     break
33                 elif answer.lower() in ["n", "no"]:
34                     return
35                 else:
36                     answer = raw_input("Invalid answer. Please type [y|n]\n")
37
38         cmd = ("python %s/functest/ci/prepare_env.py start" %
39                CONST.__getattribute__('dir_repo_functest'))
40         ft_utils.execute_command(cmd)
41
42     def show(self):
43         def _get_value(attr, default='Unknown'):
44             return attr if attr else default
45
46         install_type = _get_value(CONST.__getattribute__('INSTALLER_TYPE'))
47         installer_ip = _get_value(CONST.__getattribute__('INSTALLER_IP'))
48         installer_info = ("%s, %s" % (install_type, installer_ip))
49         scenario = _get_value(CONST.__getattribute__('DEPLOY_SCENARIO'))
50         node = _get_value(CONST.__getattribute__('NODE_NAME'))
51         repo_h = git.Repo(CONST.__getattribute__('dir_repo_functest')).head
52         if repo_h.is_detached:
53             git_branch = 'detached from FETCH_HEAD'
54             git_hash = repo_h.commit.hexsha
55         else:
56             branch = repo_h.reference
57             git_branch = branch.name
58             git_hash = branch.commit.hexsha
59         is_debug = _get_value(CONST.__getattribute__('CI_DEBUG'), 'false')
60         build_tag = CONST.__getattribute__('BUILD_TAG')
61         if build_tag is not None:
62             build_tag = build_tag.lstrip(
63                 "jenkins-").lstrip("functest").lstrip("-")
64
65         STATUS = "not ready"
66         if self.status(verbose=False) == 0:
67             STATUS = "ready"
68
69         click.echo("+======================================================+")
70         click.echo("| Functest Environment info                            |")
71         click.echo("+======================================================+")
72         click.echo("|  INSTALLER: %s|" % installer_info.ljust(41))
73         click.echo("|   SCENARIO: %s|" % scenario.ljust(41))
74         click.echo("|        POD: %s|" % node.ljust(41))
75         click.echo("| GIT BRACNH: %s|" % git_branch.ljust(41))
76         click.echo("|   GIT HASH: %s|" % git_hash.ljust(41))
77         if build_tag:
78             click.echo("|  BUILD TAG: %s|" % build_tag.ljust(41))
79         click.echo("| DEBUG FLAG: %s|" % is_debug.ljust(41))
80         click.echo("+------------------------------------------------------+")
81         click.echo("|     STATUS: %s|" % STATUS.ljust(41))
82         click.echo("+------------------------------------------------------+")
83         click.echo("")
84
85     def status(self, verbose=True):
86         ret_val = 0
87         if not os.path.isfile(CONST.__getattribute__('env_active')):
88             if verbose:
89                 click.echo("Functest environment is not installed.\n")
90             ret_val = 1
91         elif verbose:
92             click.echo("Functest environment ready to run tests.\n")
93
94         return ret_val