def ztp_module_hooks(request): st.add_prevent("ztp") initialize_variables() pre_config_ztp() path_list = [ ztp_params.home_path + ztp_params.config_path, ztp_params.home_path + ztp_params.provision_script_path ] ztp_obj.create_required_folders(ssh_conn_obj, path_list) basic_obj.move_file_to_local_path(vars.D1, "/etc/sonic/snmp.yml", "/etc/sonic/snmp.yml.bkp") source_path = "{}{}".format(config_params.local_provision_scripts_folder, config_params.snmp_filename) st.upload_file_to_dut(vars.D1, source_path, "/home/admin/snmp.yml") basic_obj.copy_file_to_local_path(vars.D1, "/home/admin/snmp.yml", "/etc/sonic/snmp.yml") yield 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) reset_module_config() basic_obj.move_file_to_local_path(vars.D1, "/etc/sonic/snmp.yml.bkp", "/etc/sonic/snmp.yml")
def ztp_push_full_config(dut, cli_type=""): """ NOT USED ANYWHERE Author: Chaitanya Vella ([email protected]) APU to push full config :param dut: :return: """ cli_type = st.get_ui_type(dut, cli_type=cli_type) config_dbjson = "config_db.json" config_file = "ztp_data_local.json" plugin_file_path = "/etc/sonic/ztp/{}".format(config_file) source = "/tmp/{}".format(config_dbjson) plugin_json = {config_dbjson: {"url": {"source": "file://{}".format(source), "timeout": 300}, "save-config": "true"}} file_path = basic_obj.write_to_json_file(plugin_json) st.upload_file_to_dut(dut, file_path, plugin_file_path) running_config = switch_conf_obj.get_running_config(dut) file_path = basic_obj.write_to_json_file(running_config) st.upload_file_to_dut(dut, file_path, source) st.wait(wait_5) ztp_operations(dut, "run") st.wait(wait_60) show_ztp_status(dut, cli_type=cli_type) st.wait(wait_10) show_ztp_status(dut, cli_type=cli_type)
def ztp_module_hooks(request): st.add_prevent("ztp") initialize_config_params() pre_config_ztp() initialize_data_variables() path_list = [ ztp_params.home_path + ztp_params.config_path, ztp_params.home_path + ztp_params.firmware_path, ztp_params.home_path + ztp_params.provision_script_path, ztp_params.home_path + ztp_params.xml_path ] ztp_obj.create_required_folders(ssh_conn_obj, path_list) if inband_ssh_conn_obj: basic_obj.service_operations(inband_ssh_conn_obj, data.dhcp_service_name, "stop", "server") basic_obj.move_file_to_local_path(vars.D1, "/etc/sonic/snmp.yml", "/etc/sonic/snmp.yml.bkp") source_path = "{}{}".format(config_params.local_provision_scripts_folder, config_params.snmp_filename) st.upload_file_to_dut(vars.D1, source_path, "/home/admin/snmp.yml") basic_obj.copy_file_to_local_path(vars.D1, "/home/admin/snmp.yml", "/etc/sonic/snmp.yml") yield reset_module_config() basic_obj.move_file_to_local_path(vars.D1, "/etc/sonic/snmp.yml.bkp", "/etc/sonic/snmp.yml")
def ztp_module_hooks(request): st.add_prevent("ztp") initialize_variables() pre_config_ztp() path_list = [ztp_params.home_path+ztp_params.config_path, ztp_params.home_path+ztp_params.provision_script_path] ztp_obj.create_required_folders(ssh_conn_obj, path_list) basic_obj.move_file_to_local_path(vars.D1, "/etc/sonic/snmp.yml", "/etc/sonic/snmp.yml.bkp") source_path = "{}{}".format(config_params.local_provision_scripts_folder, config_params.snmp_filename) st.upload_file_to_dut(vars.D1, source_path, "/home/admin/snmp.yml") basic_obj.copy_file_to_local_path(vars.D1, "/home/admin/snmp.yml", "/etc/sonic/snmp.yml") if ztp_params.breakout: if breakout(vars.D1, ztp_params.breakout, cli_type="klish", skip_error=True): intf_details = get_interface_details_by_alt_name(vars.D1, ztp_params.inband_port) if isinstance(intf_details, dict): inband_port = intf_details["interface"] else: inband_port = intf_details interface_noshutdown(vars.D1, inband_port) config_ip_addr_interface(vars.D1, inband_port, ztp_params.dhcp6.client_ip_inband, ztp_params.dhcp6.inband_v6_subnet, family="ipv6") # enable_dhcp_on_interface(vars.D1, ztp_params.inband_port) if not ping(vars.D1, ztp_params.dhcp6.inband_static_ip, family="ipv6"): st.log("Pinging to DHCP server failed from DUT, issue either with DUT or server") # config_save(vars.D1) yield 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) reset_module_config() basic_obj.move_file_to_local_path(vars.D1, "/etc/sonic/snmp.yml.bkp", "/etc/sonic/snmp.yml")
def ztp_module_hooks(request): st.add_prevent("ztp") initialize_config_params() pre_config_ztp() initialize_data_variables() path_list = [ztp_params.home_path+ztp_params.config_path, ztp_params.home_path+ztp_params.firmware_path, ztp_params.home_path+ztp_params.provision_script_path, ztp_params.home_path+ztp_params.xml_path] ztp_obj.create_required_folders(ssh_conn_obj, path_list) basic_obj.move_file_to_local_path(vars.D1, "/etc/sonic/snmp.yml", "/etc/sonic/snmp.yml.bkp") source_path = "{}{}".format(config_params.local_provision_scripts_folder, config_params.snmp_filename) st.upload_file_to_dut(vars.D1, source_path, "/home/admin/snmp.yml") basic_obj.copy_file_to_local_path(vars.D1, "/home/admin/snmp.yml", "/etc/sonic/snmp.yml") if ztp_params.breakout: if breakout(vars.D1, ztp_params.breakout, cli_type="klish", skip_error=True): intf_details = get_interface_details_by_alt_name(vars.D1, ztp_params.inband_port) if isinstance(intf_details, dict): inband_port = intf_details["interface"] else: inband_port = intf_details interface_noshutdown(vars.D1, inband_port) config_ip_addr_interface(vars.D1, inband_port, ztp_params.dhcp.client_ip_inband, ztp_params.dhcp.inband_v4_subnet) # enable_dhcp_on_interface(vars.D1, ztp_params.inband_port) if not ping(vars.D1, ztp_params.dhcp.inband_static_ip): st.log("Pinging to DHCP server failed from DUT, issue either with DUT or server") # config_save(vars.D1) # write_config_db(vars.D1, interface_data) yield reset_module_config() basic_obj.move_file_to_local_path(vars.D1, "/etc/sonic/snmp.yml.bkp", "/etc/sonic/snmp.yml")
def config_ztp_backdoor_options(dut, ztp_cfg={"admin-mode": True, "restart-ztp-interval": 30}, dut_ztp_cfg_file="/host/ztp/ztp_cfg.json"): """ Author: Chaitanya Vella ([email protected]) Function to enable backward options for ZTP :param dut: :param ztp_cfg: :param dut_ztp_cfg_file: :return: """ ztp_cfg_file = basic_obj.write_to_json_file(ztp_cfg) st.upload_file_to_dut(dut, ztp_cfg_file, dut_ztp_cfg_file)
def generating_certificates_to_dut(): local_path = os.path.dirname(__file__) local_certificates_path = "{}/certificates/".format(local_path) file_name1 = "rootgen" file_name2 = "certgen" file_path1 = "{}{}".format(local_certificates_path, file_name1) file_path2 = "{}{}".format(local_certificates_path, file_name2) st.upload_file_to_dut(vars.D1, file_path1, "/home/admin/") st.upload_file_to_dut(vars.D1, file_path2, "/home/admin/") change_permissions(vars.D1, folder_path=file_name1) change_permissions(vars.D1, folder_path=file_name2) # Api's are not availale to generate cert st.config(vars.D1, "sudo ./rootgen") st.config(vars.D1, "sudo ./certgen admin")
def set_copp_config(dut, *argv): """ To set the config into copp Author : Chaitanya Lohith Bollapragada ([email protected]) Expected input from user should be [[table_name,attribute,value],[table_name,attribute,value],...] :param dut: :param table_name: :param attribute: :param value: :return bool: Example : set_copp_config(dut, ["COPP_TABLE:trap.group.bgp.lacp","queue","4"]) set_copp_config(dut, ["COPP_TABLE:trap.group.bgp.lacp","queue","4"],["COPP_TABLE:trap.group.lldp.dhcp.udld","trap_priority","6"]) """ command = "docker exec swss cat /etc/swss/config.d/00-copp.config.json" output = st.show(dut, command, skip_tmpl=True) reg_output = utils_obj.remove_last_line_from_string(output) try: data = do_eval(reg_output) except Exception as e: st.log(e) reg_output = "{} ]".format(reg_output) data = do_eval(reg_output) st.log("ARGV {}".format(argv)) for eachli in argv: if len(eachli) != 3: st.error("Invalid input is provided {}".format(eachli)) return False table = eachli[0] attribute = eachli[1] value = eachli[2] for each in data: if table in each: each[table][attribute] = value break else: st.error("Table not found {}".format(table)) return False file_path = utils_obj.write_to_json_file(data, "/tmp/00-copp.config.json") st.log("FILE PATH -- {}".format(file_path)) st.upload_file_to_dut(dut, file_path, "/tmp/00-copp.config.json") command = "docker cp /tmp/00-copp.config.json swss:/etc/swss/config.d/00-copp.config.json" st.config(dut, command) command = "rm /tmp/00-copp.config.json" st.config(dut, command) return True
def set_copp_pir_config(dut, config, *args): """ To set the config into copp_config.json Author : [email protected] :param dut: :param config: :param args: :return: """ command = "sudo cat /etc/sonic/copp_config.json" output = st.show(dut, command, skip_tmpl=True) reg_output = utils_obj.remove_last_line_from_string(output) try: data = eval(reg_output) except Exception as e: st.log(e) reg_output = str(reg_output) + "\n" + "}" data = eval(reg_output) st.log("ARGS {}".format(args)) if config == "get": return data for eachli in args: if len(eachli) != 3: st.error("Invalid input is provided {}".format(eachli)) return False table = eachli[0] attribute = eachli[1] value = eachli[2] found_table = False if table in data['SCHEDULER'].keys(): data['SCHEDULER'][table][attribute] = value found_table = True if not found_table: st.error("Table not found {}".format(table)) return False file_path = utils_obj.write_to_json_file(data, "/tmp/00-copp.config.json") st.log("FILE PATH -- {}".format(file_path)) st.upload_file_to_dut(dut, file_path, "/tmp/00-copp.config.json") command = "sudo cp /tmp/00-copp.config.json /etc/sonic/copp_config.json" st.config(dut, command) command = "rm /tmp/00-copp.config.json" st.config(dut, command) return True
def config_acl_loader_update(dut, type_name, json_data, config_type="acl_loader"): """ Config ACL loader update and add. Author: Chaitanya Vella ([email protected]) :param dut: :param type_name: :param json_data: :param config_type: acl_loader and config_update :return: """ try: if type_name not in ["full", "incremental", "add"]: st.log("Invalid type - {}".format(type_name)) return False temp_file_path = tempfile.gettempdir() current_datetime = util_obj.get_current_datetime() file_name = "sonic_{}.json".format(current_datetime) file_path = "{}/{}".format(temp_file_path, file_name) file_path = util_obj.write_to_json_file(json_data, file_path) st.upload_file_to_dut(dut, file_path, file_name) if config_type == "acl_loader" and type_name != 'add': command = "acl-loader update {} {}".format(type_name, file_path) elif config_type == "acl_loader" and type_name == 'add': command = "acl-loader {} {}".format(type_name, file_path) else: if type_name == "add": command = "config acl {} {}".format(type_name, file_path) else: command = "config acl update {} {}".format( type_name, file_path) st.config(dut, command) os.remove(file_path) return True except ValueError as e: st.log(e) return False
def test_ft_ztp_validate_cfg_file_with_incorrect_data(ztp_func_hooks): vars = st.ensure_min_topology("D1") data.ztp_cfg = { "admin": True, "restart-ztp-interval": 30, "reboot-on-success": "abcd" } ztp_cfg_file = basic_obj.write_to_json_file(data.ztp_cfg) st.upload_file_to_dut(vars.D1, ztp_cfg_file, data.dut_ztp_cfg_file) ztp_obj.enable_ztp_if_disabled(vars.D1) if st.get_ui_type(vars.D1) == "click": ztp_obj.ztp_operations(vars.D1, "run") else: st.reboot(vars.D1, "normal", skip_port_wait=True) log_string = "Invalid data type used for reboot-on-success" if not basic_obj.poll_for_error_logs(vars.D1, data.ztp_log_path, log_string): st.log("ZTP log {} verification failed for message {}".format( data.ztp_log_path, log_string)) st.report_fail("ztp_log_verification_failed", data.ztp_log_path, log_string) st.report_pass("test_case_passed")
def gnmi_set(dut, xpath, json_content, **kwargs): """ API to set GNMI configuration Author: Chaitanya Vella ([email protected]) :param dut: :param xpath: :param json_content: :param kwargs: :return: """ gnmi_debug(dut) credentails = st.get_credentials(dut) ip_address = kwargs.get('ip_address', '127.0.0.1') port = kwargs.get('port', '8080') insecure = kwargs.get('insecure', '') username = kwargs.get('username', credentails[0]) password = kwargs.get('password', credentails[3]) cert = kwargs.get('cert') target_name = kwargs.get('target_name') pretty = kwargs.get('pretty') logstostderr = kwargs.get('logstostderr') mode = kwargs.get('mode', '-update') docker_command = get_docker_command() if not docker_command: st.log("Docker command not found ..") return False if json_content: temp_dir = tempfile.gettempdir() current_datetime = util_obj.get_current_datetime() file_name = "sonic_gnmi_{}.json".format(current_datetime) tmp_path = "{}/{}".format(temp_dir, file_name) docker_path = '/{}'.format(file_name) cp_cmd = 'docker cp {} telemetry:{}'.format(tmp_path, docker_path) rm_cmds = ['rm {}'.format(tmp_path), '{} -c "rm {}"'.format(docker_command, docker_path)] file_operation = util_obj.write_to_json_file(json_content, tmp_path) if not file_operation: st.log("File operation failed.") return False st.upload_file_to_dut(dut, tmp_path, tmp_path) st.config(dut, cp_cmd) gnmi_command = 'gnmi_set {} {}:@{} -target_addr {}:{}'.format(mode, xpath, docker_path, ip_address, port) if username: gnmi_command += " -username {}".format(username) if password: gnmi_command += " -password {}".format(password) if cert: gnmi_command += " -cert {}".format(cert) if target_name: gnmi_command += " -target_name {}".format(target_name) if pretty: gnmi_command += " -pretty" if logstostderr: gnmi_command += " -alsologstostderr" if insecure != 'none': gnmi_command += " -insecure {}".format(insecure) command = '{} -c "{}"'.format(docker_command, gnmi_command) output = st.config(dut, command) for rm_cmd in rm_cmds: st.config(dut, rm_cmd) error_strings = ["Error response", "rpc error", "Set failed", "Unknown desc", "failed"] for err_code in error_strings: if err_code in util_obj.remove_last_line_from_string(output): st.log(output) return False return output else: return False