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
15 from functest.core import testcase
16 from functest.opnfv_tests.openstack.tempest import conf_utils
17 from functest.utils import openstack_utils
18 from functest.utils.constants import CONST
19 import functest.utils.functest_logger as ft_logger
20 import functest.utils.functest_utils as ft_utils
22 """ logging configuration """
23 logger = ft_logger.Logger("refstack_defcore").getLogger()
26 class RefstackClient(testcase.TestCase):
29 super(RefstackClient, self).__init__()
30 self.case_name = "refstack_defcore"
31 self.FUNCTEST_TEST = CONST.dir_functest_test
32 self.CONF_PATH = CONST.refstack_tempest_conf_path
33 self.DEFCORE_LIST = CONST.refstack_defcore_list
34 self.confpath = os.path.join(self.FUNCTEST_TEST,
36 self.defcorelist = os.path.join(self.FUNCTEST_TEST,
38 self.VERIFIER_ID = conf_utils.get_verifier_id()
39 self.VERIFIER_REPO_DIR = conf_utils.get_verifier_repo_dir(
41 self.DEPLOYMENT_ID = conf_utils.get_verifier_deployment_id()
42 self.DEPLOYMENT_DIR = conf_utils.get_verifier_deployment_dir(
43 self.VERIFIER_ID, self.DEPLOYMENT_ID)
45 def source_venv(self):
48 ". .venv/bin/activate;"
49 "cd -;".format(CONST.dir_refstack_client))
50 ft_utils.execute_command(cmd)
52 def run_defcore(self, conf, testlist):
53 logger.debug("Generating test case list...")
56 "./refstack-client test -c {1} -v --test-list {2};"
57 "cd -;".format(CONST.dir_refstack_client,
60 ft_utils.execute_command(cmd)
62 def run_defcore_default(self):
63 logger.debug("Generating test case list...")
66 "./refstack-client test -c {1} -v --test-list {2};"
67 "cd -;".format(CONST.dir_refstack_client,
70 logger.info("Starting Refstack_defcore test case: '%s'." % cmd)
72 header = ("Refstack environment:\n"
73 " SUT: %s\n Scenario: %s\n Node: %s\n Date: %s\n" %
74 (CONST.INSTALLER_TYPE,
75 CONST.DEPLOY_SCENARIO,
77 time.strftime("%a %b %d %H:%M:%S %Z %Y")))
80 os.path.join(conf_utils.REFSTACK_RESULTS_DIR,
81 "refstack.log"), 'w+')
82 f_env = open(os.path.join(conf_utils.REFSTACK_RESULTS_DIR,
83 "environment.log"), 'w+')
86 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
87 stderr=subprocess.STDOUT, bufsize=1)
90 for line in iter(p.stdout.readline, b''):
93 if re.search("\} tempest\.", line):
94 logger.info(line.replace('\n', ''))
101 def parse_refstack_result(self):
103 with open(os.path.join(conf_utils.REFSTACK_RESULTS_DIR,
104 "refstack.log"), 'r') as logfile:
105 output = logfile.read()
107 for match in re.findall("Ran: (\d+) tests in (\d+\.\d{4}) sec.",
110 logger.info("Ran: %s tests in %s sec." % (num_tests, match[1]))
111 for match in re.findall("(- Passed: )(\d+)", output):
112 num_success = match[1]
113 logger.info("".join(match))
114 for match in re.findall("(- Skipped: )(\d+)", output):
115 num_skipped = match[1]
116 logger.info("".join(match))
117 for match in re.findall("(- Failed: )(\d+)", output):
118 num_failures = match[1]
119 logger.info("".join(match))
120 success_testcases = ""
121 for match in re.findall(r"\{0\}(.*?)[. ]*ok", output):
122 success_testcases += match + ", "
123 failed_testcases = ""
124 for match in re.findall(r"\{0\}(.*?)[. ]*FAILED", output):
125 failed_testcases += match + ", "
126 skipped_testcases = ""
127 for match in re.findall(r"\{0\}(.*?)[. ]*SKIPPED:", output):
128 skipped_testcases += match + ", "
130 num_executed = int(num_tests) - int(num_skipped)
131 success_rate = 100 * int(num_success) / int(num_executed)
133 self.details = {"tests": int(num_tests),
134 "failures": int(num_failures),
135 "success": success_testcases,
136 "errors": failed_testcases,
137 "skipped": skipped_testcases}
141 self.criteria = ft_utils.check_success_rate(
142 self.case_name, success_rate)
143 logger.info("Testcase %s success_rate is %s%%, is marked as %s"
144 % (self.case_name, success_rate, self.criteria))
146 def defcore_env_prepare(self):
148 img_flavor_dict = conf_utils.create_tempest_resources(
149 use_custom_images=True, use_custom_flavors=True)
150 conf_utils.configure_tempest_defcore(
151 self.DEPLOYMENT_DIR, img_flavor_dict)
153 res = testcase.TestCase.EX_OK
154 except KeyError as e:
155 logger.error("defcore prepare env error with: %s", e)
156 res = testcase.TestCase.EX_RUN_ERROR
161 self.start_time = time.time()
163 if not os.path.exists(conf_utils.REFSTACK_RESULTS_DIR):
164 os.makedirs(conf_utils.REFSTACK_RESULTS_DIR)
167 self.defcore_env_prepare()
168 self.run_defcore_default()
169 self.parse_refstack_result()
170 res = testcase.TestCase.EX_OK
171 except Exception as e:
172 logger.error('Error with run: %s', e)
173 res = testcase.TestCase.EX_RUN_ERROR
175 self.stop_time = time.time()
178 def main(self, **kwargs):
180 tempestconf = kwargs['config']
181 testlist = kwargs['testlist']
182 except KeyError as e:
183 logger.error("Cannot run refstack client. Please check "
185 return self.EX_RUN_ERROR
187 openstack_utils.source_credentials(CONST.openstack_creds)
188 self.defcore_env_prepare()
189 self.run_defcore(tempestconf, testlist)
190 res = testcase.TestCase.EX_OK
191 except Exception as e:
192 logger.error('Error with run: %s', e)
193 res = testcase.TestCase.EX_RUN_ERROR
198 class RefstackClientParser(testcase.TestCase):
201 super(RefstackClientParser, self).__init__()
202 self.FUNCTEST_TEST = CONST.dir_functest_test
203 self.CONF_PATH = CONST.refstack_tempest_conf_path
204 self.DEFCORE_LIST = CONST.refstack_defcore_list
205 self.confpath = os.path.join(self.FUNCTEST_TEST,
207 self.defcorelist = os.path.join(self.FUNCTEST_TEST,
209 self.parser = argparse.ArgumentParser()
210 self.parser.add_argument(
212 help='the file path of tempest.conf',
213 default=self.confpath)
214 self.parser.add_argument(
216 help='Specify the file path or URL of a test list text file. '
217 'This test list will contain specific test cases that '
219 default=self.defcorelist)
221 def parse_args(self, argv=[]):
222 return vars(self.parser.parse_args(argv))
225 if __name__ == '__main__':
226 refstackclient = RefstackClient()
227 parser = RefstackClientParser()
228 args = parser.parse_args(sys.argv[1:])
230 result = refstackclient.main(**args)
231 if result != testcase.TestCase.EX_OK:
234 sys.exit(testcase.TestCase.EX_RUN_ERROR)