# 异步回调函数参数错误返回 SCHEDULE_CALLBACK_DATA_ERROR_OUTPUTS = { "ex_data": "invalid callback_data, job_instance_id: None, status: None" } # 异步回调函数成功输出 SCHEDULE_SUCCESS_OUTPUTS = {"name": "value"} # 上传文件成功异步执行失败样例 LOCAL_CONTENT_UPLOAD_SUCCESS_SCHEDULE_CALLBACK_DATA_ERROR_CASE = ComponentTestCase( name="local content upload success schedule callback data error test case", inputs=INPUTS, parent_data=PARENT_DATA, execute_assertion=ExecuteAssertion(success=True, outputs=SUCCESS_OUTPUTS), schedule_assertion=ScheduleAssertion( success=True, schedule_finished=True, outputs=dict(list(SUCCESS_OUTPUTS.items())), ), execute_call_assertion=[ CallAssertion( func=LOCAL_CONTENT_UPLOAD_SUCCESS_CLIENT.job.push_config_file, calls=[Call(KWARGS)]), ], patchers=[ Patcher( target=CC_GET_IPS_INFO_BY_STR, return_value={"ip_result": []}, ), Patcher(target=GET_CLIENT_BY_USER, return_value=LOCAL_CONTENT_UPLOAD_SUCCESS_CLIENT), Patcher(target=GET_JOB_INSTANCE_URL,
outputs={ "job_inst_url": "instance_url_token", "job_inst_id": 56789, "job_inst_name": "job_name_token", "client": INVALID_CALLBACK_DATA_CLIENT, }, ), schedule_assertion=ScheduleAssertion( success=False, outputs={ "job_inst_url": "instance_url_token", "job_inst_id": 56789, "job_inst_name": "job_name_token", "client": INVALID_CALLBACK_DATA_CLIENT, "ex_data": "invalid callback_data, " "job_instance_id: None, status: None", }, callback_data={}, ), execute_call_assertion=[ CallAssertion( func=CC_GET_IPS_INFO_BY_STR, calls=[ Call(username="******", biz_cc_id=1, ip_str="1.1.1.1,2.2.2.2",
"job_inst_id": 10000, "job_inst_name": "API execute_job_plan test", "client": EXECUTE_JOB_PLAN_SUCCESS_CASE_CLIENT, "biz_cc_id": 2, }, ), schedule_assertion=ScheduleAssertion( success=True, outputs={ "job_inst_url": "instance_url_token", "job_inst_id": 10000, "job_inst_name": "API execute_job_plan test", "client": EXECUTE_JOB_PLAN_SUCCESS_CASE_CLIENT, "biz_cc_id": 2, "job_tagged_ip_dict": { "tag2": "192.168.20.218" }, "name": "test", "log_outputs": {}, }, callback_data={ "job_instance_id": 10000, "status": 3 }, ), execute_call_assertion=[ CallAssertion( func=EXECUTE_JOB_PLAN_SUCCESS_CASE_CLIENT.jobv3.execute_job_plan, calls=[ Call({ "bk_scope_type":
def SUCCESS_CASE(): SUCCESS_RESULT = {'result': True, 'data': {'job_id': 12345}} SUCCESS_ESB_CLIENT = MagicMock() SUCCESS_MANAGER = MagicMock() SUCCESS_MANAGER.push_files_to_ips = MagicMock(return_value=SUCCESS_RESULT) return ComponentTestCase( name='push_local_files success case', inputs={ 'biz_cc_id': 'biz_cc_id', 'job_local_files': [{ 'tag': 'tag_1' }, { 'tag': 'tag_2' }], 'job_target_ip_list': 'job_target_ip_list', 'job_target_account': 'job_target_account', 'job_target_path': 'job_target_path' }, parent_data={ 'executor': 'executor', 'project_id': 'project_id' }, execute_assertion=ExecuteAssertion( success=True, outputs={ 'job_inst_id': SUCCESS_RESULT['data']['job_id'], 'job_inst_url': 'url_token' }), schedule_assertion=ScheduleAssertion( success=True, outputs={ 'job_inst_id': SUCCESS_RESULT['data']['job_id'], 'job_inst_url': 'url_token' }, callback_data={ 'status': 3, 'job_instance_id': 12345 }, schedule_finished=True), execute_call_assertion=[ CallAssertion(func=GET_CLIENT_BY_USER, calls=[Call('executor')]), CallAssertion( func=CC_GET_IPS_INFO_BY_STR, calls=[Call('executor', 'biz_cc_id', 'job_target_ip_list')]), CallAssertion(func=SUCCESS_MANAGER.push_files_to_ips, calls=[ Call(esb_client=SUCCESS_ESB_CLIENT, bk_biz_id='biz_cc_id', file_tags=['tag_1', 'tag_2'], target_path='job_target_path', ips=[{ 'ip': '1.1.1.1', 'bk_cloud_id': 0 }], account='job_target_account', callback_url='callback_url') ]) ], patchers=[ Patcher(target=ENVIRONMENT_VAR_GET, return_value='a_type'), Patcher(target=FACTORY_GET_MANAGER, return_value=SUCCESS_MANAGER), Patcher(target=GET_CLIENT_BY_USER, return_value=SUCCESS_ESB_CLIENT), Patcher(target=CC_GET_IPS_INFO_BY_STR, return_value={ 'ip_result': [{ 'InnerIP': '1.1.1.1', 'Source': 0 }] }), Patcher(target=GET_NODE_CALLBACK_URL, return_value='callback_url'), Patcher(target=GET_JOB_INSTANCE_URL, return_value='url_token') ])
parent_data=COMMON_PARENT, execute_assertion=ExecuteAssertion( success=True, outputs={ "module_update_success": [{ "cc_module_select_text": "set>module", "bk_module_name": "test", "bk_module_type": "", "operator": "", "bk_bak_operator": "", }], "module_update_failed": [], }, ), schedule_assertion=ScheduleAssertion(success=True, schedule_finished=True, outputs={}), patchers=[ Patcher(target=GET_CLIENT_BY_USER, return_value=UPDATE_MODULE_SUCCESS_CLIENT), Patcher(target=CC_GET_CLIENT_BY_USER, return_value=UPDATE_MODULE_SUCCESS_CLIENT), ], ) UPDATE_MODULE_FAILED_BY_CUSTOM = ComponentTestCase( name="update module failed by custom", inputs={ "cc_tag_method": "custom", "cc_module_update_data": [{
# ------------------------------------------------ HTTP_CALL_REQUEST_ERR_CASE = ComponentTestCase( name="http call request error case", inputs={ "bk_http_request_method": "method_token", "bk_http_request_url": "url_token", "bk_http_request_body": "body_token", "bk_http_request_header": [], "bk_http_success_exp": "exp_token", "bk_http_timeout": 0, }, parent_data={}, execute_assertion=ExecuteAssertion(success=True, outputs={}), schedule_assertion=ScheduleAssertion( success=False, outputs={"ex_data": u"请求异常,详细信息: exc_token1"}), schedule_call_assertion=[ CallAssertion( func=HTTP_REQUEST, calls=[ Call( method="method_token", url="url_token", verify=False, data="body_token".encode("utf-8"), timeout=60, headers={"Content-type": "application/json"}, ) ], ) ],
def CALLBACK_FAIL_CASE(): CALLBACK_FAIL_RESULT = {'result': True, 'data': {'job_id': 12345}} CALLBACK_FAIL_ESB_CLIENT = MagicMock() CALLBACK_FAIL_MANAGER = MagicMock() CALLBACK_FAIL_MANAGER.push_files_to_ips = MagicMock( return_value=CALLBACK_FAIL_RESULT) return ComponentTestCase( name='push_local_files callback fail case', inputs={ 'biz_cc_id': 'biz_cc_id', 'job_local_files': [{ 'tag': 'tag_1' }, { 'tag': 'tag_2' }], 'job_target_ip_list': 'job_target_ip_list', 'job_target_account': 'job_target_account', 'job_target_path': 'job_target_path' }, parent_data={ 'executor': 'executor', 'project_id': 'project_id' }, execute_assertion=ExecuteAssertion( success=True, outputs={ 'job_inst_id': CALLBACK_FAIL_RESULT['data']['job_id'], 'job_inst_url': 'url_token' }), schedule_assertion=ScheduleAssertion( success=False, outputs={ 'job_inst_id': CALLBACK_FAIL_RESULT['data']['job_id'], 'job_inst_url': 'url_token', 'ex_data': { 'exception_msg': u"任务执行失败,<a href='{}' target='_blank'>" u"前往作业平台(JOB)查看详情</a>".format('url_token'), 'show_ip_log': True, 'task_inst_id': 12345 } }, callback_data={ 'status': 4, 'job_instance_id': 12345 }, schedule_finished=False), execute_call_assertion=[ CallAssertion(func=GET_CLIENT_BY_USER, calls=[Call('executor')]), CallAssertion( func=CC_GET_IPS_INFO_BY_STR, calls=[Call('executor', 'biz_cc_id', 'job_target_ip_list')]), CallAssertion(func=CALLBACK_FAIL_MANAGER.push_files_to_ips, calls=[ Call(esb_client=CALLBACK_FAIL_ESB_CLIENT, bk_biz_id='biz_cc_id', file_tags=['tag_1', 'tag_2'], target_path='job_target_path', ips=[{ 'ip': '1.1.1.1', 'bk_cloud_id': 0 }], account='job_target_account', callback_url='callback_url') ]) ], patchers=[ Patcher(target=ENVIRONMENT_VAR_GET, return_value='a_type'), Patcher(target=FACTORY_GET_MANAGER, return_value=CALLBACK_FAIL_MANAGER), Patcher(target=GET_CLIENT_BY_USER, return_value=CALLBACK_FAIL_ESB_CLIENT), Patcher(target=CC_GET_IPS_INFO_BY_STR, return_value={ 'ip_result': [{ 'InnerIP': '1.1.1.1', 'Source': 0 }] }), Patcher(target=GET_NODE_CALLBACK_URL, return_value='callback_url'), Patcher(target=GET_JOB_INSTANCE_URL, return_value='url_token') ])
outputs={ 'job_inst_url': 'instance_url_token', 'job_inst_id': 56789, 'job_inst_name': 'job_name_token', 'client': INVALID_CALLBACK_DATA_CLIENT }), schedule_assertion=ScheduleAssertion( success=False, outputs={ 'job_inst_url': 'instance_url_token', 'job_inst_id': 56789, 'job_inst_name': 'job_name_token', 'client': INVALID_CALLBACK_DATA_CLIENT, 'ex_data': 'invalid callback_data, ' 'job_instance_id: None, status: None' }, callback_data={}), execute_call_assertion=[ CallAssertion(func=CC_GET_IPS_INFO_BY_STR, calls=[ Call(username='******', biz_cc_id=1, ip_str='1.1.1.1,2.2.2.2', use_cache=False) ]),
def SUCCESS_CASE(): SUCCESS_RESULT = {"result": True, "data": {"job_id": 12345}} SUCCESS_ESB_CLIENT = MagicMock() SUCCESS_MANAGER = MagicMock() SUCCESS_MANAGER.push_files_to_ips = MagicMock(return_value=SUCCESS_RESULT) return ComponentTestCase( name="push_local_files success case", inputs={ "biz_cc_id": "biz_cc_id", "job_local_files": [ { "response": { "result": True, "tag": "tag_1" } }, { "response": { "result": True, "tag": "tag_2" } }, ], "job_target_ip_list": "1.1.1.1", "job_target_account": "job_target_account", "job_target_path": "job_target_path", }, parent_data={ "executor": "executor", "project_id": "project_id" }, execute_assertion=ExecuteAssertion( success=True, outputs={ "job_inst_id": SUCCESS_RESULT["data"]["job_id"], "job_inst_url": "url_token" }), schedule_assertion=ScheduleAssertion( success=True, outputs={ "job_inst_id": SUCCESS_RESULT["data"]["job_id"], "job_inst_url": "url_token" }, callback_data={ "status": 3, "job_instance_id": 12345 }, schedule_finished=True, ), execute_call_assertion=[ CallAssertion(func=GET_CLIENT_BY_USER, calls=[Call("executor")]), CallAssertion( func=CC_GET_IPS_INFO_BY_STR, calls=[ Call(username="******", biz_cc_id="biz_cc_id", ip_str="1.1.1.1", use_cache=False) ], ), CallAssertion( func=SUCCESS_MANAGER.push_files_to_ips, calls=[ Call( esb_client=SUCCESS_ESB_CLIENT, bk_biz_id="biz_cc_id", file_tags=["tag_1", "tag_2"], target_path="job_target_path", ips=[{ "ip": "1.1.1.1", "bk_cloud_id": 0 }], account="job_target_account", callback_url="callback_url", ) ], ), ], patchers=[ Patcher(target=ENVIRONMENT_VAR_GET, return_value="a_type"), Patcher(target=FACTORY_GET_MANAGER, return_value=SUCCESS_MANAGER), Patcher(target=GET_CLIENT_BY_USER, return_value=SUCCESS_ESB_CLIENT), Patcher(target=GET_NODE_CALLBACK_URL, return_value="callback_url"), Patcher(target=GET_JOB_INSTANCE_URL, return_value="url_token"), Patcher(target=CC_GET_IPS_INFO_BY_STR, return_value={ "ip_result": [{ "InnerIP": "1.1.1.1", "Source": 0 }] }), ], )
"job_inst_url": "instance_url_token", "client": FAST_EXECUTE_SCRIPT_SUCCESS_CLIENT, } # 异步回调函数参数错误返回 SCHEDULE_CALLBACK_DATA_ERROR_OUTPUTS = {"ex_data": "invalid callback_data, job_instance_id: None, status: None"} # 脚本成功异步执行失败样例 FAST_EXECUTE_MANUAL_SCRIPT_SUCCESS_SCHEDULE_CALLBACK_DATA_ERROR_CASE = ComponentTestCase( name="all biz fast execute script success schedule callback data error test case", inputs=MANUAL_INPUTS, parent_data=PARENT_DATA, execute_assertion=ExecuteAssertion(success=True, outputs=MANUAL_SUCCESS_OUTPUTS), schedule_assertion=ScheduleAssertion( success=False, outputs=dict(list(MANUAL_SUCCESS_OUTPUTS.items()) + list(SCHEDULE_CALLBACK_DATA_ERROR_OUTPUTS.items())), callback_data={}, ), execute_call_assertion=[ CallAssertion(func=FAST_EXECUTE_SCRIPT_SUCCESS_CLIENT.job.fast_execute_script, calls=[Call(MANUAL_KWARGS)]), ], patchers=[ Patcher(target=GET_NODE_CALLBACK_URL, return_value=GET_NODE_CALLBACK_URL_MOCK()), Patcher(target=GET_CLIENT_BY_USER, return_value=FAST_EXECUTE_SCRIPT_SUCCESS_CLIENT), Patcher(target=GET_JOB_INSTANCE_URL, return_value="instance_url_token"), ], ) # 脚本失执行败样例 FAST_EXECUTE_SCRIPT_FAIL_CASE = ComponentTestCase( name="all biz fast execute script fail test case",
'os_type': 'LINUX', 'has_cygwin': False, 'port': '22', 'account': 'test', 'auth_type': 'PASSWORD', 'password': '******', 'key': '' }] }, parent_data={ 'executor': 'tester', 'biz_cc_id': "1" }, execute_assertion=ExecuteAssertion(success=True, outputs={'job_id': '1'}), schedule_assertion=ScheduleAssertion(success=True, callback_data=None, schedule_finished=True, outputs={'job_id': '1'}), execute_call_assertion=[ CallAssertion(func=CREATE_TASK_SUCCESS_CLIENT.nodeman.create_task, calls=[ Call({ 'bk_biz_id': '1', 'bk_cloud_id': '1', 'node_type': 'AGENT', 'op_type': 'INSTALL', 'creator': 'tester',
"port": "22", "account": "test", "auth_type": "PASSWORD", "auth_key": "123", }], }, parent_data={ "executor": "tester", "biz_cc_id": "1" }, execute_assertion=ExecuteAssertion(success=True, outputs={"job_id": "1"}), schedule_assertion=ScheduleAssertion( success=True, callback_data=None, schedule_finished=True, outputs={ "fail_num": 0, "job_id": "1", "success_num": 1 }, ), execute_call_assertion=[ CallAssertion( func=CREATE_TASK_SUCCESS_CLIENT.create_task, calls=[ Call( **{ "bk_biz_id": "1", "bk_cloud_id": "1", "node_type":
} # 异步回调函数成功输出 SCHEDULE_SUCCESS_OUTPUTS = {"name": "value"} # 手动输入脚本成功异步执行失败样例 FAST_EXECUTE_MANUAL_SCRIPT_SUCCESS_SCHEDULE_CALLBACK_DATA_ERROR_CASE = ComponentTestCase( name= "fast execute manual script success schedule callback data error test case", inputs=MANUAL_INPUTS, parent_data=PARENT_DATA, execute_assertion=ExecuteAssertion(success=True, outputs=MANUAL_SUCCESS_OUTPUTS), schedule_assertion=ScheduleAssertion( success=False, outputs=dict( list(MANUAL_SUCCESS_OUTPUTS.items()) + list(SCHEDULE_CALLBACK_DATA_ERROR_OUTPUTS.items())), callback_data={}, ), execute_call_assertion=[ CallAssertion( func=FAST_EXECUTE_SCRIPT_SUCCESS_CLIENT.job.fast_execute_script, calls=[Call(MANUAL_KWARGS)]), ], patchers=[ Patcher(target=GET_NODE_CALLBACK_URL, return_value=GET_NODE_CALLBACK_URL_MOCK()), Patcher(target=GET_CLIENT_BY_USER, return_value=FAST_EXECUTE_SCRIPT_SUCCESS_CLIENT), Patcher(target=GET_JOB_INSTANCE_URL, return_value="instance_url_token"),
def PUSH_FILE_TO_IPS_FAIL_CASE(): return ComponentTestCase( name="all buz fast_push_files v1.0 call fail case", inputs=INPUT, parent_data={ "executor": "executor", "project_id": "project_id" }, execute_assertion=ExecuteAssertion( success=True, outputs={ "requests_error": "Request Error:\nfailed\n", "task_count": 4, "job_instance_id_list": [10000, 10001, 10002], "job_id_of_batch_execute": [10000, 10001, 10002], "job_inst_url": [ "job.com/api_execute/", "job.com/api_execute/", "job.com/api_execute/" ], "request_success_count": 3, "success_count": 0, "final_res": False, }, ), execute_call_assertion=[ CallAssertion( func=FAST_PUSH_FILE_REQUEST_FAILURE_CLIENT.job.fast_push_file, calls=[ Call(**CLL_INFO()), Call(**CLL_INFO()), Call(**CLL_INFO()), Call(**CLL_INFO()) ], ), ], schedule_assertion=ScheduleAssertion( success=False, outputs={ "requests_error": "Request Error:\nfailed\n", "task_count": 4, "job_instance_id_list": [10000, 10001, 10002], "job_id_of_batch_execute": [], "job_inst_url": [ "job.com/api_execute/", "job.com/api_execute/", "job.com/api_execute/" ], "request_success_count": 3, "success_count": 3, "final_res": False, "ex_data": "Request Error:\nfailed\n\n Get Result Error:\n", }, schedule_finished=True, ), patchers=[ Patcher(target=GET_CLIENT_BY_USER, return_value=FAST_PUSH_FILE_REQUEST_FAILURE_CLIENT), Patcher(target=BASE_GET_CLIENT_BY_USER, return_value=FAST_PUSH_FILE_REQUEST_FAILURE_CLIENT), Patcher(target=JOB_HANDLE_API_ERROR, return_value="failed"), Patcher(target=GET_JOB_INSTANCE_URL, return_value="job.com/api_execute/"), ], )
CREATE_APPROVE_TICKET_SUCCESS_CASE = ComponentTestCase( name="create approve ticket success case", inputs=INPUTS, parent_data=COMMON_PARENT, execute_assertion=ExecuteAssertion(success=True, outputs={"sn": "NO2019090519542603"}), execute_call_assertion=[ CallAssertion( func=CREAT_TICKET_SUCCESS_CLIENT.create_ticket, calls=[Call(**CREAT_TICKET_CALL)], ) ], schedule_assertion=ScheduleAssertion( success=True, outputs={ "approve_result": "通过", "sn": "NO2019090519542603" }, callback_data=CALLBACK_URL_SUCCESS_RETURN, ), patchers=[ Patcher(target=GET_CLIENT_BY_USER, return_value=CREAT_TICKET_SUCCESS_CLIENT), Patcher(target=BK_HANDLE_API_ERROR, return_value=""), Patcher(target=GET_NODE_CALLBACK_URL, return_value="callback_url"), ], ) BLOCKED_INPUTS = { "bk_verifier": "tester, tester1", "bk_approve_title": "this is a test", "bk_notify_title": "title", "bk_approve_content": "test content",
}, }, parent_data={"executor": "tester"}, execute_assertion=ExecuteAssertion( success=True, outputs={ "job_url": ["{}/#/task-history/1/log/host|instance|host|1".format(settings.BK_NODEMAN_HOST)], "job_id": "1", }, ), schedule_assertion=ScheduleAssertion( success=True, callback_data=None, schedule_finished=True, outputs={ "job_url": ["{}/#/task-history/1/log/host|instance|host|1".format(settings.BK_NODEMAN_HOST)], "job_id": "1", "success_num": 1, "fail_num": 0, }, ), execute_call_assertion=[ CallAssertion( func=INSTALL_OR_OPERATE_SUCCESS_CLIENT.plugin_operate, calls=[ Call( { "job_type": "MAIN_INSTALL_PLUGIN", "bk_biz_id": ["1"], "bk_host_id": [1], "plugin_params": {"name": "plugin", "version": "plugin_version", "keep_config": 1},
Patcher(target=PLUGIN_SERVICE_API_CLIENT, return_value=ONLY_EXECUTE_AND_SUCCESS_CLIENT) ], ) # both execute and schedule success EXECUTE_AND_SCHEDULE_SUCCESS_CASE = ComponentTestCase( name="Both execute and schedule success case", inputs=BASE_INPUTS, parent_data=PARENT_DATA, execute_assertion=ExecuteAssertion(success=True, outputs={**TRACE_ID_OUTPUTS}), schedule_assertion=ScheduleAssertion(success=True, outputs={ **TRACE_ID_OUTPUTS, **BASE_OUTPUTS }, callback_data={}, schedule_finished=True), patchers=[ Patcher(target=PLUGIN_SERVICE_API_CLIENT, return_value=EXECUTE_AND_SCHEDULE_SUCCESS_CLIENT) ], ) # execute fail because invoke api fail INVOKE_API_FAIL_CASE = ComponentTestCase( name="execute fail because invoke api fail", inputs=BASE_INPUTS, parent_data=PARENT_DATA, execute_assertion=ExecuteAssertion(success=False,