Record the test cases passed in tempest
[functest.git] / functest / opnfv_tests / openstack / refstack_client / refstack_client.py
1 #!/usr/bin/env python
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 # matthew.lijun@huawei.com wangwulin@huawei.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 """Refstack client testcase implemenation."""
10
11 from __future__ import division
12
13
14 import argparse
15 import logging
16 import os
17 import re
18 import sys
19 import subprocess
20 import time
21
22 import pkg_resources
23
24 from functest.core import testcase
25 from functest.energy import energy
26 from functest.opnfv_tests.openstack.refstack_client.tempest_conf \
27     import TempestConf
28 from functest.opnfv_tests.openstack.tempest import conf_utils
29 from functest.utils.constants import CONST
30 import functest.utils.functest_utils as ft_utils
31
32 # logging configuration """
33 LOGGER = logging.getLogger(__name__)
34
35
36 class RefstackClient(testcase.OSGCTestCase):
37     """RefstackClient testcase implementation class."""
38
39     def __init__(self, **kwargs):
40         """Initialize RefstackClient testcase object."""
41         if "case_name" not in kwargs:
42             kwargs["case_name"] = "refstack_defcore"
43         super(RefstackClient, self).__init__(**kwargs)
44         self.conf_path = pkg_resources.resource_filename(
45             'functest',
46             'opnfv_tests/openstack/refstack_client/refstack_tempest.conf')
47         self.functest_test = pkg_resources.resource_filename(
48             'functest', 'opnfv_tests')
49         self.defcore_list = 'openstack/refstack_client/defcore.txt'
50         self.confpath = os.path.join(self.functest_test,
51                                      self.conf_path)
52         self.defcorelist = pkg_resources.resource_filename(
53             'functest', 'opnfv_tests/openstack/refstack_client/defcore.txt')
54         self.testlist = None
55         self.insecure = ''
56         if ('https' in CONST.__getattribute__('OS_AUTH_URL') and
57                 CONST.__getattribute__('OS_INSECURE').lower() == 'true'):
58             self.insecure = '-k'
59
60     def run_defcore(self, conf, testlist):
61         """Run defcore sys command."""
62         cmd = ("refstack-client test {0} -c {1} -v --test-list {2}"
63                .format(self.insecure, conf, testlist))
64         LOGGER.info("Starting Refstack_defcore test case: '%s'.", cmd)
65         ft_utils.execute_command(cmd)
66
67     def run_defcore_default(self):
68         """Run default defcore sys command."""
69         options = ["-v"] if not self.insecure else ["-v", self.insecure]
70         cmd = (["refstack-client", "test", "-c", self.confpath] +
71                options + ["--test-list",  self.defcorelist])
72         LOGGER.info("Starting Refstack_defcore test case: '%s'.", cmd)
73
74         with open(os.path.join(conf_utils.REFSTACK_RESULTS_DIR,
75                                "environment.log"), 'w+') as f_env:
76             f_env.write(
77                 ("Refstack environment:\n"
78                  "  SUT: {}\n  Scenario: {}\n  Node: {}\n  Date: {}\n").format(
79                     CONST.__getattribute__('INSTALLER_TYPE'),
80                     CONST.__getattribute__('DEPLOY_SCENARIO'),
81                     CONST.__getattribute__('NODE_NAME'),
82                     time.strftime("%a %b %d %H:%M:%S %Z %Y")))
83
84         with open(os.path.join(conf_utils.REFSTACK_RESULTS_DIR,
85                                "refstack.log"), 'w+') as f_stdout:
86             subprocess.call(cmd, shell=False, stdout=f_stdout,
87                             stderr=subprocess.STDOUT)
88
89     def parse_refstack_result(self):
90         """Parse Refstact results."""
91         try:
92             with open(os.path.join(conf_utils.REFSTACK_RESULTS_DIR,
93                                    "refstack.log"), 'r') as logfile:
94                 output = logfile.read()
95
96             for match in re.findall(r"Ran: (\d+) tests in (\d+\.\d{4}) sec.",
97                                     output):
98                 num_tests = match[0]
99                 LOGGER.info("Ran: %s tests in %s sec.", num_tests, match[1])
100             for match in re.findall(r"(- Passed: )(\d+)", output):
101                 num_success = match[1]
102                 LOGGER.info("".join(match))
103             for match in re.findall(r"(- Skipped: )(\d+)", output):
104                 num_skipped = match[1]
105                 LOGGER.info("".join(match))
106             for match in re.findall(r"(- Failed: )(\d+)", output):
107                 num_failures = match[1]
108                 LOGGER.info("".join(match))
109             success_testcases = []
110             for match in re.findall(r"\{0\}(.*?)[. ]*ok", output):
111                 success_testcases.append(match)
112             failed_testcases = []
113             for match in re.findall(r"\{0\}(.*?)[. ]*FAILED", output):
114                 failed_testcases.append(match)
115             skipped_testcases = []
116             for match in re.findall(r"\{0\}(.*?)[. ]*SKIPPED:", output):
117                 skipped_testcases.append(match)
118
119             num_executed = int(num_tests) - int(num_skipped)
120
121             try:
122                 self.result = 100 * int(num_success) / int(num_executed)
123             except ZeroDivisionError:
124                 LOGGER.error("No test has been executed")
125
126             self.details = {"tests": int(num_tests),
127                             "failures": int(num_failures),
128                             "success": success_testcases,
129                             "errors": failed_testcases,
130                             "skipped": skipped_testcases}
131         except Exception:
132             self.result = 0
133
134         LOGGER.info("Testcase %s success_rate is %s%%",
135                     self.case_name, self.result)
136
137     @energy.enable_recording
138     def run(self, **kwargs):
139         """
140         Start RefstackClient testcase.
141
142         used for functest command line,
143         functest testcase run refstack_defcore
144         """
145         self.start_time = time.time()
146
147         if not os.path.exists(conf_utils.REFSTACK_RESULTS_DIR):
148             os.makedirs(conf_utils.REFSTACK_RESULTS_DIR)
149
150         try:
151             tempestconf = TempestConf()
152             tempestconf.generate_tempestconf()
153             self.run_defcore_default()
154             self.parse_refstack_result()
155             res = testcase.TestCase.EX_OK
156         except Exception:
157             LOGGER.exception("Error with run")
158             res = testcase.TestCase.EX_RUN_ERROR
159
160         self.stop_time = time.time()
161         return res
162
163     def _prep_test(self):
164         """Check that the config file exists."""
165         if not os.path.isfile(self.confpath):
166             LOGGER.error("Conf file not valid: %s", self.confpath)
167         if not os.path.isfile(self.testlist):
168             LOGGER.error("testlist file not valid: %s", self.testlist)
169
170     def main(self, **kwargs):
171         """
172         Execute RefstackClient testcase manually.
173
174         used for manually running,
175            python refstack_client.py -c <tempest_conf_path>
176            --testlist <testlist_path>
177            can generate a reference refstack_tempest.conf by
178            python tempest_conf.py
179         """
180         try:
181             self.confpath = kwargs['config']
182             self.testlist = kwargs['testlist']
183         except KeyError as exc:
184             LOGGER.error("Cannot run refstack client. Please check "
185                          "%s", exc)
186             return self.EX_RUN_ERROR
187         try:
188             self._prep_test()
189             self.run_defcore(self.confpath, self.testlist)
190             res = testcase.TestCase.EX_OK
191         except Exception as exc:
192             LOGGER.error('Error with run: %s', exc)
193             res = testcase.TestCase.EX_RUN_ERROR
194
195         return res
196
197
198 class RefstackClientParser(object):  # pylint: disable=too-few-public-methods
199     """Command line argument parser helper."""
200
201     def __init__(self):
202         """Initialize helper object."""
203         self.functest_test = pkg_resources.resource_filename(
204             'functest', 'opnfv_tests')
205         self.conf_path = pkg_resources.resource_filename(
206             'functest',
207             'opnfv_tests/openstack/refstack_client/refstack_tempest.conf')
208         self.defcore_list = pkg_resources.resource_filename(
209             'functest', 'opnfv_tests/openstack/refstack_client/defcore.txt')
210         self.confpath = os.path.join(self.functest_test,
211                                      self.conf_path)
212         self.defcorelist = os.path.join(self.functest_test,
213                                         self.defcore_list)
214         self.parser = argparse.ArgumentParser()
215         self.parser.add_argument(
216             '-c', '--config',
217             help='the file path of refstack_tempest.conf',
218             default=self.confpath)
219         self.parser.add_argument(
220             '-t', '--testlist',
221             help='Specify the file path or URL of a test list text file. '
222                  'This test list will contain specific test cases that '
223                  'should be tested.',
224             default=self.defcorelist)
225
226     def parse_args(self, argv=None):
227         """Parse command line arguments."""
228         return vars(self.parser.parse_args(argv))
229
230
231 def main():
232     """Run RefstackClient testcase with CLI."""
233     logging.basicConfig()
234     refstackclient = RefstackClient()
235     parser = RefstackClientParser()
236     args = parser.parse_args(sys.argv[1:])
237     try:
238         result = refstackclient.main(**args)
239         if result != testcase.TestCase.EX_OK:
240             return result
241     except Exception:
242         return testcase.TestCase.EX_RUN_ERROR