def ztp_func_hooks(request): initialize_variables() basic_obj.remove_file(vars.D1, config_params.ztp_local_json_path) basic_obj.copy_config_db_to_temp(vars.D1, config_params.config_db_path, config_params.config_db_temp) basic_obj.change_permissions(vars.D1, config_params.config_db_temp) ztp_obj.enable_ztp_if_disabled(vars.D1) config_params.func_name = st.get_func_name(request) yield ztp_obj.ztp_operations(vars.D1, "disable") basic_obj.copy_config_db_to_temp(vars.D1, config_params.config_db_temp, config_params.config_db_path) config_params.dhcp_config_file = dhcp6_config_file config_params.option_type = "option_59" ztp_obj.clear_options_from_dhcp_server(ssh_conn_obj, config_params)
def ztp_func_hooks(request): initialize_config_params() initialize_data_variables() basic_obj.remove_file(vars.D1, data.ztp_local_json_path) basic_obj.copy_config_db_to_temp(vars.D1, data.config_db_path, data.config_db_temp) basic_obj.change_permissions(vars.D1, data.config_db_temp) ztp_obj.enable_ztp_if_disabled(vars.D1) config_params.func_name = st.get_func_name(request) yield ztp_obj.ztp_operations(vars.D1, "disable") basic_obj.copy_config_db_to_temp(vars.D1, data.config_db_temp, data.config_db_path) config_params.dhcp_config_file = ztp_params.dhcp.inband_config_file if st.get_func_name(request) == "test_ft_ztp_config_section_check_with_option_67_with_inband": config_params.option_type = "option_67" ztp_obj.clear_options_from_dhcp_server(ssh_conn_obj, config_params) config_params.option_type = "option_239" ztp_obj.clear_options_from_dhcp_server(ssh_conn_obj, config_params)
def config_and_verify_option_225(ssh_conn_obj, dut, ztp_params, data, cli_type=""): cli_type = st.get_ui_type(dut, cli_type=cli_type) if data.option_type == "225": if "func_name" in data: syslog_file_names = ["syslog_1_{}".format(data.func_name), "syslog_{}".format(data.func_name)] data.search_pattern = r'\s*option option-225\s*\S*\s*"\S+";' data.option_string = "option option-225 " # "option dhcp6.boot-file-url " data.option_url = data.minigraph_path data.option_type = "option_67" clear_options_from_dhcp_server(ssh_conn_obj, data) data.option_type = "option_239" clear_options_from_dhcp_server(ssh_conn_obj, data) write_option_to_dhcp_server(ssh_conn_obj, data) # write_option_225_to_dhcp_server(ssh_conn_obj, data) basic_obj.service_operations(ssh_conn_obj, data.dhcp_service_name, data.action, data.device) if not verify_dhcpd_service_status(ssh_conn_obj, data.dhcpd_pid): st.log("{} service not running".format(data.dhcp_service_name)) st.report_fail("service_not_running", data.dhcp_service_name) data.device_action = "reboot" if cli_type == "klish" else data.device_action if data.device_action == "reboot": reboot_type = data.reboot_type if "reboot_type" in data and data.reboot_type else "normal" basic_obj.remove_file(dut, data.config_db_path) st.reboot(dut, reboot_type, skip_port_wait=True) st.wait_system_status(dut, 400) elif data.device_action == "run": ztp_operations(dut, data.device_action) if not verify_ztp_status(dut, cli_type=cli_type): if "logs_path" in data and "func_name" in data: capture_syslogs(dut, data.logs_path, syslog_file_names) st.log("ZTP status verification failed") st.report_fail("ztp_status_verification_failed") verify_ztp_filename_logs(dut, data) if "ztp_log_string" in data and data.ztp_log_string: if not basic_obj.poll_for_error_logs(dut, data.ztp_log_path, data.ztp_log_string): st.log("ZTP log {} verification failed for message {}".format(data.ztp_log_path, data.ztp_log_string)) if not basic_obj.poll_for_error_logs(dut, data.ztp_log_path_1, data.ztp_log_string): st.log("ZTP log {} verification failed for message {}".format(data.ztp_log_path_1, data.ztp_log_string)) st.report_fail("ztp_log_verification_failed", data.ztp_log_path_1, data.ztp_log_string)
def ztp_func_hooks(request): initialize_config_params() initialize_data_variables() basic_obj.remove_file(vars.D1, data.ztp_local_json_path) if st.get_func_name( request) == 'test_ft_ztp_behv_invalid_json_and_config_sections': mvrfconfig(vars.D1, cli_type="click") reboot_obj.config_save(vars.D1) basic_obj.copy_config_db_to_temp(vars.D1, data.config_db_path, data.config_db_temp) basic_obj.change_permissions(vars.D1, data.config_db_temp) # st.log("Shutting down the inband interface ...") # intf_obj.interface_shutdown(vars.D1, ztp_params.inband_port) reboot_obj.config_save(vars.D1) ztp_obj.enable_ztp_if_disabled(vars.D1) config_params.func_name = st.get_func_name(request) yield basic_obj.copy_config_db_to_temp(vars.D1, data.config_db_temp, data.config_db_path) ztp_obj.ztp_operations(vars.D1, "disable") ztp_cfg = { "admin-mode": True, "restart-ztp-interval": 30, "feat-console-logging": feat_logging_console } ztp_obj.config_ztp_backdoor_options(vars.D1, ztp_cfg) if st.get_func_name( request) == 'test_ft_ztp_behv_invalid_json_and_config_sections': mvrfconfig(vars.D1, no_form=True, cli_type="click") config_params.dhcp_config_file = ztp_params.dhcp.config_file config_params.option_type = "" if st.get_func_name(request) in functions_67: config_params.option_type = "option_67" elif st.get_func_name(request) in functions_225: config_params.option_type = "option_225" elif st.get_func_name(request) in functions_239: config_params.option_type = "option_239" if config_params.option_type: ztp_obj.clear_options_from_dhcp_server(ssh_conn_obj, config_params)
def config_dhcpv6_options(ssh_conn_obj, ztp_params, config_params, options=dict(), cli_type=""): """ Common function to configure dhcpv6 options and verify the result on both inband and out of band interfaces :param ssh_conn_obj: :param ztp_params: :param config_params: :param options: :return: """ cli_type = st.get_ui_type(config_params.dut, cli_type=cli_type) retry_count = config_params.retry_count if "retry_count" in config_params and config_params.retry_count else 0 iteration = config_params.iteration if "iteration" in config_params and config_params.iteration else 300 delay = config_params.delay if "delay" in config_params and config_params.delay else 3 expect_reboot = True if "expect_reboot" in options and options ["expect_reboot"] else False st.log(config_params) if "func_name" in config_params: syslog_file_names = ["syslog_1_{}".format(config_params.func_name), "syslog_{}".format(config_params.func_name)] if "json_content" in config_params: file_path = basic_obj.write_to_json_file(config_params.json_content) st.log(file_path) if file_path: destination_path = "{}{}/{}".format(config_params.home_path, ztp_params.config_path, config_params.ztp_file) st.log(destination_path) basic_obj.copy_file_from_client_to_server(ssh_conn_obj, src_path=file_path, dst_path=destination_path) config_params.option_59_url = "http://[{}]{}/{}".format(config_params.static_ip, ztp_params.config_path, config_params.ztp_file) config_params.search_pattern = r'\s*option\s+dhcp6.boot-file-url\s+"\S+";' write_option_59_to_dhcp_server(ssh_conn_obj, config_params) basic_obj.service_operations(ssh_conn_obj, config_params.dhcp6_service_name, "restart", "server") if not verify_dhcpd_service_status(ssh_conn_obj, config_params.dhcpd6_pid): st.log("{} service is running which is not expected".format(config_params.dhcp6_service_name)) st.report_fail("service_running_not_expected", config_params.dhcp6_service_name) reboot_type = config_params.reboot_type if "reboot_type" in config_params and config_params.reboot_type else "normal" if "ztp_operation" in config_params: config_params.ztp_operation = "reboot" if cli_type == "klish" else config_params.ztp_operation if config_params.ztp_operation == "reboot": basic_obj.remove_file(config_params.dut, config_params.config_db_path) st.reboot(config_params.dut, reboot_type, skip_port_wait=True) elif config_params.ztp_operation == "run": ztp_operations(config_params.dut, config_params.ztp_operation) else: st.log("ZTP operation is not mentioned hence rebooting the device ...") basic_obj.remove_file(config_params.dut, config_params.config_db_path) st.reboot(config_params.dut, reboot_type, skip_port_wait=True) if "reboot_on_success" in options and options["reboot_on_success"]: result = verify_ztp_status(config_params.dut, retry_count, iteration, delay, expect_reboot=expect_reboot, reboot_on_success=options["reboot_on_success"], cli_type=cli_type) else: result = verify_ztp_status(config_params.dut, retry_count, iteration, delay, expect_reboot=expect_reboot, cli_type=cli_type) if not result: if "logs_path" in config_params and "func_name" in config_params: capture_syslogs(config_params.dut, config_params.logs_path, syslog_file_names) st.log("ZTP status verification failed") st.report_fail("ztp_status_verification_failed") if "reboot_on_success" in options and options["reboot_on_success"]: reboot_obj.config_reload(config_params.dut) st.wait(5) if not ip_obj.ping(config_params.dut, config_params.static_ip, family="ipv6"): st.log("Pinging to DHCP server failed from DUT, issue either with DUT or server") # intf_obj.enable_dhcp_on_interface(config_params.dut, config_params.network_port, "v6") if not verify_ztp_status(config_params.dut, retry_count, iteration, delay, cli_type=cli_type): if "logs_path" in config_params and "func_name" in config_params: capture_syslogs(config_params.dut, config_params.logs_path, syslog_file_names) st.log("ZTP status verification failed") st.report_fail("ztp_status_verification_failed") verify_ztp_filename_logs(config_params.dut, config_params) if "ztp_log_string" in config_params and config_params.ztp_log_string: if not basic_obj.poll_for_error_logs(config_params.dut, config_params.ztp_log_path, config_params.ztp_log_string): st.log("ZTP log {} verification failed for message {}".format(config_params.ztp_log_path, config_params.ztp_log_string)) if not basic_obj.poll_for_error_logs(config_params.dut, config_params.ztp_log_path_1, config_params.ztp_log_string): st.log("ZTP log {} verification failed for message {}".format(config_params.ztp_log_path_1, config_params.ztp_log_string)) st.report_fail("ztp_log_verification_failed", config_params.ztp_log_path_1, config_params.ztp_log_string) if "result" in config_params and config_params.result == "pass": st.report_pass("test_case_passed")
def config_and_verify_dhcp_option(ssh_conn_obj, dut, ztp_params, data, expect_reboot=False, reboot_on_success=list(), cli_type=""): """ Common function to configure DHCP option along with status / logs verification Author: Chaitanya Vella ([email protected]) :param ssh_conn_obj: :param dut: :param ztp_params: :param data: :return: """ cli_type = st.get_ui_type(dut,cli_type=cli_type) cli_type = "klish" if cli_type in ["rest-put", "rest-patch"] else cli_type retry_count = data.retry_count if "retry_count" in data and data.retry_count else 0 iteration = data.iteration if "iteration" in data and data.iteration else 300 delay = data.delay if "delay" in data and data.delay else 3 if "func_name" in data: syslog_file_names = ["syslog_1_{}".format(data.func_name), "syslog_{}".format(data.func_name)] # basic_obj.copy_config_db_to_temp(dut, data.config_db_path, data.config_db_temp) if "config_file_type" in data and data.config_file_type == "text": file_path = "/tmp/file_temp.json" basic_obj.write_to_file(ssh_conn_obj, data.json_content, file_path, device="server") elif "config_file_type" in data and data.config_file_type == "EoL": file_path = "" else: file_path = basic_obj.write_to_json_file(data.json_content) if file_path: destination_path = "{}{}/{}".format(ztp_params.home_path, ztp_params.config_path, data.config_file) basic_obj.copy_file_from_client_to_server(ssh_conn_obj, src_path=file_path, dst_path=destination_path) if "config_db_location" in data and data.config_db_location == "json": st.download_file_from_dut(dut, data.config_db_temp, file_path) destination_path = "{}{}/{}".format(ztp_params.home_path, ztp_params.config_path, data.config_db_file_name) basic_obj.copy_file_from_client_to_server(ssh_conn_obj, src_path=file_path, dst_path=destination_path) if "scenario" in data and data.scenario == "invalid-json": st.log("Writing invalid content to make invalid json ...") basic_obj.write_to_file_to_line(ssh_conn_obj, ",", 5, destination_path, "server") if data.option_type == "67": st.log("Creating {} file on DHCP server ...".format(data.config_file)) data.search_pattern = r'\s*option\s+bootfile-name\s*\S*\s*"\S+";' data.option_string = "option bootfile-name" if data.type == "http": data.option_url = "http://{}{}/{}".format(data.static_ip, data.config_path, data.config_file) elif data.type == "tftp": data.option_url = "tftp://{}/{}/{}".format(data.static_ip, data.config_path, data.config_file) elif data.type == "ftp": data.option_url = "ftp://{}/{}/{}".format(data.static_ip, data.config_path, data.config_file) write_option_to_dhcp_server(ssh_conn_obj, data) basic_obj.service_operations(ssh_conn_obj, data.dhcp_service_name, data.action, data.device) if not verify_dhcpd_service_status(ssh_conn_obj, data.dhcpd_pid): st.log("{} service not running".format(data.dhcp_service_name)) st.report_fail("service_not_running", data.dhcp_service_name) # write_option_67_to_dhcp_server(ssh_conn_obj, data) data.device_action = "reboot" if cli_type == "klish" else data.device_action if data.device_action == "reboot": reboot_type = data.reboot_type if "reboot_type" in data and data.reboot_type else "normal" basic_obj.remove_file(dut, data.config_db_path) st.reboot(dut, reboot_type, skip_port_wait=True) st.wait_system_status(dut, 500) elif data.device_action == "run": ztp_operations(dut, data.device_action) if "band_type" in data and data.band_type=="inband": if not basic_obj.poll_for_system_status(dut): st.log("Sytem is not ready ..") st.report_env_fail("system_not_ready") if not basic_obj.check_interface_status(dut, ztp_params.oob_port,"up"): basic_obj.ifconfig_operation(dut, ztp_params.oob_port, "down") interface_status = basic_obj.check_interface_status(dut, ztp_params.inband_port, "up") if interface_status is not None: if not interface_status: intf_obj.interface_noshutdown(dut, ztp_params.inband_port, cli_type=cli_type) if "service" in data and data.service == "disable": basic_obj.service_operations_by_systemctl(dut, "ztp", "stop") if basic_obj.verify_service_status(dut, "ztp"): st.log("ZTP status is not stopped") st.report_fail("service_not_stopped", "ztp") basic_obj.service_operations_by_systemctl(dut, "ztp", "start") if not poll_ztp_status(dut, ["IN-PROGRESS", "Not Started", "SUCCESS"], cli_type=cli_type): st.report_fail("ztp_max_polling_interval") if "check" in data and data.check == "not": if verify_ztp_status(dut, retry_count, iteration, delay, cli_type=cli_type): if "logs_path" in data and "func_name" in data: capture_syslogs(dut, data.logs_path, syslog_file_names) st.log("ZTP status verification failed") st.report_fail("ztp_status_verification_failed") else: st.log("Iteration count {}".format(iteration)) st.log("REBOOT ON SUCCESS - {}".format(reboot_on_success)) if reboot_on_success: if "configdb-json" in reboot_on_success: st.wait_system_reboot(dut) st.wait_system_status(dut, 300) result = verify_ztp_status(dut, retry_count, iteration, delay, expect_reboot=expect_reboot, reboot_on_success=reboot_on_success, cli_type=cli_type) else: result = verify_ztp_status(dut, retry_count, iteration, delay, expect_reboot=expect_reboot, cli_type=cli_type) if not result: if "logs_path" in data and "func_name" in data: capture_syslogs(dut, data.logs_path, syslog_file_names) st.log("ZTP status verification failed") st.report_fail("ztp_status_verification_failed") if reboot_on_success: output = show_ztp_status(dut, cli_type=cli_type) if output["status"] != "SUCCESS": st.wait(300, "Waiting for device to reboot after success...") st.wait_system_status(dut, 300) # st.wait_system_reboot(dut) if not verify_ztp_status(dut, retry_count, iteration, delay, cli_type=cli_type): if "logs_path" in data and "func_name" in data: capture_syslogs(dut, data.logs_path, syslog_file_names) st.log("ZTP status verification failed") st.report_fail("ztp_status_verification_failed") st.banner(boot_up_obj.sonic_installer_list(dut)) verify_ztp_filename_logs(dut, data) if "ztp_log_string" in data and data.ztp_log_string: if not basic_obj.poll_for_error_logs(dut, data.ztp_log_path, data.ztp_log_string): st.log("ZTP log {} verification failed for message {}".format(data.ztp_log_path, data.ztp_log_string)) if not basic_obj.poll_for_error_logs(dut, data.ztp_log_path_1, data.ztp_log_string): st.log("ZTP log {} verification failed for message {}".format(data.ztp_log_path_1, data.ztp_log_string)) st.report_fail("ztp_log_verification_failed", data.ztp_log_path_1, data.ztp_log_string) if "result" in data and data.result == "pass": st.report_pass("test_case_passed")
def test_ft_ztp_option_239(ztp_func_hooks): # ################ Author Details ################ # Name: Chaitanya Vella # Email: [email protected] # ################################################ # ############## Test bed details ################ # D1 ---- DHCP Server # ztp_option_239 : Verify that ZTP works fine for DHCP option 239 to # execute a simple shell script as an alternative to ZTP JSON. # ################################################# vars = st.ensure_min_topology("D1") source_path = "{}{}".format(data.local_provision_scripts_folder, data.option_239_sh_file) destination_path = "{}{}/{}".format(ztp_params.home_path, ztp_params.provision_script_path, data.option_239_sh_file) basic_obj.copy_file_from_client_to_server(ssh_conn_obj, src_path=source_path, dst_path=destination_path, persist=True) options = SpyTestDict() options.dhcp_config_file = ztp_params.dhcp.config_file options.server_ip = ztp_params.dhcp.static_ip options.config_path = ztp_params.provision_script_path options.provision_script = data.option_239_sh_file options.search_pattern = r'\s*option\s+provision-url\s*\S*\s*"\S+";' options.option_string = 'option provision-url ' options.option_url = "http://{}{}/{}".format( ztp_params.dhcp.static_ip, ztp_params.provision_script_path, data.option_239_sh_file) ztp_obj.write_option_to_dhcp_server(ssh_conn_obj, options) # ztp_obj.write_option_239_to_dhcp_server(ssh_conn_obj, options) basic_obj.service_operations(ssh_conn_obj, data.dhcp_service_name, 'restart', "server") if not ztp_obj.verify_dhcpd_service_status(ssh_conn_obj, config_params.dhcpd_pid): st.log("{} service not running".format(data.dhcp_service_name)) st.report_fail("service_not_running", data.dhcp_service_name) basic_obj.copy_config_db_to_temp(vars.D1, data.config_db_path, data.config_db_temp) basic_obj.remove_file(vars.D1, data.config_db_path) st.reboot(vars.D1, "normal", skip_port_wait=True) if not ztp_obj.poll_ztp_status(vars.D1, ["IN-PROGRESS", "Not Started", "SUCCESS"]): st.report_fail("ztp_max_polling_interval") if not ztp_obj.verify_ztp_status(vars.D1): if "logs_path" in config_params and "func_name" in config_params: syslog_file_names = [ "syslog_1_{}".format(config_params.func_name), "syslog_{}".format(config_params.func_name) ] ztp_obj.capture_syslogs(vars.D1, config_params.logs_path, syslog_file_names) st.log("ZTP status verification failed") st.report_fail("ztp_status_verification_failed") ztp_status = ztp_obj.show_ztp_status(vars.D1) if "dhcp-opt239" not in ztp_status["source"]: st.log("ZTP source verification failed with {} against dhcp-opt239". format(ztp_status["source"])) st.report_fail("ztp_source_verification_failed", "dhcp-opt239", ztp_status["source"]) st.report_pass("test_case_passed")