diff options
author | Shubham Mishra <shivam828787@gmail.com> | 2020-07-22 15:56:25 +0530 |
---|---|---|
committer | Shubham Mishra <shivam828787@gmail.com> | 2020-08-25 21:54:25 +0530 |
commit | dcdd46fe975c13c56a246ba0a6c79516a6afda77 (patch) | |
tree | 3a45f7cdf9a8b14c4c1dba63058ea108d9d48dc1 /hdv/redfish | |
parent | 1bf4bd7a74febb5f837f606940a17c48530eebbd (diff) |
add pytest for main engine and integrated testapi
Change-Id: I9fffd0e04758ddbd0fd05a6358dd4e162c9c8e05
Signed-off-by: Shubham Mishra <shivam828787@gmail.com>
Diffstat (limited to 'hdv/redfish')
-rw-r--r-- | hdv/redfish/conf/cases.yaml | 2 | ||||
-rw-r--r-- | hdv/redfish/conftest.py | 19 | ||||
-rw-r--r-- | hdv/redfish/hdv_redfish.py | 149 | ||||
-rw-r--r-- | hdv/redfish/log_utils.py | 2 | ||||
-rw-r--r-- | hdv/redfish/test/test_hdv_redfish.py | 8 | ||||
-rw-r--r-- | hdv/redfish/test_api.py | 58 |
6 files changed, 169 insertions, 69 deletions
diff --git a/hdv/redfish/conf/cases.yaml b/hdv/redfish/conf/cases.yaml index 3a64ebc..1fb53fe 100644 --- a/hdv/redfish/conf/cases.yaml +++ b/hdv/redfish/conf/cases.yaml @@ -1,6 +1,6 @@ --- - case_name: get asset code - case_sn: 1 + case_sn: 0 enabled: false expected_code: 200 expected_result: diff --git a/hdv/redfish/conftest.py b/hdv/redfish/conftest.py new file mode 100644 index 0000000..9bc7bcb --- /dev/null +++ b/hdv/redfish/conftest.py @@ -0,0 +1,19 @@ +import pytest +from hdv_redfish import read_yaml + +def pytest_addoption(parser): + parser.addoption( + "--cases", action="store", default="./conf/cases.yaml", help="case yaml file" + ) + parser.addoption( + "--config", action="store", default="./conf/config.yaml", help="given global config.yaml file" + ) + +@pytest.fixture(scope = 'session') +def conf_file(request): + return request.config.getoption("--config") + +def pytest_generate_tests(metafunc): + if "case" in metafunc.fixturenames: + cases_file = metafunc.config.getoption("--cases") + metafunc.parametrize("case", read_yaml(cases_file))
\ No newline at end of file diff --git a/hdv/redfish/hdv_redfish.py b/hdv/redfish/hdv_redfish.py index b965f9f..f0aaa4b 100644 --- a/hdv/redfish/hdv_redfish.py +++ b/hdv/redfish/hdv_redfish.py @@ -12,6 +12,7 @@ an implementation of hardware delivery validation based on redfish interface. import time import os import re +import pytest from re import DOTALL as DT import json import copy @@ -22,11 +23,13 @@ from http_handler import UrllibHttpHandler, HEADERS # pylint: disable=E0611 from log_utils import BASE_DIR, LOG_FILE, LOGGER from errors import ERROR_CODE, WARN_CODE +from test_api import PushResults LOGGER.info(BASE_DIR) ACCOUNT_INFO = {} WAIT_INTERVAL = 5 +cases_result = [] def parse_config(config_yaml): @@ -144,7 +147,6 @@ def compare_data(value, flag): flag += 1 return "Failure, expect value: " + str(value[0]) + \ ", return value: " + str(value[1]), flag - elif isinstance(value, list): for elem in enumerate(value, start=0): index = elem[0] @@ -167,7 +169,8 @@ def get_component_ids_yaml(file): return None return yaml.load(open(file, "r")) -def create_real_url(url_value, id_dict, config_file, key_flag_dict, http_handler, bmc_ip): + +def create_real_url(url_value, id_dict, key_flag_dict, http_handler, bmc_ip): ''' create the real url either a static url, or a replaced url by depended_id @@ -182,8 +185,8 @@ def create_real_url(url_value, id_dict, config_file, key_flag_dict, http_handler for match in matches: value = match.groupdict() - #stripping out value['var'] from end of the URL - parent_url = match.group().rstrip('{' +value['var']+ '}') + # stripping out value['var'] from end of the URL + parent_url = match.group().rstrip('{' + value['var'] + '}') if value['var'] in id_dict: replaced = 1 @@ -197,9 +200,11 @@ def create_real_url(url_value, id_dict, config_file, key_flag_dict, http_handler for index in range(len(url_list)): url_list[index] = url_list[index] + parent_url - response_list = handle_depend_url("GET", url_list, http_handler, bmc_ip) - - url_list = create_obj_id_list(key_flag_dict[value['var']], response_list) + response_list = handle_depend_url( + "GET", url_list, http_handler, bmc_ip) + + url_list = create_obj_id_list( + key_flag_dict[value['var']], response_list) if url_list is None or url_list.__len__() == 0: LOGGER.error("%s,%s", ERROR_CODE['E300003'], value['var']) @@ -209,10 +214,10 @@ def create_real_url(url_value, id_dict, config_file, key_flag_dict, http_handler else: replaced = 3 LOGGER.error("%s for parameter %s", - ERROR_CODE['E300002'], value['var']) - + ERROR_CODE['E300002'], value['var']) + LOGGER.debug('url_list content is %s', url_list) - + # combine single case with list case together. if replaced == 0: LOGGER.info("adding static url %s into list", url_value) @@ -220,7 +225,7 @@ def create_real_url(url_value, id_dict, config_file, key_flag_dict, http_handler for index in range(len(url_list)): url_list[index] = url_list[index] + url_value.split('}')[-1] - + LOGGER.debug("created real url list is %s", url_list) return url_list @@ -298,6 +303,7 @@ def create_obj_id_list(key_flags, response_list): LOGGER.error("%s %s", ERROR_CODE['E400003'], key_flags) return end_id_list + def execute_post_url(body, handler, url): ''' execute post url @@ -425,7 +431,6 @@ def parse_test_result(expect_return_value, expect_return_code, exp_act_pairs[key] = \ (value, "Can't find key {} in return value".format(key)) LOGGER.debug("real_result:%s", exp_act_pairs) - # comparing expected result with real result. if exp_act_pairs: for key, value in exp_act_pairs.items(): @@ -434,58 +439,21 @@ def parse_test_result(expect_return_value, expect_return_code, return return_value_list, return_code_list, final_result, flag -def execute_final_url(config_file, depends_id, http_handler, - method, url, req_body, key_flag_dict, bmc_ip): +def execute_final_url(depends_id, http_handler, method, + url, req_body, key_flag_dict, bmc_ip): ''' execute final url to get the request result ''' - url_list = create_real_url(url, depends_id, config_file, key_flag_dict, http_handler, bmc_ip) - rsp_list = handle_final_url(bmc_ip, method, url_list, req_body, http_handler) + url_list = create_real_url( + url, depends_id, key_flag_dict, http_handler, bmc_ip) + rsp_list = handle_final_url( + bmc_ip, method, url_list, req_body, http_handler) return rsp_list -def run_test_case_yaml(config_file, case_file, depends_id, http_handler, bmc_ip): - '''run test case from cases.yaml - ''' - LOGGER.info("############### start perform test case #################") - cases_result = [] - cases = read_yaml(case_file) - for case in cases: - if(case['enabled'] is False): - LOGGER.debug("skipping case: %s", case["case_name"]) - continue - - LOGGER.debug("running case: %s", case["case_name"]) - method, url, req_body, expected_code, expected_value, tc_name, key_flag_dict \ - = case['method'], case['url'], case['request_body'], \ - case['expected_code'], case['expected_result'], case['case_name'], case['key_flag_dict'] - - flag = 0 - final_rst = {} - rsp_list = execute_final_url(config_file, depends_id, - http_handler, method, url, req_body, key_flag_dict, bmc_ip) - if rsp_list is not None and len(rsp_list) > 0: - return_value_list, return_code_list, final_rst, flag = \ - parse_test_result( - expected_value, expected_code, rsp_list, final_rst) - final_rst.update({'info': return_value_list}) - LOGGER.debug("return_code_list:%s", return_code_list) - case['return_code_seq'] = str(return_code_list) - else: - LOGGER.error("%s", ERROR_CODE['E600001']) - flag += 1 - case['final_rst'] = "Success" if flag == 0 else "Failure" - case['details_result'] = \ - str(final_rst) if len(final_rst) > 0 else "N/A" - cases_result.append(case) - LOGGER.info("writing test final_rst for case %s", tc_name) - write_result_2_yaml(cases_result) - - LOGGER.info("############### end perform test case ###################") - - def read_yaml(file): - '''read a yaml file + ''' + read a yaml file ''' if not os.path.exists(file): LOGGER.info("%s %s", ERROR_CODE['E400001'], file) @@ -493,6 +461,44 @@ def read_yaml(file): return yaml.load(open(file, "r")) +def test_case_yaml_run(run, case): + ''' + run test case from cases.yaml + ''' + depends_id, http_handler, bmc_ip = run + if(case['enabled'] is False): + cases_result.append(case) + LOGGER.debug("skipping case: %s", case["case_name"]) + pytest.skip() + + LOGGER.debug("running case: %s", case["case_name"]) + method, url, req_body, expected_code, expected_value, tc_name, key_flag_dict \ + = case['method'], case['url'], case['request_body'], \ + case['expected_code'], case['expected_result'], case['case_name'], case['key_flag_dict'] + + flag = 0 + final_rst = {} + rsp_list = execute_final_url( + depends_id, http_handler, method, url, req_body, key_flag_dict, bmc_ip) + if rsp_list is not None and len(rsp_list) > 0: + return_value_list, return_code_list, final_rst, flag = \ + parse_test_result( + expected_value, expected_code, rsp_list, final_rst) + final_rst.update({'info': return_value_list}) + LOGGER.debug("final_rst:%s", final_rst) + LOGGER.debug("return_code_list:%s", return_code_list) + case['return_code_seq'] = str(return_code_list) + else: + LOGGER.error("%s", ERROR_CODE['E600001']) + flag += 1 + case['final_rst'] = "Success" if flag == 0 else "Failure" + case['details_result'] = \ + str(final_rst) if len(final_rst) > 0 else "N/A" + cases_result.append(case) + LOGGER.info("writing test final_rst for case %s", tc_name) + assert flag == 0, final_rst + + def write_result_2_yaml(result): ''' write test result to new report.yaml @@ -502,7 +508,22 @@ def write_result_2_yaml(result): explicit_start=True) -def run(conf_file, case_file=None): +def generate_testapi_result(cases_result): + ''' + convert cases_result from list to dictionary for pushing to testapi + ''' + testapi_result = {} + + for case_result in cases_result: + testapi_result[case_result['case_sn']] = case_result + print(case_result) + LOGGER.info("generated result for testapi %s", testapi_result) + + return testapi_result + + +@pytest.fixture(scope='session') +def run(conf_file): ''' @param conf_file: config.yaml @param case_file: case yaml file @@ -531,12 +552,14 @@ def run(conf_file, case_file=None): id_info_list = None depends_id = {} + LOGGER.info("############### start perform test case #################") - # read the test case sheet and perform test - run_test_case_yaml(config_file, - case_file, depends_id, http_handler, bmc_ip) - + yield depends_id, http_handler, bmc_ip + write_result_2_yaml(cases_result) + LOGGER.info("############### end perform test case ###################") LOGGER.info("done,checking the log %s", LOG_FILE) - + # parse cases_result for testapi and push it + testapi_result = generate_testapi_result(cases_result) + PushResults(testapi_result, LOGGER) return True diff --git a/hdv/redfish/log_utils.py b/hdv/redfish/log_utils.py index 996a1d1..ed5a013 100644 --- a/hdv/redfish/log_utils.py +++ b/hdv/redfish/log_utils.py @@ -12,7 +12,7 @@ define the LOGGER settings import logging import sys -BASE_DIR = sys.path[0] +BASE_DIR = sys.path[0]+"/hdv/redfish" LOG_FILE = BASE_DIR + "/" + "logs" + "/" + 'hdv.log' LOGGER = logging.getLogger("redfish") diff --git a/hdv/redfish/test/test_hdv_redfish.py b/hdv/redfish/test/test_hdv_redfish.py index 078f2b7..ac951a4 100644 --- a/hdv/redfish/test/test_hdv_redfish.py +++ b/hdv/redfish/test/test_hdv_redfish.py @@ -36,7 +36,7 @@ def test_create_real_url(sampleCase, expected_list): bmc_ip = "" depends_id = {} http_handler = UrllibHttpHandler() - url_list = create_real_url(url, depends_id, None, key_flag_dict, http_handler, bmc_ip) + url_list = create_real_url(url, depends_id, key_flag_dict, http_handler, bmc_ip) assert expected_list == url_list @@ -49,7 +49,7 @@ def test_execute_final_url(sampleCase): depends_id = {} http_handler = UrllibHttpHandler() config_file = {} - rsp_list = execute_final_url(config_file, depends_id, + rsp_list = execute_final_url(depends_id, http_handler, method, url, req_body, key_flag_dict, bmc_ip) assert mock_server[url] == rsp_list @@ -71,7 +71,7 @@ def test_parse_result(sampleCase, expected_return_value_list): depends_id = {} http_handler = UrllibHttpHandler() config_file = {} - rsp_list = execute_final_url(config_file, depends_id, + rsp_list = execute_final_url(depends_id, http_handler, method, url, req_body, key_flag_dict, bmc_ip) return_value_list, return_code_list, final_rst, flag = \ @@ -98,7 +98,7 @@ def test_parse_data(sampleCase, expected_act_pairs_list): depends_id = {} http_handler = UrllibHttpHandler() config_file = {} - rsp_list = execute_final_url(config_file, depends_id, + rsp_list = execute_final_url(depends_id, http_handler, method, url, req_body, key_flag_dict, bmc_ip) for i in range (0,len(rsp_list)): diff --git a/hdv/redfish/test_api.py b/hdv/redfish/test_api.py new file mode 100644 index 0000000..7d0eeec --- /dev/null +++ b/hdv/redfish/test_api.py @@ -0,0 +1,58 @@ +from datetime import datetime as dt +import requests + +OPNFV_URL = "http://testresults.opnfv.org/test/api/v1" +POD_NAME = 'intel-pod10' +INSTALLER = 'none' +BUILD_TAG = "none" +PKG_LIST = 'package-list.mk' +START_TIME = dt.now().strftime('%Y-%m-%d %H:%M:%S') +STOP_TIME = dt.now().strftime('%Y-%m-%d %H:%M:%S') +TC_NAME = 'HDV_Redfish_Basic' +VERSION = '1.0' +CRITERIA = 'PASS' + +class PushResults(): + """ Push results to opnfv test api """ + def __init__(self, results, logger): + """ constructor """ + # store external values + self.results = results + self.logger = logger + # initialize internal values + self.push_vals = dict() + # call functions + self.generate_response() + self.push_results() + + def generate_response(self): + """ generate json output to be pushed """ + # Build body + body = { + "project_name": "cirv", + "scenario": "none", + "start_date": START_TIME, + "stop_date": STOP_TIME, + "case_name": TC_NAME, + "pod_name": POD_NAME, + "installer": INSTALLER, + "version": VERSION, + "build_tag": BUILD_TAG, + "criteria": CRITERIA, + "details": self.results + } + + self.logger.debug("The generated json response to be pushed:%s", body) + # store this value in the class variable + self.push_vals = body + + def push_results(self): + """ push results to testapi """ + url = OPNFV_URL + "/results" + print(self.push_vals) + try: + response = requests.post(url, json=self.push_vals) + self.logger.info("testapi push response:%s", response) + except ConnectionError: + self.logger.exception("error while pushing results to testapi") + self.logger.error("failed to push results") |