From 8e1a4b4e19522332d5561e7181a924d38b0a3e94 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Thu, 4 Jul 2024 12:14:00 +0500 Subject: [PATCH 001/127] kdump-remote-configurations --- config/kdump.py | 38 ++++++- scripts/sonic-kdump-config | 218 ++++++++++++++++++++++++++++++++++++- show/kdump.py | 8 ++ 3 files changed, 259 insertions(+), 5 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index c61e79099d..fbdbaabb5d 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -79,7 +79,7 @@ def kdump_memory(db, kdump_memory): # -# 'num_dumps' command ('sudo config keump num_dumps ...') +# 'num_dumps' command ('sudo config kdump num_dumps ...') # @kdump.command(name="num_dumps", short_help="Configure the maximum dump files of KDUMP mechanism") @click.argument('kdump_num_dumps', metavar='', required=True, type=int) @@ -90,3 +90,39 @@ def kdump_num_dumps(db, kdump_num_dumps): check_kdump_table_existence(kdump_table) db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps}) + + +@kdump.command(name="remote", short_help="Configure remote KDUMP mechanism") +@click.argument("action", type=click.Choice(["ssh", "disable"])) +@click.option("-c", "ssh_connection_string", + metavar='', + help="SSH user and host. e.g user@hostname/ip") +@click.option("-p", "ssh_private_key_path", + metavar='', + help="Path to private key. e.g /root/.ssh/kdump_id_rsa") +@pass_db +def kdump_remote(db, action, ssh_connection_string, ssh_private_key_path): + """Configure remote KDUMP mechanism""" + + # Ensure the KDUMP table and 'config' key exist + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) + + if action == "ssh": + # Validate arguments for SSH configuration + if ssh_connection_string is None or ssh_private_key_path is None: + click.echo("Error: Both --ssh-connection-string and --ssh-private-key-path\ + \are required for SSH configuration.") + sys.exit(1) + + db.cfgdb.mod_entry("KDUMP", "config", {"remote_enabled": "true"}) + if ssh_connection_string is not None: + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": ssh_connection_string}) + if ssh_private_key_path is not None: + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": ssh_private_key_path}) + elif action == "disable": + # Set remote_enabled to "false" + db.cfgdb.mod_entry("KDUMP", "config", {"remote_enabled": "false"}) + + click.echo("KDUMP configuration changes may require a reboot to take effect.") + click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.") diff --git a/scripts/sonic-kdump-config b/scripts/sonic-kdump-config index cafda7db22..1fcf4e709b 100755 --- a/scripts/sonic-kdump-config +++ b/scripts/sonic-kdump-config @@ -25,6 +25,7 @@ import sys import syslog import subprocess +from swsssdk import ConfigDBConnector from swsscommon.swsscommon import ConfigDBConnector from sonic_installer.bootloader import get_bootloader from sonic_installer.common import IMAGE_PREFIX @@ -172,9 +173,16 @@ def cmd_dump_config_json(): kdump_enabled = get_kdump_administrative_mode() kdump_memory = get_kdump_memory() kdump_num_dumps = get_kdump_num_dumps() + # remote SSH variables + ssh_connection_string = get_ssh_connection_string() + ssh_private_key_path = get_ssh_private_key_path() + kdump_remote_enabled = get_kdump_remote_enabled() data = { "enable" : kdump_enabled, \ "memory" : kdump_memory, \ - "max-dumps" : int(kdump_num_dumps) } + "max-dumps" : int(kdump_num_dumps), \ + "ssh_connection_string" : str(ssh_connection_string), \ + "ssh_private_key-path" : str(ssh_private_key_path), \ + "remote_enabled" : kdump_remote_enabled } print(json.dumps(data, indent=4)) def cmd_dump_kdump_records_json(): @@ -223,11 +231,18 @@ def cmd_dump_status_json(): kdump_oper_state = get_kdump_oper_mode(kdump_enabled) kdump_memory = get_kdump_memory() kdump_num_dumps = get_kdump_num_dumps() + # remote SSH variables + ssh_connection_string = get_ssh_connection_string() + ssh_private_key_path = get_ssh_private_key_path() + kdump_remote_enabled = get_kdump_remote_enabled() data = { "enable" : kdump_enabled, \ "current-state" : kdump_oper_state, \ "memory" : kdump_memory, \ "allocated-memory" : get_crash_kernel_size(), \ - "max-dumps" : int(kdump_num_dumps) } + "max-dumps" : int(kdump_num_dumps), \ + "ssh-connection-string" : str(ssh_connection_string), \ + "ssh-private-key-path" : str(ssh_private_key_path), \ + "remote-enabled" : kdump_remote_enabled } print(json.dumps(data, indent=4)) ## Query current configuration to check if kdump is enabled or disabled @@ -299,6 +314,62 @@ def get_kdump_num_dumps(): num_dumps = num return num_dumps +## Query current configuration for kdump ssh_connection_string +# +# @return The ssh remote connection string for storing kernel dump files remotely +# (read from running configuration) +def get_ssh_connection_string(): + ssh_connection_string = "user@ip/hostname --default, please change--" + config_db = ConfigDBConnector(use_unix_socket_path=True) + if config_db is not None: + config_db.connect() + table_data = config_db.get_table('KDUMP') + if table_data is not None: + config_data = table_data.get('config') + if config_data is not None: + conn = config_data.get('ssh_connection_string') + if conn: + ssh_connection_string = conn + return ssh_connection_string + +## Query current configuration for kdump ssh_private_key_path +# +# @return The ssh remote connection private_key_file_path for connecting to remote server without authentication (command "sudo kdump-config propagate" required) +# (read from running configuration) +def get_ssh_private_key_path(): + ssh_private_key_path = "/root/.ssh/kdump_id_rsa --default, please change--" + config_db = ConfigDBConnector(use_unix_socket_path=True) + if config_db is not None: + config_db.connect() + table_data = config_db.get_table('KDUMP') + if table_data is not None: + config_data = table_data.get('config') + if config_data is not None: + pr_key_path = config_data.get('ssh_private_key_path') + if pr_key_path: + ssh_private_key_path = pr_key_path + return ssh_private_key_path + +## Query current configuration for kdump remote_enabled +# +# @return The if remote connection is enabled for connecting to remote server +# (read from running configuration) +def get_kdump_remote_enabled(): + remote_enabled = False + config_db = ConfigDBConnector(use_unix_socket_path=True) + if config_db is not None: + config_db.connect() + table_data = config_db.get_table('KDUMP') + if table_data is not None: + config_data = table_data.get('config') + if config_data is not None: + remote_en = config_data.get('remote_enabled') + if remote_en: + remote_enabled = remote_en + return remote_enabled + + + ## Read current value for USE_KDUMP in kdump config file # # @return The integer value X from USE_KDUMP=X in /etc/default/kdump-tools @@ -362,6 +433,138 @@ def write_num_dumps(num_dumps): print_err("Error while writing KDUMP_NUM_DUMPS into %s" % kdump_cfg) sys.exit(1) +## Read current value for SSH SSH kdump-ssh-connection-string in kdump config file +# +# @return The string value X from SSH=X in /etc/default/kdump-tools +def read_ssh_connection_string_from_db(): + config_db = ConfigDBConnector(use_unix_socket_path=True) + config_db.connect() + + kdump_table = config_db.get_table('KDUMP') + if kdump_table: + config_data = kdump_table.get('config') + if config_data: + return config_data.get('ssh_connection_string') + return None + +## Change the value for SSH kdump-ssh-connection-string in kdump config file /etc/default/kdump-tools +# +# #param ssh-connection-string value for new value +def write_ssh_connection_string_to_kdump_tools(): + ssh_connection_string = read_ssh_connection_string_from_db() + if not ssh_connection_string: + print("Error: SSH connection string not found in Config DB.") + return False + + try: + # Use sed to update the SSH connection string in the kdump-tools file + subprocess.run(['sudo', 'sed', '-i', 's/#SSH=.*/SSH="{}"/g'.format(ssh_connection_string), kdump_cfg], check=True) + + return True + except Exception as e: + print("Error: Unable to write to {}. Exception: {}".format(kdump_cfg, str(e))) + return False + + +## Read current value for SSH_KEY kdump-ssh-private-key-path in kdump config file +# +# @return The string value X from SSH_KEY=X in /etc/default/kdump-tools +def read_ssh_private_key_path(kdump_cfg="/etc/default/kdump-tools"): + """Reads the SSH private key path from the kdump configuration file. + + Args: + kdump_cfg (str, optional): The path to the kdump configuration file. + Defaults to "/etc/default/kdump-tools". + + Returns: + str: The extracted SSH private key path from the file or None if not found. + Raises: + Exception: If there's an error running the grep command. + """ + + try: + # Construct the full command string safely + command = f"grep '#*SSH_KEY=.*' {kdump_cfg} | cut -d = -f 2" + + # Use subprocess.run without shell execution + output = subprocess.run( + command.split(), capture_output=True, text=True, check=True + ).stdout.strip() + + if output: + return output # Existing SSH private key path found + else: + return None # No matching line found + + except subprocess.CalledProcessError as e: + raise Exception(f"Error reading SSH private key path from {kdump_cfg}: {str(e)}") + + +def write_ssh_private_key_path(ssh_private_key_path, kdump_cfg="/etc/default/kdump-tools"): + """Writes the SSH private key path to the kdump configuration file. + + Args: + ssh_private_key_path (str): The SSH private key path to write. + kdump_cfg (str, optional): The path to the kdump configuration file. + Defaults to "/etc/default/kdump-tools". + + Raises: + Exception: If there's an error writing to the file. + """ + + try: + # Construct the full command string safely + command = f"sed -i 's/#*SSH_KEY=.*$/SSH_KEY={ssh_private_key_path}/' {kdump_cfg}" + + # Use subprocess.run without shell execution + subprocess.run(command.split(), check=True) + + except subprocess.CalledProcessError as e: + raise Exception(f"Error writing SSH private key path to {kdump_cfg}: {str(e)}") + + +# Example usage +ssh_private_key_path = get_ssh_private_key_path() + +# Write the retrieved value to kdump_cfg only if it's not the default value +if ssh_private_key_path != "/root/.ssh/kdump_id_rsa --default, please change--": + write_ssh_private_key_path(ssh_private_key_path) + +# Now you can potentially read the written value using read_ssh_private_key_path(kdump_cfg) + +## Disable remote: Comment the value for SSH and SSH_KEY in kdump config file /etc/default/kdump-tools +# +def kdump_remote_disable(): + """ + Disables remote kdump in the configuration file (kdump_cfg). + + This function checks if remote kdump is already enabled before attempting + to disable it. If enabled, it uses sed to modify the configuration file + in-place, removing any comment character (#) before the SSH and SSH_KEY lines. + The function exits with an error message if any of the sed commands fail. + + Returns: + None + """ + + # Check if remote kdump is already enabled + if get_kdump_remote_enabled().lower() == "true": + # Disable SSH key authentication + rc = run_command("/bin/sed -i '/*SSH=\"/s/\#/' %s" % (kdump_cfg), use_shell=False) + if rc != 0: + print_err("Error while disabling SSH key auth in", kdump_cfg) + sys.exit(1) + + # Disable SSH authentication (optional, depending on configuration) + rc = run_command("/bin/sed -i '/*SSH_KEY=\"/s/\#/' %s" % (kdump_cfg), use_shell=False) + if rc != 0: + print_err("Error while disabling SSH auth in", kdump_cfg) + sys.exit(1) + + print("Successfully disabled remote kdump in", kdump_cfg) + else: + print("Remote kdump is already disabled in", kdump_cfg) + ## Enable kdump # # @param verbose If True, the function will display a few additinal information @@ -417,7 +620,7 @@ def kdump_enable(verbose, kdump_enabled, memory, num_dumps, image, cmdline_file) ## Read kdump configuration saved in the startup configuration file # -# @param config_param If True, the function will display a few additional information +# @param config_param If True, the function will display a few additional information. # @return Value of the configuration parameter saved in the startup configuration file # @return None if the startup configuration file does not exist or the kdump # configuration parameter is not present in the file. @@ -448,8 +651,15 @@ def cmd_kdump_enable(verbose, image): kdump_enabled = get_kdump_administrative_mode() memory = get_kdump_memory() num_dumps = get_kdump_num_dumps() + + ## + # ToDo - 02 + ## + ssh_connection_string = get_ssh_connection_string() + ssh_private_key_path = get_ssh_private_key_path() + remote_enabled = get_kdump_remote_enabled() if verbose: - print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d" % (kdump_enabled, memory, num_dumps)) + print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d ssh_connection_string=%d ssh_private_key_path=%d remote_enabled=%d" % (kdump_enabled, memory, num_dumps, ssh_connection_string, ssh_private_key_path, remote_enabled)) if os.path.exists(grub_cfg): return kdump_enable(verbose, kdump_enabled, memory, num_dumps, image, grub_cfg) diff --git a/show/kdump.py b/show/kdump.py index 6eba55082e..d07804b26a 100644 --- a/show/kdump.py +++ b/show/kdump.py @@ -83,6 +83,14 @@ def config(): num_files_config = get_kdump_config("num_dumps") click.echo("Maximum number of Kdump files: {}".format(num_files_config)) + # remote SSH configs + if get_kdump_config("remote_enabled") == "true": + ssh_conn_str = get_kdump_config("ssh_connection_string") + click.echo("Kdump remote server user@ip/hostname: {}".format(ssh_conn_str)) + + ssh_prv_key = get_kdump_config("ssh_private_key_path") + click.echo("Kdump private key file path for remote ssh connection: {}".format(ssh_prv_key)) + def get_kdump_core_files(): """Retrieves the kernel core dump files from directory '/var/crash/'. From 3893fc9ff91d12d92af9e0a7b9561b2cd4fc5c47 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Wed, 10 Jul 2024 13:21:09 +0500 Subject: [PATCH 002/127] Corrected Functions --- config/kdump.py | 98 ++++++++++++++++++++++---------------- scripts/sonic-kdump-config | 24 +++++----- show/kdump.py | 2 +- 3 files changed, 70 insertions(+), 54 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index fbdbaabb5d..10eafe0592 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -1,9 +1,7 @@ import sys - import click from utilities_common.cli import AbbreviationGroup, pass_db - # # 'kdump' group ('sudo config kdump ...') # @@ -12,7 +10,6 @@ def kdump(): """Configure the KDUMP mechanism""" pass - def check_kdump_table_existence(kdump_table): """Checks whether the 'KDUMP' table is configured in Config DB. @@ -31,10 +28,15 @@ def check_kdump_table_existence(kdump_table): click.echo("Unable to retrieve key 'config' from KDUMP table.") sys.exit(2) - +def echo_reboot_warning(): + """Prints the warning message about reboot requirements.""" + click.echo("KDUMP configuration changes may require a reboot to take effect.") + click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.") # # 'disable' command ('sudo config kdump disable') # + + @kdump.command(name="disable", short_help="Disable the KDUMP mechanism") @pass_db def kdump_disable(db): @@ -43,13 +45,13 @@ def kdump_disable(db): check_kdump_table_existence(kdump_table) db.cfgdb.mod_entry("KDUMP", "config", {"enabled": "false"}) - click.echo("KDUMP configuration changes may require a reboot to take effect.") - click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.") - + echo_reboot_warning() # # 'enable' command ('sudo config kdump enable') # + + @kdump.command(name="enable", short_help="Enable the KDUMP mechanism") @pass_db def kdump_enable(db): @@ -58,13 +60,13 @@ def kdump_enable(db): check_kdump_table_existence(kdump_table) db.cfgdb.mod_entry("KDUMP", "config", {"enabled": "true"}) - click.echo("KDUMP configuration changes may require a reboot to take effect.") - click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.") - + echo_reboot_warning() # # 'memory' command ('sudo config kdump memory ...') # + + @kdump.command(name="memory", short_help="Configure the memory for KDUMP mechanism") @click.argument('kdump_memory', metavar='', required=True) @pass_db @@ -74,13 +76,13 @@ def kdump_memory(db, kdump_memory): check_kdump_table_existence(kdump_table) db.cfgdb.mod_entry("KDUMP", "config", {"memory": kdump_memory}) - click.echo("KDUMP configuration changes may require a reboot to take effect.") - click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.") - + echo_reboot_warning() # # 'num_dumps' command ('sudo config kdump num_dumps ...') # + + @kdump.command(name="num_dumps", short_help="Configure the maximum dump files of KDUMP mechanism") @click.argument('kdump_num_dumps', metavar='', required=True, type=int) @pass_db @@ -90,39 +92,53 @@ def kdump_num_dumps(db, kdump_num_dumps): check_kdump_table_existence(kdump_table) db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps}) + echo_reboot_warning() + +# +# 'remote' command ('sudo config kdump remote ...') +# -@kdump.command(name="remote", short_help="Configure remote KDUMP mechanism") -@click.argument("action", type=click.Choice(["ssh", "disable"])) -@click.option("-c", "ssh_connection_string", - metavar='', - help="SSH user and host. e.g user@hostname/ip") -@click.option("-p", "ssh_private_key_path", - metavar='', - help="Path to private key. e.g /root/.ssh/kdump_id_rsa") +@kdump.command(name="remote", short_help="Enable or Disable Kdump Remote") +@click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) @pass_db -def kdump_remote(db, action, ssh_connection_string, ssh_private_key_path): - """Configure remote KDUMP mechanism""" +def kdump_remote(db, action): + """Enable or Disable Kdump Remote Mode""" + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) + + remote_enabled = 'true' if action.lower() == 'enable' else 'false' + db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote_enabled}) + echo_reboot_warning() + +# +# 'ssh_connection_string' command ('sudo config kdump ssh_connection_string ...') +# + - # Ensure the KDUMP table and 'config' key exist +@kdump.command(name="ssh_connection_string", short_help="Set SSH connection string") +@click.argument('ssh_connection_string', metavar='', required=True) +@pass_db +def set_ssh_connection_string(db, ssh_connection_string): + """Set SSH connection string (username@serverip)""" kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) - if action == "ssh": - # Validate arguments for SSH configuration - if ssh_connection_string is None or ssh_private_key_path is None: - click.echo("Error: Both --ssh-connection-string and --ssh-private-key-path\ - \are required for SSH configuration.") - sys.exit(1) - - db.cfgdb.mod_entry("KDUMP", "config", {"remote_enabled": "true"}) - if ssh_connection_string is not None: - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": ssh_connection_string}) - if ssh_private_key_path is not None: - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": ssh_private_key_path}) - elif action == "disable": - # Set remote_enabled to "false" - db.cfgdb.mod_entry("KDUMP", "config", {"remote_enabled": "false"}) + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": ssh_connection_string}) + echo_reboot_warning() - click.echo("KDUMP configuration changes may require a reboot to take effect.") - click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.") +# +# 'ssh_private_key_path' command ('sudo config kdump ssh_private_key_path ...') +# + + +@kdump.command(name="ssh_private_key_path", short_help="Set path to SSH private key") +@click.argument('ssh_private_key_path', metavar='', required=True) +@pass_db +def set_ssh_private_key_path(db, ssh_private_key_path): + """Set path to SSH private key""" + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) + + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": ssh_private_key_path}) + echo_reboot_warning() diff --git a/scripts/sonic-kdump-config b/scripts/sonic-kdump-config index 1fcf4e709b..0fb7c490e6 100755 --- a/scripts/sonic-kdump-config +++ b/scripts/sonic-kdump-config @@ -176,13 +176,13 @@ def cmd_dump_config_json(): # remote SSH variables ssh_connection_string = get_ssh_connection_string() ssh_private_key_path = get_ssh_private_key_path() - kdump_remote_enabled = get_kdump_remote_enabled() + kdump_remote = get_kdump_remote() data = { "enable" : kdump_enabled, \ "memory" : kdump_memory, \ "max-dumps" : int(kdump_num_dumps), \ "ssh_connection_string" : str(ssh_connection_string), \ "ssh_private_key-path" : str(ssh_private_key_path), \ - "remote_enabled" : kdump_remote_enabled } + "remote" : kdump_remote } print(json.dumps(data, indent=4)) def cmd_dump_kdump_records_json(): @@ -234,7 +234,7 @@ def cmd_dump_status_json(): # remote SSH variables ssh_connection_string = get_ssh_connection_string() ssh_private_key_path = get_ssh_private_key_path() - kdump_remote_enabled = get_kdump_remote_enabled() + kdump_remote = get_kdump_remote() data = { "enable" : kdump_enabled, \ "current-state" : kdump_oper_state, \ "memory" : kdump_memory, \ @@ -242,7 +242,7 @@ def cmd_dump_status_json(): "max-dumps" : int(kdump_num_dumps), \ "ssh-connection-string" : str(ssh_connection_string), \ "ssh-private-key-path" : str(ssh_private_key_path), \ - "remote-enabled" : kdump_remote_enabled } + "remote" : kdump_remote } print(json.dumps(data, indent=4)) ## Query current configuration to check if kdump is enabled or disabled @@ -354,8 +354,8 @@ def get_ssh_private_key_path(): # # @return The if remote connection is enabled for connecting to remote server # (read from running configuration) -def get_kdump_remote_enabled(): - remote_enabled = False +def get_kdump_remote(): + remote = False config_db = ConfigDBConnector(use_unix_socket_path=True) if config_db is not None: config_db.connect() @@ -363,10 +363,10 @@ def get_kdump_remote_enabled(): if table_data is not None: config_data = table_data.get('config') if config_data is not None: - remote_en = config_data.get('remote_enabled') + remote_en = config_data.get('remote') if remote_en: - remote_enabled = remote_en - return remote_enabled + remote = remote_en + return remote @@ -548,7 +548,7 @@ def kdump_remote_disable(): """ # Check if remote kdump is already enabled - if get_kdump_remote_enabled().lower() == "true": + if get_kdump_remote().lower() == "true": # Disable SSH key authentication rc = run_command("/bin/sed -i '/*SSH=\"/s/\#/' %s" % (kdump_cfg), use_shell=False) if rc != 0: @@ -657,9 +657,9 @@ def cmd_kdump_enable(verbose, image): ## ssh_connection_string = get_ssh_connection_string() ssh_private_key_path = get_ssh_private_key_path() - remote_enabled = get_kdump_remote_enabled() + remote = get_kdump_remote() if verbose: - print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d ssh_connection_string=%d ssh_private_key_path=%d remote_enabled=%d" % (kdump_enabled, memory, num_dumps, ssh_connection_string, ssh_private_key_path, remote_enabled)) + print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d ssh_connection_string=%d ssh_private_key_path=%d remote=%d" % (kdump_enabled, memory, num_dumps, ssh_connection_string, ssh_private_key_path, remote)) if os.path.exists(grub_cfg): return kdump_enable(verbose, kdump_enabled, memory, num_dumps, image, grub_cfg) diff --git a/show/kdump.py b/show/kdump.py index d07804b26a..3c4e62c92f 100644 --- a/show/kdump.py +++ b/show/kdump.py @@ -84,7 +84,7 @@ def config(): click.echo("Maximum number of Kdump files: {}".format(num_files_config)) # remote SSH configs - if get_kdump_config("remote_enabled") == "true": + if get_kdump_config("remote") == "true": ssh_conn_str = get_kdump_config("ssh_connection_string") click.echo("Kdump remote server user@ip/hostname: {}".format(ssh_conn_str)) From 03c2b637e5109233e371a8049ca117e4c933c059 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Wed, 10 Jul 2024 13:43:27 +0500 Subject: [PATCH 003/127] Corrected Functions --- config/kdump.py | 1 + 1 file changed, 1 insertion(+) diff --git a/config/kdump.py b/config/kdump.py index 10eafe0592..a7a17cb742 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -28,6 +28,7 @@ def check_kdump_table_existence(kdump_table): click.echo("Unable to retrieve key 'config' from KDUMP table.") sys.exit(2) + def echo_reboot_warning(): """Prints the warning message about reboot requirements.""" click.echo("KDUMP configuration changes may require a reboot to take effect.") From 6f34f88f4eef22affce021ee5ae95d32915322d3 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Wed, 10 Jul 2024 15:19:44 +0500 Subject: [PATCH 004/127] Corrected Functions --- config/kdump.py | 125 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 107 insertions(+), 18 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index a7a17cb742..446298fc7a 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -108,6 +108,15 @@ def kdump_remote(db, action): kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) + current_remote_status = kdump_table.get("config", {}).get("remote", "false").lower() + + if action.lower() == 'enable' and current_remote_status == 'true': + click.echo("Error: Kdump Remote Mode is already enabled.") + return + elif action.lower() == 'disable' and current_remote_status == 'false': + click.echo("Error: Kdump Remote Mode is already disabled.") + return + remote_enabled = 'true' if action.lower() == 'enable' else 'false' db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote_enabled}) echo_reboot_warning() @@ -117,29 +126,109 @@ def kdump_remote(db, action): # -@kdump.command(name="ssh_connection_string", short_help="Set SSH connection string") -@click.argument('ssh_connection_string', metavar='', required=True) +@kdump.command(name="add") +@click.argument('item', type=click.Choice(['ssh_connection_string'])) +@click.argument('value', metavar='', required=True) @pass_db -def set_ssh_connection_string(db, ssh_connection_string): - """Set SSH connection string (username@serverip)""" - kdump_table = db.cfgdb.get_table("KDUMP") - check_kdump_table_existence(kdump_table) - - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": ssh_connection_string}) - echo_reboot_warning() +def ssh_connection_string(db, item, value): + """Add configuration item for kdump""" + if item == 'ssh_connection_string': + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) + + # Check if remote mode is enabled + remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() + if remote_mode_enabled != "true": + click.echo("Error: Enable remote mode first.") + return + + # Check if SSH connection string is already added + existing_ssh_connection_string = kdump_table.get("config", {}).get("ssh_connection_string") + if existing_ssh_connection_string: + click.echo("Error: SSH connection string is already added. Please remove it first before adding a new one.") + return + + # Add SSH connection string to config_db + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": value}) + echo_reboot_warning() + else: + click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") + + +@kdump.command(name="remove", aliases=["rem"]) +@click.argument('item', type=click.Choice(['ssh_connection_string'])) +@pass_db +def remove_ssh_connection_string(db, item): + """Remove configuration item for kdump""" + if item == 'ssh_connection_string': + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) + + # Check if SSH connection string is already added + existing_ssh_connection_string = kdump_table.get("config", {}).get("ssh_connection_string") + if not existing_ssh_connection_string: + click.echo("Error: SSH connection string is not configured.") + return + + # Remove SSH connection string from config_db + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": None}) + click.echo("SSH connection string removed successfully.") + echo_reboot_warning() + else: + click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") # # 'ssh_private_key_path' command ('sudo config kdump ssh_private_key_path ...') # -@kdump.command(name="ssh_private_key_path", short_help="Set path to SSH private key") -@click.argument('ssh_private_key_path', metavar='', required=True) +@kdump.command(name="add") +@click.argument('item', type=click.Choice(['ssh_private_key_path'])) +@click.argument('value', metavar='', required=True) @pass_db -def set_ssh_private_key_path(db, ssh_private_key_path): - """Set path to SSH private key""" - kdump_table = db.cfgdb.get_table("KDUMP") - check_kdump_table_existence(kdump_table) - - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": ssh_private_key_path}) - echo_reboot_warning() +def add_ssh_private_key_path(db, item, value): + """Add configuration item for kdump""" + if item == 'ssh_private_key_path': + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) + + # Check if remote mode is enabled + remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() + if remote_mode_enabled != "true": + click.echo("Error: Enable remote mode first.") + return + + # Check if SSH connection string is already added + existing_ssh_private_key_path = kdump_table.get("config", {}).get("ssh_private_key_path") + if existing_ssh_private_key_path: + click.echo("Error: SSH private key path is already added. Please remove it first before adding a new one.") + return + + # Add SSH connection string to config_db + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": value}) + echo_reboot_warning() + else: + click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") + + +@kdump.command(name="remove", aliases=["rem"]) +@click.argument('item', type=click.Choice(['ssh_private_key_path'])) +@pass_db +def remove_ssh_private_key_path(db, item): + """Remove configuration item for kdump""" + if item == 'ssh_private_key_path': + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) + + # Check if SSH connection string is already added + existing_ssh_private_key_path = kdump_table.get("config", {}).get("ssh_private_key_path") + if not existing_ssh_private_key_path: + click.echo("Error: SSH key path is not configured.") + return + + # Remove SSH connection string from config_db + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": None}) + click.echo("SSH key path removed successfully.") + echo_reboot_warning() + else: + click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") \ No newline at end of file From 18caaba1b617971e11dea83a9edb85b67e17be11 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Wed, 10 Jul 2024 15:38:02 +0500 Subject: [PATCH 005/127] Corrected Functions --- config/kdump.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 446298fc7a..3f74e5d381 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -109,7 +109,7 @@ def kdump_remote(db, action): check_kdump_table_existence(kdump_table) current_remote_status = kdump_table.get("config", {}).get("remote", "false").lower() - + if action.lower() == 'enable' and current_remote_status == 'true': click.echo("Error: Kdump Remote Mode is already enabled.") return @@ -231,4 +231,4 @@ def remove_ssh_private_key_path(db, item): click.echo("SSH key path removed successfully.") echo_reboot_warning() else: - click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") \ No newline at end of file + click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") From 7d7226c3adb678ba64874162d6a69ccb7dcdf932 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Wed, 10 Jul 2024 15:51:02 +0500 Subject: [PATCH 006/127] Corrected Functions --- config/kdump.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 3f74e5d381..e1be06860f 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -126,7 +126,7 @@ def kdump_remote(db, action): # -@kdump.command(name="add") +@kdump.command(name="add",short_help="Add ssh connection string." ) @click.argument('item', type=click.Choice(['ssh_connection_string'])) @click.argument('value', metavar='', required=True) @pass_db @@ -155,7 +155,7 @@ def ssh_connection_string(db, item, value): click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") -@kdump.command(name="remove", aliases=["rem"]) +@kdump.command(name="remove", short_help="Remove ssh connection string.") @click.argument('item', type=click.Choice(['ssh_connection_string'])) @pass_db def remove_ssh_connection_string(db, item): @@ -182,7 +182,7 @@ def remove_ssh_connection_string(db, item): # -@kdump.command(name="add") +@kdump.command(name="add", short_help="Add ssh private key path.") @click.argument('item', type=click.Choice(['ssh_private_key_path'])) @click.argument('value', metavar='', required=True) @pass_db @@ -211,7 +211,7 @@ def add_ssh_private_key_path(db, item, value): click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") -@kdump.command(name="remove", aliases=["rem"]) +@kdump.command(name="remove", shot_help="Add ssh private key path") @click.argument('item', type=click.Choice(['ssh_private_key_path'])) @pass_db def remove_ssh_private_key_path(db, item): From 49b511a9176c428be4a75c581bee6678f87b598f Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Wed, 10 Jul 2024 16:32:43 +0500 Subject: [PATCH 007/127] Corrected Functions --- config/kdump.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index e1be06860f..9abe10cdbb 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -126,7 +126,7 @@ def kdump_remote(db, action): # -@kdump.command(name="add",short_help="Add ssh connection string." ) +@kdump.command(name="add", short_help="Add ssh connection string.") @click.argument('item', type=click.Choice(['ssh_connection_string'])) @click.argument('value', metavar='', required=True) @pass_db @@ -211,7 +211,7 @@ def add_ssh_private_key_path(db, item, value): click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") -@kdump.command(name="remove", shot_help="Add ssh private key path") +@kdump.command(name="remove", short_help="Add ssh private key path") @click.argument('item', type=click.Choice(['ssh_private_key_path'])) @pass_db def remove_ssh_private_key_path(db, item): From a67a77735daac328936b9caa500ca95bc2e0997a Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Thu, 11 Jul 2024 10:12:30 +0500 Subject: [PATCH 008/127] Corrected Functions --- config/kdump.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 9abe10cdbb..62e848a6fd 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -117,8 +117,8 @@ def kdump_remote(db, action): click.echo("Error: Kdump Remote Mode is already disabled.") return - remote_enabled = 'true' if action.lower() == 'enable' else 'false' - db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote_enabled}) + remote = 'true' if action.lower() == 'enable' else 'false' + db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) echo_reboot_warning() # @@ -130,7 +130,7 @@ def kdump_remote(db, action): @click.argument('item', type=click.Choice(['ssh_connection_string'])) @click.argument('value', metavar='', required=True) @pass_db -def ssh_connection_string(db, item, value): +def add_ssh_connection_string(db, item, value): """Add configuration item for kdump""" if item == 'ssh_connection_string': kdump_table = db.cfgdb.get_table("KDUMP") @@ -171,7 +171,7 @@ def remove_ssh_connection_string(db, item): return # Remove SSH connection string from config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": None}) + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": ""}) click.echo("SSH connection string removed successfully.") echo_reboot_warning() else: @@ -211,7 +211,7 @@ def add_ssh_private_key_path(db, item, value): click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") -@kdump.command(name="remove", short_help="Add ssh private key path") +@kdump.command(name="remove", short_help="Remove ssh private key path") @click.argument('item', type=click.Choice(['ssh_private_key_path'])) @pass_db def remove_ssh_private_key_path(db, item): @@ -227,7 +227,7 @@ def remove_ssh_private_key_path(db, item): return # Remove SSH connection string from config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": None}) + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": ""}) click.echo("SSH key path removed successfully.") echo_reboot_warning() else: From 2094bc7a7b89808a63823438b90fb3a50bd1cb32 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Thu, 11 Jul 2024 11:02:49 +0500 Subject: [PATCH 009/127] Corrected Functions --- config/kdump.py | 52 +++++++++++++-------------- scripts/sonic-kdump-config | 74 +++++++++++++++++++------------------- 2 files changed, 63 insertions(+), 63 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 62e848a6fd..6ab18bd541 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -122,17 +122,17 @@ def kdump_remote(db, action): echo_reboot_warning() # -# 'ssh_connection_string' command ('sudo config kdump ssh_connection_string ...') +# 'ssh_string' command ('sudo config kdump ssh_string ...') # @kdump.command(name="add", short_help="Add ssh connection string.") -@click.argument('item', type=click.Choice(['ssh_connection_string'])) +@click.argument('item', type=click.Choice(['ssh_string'])) @click.argument('value', metavar='', required=True) @pass_db -def add_ssh_connection_string(db, item, value): +def add_ssh_string(db, item, value): """Add configuration item for kdump""" - if item == 'ssh_connection_string': + if item == 'ssh_string': kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) @@ -143,52 +143,52 @@ def add_ssh_connection_string(db, item, value): return # Check if SSH connection string is already added - existing_ssh_connection_string = kdump_table.get("config", {}).get("ssh_connection_string") - if existing_ssh_connection_string: + existing_ssh_string = kdump_table.get("config", {}).get("ssh_string") + if existing_ssh_string: click.echo("Error: SSH connection string is already added. Please remove it first before adding a new one.") return # Add SSH connection string to config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": value}) + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": value}) echo_reboot_warning() else: click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") @kdump.command(name="remove", short_help="Remove ssh connection string.") -@click.argument('item', type=click.Choice(['ssh_connection_string'])) +@click.argument('item', type=click.Choice(['ssh_string'])) @pass_db -def remove_ssh_connection_string(db, item): +def remove_ssh_string(db, item): """Remove configuration item for kdump""" - if item == 'ssh_connection_string': + if item == 'ssh_string': kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) # Check if SSH connection string is already added - existing_ssh_connection_string = kdump_table.get("config", {}).get("ssh_connection_string") - if not existing_ssh_connection_string: + existing_ssh_string = kdump_table.get("config", {}).get("ssh_string") + if not existing_ssh_string: click.echo("Error: SSH connection string is not configured.") return # Remove SSH connection string from config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": ""}) + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": ""}) click.echo("SSH connection string removed successfully.") echo_reboot_warning() else: click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") # -# 'ssh_private_key_path' command ('sudo config kdump ssh_private_key_path ...') +# 'ssh_path' command ('sudo config kdump ssh_path ...') # @kdump.command(name="add", short_help="Add ssh private key path.") -@click.argument('item', type=click.Choice(['ssh_private_key_path'])) +@click.argument('item', type=click.Choice(['ssh_path'])) @click.argument('value', metavar='', required=True) @pass_db -def add_ssh_private_key_path(db, item, value): +def add_ssh_path(db, item, value): """Add configuration item for kdump""" - if item == 'ssh_private_key_path': + if item == 'ssh_path': kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) @@ -199,35 +199,35 @@ def add_ssh_private_key_path(db, item, value): return # Check if SSH connection string is already added - existing_ssh_private_key_path = kdump_table.get("config", {}).get("ssh_private_key_path") - if existing_ssh_private_key_path: + existing_ssh_path = kdump_table.get("config", {}).get("ssh_path") + if existing_ssh_path: click.echo("Error: SSH private key path is already added. Please remove it first before adding a new one.") return # Add SSH connection string to config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": value}) + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": value}) echo_reboot_warning() else: click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") @kdump.command(name="remove", short_help="Remove ssh private key path") -@click.argument('item', type=click.Choice(['ssh_private_key_path'])) +@click.argument('item', type=click.Choice(['ssh_path'])) @pass_db -def remove_ssh_private_key_path(db, item): +def remove_ssh_path(db, item): """Remove configuration item for kdump""" - if item == 'ssh_private_key_path': + if item == 'ssh_path': kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) # Check if SSH connection string is already added - existing_ssh_private_key_path = kdump_table.get("config", {}).get("ssh_private_key_path") - if not existing_ssh_private_key_path: + existing_ssh_path = kdump_table.get("config", {}).get("ssh_path") + if not existing_ssh_path: click.echo("Error: SSH key path is not configured.") return # Remove SSH connection string from config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": ""}) + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": ""}) click.echo("SSH key path removed successfully.") echo_reboot_warning() else: diff --git a/scripts/sonic-kdump-config b/scripts/sonic-kdump-config index 0fb7c490e6..c68640308c 100755 --- a/scripts/sonic-kdump-config +++ b/scripts/sonic-kdump-config @@ -174,14 +174,14 @@ def cmd_dump_config_json(): kdump_memory = get_kdump_memory() kdump_num_dumps = get_kdump_num_dumps() # remote SSH variables - ssh_connection_string = get_ssh_connection_string() - ssh_private_key_path = get_ssh_private_key_path() + ssh_string = get_ssh_string() + ssh_path = get_ssh_path() kdump_remote = get_kdump_remote() data = { "enable" : kdump_enabled, \ "memory" : kdump_memory, \ "max-dumps" : int(kdump_num_dumps), \ - "ssh_connection_string" : str(ssh_connection_string), \ - "ssh_private_key-path" : str(ssh_private_key_path), \ + "ssh_string" : str(ssh_string), \ + "ssh_path" : str(ssh_path), \ "remote" : kdump_remote } print(json.dumps(data, indent=4)) @@ -232,16 +232,16 @@ def cmd_dump_status_json(): kdump_memory = get_kdump_memory() kdump_num_dumps = get_kdump_num_dumps() # remote SSH variables - ssh_connection_string = get_ssh_connection_string() - ssh_private_key_path = get_ssh_private_key_path() + ssh_string = get_ssh_string() + ssh_path = get_ssh_path() kdump_remote = get_kdump_remote() data = { "enable" : kdump_enabled, \ "current-state" : kdump_oper_state, \ "memory" : kdump_memory, \ "allocated-memory" : get_crash_kernel_size(), \ "max-dumps" : int(kdump_num_dumps), \ - "ssh-connection-string" : str(ssh_connection_string), \ - "ssh-private-key-path" : str(ssh_private_key_path), \ + "ssh-string" : str(ssh_string), \ + "ssh-path" : str(ssh_path), \ "remote" : kdump_remote } print(json.dumps(data, indent=4)) @@ -314,12 +314,12 @@ def get_kdump_num_dumps(): num_dumps = num return num_dumps -## Query current configuration for kdump ssh_connection_string +## Query current configuration for kdump ssh_string # # @return The ssh remote connection string for storing kernel dump files remotely # (read from running configuration) -def get_ssh_connection_string(): - ssh_connection_string = "user@ip/hostname --default, please change--" +def get_ssh_string(): + ssh_string = "username@serverip" config_db = ConfigDBConnector(use_unix_socket_path=True) if config_db is not None: config_db.connect() @@ -327,17 +327,17 @@ def get_ssh_connection_string(): if table_data is not None: config_data = table_data.get('config') if config_data is not None: - conn = config_data.get('ssh_connection_string') + conn = config_data.get('ssh_string') if conn: - ssh_connection_string = conn - return ssh_connection_string + ssh_string = conn + return ssh_string -## Query current configuration for kdump ssh_private_key_path +## Query current configuration for kdump ssh_path # # @return The ssh remote connection private_key_file_path for connecting to remote server without authentication (command "sudo kdump-config propagate" required) # (read from running configuration) -def get_ssh_private_key_path(): - ssh_private_key_path = "/root/.ssh/kdump_id_rsa --default, please change--" +def get_ssh_path(): + ssh_path = "/root/.ssh/kdump_id_rsa" config_db = ConfigDBConnector(use_unix_socket_path=True) if config_db is not None: config_db.connect() @@ -345,10 +345,10 @@ def get_ssh_private_key_path(): if table_data is not None: config_data = table_data.get('config') if config_data is not None: - pr_key_path = config_data.get('ssh_private_key_path') + pr_key_path = config_data.get('ssh_path') if pr_key_path: - ssh_private_key_path = pr_key_path - return ssh_private_key_path + ssh_path = pr_key_path + return ssh_path ## Query current configuration for kdump remote_enabled # @@ -436,7 +436,7 @@ def write_num_dumps(num_dumps): ## Read current value for SSH SSH kdump-ssh-connection-string in kdump config file # # @return The string value X from SSH=X in /etc/default/kdump-tools -def read_ssh_connection_string_from_db(): +def read_ssh_string_from_db(): config_db = ConfigDBConnector(use_unix_socket_path=True) config_db.connect() @@ -444,21 +444,21 @@ def read_ssh_connection_string_from_db(): if kdump_table: config_data = kdump_table.get('config') if config_data: - return config_data.get('ssh_connection_string') + return config_data.get('ssh_string') return None ## Change the value for SSH kdump-ssh-connection-string in kdump config file /etc/default/kdump-tools # # #param ssh-connection-string value for new value -def write_ssh_connection_string_to_kdump_tools(): - ssh_connection_string = read_ssh_connection_string_from_db() - if not ssh_connection_string: +def write_ssh_string_to_kdump_tools(): + ssh_string = read_ssh_string_from_db() + if not ssh_string: print("Error: SSH connection string not found in Config DB.") return False try: # Use sed to update the SSH connection string in the kdump-tools file - subprocess.run(['sudo', 'sed', '-i', 's/#SSH=.*/SSH="{}"/g'.format(ssh_connection_string), kdump_cfg], check=True) + subprocess.run(['sudo', 'sed', '-i', 's/#SSH=.*/SSH="{}"/g'.format( ssh_string), kdump_cfg], check=True) return True except Exception as e: @@ -469,7 +469,7 @@ def write_ssh_connection_string_to_kdump_tools(): ## Read current value for SSH_KEY kdump-ssh-private-key-path in kdump config file # # @return The string value X from SSH_KEY=X in /etc/default/kdump-tools -def read_ssh_private_key_path(kdump_cfg="/etc/default/kdump-tools"): +def read_ssh_path(kdump_cfg="/etc/default/kdump-tools"): """Reads the SSH private key path from the kdump configuration file. Args: @@ -500,11 +500,11 @@ def read_ssh_private_key_path(kdump_cfg="/etc/default/kdump-tools"): raise Exception(f"Error reading SSH private key path from {kdump_cfg}: {str(e)}") -def write_ssh_private_key_path(ssh_private_key_path, kdump_cfg="/etc/default/kdump-tools"): +def write_ssh_path( ssh_path, kdump_cfg="/etc/default/kdump-tools"): """Writes the SSH private key path to the kdump configuration file. Args: - ssh_private_key_path (str): The SSH private key path to write. + ssh_path (str): The SSH private key path to write. kdump_cfg (str, optional): The path to the kdump configuration file. Defaults to "/etc/default/kdump-tools". @@ -514,7 +514,7 @@ def write_ssh_private_key_path(ssh_private_key_path, kdump_cfg="/etc/default/kdu try: # Construct the full command string safely - command = f"sed -i 's/#*SSH_KEY=.*$/SSH_KEY={ssh_private_key_path}/' {kdump_cfg}" + command = f"sed -i 's/#*SSH_KEY=.*$/SSH_KEY={ssh_path}/' {kdump_cfg}" # Use subprocess.run without shell execution subprocess.run(command.split(), check=True) @@ -524,13 +524,13 @@ def write_ssh_private_key_path(ssh_private_key_path, kdump_cfg="/etc/default/kdu # Example usage -ssh_private_key_path = get_ssh_private_key_path() +ssh_path = get_ssh_path() # Write the retrieved value to kdump_cfg only if it's not the default value -if ssh_private_key_path != "/root/.ssh/kdump_id_rsa --default, please change--": - write_ssh_private_key_path(ssh_private_key_path) +if ssh_path != "/root/.ssh/kdump_id_rsa --default, please change--": + write_ssh_path(ssh_path) -# Now you can potentially read the written value using read_ssh_private_key_path(kdump_cfg) +# Now you can potentially read the written value using read_ssh_path(kdump_cfg) ## Disable remote: Comment the value for SSH and SSH_KEY in kdump config file /etc/default/kdump-tools # @@ -655,11 +655,11 @@ def cmd_kdump_enable(verbose, image): ## # ToDo - 02 ## - ssh_connection_string = get_ssh_connection_string() - ssh_private_key_path = get_ssh_private_key_path() + ssh_string = get_ssh_string() + ssh_path = get_ssh_path() remote = get_kdump_remote() if verbose: - print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d ssh_connection_string=%d ssh_private_key_path=%d remote=%d" % (kdump_enabled, memory, num_dumps, ssh_connection_string, ssh_private_key_path, remote)) + print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d ssh_string=%d ssh_path=%d remote=%d" % (kdump_enabled, memory, num_dumps, ssh_string, ssh_path, remote)) if os.path.exists(grub_cfg): return kdump_enable(verbose, kdump_enabled, memory, num_dumps, image, grub_cfg) From 7ae06354f81dc46f79fb6c067f08fbb03ad6fe02 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Thu, 11 Jul 2024 11:40:09 +0500 Subject: [PATCH 010/127] Corrected Functions --- scripts/sonic-kdump-config | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/scripts/sonic-kdump-config b/scripts/sonic-kdump-config index c68640308c..76f985b3e6 100755 --- a/scripts/sonic-kdump-config +++ b/scripts/sonic-kdump-config @@ -500,11 +500,11 @@ def read_ssh_path(kdump_cfg="/etc/default/kdump-tools"): raise Exception(f"Error reading SSH private key path from {kdump_cfg}: {str(e)}") -def write_ssh_path( ssh_path, kdump_cfg="/etc/default/kdump-tools"): +def write_ssh_path(ssh_path, kdump_cfg="/etc/default/kdump-tools"): """Writes the SSH private key path to the kdump configuration file. Args: - ssh_path (str): The SSH private key path to write. + ssh_path (str): The SSH private key path to write. kdump_cfg (str, optional): The path to the kdump configuration file. Defaults to "/etc/default/kdump-tools". @@ -513,23 +513,29 @@ def write_ssh_path( ssh_path, kdump_cfg="/etc/default/kdump-tools"): """ try: - # Construct the full command string safely - command = f"sed -i 's/#*SSH_KEY=.*$/SSH_KEY={ssh_path}/' {kdump_cfg}" + # Construct the full command as a list of arguments + command = ["sed", "-i", f"s|#*SSH_KEY=.*$|SSH_KEY={ssh_path}|", kdump_cfg] - # Use subprocess.run without shell execution - subprocess.run(command.split(), check=True) + # Use subprocess.run with the command list + subprocess.run(command, check=True) except subprocess.CalledProcessError as e: raise Exception(f"Error writing SSH private key path to {kdump_cfg}: {str(e)}") # Example usage +def get_ssh_path(): + # This is a placeholder for the function that retrieves the SSH path. + # Replace it with the actual implementation. + return "/root/.ssh/kdump_id_rsa" + ssh_path = get_ssh_path() # Write the retrieved value to kdump_cfg only if it's not the default value -if ssh_path != "/root/.ssh/kdump_id_rsa --default, please change--": +if ssh_path != "/root/.ssh/kdump_id_rsa": write_ssh_path(ssh_path) + # Now you can potentially read the written value using read_ssh_path(kdump_cfg) ## Disable remote: Comment the value for SSH and SSH_KEY in kdump config file /etc/default/kdump-tools From 518837ee5c6aa595d126837feb51ff43dafd3193 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Thu, 11 Jul 2024 15:21:52 +0500 Subject: [PATCH 011/127] Corrected Function Names --- config/kdump.py | 140 +++++++++++++++++++++++------------------------- show/kdump.py | 4 +- 2 files changed, 68 insertions(+), 76 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 6ab18bd541..8c5846c63a 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -122,113 +122,105 @@ def kdump_remote(db, action): echo_reboot_warning() # -# 'ssh_string' command ('sudo config kdump ssh_string ...') +# 'add_ssh_string' command ('sudo config kdump add ssh_connection_string ...') # - @kdump.command(name="add", short_help="Add ssh connection string.") -@click.argument('item', type=click.Choice(['ssh_string'])) +@click.argument('item', type=click.Choice(['ssh_connection_string'])) @click.argument('value', metavar='', required=True) @pass_db def add_ssh_string(db, item, value): """Add configuration item for kdump""" - if item == 'ssh_string': - kdump_table = db.cfgdb.get_table("KDUMP") - check_kdump_table_existence(kdump_table) + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) - # Check if remote mode is enabled - remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() - if remote_mode_enabled != "true": - click.echo("Error: Enable remote mode first.") - return + # Check if remote mode is enabled + remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() + if remote_mode_enabled != "true": + click.echo("Error: Enable remote mode first.") + return - # Check if SSH connection string is already added - existing_ssh_string = kdump_table.get("config", {}).get("ssh_string") - if existing_ssh_string: - click.echo("Error: SSH connection string is already added. Please remove it first before adding a new one.") - return + # Check if SSH connection string is already added + existing_ssh_string = kdump_table.get("config", {}).get("ssh_connection_string") + if existing_ssh_string: + click.echo("Error: SSH connection string is already added. Please remove it first before adding a new one.") + return - # Add SSH connection string to config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": value}) - echo_reboot_warning() - else: - click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") + # Add SSH connection string to config_db + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": value}) + echo_reboot_warning() +# +# 'remove_ssh_string' command ('sudo config kdump remove ssh_connection_string ...') +# @kdump.command(name="remove", short_help="Remove ssh connection string.") -@click.argument('item', type=click.Choice(['ssh_string'])) +@click.argument('item', type=click.Choice(['ssh_connection_string'])) @pass_db def remove_ssh_string(db, item): """Remove configuration item for kdump""" - if item == 'ssh_string': - kdump_table = db.cfgdb.get_table("KDUMP") - check_kdump_table_existence(kdump_table) + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) - # Check if SSH connection string is already added - existing_ssh_string = kdump_table.get("config", {}).get("ssh_string") - if not existing_ssh_string: - click.echo("Error: SSH connection string is not configured.") - return + # Check if SSH connection string is already added + existing_ssh_string = kdump_table.get("config", {}).get("ssh_connection_string") + if not existing_ssh_string: + click.echo("Error: SSH connection string is not configured.") + return - # Remove SSH connection string from config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": ""}) - click.echo("SSH connection string removed successfully.") - echo_reboot_warning() - else: - click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") + # Remove SSH connection string from config_db + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": ""}) + click.echo("SSH connection string removed successfully.") + echo_reboot_warning() # -# 'ssh_path' command ('sudo config kdump ssh_path ...') +# 'add_ssh_path' command ('sudo config kdump add ssh_private_key_path ...') # - @kdump.command(name="add", short_help="Add ssh private key path.") -@click.argument('item', type=click.Choice(['ssh_path'])) +@click.argument('item', type=click.Choice(['ssh_private_key_path'])) @click.argument('value', metavar='', required=True) @pass_db def add_ssh_path(db, item, value): """Add configuration item for kdump""" - if item == 'ssh_path': - kdump_table = db.cfgdb.get_table("KDUMP") - check_kdump_table_existence(kdump_table) + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) - # Check if remote mode is enabled - remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() - if remote_mode_enabled != "true": - click.echo("Error: Enable remote mode first.") - return + # Check if remote mode is enabled + remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() + if remote_mode_enabled != "true": + click.echo("Error: Enable remote mode first.") + return - # Check if SSH connection string is already added - existing_ssh_path = kdump_table.get("config", {}).get("ssh_path") - if existing_ssh_path: - click.echo("Error: SSH private key path is already added. Please remove it first before adding a new one.") - return + # Check if SSH private key path is already added + existing_ssh_path = kdump_table.get("config", {}).get("ssh_private_key_path") + if existing_ssh_path: + click.echo("Error: SSH private key path is already added. Please remove it first before adding a new one.") + return - # Add SSH connection string to config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": value}) - echo_reboot_warning() - else: - click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") + # Add SSH private key path to config_db + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": value}) + echo_reboot_warning() +# +# 'remove_ssh_path' command ('sudo config kdump remove ssh_private_key_path ...') +# @kdump.command(name="remove", short_help="Remove ssh private key path") -@click.argument('item', type=click.Choice(['ssh_path'])) +@click.argument('item', type=click.Choice(['ssh_private_key_path'])) @pass_db def remove_ssh_path(db, item): """Remove configuration item for kdump""" - if item == 'ssh_path': - kdump_table = db.cfgdb.get_table("KDUMP") - check_kdump_table_existence(kdump_table) - - # Check if SSH connection string is already added - existing_ssh_path = kdump_table.get("config", {}).get("ssh_path") - if not existing_ssh_path: - click.echo("Error: SSH key path is not configured.") - return - - # Remove SSH connection string from config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": ""}) - click.echo("SSH key path removed successfully.") - echo_reboot_warning() - else: - click.echo(f"Error: '{item}' is not a valid configuration item for kdump.") + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) + + # Check if SSH private key path is already added + existing_ssh_path = kdump_table.get("config", {}).get("ssh_private_key_path") + if not existing_ssh_path: + click.echo("Error: SSH private key path is not configured.") + return + + # Remove SSH private key path from config_db + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": ""}) + click.echo("SSH private key path removed successfully.") + echo_reboot_warning() diff --git a/show/kdump.py b/show/kdump.py index 3c4e62c92f..77b2198746 100644 --- a/show/kdump.py +++ b/show/kdump.py @@ -85,10 +85,10 @@ def config(): # remote SSH configs if get_kdump_config("remote") == "true": - ssh_conn_str = get_kdump_config("ssh_connection_string") + ssh_conn_str = get_kdump_config("ssh_string") click.echo("Kdump remote server user@ip/hostname: {}".format(ssh_conn_str)) - ssh_prv_key = get_kdump_config("ssh_private_key_path") + ssh_prv_key = get_kdump_config("ssh_path") click.echo("Kdump private key file path for remote ssh connection: {}".format(ssh_prv_key)) From d74f9099c8927afec19cc4f0bf1aae0c72d1af5e Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Thu, 11 Jul 2024 15:26:28 +0500 Subject: [PATCH 012/127] Corrected Function Names --- config/kdump.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/kdump.py b/config/kdump.py index 8c5846c63a..826aa1055f 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -125,6 +125,7 @@ def kdump_remote(db, action): # 'add_ssh_string' command ('sudo config kdump add ssh_connection_string ...') # + @kdump.command(name="add", short_help="Add ssh connection string.") @click.argument('item', type=click.Choice(['ssh_connection_string'])) @click.argument('value', metavar='', required=True) @@ -154,6 +155,7 @@ def add_ssh_string(db, item, value): # 'remove_ssh_string' command ('sudo config kdump remove ssh_connection_string ...') # + @kdump.command(name="remove", short_help="Remove ssh connection string.") @click.argument('item', type=click.Choice(['ssh_connection_string'])) @pass_db @@ -177,6 +179,7 @@ def remove_ssh_string(db, item): # 'add_ssh_path' command ('sudo config kdump add ssh_private_key_path ...') # + @kdump.command(name="add", short_help="Add ssh private key path.") @click.argument('item', type=click.Choice(['ssh_private_key_path'])) @click.argument('value', metavar='', required=True) @@ -206,6 +209,7 @@ def add_ssh_path(db, item, value): # 'remove_ssh_path' command ('sudo config kdump remove ssh_private_key_path ...') # + @kdump.command(name="remove", short_help="Remove ssh private key path") @click.argument('item', type=click.Choice(['ssh_private_key_path'])) @pass_db From 17e079054e9ab32219f7b1c54a7ec9db2e399517 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Thu, 11 Jul 2024 16:07:03 +0500 Subject: [PATCH 013/127] Corrected Function Names --- config/kdump.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 826aa1055f..54890e4ad4 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -142,13 +142,13 @@ def add_ssh_string(db, item, value): return # Check if SSH connection string is already added - existing_ssh_string = kdump_table.get("config", {}).get("ssh_connection_string") + existing_ssh_string = kdump_table.get("config", {}).get("ssh_string") if existing_ssh_string: click.echo("Error: SSH connection string is already added. Please remove it first before adding a new one.") return # Add SSH connection string to config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": value}) + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": value}) echo_reboot_warning() # @@ -157,7 +157,7 @@ def add_ssh_string(db, item, value): @kdump.command(name="remove", short_help="Remove ssh connection string.") -@click.argument('item', type=click.Choice(['ssh_connection_string'])) +@click.argument('item', type=click.Choice(['ssh_string'])) @pass_db def remove_ssh_string(db, item): """Remove configuration item for kdump""" @@ -165,13 +165,13 @@ def remove_ssh_string(db, item): check_kdump_table_existence(kdump_table) # Check if SSH connection string is already added - existing_ssh_string = kdump_table.get("config", {}).get("ssh_connection_string") + existing_ssh_string = kdump_table.get("config", {}).get("ssh_string") if not existing_ssh_string: click.echo("Error: SSH connection string is not configured.") return # Remove SSH connection string from config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_connection_string": ""}) + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": ""}) click.echo("SSH connection string removed successfully.") echo_reboot_warning() @@ -181,7 +181,7 @@ def remove_ssh_string(db, item): @kdump.command(name="add", short_help="Add ssh private key path.") -@click.argument('item', type=click.Choice(['ssh_private_key_path'])) +@click.argument('item', type=click.Choice(['ssh_path'])) @click.argument('value', metavar='', required=True) @pass_db def add_ssh_path(db, item, value): @@ -196,13 +196,13 @@ def add_ssh_path(db, item, value): return # Check if SSH private key path is already added - existing_ssh_path = kdump_table.get("config", {}).get("ssh_private_key_path") + existing_ssh_path = kdump_table.get("config", {}).get("ssh_path") if existing_ssh_path: click.echo("Error: SSH private key path is already added. Please remove it first before adding a new one.") return # Add SSH private key path to config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": value}) + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": value}) echo_reboot_warning() # @@ -211,7 +211,7 @@ def add_ssh_path(db, item, value): @kdump.command(name="remove", short_help="Remove ssh private key path") -@click.argument('item', type=click.Choice(['ssh_private_key_path'])) +@click.argument('item', type=click.Choice(['ssh_path'])) @pass_db def remove_ssh_path(db, item): """Remove configuration item for kdump""" @@ -225,6 +225,6 @@ def remove_ssh_path(db, item): return # Remove SSH private key path from config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_private_key_path": ""}) + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": ""}) click.echo("SSH private key path removed successfully.") echo_reboot_warning() From 999d298590ba6960a7190932e4cc8d471a0f0263 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Thu, 11 Jul 2024 17:37:01 +0500 Subject: [PATCH 014/127] Corrected Function Names --- scripts/sonic-kdump-config | 164 ++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 86 deletions(-) diff --git a/scripts/sonic-kdump-config b/scripts/sonic-kdump-config index 76f985b3e6..f63f1399ed 100755 --- a/scripts/sonic-kdump-config +++ b/scripts/sonic-kdump-config @@ -176,12 +176,12 @@ def cmd_dump_config_json(): # remote SSH variables ssh_string = get_ssh_string() ssh_path = get_ssh_path() - kdump_remote = get_kdump_remote() + kdump_remote = get_remote() data = { "enable" : kdump_enabled, \ "memory" : kdump_memory, \ "max-dumps" : int(kdump_num_dumps), \ - "ssh_string" : str(ssh_string), \ - "ssh_path" : str(ssh_path), \ + "ssh_string" : ssh_string, \ + "ssh_path" : ssh_path, \ "remote" : kdump_remote } print(json.dumps(data, indent=4)) @@ -234,14 +234,14 @@ def cmd_dump_status_json(): # remote SSH variables ssh_string = get_ssh_string() ssh_path = get_ssh_path() - kdump_remote = get_kdump_remote() + kdump_remote = get_remote() data = { "enable" : kdump_enabled, \ "current-state" : kdump_oper_state, \ "memory" : kdump_memory, \ "allocated-memory" : get_crash_kernel_size(), \ "max-dumps" : int(kdump_num_dumps), \ - "ssh-string" : str(ssh_string), \ - "ssh-path" : str(ssh_path), \ + "ssh_string" : ssh_string, \ + "ssh_path" : ssh_path, \ "remote" : kdump_remote } print(json.dumps(data, indent=4)) @@ -354,7 +354,7 @@ def get_ssh_path(): # # @return The if remote connection is enabled for connecting to remote server # (read from running configuration) -def get_kdump_remote(): +def get_remote(): remote = False config_db = ConfigDBConnector(use_unix_socket_path=True) if config_db is not None: @@ -436,91 +436,59 @@ def write_num_dumps(num_dumps): ## Read current value for SSH SSH kdump-ssh-connection-string in kdump config file # # @return The string value X from SSH=X in /etc/default/kdump-tools -def read_ssh_string_from_db(): - config_db = ConfigDBConnector(use_unix_socket_path=True) - config_db.connect() - - kdump_table = config_db.get_table('KDUMP') - if kdump_table: - config_data = kdump_table.get('config') - if config_data: - return config_data.get('ssh_string') - return None +def read_ssh_string(): + (rc, lines, err_str) = run_command("grep '#*SSH=.*' %s | cut -d = -f 2" % kdump_cfg, use_shell=True); + if rc == 0 and type(lines) == list and len(lines) >= 1: + try: + return int(lines[0]) + except Exception as e: + print_err('Error! Exception[%s] occured while reading from %s' %(str(e), kdump_cfg)) + sys.exit(1) + else: + print_err("Unable to read SSH from %s" % kdump_cfg) + sys.exit(1) ## Change the value for SSH kdump-ssh-connection-string in kdump config file /etc/default/kdump-tools # # #param ssh-connection-string value for new value -def write_ssh_string_to_kdump_tools(): - ssh_string = read_ssh_string_from_db() - if not ssh_string: - print("Error: SSH connection string not found in Config DB.") - return False - - try: - # Use sed to update the SSH connection string in the kdump-tools file - subprocess.run(['sudo', 'sed', '-i', 's/#SSH=.*/SSH="{}"/g'.format( ssh_string), kdump_cfg], check=True) - - return True - except Exception as e: - print("Error: Unable to write to {}. Exception: {}".format(kdump_cfg, str(e))) - return False +def write_ssh_string(ssh_string): + (rc, lines, err_str) = run_command("/bin/sed -i -e 's/#*SSH=.*/SSH=%d/' %s" % (ssh_string, kdump_cfg), use_shell=False); + if rc == 0 and type(lines) == list and len(lines) == 0: + ssh_string_in_cfg = read_ssh_string() + if ssh_string_in_cfg != ssh_string: + print_err("Unable to write SSH into %s" % kdump_cfg) + sys.exit(1) + else: + print_err("Error while writing SSH into %s" % kdump_cfg) + sys.exit(1) ## Read current value for SSH_KEY kdump-ssh-private-key-path in kdump config file # # @return The string value X from SSH_KEY=X in /etc/default/kdump-tools -def read_ssh_path(kdump_cfg="/etc/default/kdump-tools"): - """Reads the SSH private key path from the kdump configuration file. - - Args: - kdump_cfg (str, optional): The path to the kdump configuration file. - Defaults to "/etc/default/kdump-tools". - - Returns: - str: The extracted SSH private key path from the file or None if not found. - Raises: - Exception: If there's an error running the grep command. - """ - - try: - # Construct the full command string safely - command = f"grep '#*SSH_KEY=.*' {kdump_cfg} | cut -d = -f 2" - - # Use subprocess.run without shell execution - output = subprocess.run( - command.split(), capture_output=True, text=True, check=True - ).stdout.strip() - - if output: - return output # Existing SSH private key path found - else: - return None # No matching line found - - except subprocess.CalledProcessError as e: - raise Exception(f"Error reading SSH private key path from {kdump_cfg}: {str(e)}") - - -def write_ssh_path(ssh_path, kdump_cfg="/etc/default/kdump-tools"): - """Writes the SSH private key path to the kdump configuration file. - - Args: - ssh_path (str): The SSH private key path to write. - kdump_cfg (str, optional): The path to the kdump configuration file. - Defaults to "/etc/default/kdump-tools". - - Raises: - Exception: If there's an error writing to the file. - """ - - try: - # Construct the full command as a list of arguments - command = ["sed", "-i", f"s|#*SSH_KEY=.*$|SSH_KEY={ssh_path}|", kdump_cfg] - - # Use subprocess.run with the command list - subprocess.run(command, check=True) +def read_ssh_path(): + (rc, lines, err_str) = run_command("grep '#*SSH_KEY=.*' %s | cut -d = -f 2" % kdump_cfg, use_shell=True); + if rc == 0 and type(lines) == list and len(lines) >= 1: + try: + return int(lines[0]) + except Exception as e: + print_err('Error! Exception[%s] occured while reading from %s' %(str(e), kdump_cfg)) + sys.exit(1) + else: + print_err("Unable to read SSH_KEY from %s" % kdump_cfg) + sys.exit(1) - except subprocess.CalledProcessError as e: - raise Exception(f"Error writing SSH private key path to {kdump_cfg}: {str(e)}") +def write_ssh_path(ssh_path): + (rc, lines, err_str) = run_command("/bin/sed -i -e 's/#*SSH_KEY=.*/SSH_KEY=%d/' %s" % (ssh_path, kdump_cfg), use_shell=False); + if rc == 0 and type(lines) == list and len(lines) == 0: + ssh_path_in_cfg = read_ssh_path() + if ssh_path_in_cfg != ssh_path: + print_err("Unable to write SSH_KEY into %s" % kdump_cfg) + sys.exit(1) + else: + print_err("Error while writing SSH_KEY into %s" % kdump_cfg) + sys.exit(1) + return False # Example usage @@ -554,7 +522,7 @@ def kdump_remote_disable(): """ # Check if remote kdump is already enabled - if get_kdump_remote().lower() == "true": + if get_remote().lower() == "true": # Disable SSH key authentication rc = run_command("/bin/sed -i '/*SSH=\"/s/\#/' %s" % (kdump_cfg), use_shell=False) if rc != 0: @@ -663,9 +631,9 @@ def cmd_kdump_enable(verbose, image): ## ssh_string = get_ssh_string() ssh_path = get_ssh_path() - remote = get_kdump_remote() + remote = get_remote() if verbose: - print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d ssh_string=%d ssh_path=%d remote=%d" % (kdump_enabled, memory, num_dumps, ssh_string, ssh_path, remote)) + print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d ssh_string=%d ssh_path=%d remote=%d" % (kdump_enabled, memory, num_dumps, ssh_string, ssh_path, remote)) if os.path.exists(grub_cfg): return kdump_enable(verbose, kdump_enabled, memory, num_dumps, image, grub_cfg) @@ -743,9 +711,11 @@ def cmd_kdump_disable(verbose): kdump_enabled = get_kdump_administrative_mode() memory = get_kdump_memory() num_dumps = get_kdump_num_dumps() + ssh_path = get_ssh_path() + ssh_string = get_ssh_string() if verbose: - print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d" % (kdump_enabled, memory, num_dumps)) + print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d" % (kdump_enabled, memory, num_dumps, ssh_string, ssh_path)) if os.path.exists(grub_cfg): return kdump_disable(verbose, image, grub_cfg) @@ -792,6 +762,24 @@ def cmd_kdump_num_dumps(verbose, num_dumps): kdump_enabled = get_kdump_administrative_mode() kdump_memory = get_kdump_memory() +def cmd_ssh_string(verbose, ssh_string): + if ssh_string is None: + (rc, lines, err_str) = run_command("show kdump config", use_shell=False); + print('\n'.join(lines)) + else: + write_num_dumps(ssh_string) + kdump_enabled = get_kdump_administrative_mode() + kdump_memory = get_kdump_memory() + +def cmd_ssh_path(verbose, ssh_path): + if ssh_path is None: + (rc, lines, err_str) = run_command("show kdump config", use_shell=False); + print('\n'.join(lines)) + else: + write_num_dumps(ssh_path) + kdump_enabled = get_kdump_administrative_mode() + kdump_memory = get_kdump_memory() + def main(): @@ -869,6 +857,10 @@ def main(): cmd_kdump_memory(options.verbose, options.memory) elif options.num_dumps != False: cmd_kdump_num_dumps(options.verbose, options.num_dumps) + elif options.ssh_string != False: + cmd_ssh_string(options.verbose, options.ssh_string) + elif options.ssh_path != False: + cmd_ssh_path(options.verbose, options.ssh_path) elif options.dump_db: cmd_dump_db() elif options.status_json: From 361bb7594ba0aa5b0852e8919d2a9281e4925e09 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Fri, 12 Jul 2024 10:13:33 +0500 Subject: [PATCH 015/127] Corrected Function Names --- scripts/sonic-kdump-config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/sonic-kdump-config b/scripts/sonic-kdump-config index f63f1399ed..27d949046d 100755 --- a/scripts/sonic-kdump-config +++ b/scripts/sonic-kdump-config @@ -715,7 +715,7 @@ def cmd_kdump_disable(verbose): ssh_string = get_ssh_string() if verbose: - print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d" % (kdump_enabled, memory, num_dumps, ssh_string, ssh_path)) + print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d ssh_string=[%s] ssh_path=[%s]" % (kdump_enabled, memory, num_dumps, ssh_string, ssh_path)) if os.path.exists(grub_cfg): return kdump_disable(verbose, image, grub_cfg) From 48eb48d7678b919ebb9e9e5ca7c8f6af6fdd03b6 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Fri, 12 Jul 2024 10:53:11 +0500 Subject: [PATCH 016/127] Corrected Function Names --- config/kdump.py | 96 +++++++++++-------------------------------------- 1 file changed, 21 insertions(+), 75 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 54890e4ad4..f156b074c6 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -122,15 +122,15 @@ def kdump_remote(db, action): echo_reboot_warning() # -# 'add_ssh_string' command ('sudo config kdump add ssh_connection_string ...') +# 'add' command ('sudo config kdump add ...') # -@kdump.command(name="add", short_help="Add ssh connection string.") -@click.argument('item', type=click.Choice(['ssh_connection_string'])) +@kdump.command(name="add", short_help="Add ssh connection string or ssh private key path.") +@click.argument('item', type=click.Choice(['ssh_connection_string', 'ssh_path'])) @click.argument('value', metavar='', required=True) @pass_db -def add_ssh_string(db, item, value): +def add_kdump_item(db, item, value): """Add configuration item for kdump""" kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) @@ -141,90 +141,36 @@ def add_ssh_string(db, item, value): click.echo("Error: Enable remote mode first.") return - # Check if SSH connection string is already added - existing_ssh_string = kdump_table.get("config", {}).get("ssh_string") - if existing_ssh_string: - click.echo("Error: SSH connection string is already added. Please remove it first before adding a new one.") + # Check if the item is already added + existing_value = kdump_table.get("config", {}).get(item) + if existing_value: + click.echo(f"Error: {item.replace('_', ' ').capitalize()} is already added. Please remove it first before adding a new one.") return - # Add SSH connection string to config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": value}) + # Add item to config_db + db.cfgdb.mod_entry("KDUMP", "config", {item: value}) echo_reboot_warning() # -# 'remove_ssh_string' command ('sudo config kdump remove ssh_connection_string ...') +# 'remove' command ('sudo config kdump remove ...') # -@kdump.command(name="remove", short_help="Remove ssh connection string.") -@click.argument('item', type=click.Choice(['ssh_string'])) +@kdump.command(name="remove", short_help="Remove ssh connection string or ssh private key path.") +@click.argument('item', type=click.Choice(['ssh_connection_string', 'ssh_path'])) @pass_db -def remove_ssh_string(db, item): +def remove_kdump_item(db, item): """Remove configuration item for kdump""" kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) - # Check if SSH connection string is already added - existing_ssh_string = kdump_table.get("config", {}).get("ssh_string") - if not existing_ssh_string: - click.echo("Error: SSH connection string is not configured.") + # Check if the item is already added + existing_value = kdump_table.get("config", {}).get(item) + if not existing_value: + click.echo(f"Error: {item.replace('_', ' ').capitalize()} is not configured.") return - # Remove SSH connection string from config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": ""}) - click.echo("SSH connection string removed successfully.") - echo_reboot_warning() - -# -# 'add_ssh_path' command ('sudo config kdump add ssh_private_key_path ...') -# - - -@kdump.command(name="add", short_help="Add ssh private key path.") -@click.argument('item', type=click.Choice(['ssh_path'])) -@click.argument('value', metavar='', required=True) -@pass_db -def add_ssh_path(db, item, value): - """Add configuration item for kdump""" - kdump_table = db.cfgdb.get_table("KDUMP") - check_kdump_table_existence(kdump_table) - - # Check if remote mode is enabled - remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() - if remote_mode_enabled != "true": - click.echo("Error: Enable remote mode first.") - return - - # Check if SSH private key path is already added - existing_ssh_path = kdump_table.get("config", {}).get("ssh_path") - if existing_ssh_path: - click.echo("Error: SSH private key path is already added. Please remove it first before adding a new one.") - return - - # Add SSH private key path to config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": value}) - echo_reboot_warning() - -# -# 'remove_ssh_path' command ('sudo config kdump remove ssh_private_key_path ...') -# - - -@kdump.command(name="remove", short_help="Remove ssh private key path") -@click.argument('item', type=click.Choice(['ssh_path'])) -@pass_db -def remove_ssh_path(db, item): - """Remove configuration item for kdump""" - kdump_table = db.cfgdb.get_table("KDUMP") - check_kdump_table_existence(kdump_table) - - # Check if SSH private key path is already added - existing_ssh_path = kdump_table.get("config", {}).get("ssh_private_key_path") - if not existing_ssh_path: - click.echo("Error: SSH private key path is not configured.") - return - - # Remove SSH private key path from config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": ""}) - click.echo("SSH private key path removed successfully.") + # Remove item from config_db + db.cfgdb.mod_entry("KDUMP", "config", {item: ""}) + click.echo(f"{item.replace('_', ' ').capitalize()} removed successfully.") echo_reboot_warning() From 194f3336b1765d5b6f55f9168f109ed2e2069266 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Fri, 12 Jul 2024 11:00:47 +0500 Subject: [PATCH 017/127] Corrected Indentation --- config/kdump.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index f156b074c6..50f8fa0085 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -144,7 +144,8 @@ def add_kdump_item(db, item, value): # Check if the item is already added existing_value = kdump_table.get("config", {}).get(item) if existing_value: - click.echo(f"Error: {item.replace('_', ' ').capitalize()} is already added. Please remove it first before adding a new one.") + click.echo(f"Error: {item.replace('_', ' ').capitalize()} is already added.\ + \Please remove it first before adding a new one.") return # Add item to config_db From 45fe33e3d1a2ce11070ffbfae34da7e8812e344f Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Fri, 12 Jul 2024 11:07:28 +0500 Subject: [PATCH 018/127] Corrected Indentation --- config/kdump.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 50f8fa0085..c524a46511 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -144,8 +144,8 @@ def add_kdump_item(db, item, value): # Check if the item is already added existing_value = kdump_table.get("config", {}).get(item) if existing_value: - click.echo(f"Error: {item.replace('_', ' ').capitalize()} is already added.\ - \Please remove it first before adding a new one.") + click.echo(f"Error: {item.replace('_', ' ').capitalize()} is already added." + "Please remove it first before adding a new one.") return # Add item to config_db From 8c9766944d2ab0e140000257aa0a75845149f757 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Fri, 12 Jul 2024 17:05:21 +0500 Subject: [PATCH 019/127] Corrected Functions --- config/kdump.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index c524a46511..bf793a59fc 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -127,7 +127,7 @@ def kdump_remote(db, action): @kdump.command(name="add", short_help="Add ssh connection string or ssh private key path.") -@click.argument('item', type=click.Choice(['ssh_connection_string', 'ssh_path'])) +@click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @click.argument('value', metavar='', required=True) @pass_db def add_kdump_item(db, item, value): @@ -158,7 +158,7 @@ def add_kdump_item(db, item, value): @kdump.command(name="remove", short_help="Remove ssh connection string or ssh private key path.") -@click.argument('item', type=click.Choice(['ssh_connection_string', 'ssh_path'])) +@click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db def remove_kdump_item(db, item): """Remove configuration item for kdump""" From 2453d25ccfefd7190f3636c3e67e2678446fa2ff Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Fri, 19 Jul 2024 12:35:06 +0500 Subject: [PATCH 020/127] Corrected Functions --- config/kdump.py | 96 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 21 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index bf793a59fc..0c106358a1 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -126,12 +126,11 @@ def kdump_remote(db, action): # -@kdump.command(name="add", short_help="Add ssh connection string or ssh private key path.") -@click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) -@click.argument('value', metavar='', required=True) +@kdump.command(name="add", short_help="Add SSH connection string for kdump.") +@click.argument('ssh_string', metavar='', required=True) @pass_db -def add_kdump_item(db, item, value): - """Add configuration item for kdump""" +def add_kdump_ssh_string(db, ssh_string): + """Add SSH connection string for kdump""" kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) @@ -141,37 +140,92 @@ def add_kdump_item(db, item, value): click.echo("Error: Enable remote mode first.") return - # Check if the item is already added - existing_value = kdump_table.get("config", {}).get(item) + # Check if the SSH string is already added + existing_value = kdump_table.get("config", {}).get("ssh_string") if existing_value: - click.echo(f"Error: {item.replace('_', ' ').capitalize()} is already added." - "Please remove it first before adding a new one.") + click.echo("Error: SSH string is already added. Please remove it first before adding a new one.") return - # Add item to config_db - db.cfgdb.mod_entry("KDUMP", "config", {item: value}) + # Add SSH string to config_db + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": ssh_string}) echo_reboot_warning() + +@kdump.command(name="add", short_help="Add SSH key path for kdump.") +@click.argument('ssh_path', metavar='', required=True) +@pass_db +def add_kdump_ssh_key_path(db, ssh_path): + """Add SSH key path for kdump""" + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) + + # Check if remote mode is enabled + remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() + if remote_mode_enabled != "true": + click.echo("Error: Enable remote mode first.") + return + + # Check if the SSH string is already added + existing_value = kdump_table.get("config", {}).get("ssh_path") + if existing_value: + click.echo("Error: SSH key path is already added. Please remove it first before adding a new one.") + return + + # Add SSH string to config_db + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": ssh_path}) + echo_reboot_warning() # # 'remove' command ('sudo config kdump remove ...') # -@kdump.command(name="remove", short_help="Remove ssh connection string or ssh private key path.") -@click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) +@kdump.command(name="remove", short_help="Remove SSH connection string for kdump.") +@click.argument('ssh_string', metavar='', required=True) @pass_db -def remove_kdump_item(db, item): - """Remove configuration item for kdump""" +def remove_kdump_ssh_string(db, ssh_string): + """Reomve SSH connection string for kdump""" kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) - # Check if the item is already added - existing_value = kdump_table.get("config", {}).get(item) - if not existing_value: - click.echo(f"Error: {item.replace('_', ' ').capitalize()} is not configured.") + # Check if remote mode is enabled + remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() + if remote_mode_enabled != "true": + click.echo("Error: Enable remote mode first.") + return + + # Check if the SSH string is already added + existing_value = kdump_table.get("config", {}).get("ssh_string") + if existing_value: + click.echo("Error: SSH string is not configured.") + return + + # Remove item from config_db + db.cfgdb.mod_entry("KDUMP", "config", {ssh_string: ""}) + click.echo(f"{ssh_string.replace('_', ' ').capitalize()} removed successfully.") + echo_reboot_warning() + + +@kdump.command(name="remove", short_help="Remove SSH key path for kdump.") +@click.argument('ssh_path', metavar='', required=True) +@pass_db +def remove_kdump_ssh_string(db, ssh_path): + """Reomve SSH key path for kdump""" + kdump_table = db.cfgdb.get_table("KDUMP") + check_kdump_table_existence(kdump_table) + + # Check if remote mode is enabled + remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() + if remote_mode_enabled != "true": + click.echo("Error: Enable remote mode first.") + return + + # Check if the SSH string is already added + existing_value = kdump_table.get("config", {}).get("ssh_path") + if existing_value: + click.echo("Error: SSH key path is not configured.") return # Remove item from config_db - db.cfgdb.mod_entry("KDUMP", "config", {item: ""}) - click.echo(f"{item.replace('_', ' ').capitalize()} removed successfully.") + db.cfgdb.mod_entry("KDUMP", "config", {ssh_path: ""}) + click.echo(f"{ssh_path.replace('_', ' ').capitalize()} removed successfully.") echo_reboot_warning() From cfbdd132b43673c533cb7823b96c3c23c907e10b Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Fri, 19 Jul 2024 12:38:16 +0500 Subject: [PATCH 021/127] Corrected Functions --- config/kdump.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index 0c106358a1..9acc5c8fd4 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -208,7 +208,7 @@ def remove_kdump_ssh_string(db, ssh_string): @kdump.command(name="remove", short_help="Remove SSH key path for kdump.") @click.argument('ssh_path', metavar='', required=True) @pass_db -def remove_kdump_ssh_string(db, ssh_path): +def remove_kdump_ssh_key_path(db, ssh_path): """Reomve SSH key path for kdump""" kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) From 1b7091bec94c29b1311beec9313bf8244d8a5f74 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Fri, 19 Jul 2024 15:47:31 +0500 Subject: [PATCH 022/127] Corrected Functions --- config/kdump.py | 93 +++++++++++-------------------------------------- 1 file changed, 20 insertions(+), 73 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 9acc5c8fd4..9430e92cb1 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -126,11 +126,12 @@ def kdump_remote(db, action): # -@kdump.command(name="add", short_help="Add SSH connection string for kdump.") -@click.argument('ssh_string', metavar='', required=True) +@kdump.command(name="add", short_help="Add SSH connection string or SSH key path for kdump.") +@click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) +@click.argument('value', metavar='', required=True) @pass_db -def add_kdump_ssh_string(db, ssh_string): - """Add SSH connection string for kdump""" +def add_kdump_item(db, item, value): + """Add SSH connection string or SSH key path for kdump""" kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) @@ -140,22 +141,22 @@ def add_kdump_ssh_string(db, ssh_string): click.echo("Error: Enable remote mode first.") return - # Check if the SSH string is already added - existing_value = kdump_table.get("config", {}).get("ssh_string") + # Check if the item is already added + existing_value = kdump_table.get("config", {}).get(item) if existing_value: - click.echo("Error: SSH string is already added. Please remove it first before adding a new one.") + click.echo(f"Error: {item.replace('_', ' ').capitalize()} is already added. Please remove it first before adding a new one.") return - # Add SSH string to config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": ssh_string}) + # Add item to config_db + db.cfgdb.mod_entry("KDUMP", "config", {item: value}) echo_reboot_warning() -@kdump.command(name="add", short_help="Add SSH key path for kdump.") -@click.argument('ssh_path', metavar='', required=True) +@kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path for kdump.") +@click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db -def add_kdump_ssh_key_path(db, ssh_path): - """Add SSH key path for kdump""" +def remove_kdump_item(db, item): + """Remove SSH connection string or SSH key path for kdump""" kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) @@ -165,67 +166,13 @@ def add_kdump_ssh_key_path(db, ssh_path): click.echo("Error: Enable remote mode first.") return - # Check if the SSH string is already added - existing_value = kdump_table.get("config", {}).get("ssh_path") - if existing_value: - click.echo("Error: SSH key path is already added. Please remove it first before adding a new one.") - return - - # Add SSH string to config_db - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": ssh_path}) - echo_reboot_warning() -# -# 'remove' command ('sudo config kdump remove ...') -# - - -@kdump.command(name="remove", short_help="Remove SSH connection string for kdump.") -@click.argument('ssh_string', metavar='', required=True) -@pass_db -def remove_kdump_ssh_string(db, ssh_string): - """Reomve SSH connection string for kdump""" - kdump_table = db.cfgdb.get_table("KDUMP") - check_kdump_table_existence(kdump_table) - - # Check if remote mode is enabled - remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() - if remote_mode_enabled != "true": - click.echo("Error: Enable remote mode first.") - return - - # Check if the SSH string is already added - existing_value = kdump_table.get("config", {}).get("ssh_string") - if existing_value: - click.echo("Error: SSH string is not configured.") - return - - # Remove item from config_db - db.cfgdb.mod_entry("KDUMP", "config", {ssh_string: ""}) - click.echo(f"{ssh_string.replace('_', ' ').capitalize()} removed successfully.") - echo_reboot_warning() - - -@kdump.command(name="remove", short_help="Remove SSH key path for kdump.") -@click.argument('ssh_path', metavar='', required=True) -@pass_db -def remove_kdump_ssh_key_path(db, ssh_path): - """Reomve SSH key path for kdump""" - kdump_table = db.cfgdb.get_table("KDUMP") - check_kdump_table_existence(kdump_table) - - # Check if remote mode is enabled - remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() - if remote_mode_enabled != "true": - click.echo("Error: Enable remote mode first.") - return - - # Check if the SSH string is already added - existing_value = kdump_table.get("config", {}).get("ssh_path") - if existing_value: - click.echo("Error: SSH key path is not configured.") + # Check if the item is already configured + existing_value = kdump_table.get("config", {}).get(item) + if not existing_value: + click.echo(f"Error: {item.replace('_', ' ').capitalize()} is not configured.") return # Remove item from config_db - db.cfgdb.mod_entry("KDUMP", "config", {ssh_path: ""}) - click.echo(f"{ssh_path.replace('_', ' ').capitalize()} removed successfully.") + db.cfgdb.mod_entry("KDUMP", "config", {item: ""}) + click.echo(f"{item.replace('_', ' ').capitalize()} removed successfully.") echo_reboot_warning() From 82f80007e1c74902c51e4e9d0911ac1dde3a1a4a Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Fri, 19 Jul 2024 15:49:38 +0500 Subject: [PATCH 023/127] Corrected Functions --- config/kdump.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index 9430e92cb1..1a1860f74e 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -144,7 +144,8 @@ def add_kdump_item(db, item, value): # Check if the item is already added existing_value = kdump_table.get("config", {}).get(item) if existing_value: - click.echo(f"Error: {item.replace('_', ' ').capitalize()} is already added. Please remove it first before adding a new one.") + click.echo(f"Error: {item.replace('_', ' ').capitalize()} is already added.\ + \Please remove it first before adding a new one.") return # Add item to config_db From 7efce5d0df91a7cf0e360e2c23ba85c9ae4b3913 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Fri, 19 Jul 2024 15:51:19 +0500 Subject: [PATCH 024/127] Corrected Functions --- config/kdump.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index 1a1860f74e..48500ff0eb 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -145,7 +145,7 @@ def add_kdump_item(db, item, value): existing_value = kdump_table.get("config", {}).get(item) if existing_value: click.echo(f"Error: {item.replace('_', ' ').capitalize()} is already added.\ - \Please remove it first before adding a new one.") + Please remove it first before adding a new one.") return # Add item to config_db From 12f02796da12ff671f2c4668299e301d5fb5b490 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Fri, 19 Jul 2024 16:44:28 +0500 Subject: [PATCH 025/127] Corrected Functions --- config/kdump.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 48500ff0eb..55a0ecf416 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -144,8 +144,7 @@ def add_kdump_item(db, item, value): # Check if the item is already added existing_value = kdump_table.get("config", {}).get(item) if existing_value: - click.echo(f"Error: {item.replace('_', ' ').capitalize()} is already added.\ - Please remove it first before adding a new one.") + click.echo(f"Error: {item} is already added.") return # Add item to config_db @@ -170,10 +169,10 @@ def remove_kdump_item(db, item): # Check if the item is already configured existing_value = kdump_table.get("config", {}).get(item) if not existing_value: - click.echo(f"Error: {item.replace('_', ' ').capitalize()} is not configured.") + click.echo(f"Error: {item} is not configured.") return # Remove item from config_db db.cfgdb.mod_entry("KDUMP", "config", {item: ""}) - click.echo(f"{item.replace('_', ' ').capitalize()} removed successfully.") + click.echo(f"{item} removed successfully.") echo_reboot_warning() From 3c22c75385b7a0668cdcb99508d395234bef73f6 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Fri, 19 Jul 2024 16:57:56 +0500 Subject: [PATCH 026/127] Corrected Functions --- config/kdump.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 55a0ecf416..57e27d139a 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -160,12 +160,6 @@ def remove_kdump_item(db, item): kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) - # Check if remote mode is enabled - remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() - if remote_mode_enabled != "true": - click.echo("Error: Enable remote mode first.") - return - # Check if the item is already configured existing_value = kdump_table.get("config", {}).get(item) if not existing_value: From 6d2f7cee10a35f3e88fa7e8533679de67d3563ab Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Mon, 22 Jul 2024 15:28:54 +0500 Subject: [PATCH 027/127] Corrected Functions --- config/kdump.py | 133 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 116 insertions(+), 17 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 57e27d139a..ba8093f0b3 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -1,5 +1,6 @@ import sys import click +import re from utilities_common.cli import AbbreviationGroup, pass_db # @@ -108,25 +109,87 @@ def kdump_remote(db, action): kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) + # Fetch the current remote status current_remote_status = kdump_table.get("config", {}).get("remote", "false").lower() - if action.lower() == 'enable' and current_remote_status == 'true': - click.echo("Error: Kdump Remote Mode is already enabled.") - return - elif action.lower() == 'disable' and current_remote_status == 'false': - click.echo("Error: Kdump Remote Mode is already disabled.") - return - - remote = 'true' if action.lower() == 'enable' else 'false' - db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) - echo_reboot_warning() + if action.lower() == 'enable': + if current_remote_status == 'true': + # Check if SSH is already uncommented + with open('/etc/default/kdump-tools', 'r') as file: + content = file.read() + + if re.search(r'^\s*SSH\s*=', content, re.MULTILINE): + click.echo("Error: Kdump Remote Mode is already enabled.") + return + + # Uncomment SSH in /etc/default/kdump-tools + with open('/etc/default/kdump-tools', 'r') as file: + lines = file.readlines() + + with open('/etc/default/kdump-tools', 'w') as file: + for line in lines: + if line.strip().startswith('#SSH'): + file.write(line.lstrip('#')) + else: + file.write(line) + + click.echo("Kdump Remote Mode enabled.") + else: + # Enable remote mode + remote = 'true' + db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) + echo_reboot_warning() + + elif action.lower() == 'disable': + if current_remote_status == 'false': + click.echo("Error: Kdump Remote Mode is already disabled.") + return + + # Disable remote mode + remote = 'false' + db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) + + # Comment out SSH and SSH_KEY in /etc/default/kdump-tools + try: + # Read the current content of the file + with open('/etc/default/kdump-tools', 'r') as file: + lines = file.readlines() + + # Prepare new content + new_lines = [] + ssh_commented = False + ssh_key_commented = False + + for line in lines: + if line.strip().startswith('SSH'): + new_lines.append(f"#{line}") + ssh_commented = True + elif line.strip().startswith('SSH_KEY'): + new_lines.append(f"#{line}") + ssh_key_commented = True + else: + new_lines.append(line) + + # If SSH or SSH_KEY were not present, add commented lines at the end + if not ssh_commented: + new_lines.append("#SSH=\n") + if not ssh_key_commented: + new_lines.append("#SSH_KEY=\n") + + # Write the updated content back to the file + with open('/etc/default/kdump-tools', 'w') as file: + file.writelines(new_lines) + + click.echo("Kdump Remote Mode disabled. SSH settings commented out.") + except Exception as e: + click.echo(f"Error updating /etc/default/kdump-tools: {e}") # # 'add' command ('sudo config kdump add ...') # -@kdump.command(name="add", short_help="Add SSH connection string or SSH key path for kdump.") +@kdump.command(name="add", short_help="Add SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @click.argument('value', metavar='', required=True) @pass_db @@ -141,14 +204,50 @@ def add_kdump_item(db, item, value): click.echo("Error: Enable remote mode first.") return - # Check if the item is already added - existing_value = kdump_table.get("config", {}).get(item) - if existing_value: - click.echo(f"Error: {item} is already added.") - return - # Add item to config_db db.cfgdb.mod_entry("KDUMP", "config", {item: value}) + click.echo(f"{item} added to configuration.") + + # Check if both parameters are added + ssh_string = kdump_table.get("config", {}).get("ssh_string") + ssh_path = kdump_table.get("config", {}).get("ssh_path") + + # If both are present, update the file + if ssh_string and ssh_path: + try: + # Read the current content of the file + with open('/etc/default/kdump-tools', 'r') as file: + lines = file.readlines() + + # Prepare new content + new_lines = [] + ssh_string_added = False + ssh_path_added = False + + for line in lines: + if line.strip().startswith('#SSH') or line.strip().startswith('SSH'): + new_lines.append(f"SSH={ssh_string}\n") + ssh_string_added = True + elif line.strip().startswith('#SSH_KEY') or line.strip().startswith('SSH_KEY'): + new_lines.append(f"SSH_KEY={ssh_path}\n") + ssh_path_added = True + else: + new_lines.append(line) + + # If either SSH or SSH_KEY was not found, add them at the end of the file + if not ssh_string_added: + new_lines.append(f"SSH={ssh_string}\n") + if not ssh_path_added: + new_lines.append(f"SSH_KEY={ssh_path}\n") + + # Write the updated content back to the file + with open('/etc/default/kdump-tools', 'w') as file: + file.writelines(new_lines) + + click.echo("Updated /etc/default/kdump-tools with new SSH settings.") + except Exception as e: + click.echo(f"Error updating /etc/default/kdump-tools: {e}") + echo_reboot_warning() From f4856c7117aff866af5f20244ab63388d861f70e Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Mon, 22 Jul 2024 16:10:10 +0500 Subject: [PATCH 028/127] Corrected Functions --- config/kdump.py | 98 +++++++++++++++++-------------------------------- 1 file changed, 34 insertions(+), 64 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index ba8093f0b3..13d57eff55 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -1,8 +1,8 @@ import sys import click -import re -from utilities_common.cli import AbbreviationGroup, pass_db +from utilities_common.cli import AbbreviationGroup, pass_db +from pathlib import Path # # 'kdump' group ('sudo config kdump ...') # @@ -115,29 +115,24 @@ def kdump_remote(db, action): if action.lower() == 'enable': if current_remote_status == 'true': # Check if SSH is already uncommented - with open('/etc/default/kdump-tools', 'r') as file: - content = file.read() + file_path = Path('/etc/default/kdump-tools') + try: + content = file_path.read_text() + if "SSH=" in content: + click.echo("Error: Kdump Remote Mode is already enabled.") + return - if re.search(r'^\s*SSH\s*=', content, re.MULTILINE): - click.echo("Error: Kdump Remote Mode is already enabled.") - return + # Uncomment SSH and SSH_KEY in /etc/default/kdump-tools + new_content = content.replace('#SSH=', 'SSH=').replace('#SSH_KEY=', 'SSH_KEY=') + file_path.write_text(new_content) - # Uncomment SSH in /etc/default/kdump-tools - with open('/etc/default/kdump-tools', 'r') as file: - lines = file.readlines() + click.echo("Kdump Remote Mode enabled. SSH configuration updated.") + except Exception as e: + click.echo(f"Error updating /etc/default/kdump-tools: {e}") - with open('/etc/default/kdump-tools', 'w') as file: - for line in lines: - if line.strip().startswith('#SSH'): - file.write(line.lstrip('#')) - else: - file.write(line) - - click.echo("Kdump Remote Mode enabled.") else: # Enable remote mode - remote = 'true' - db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) echo_reboot_warning() elif action.lower() == 'disable': @@ -146,44 +141,21 @@ def kdump_remote(db, action): return # Disable remote mode - remote = 'false' - db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - # Comment out SSH and SSH_KEY in /etc/default/kdump-tools try: - # Read the current content of the file - with open('/etc/default/kdump-tools', 'r') as file: - lines = file.readlines() - - # Prepare new content - new_lines = [] - ssh_commented = False - ssh_key_commented = False - - for line in lines: - if line.strip().startswith('SSH'): - new_lines.append(f"#{line}") - ssh_commented = True - elif line.strip().startswith('SSH_KEY'): - new_lines.append(f"#{line}") - ssh_key_commented = True - else: - new_lines.append(line) + # Comment out SSH and SSH_KEY in /etc/default/kdump-tools + file_path = Path('/etc/default/kdump-tools') + content = file_path.read_text() + new_content = content.replace('SSH=', '#SSH=').replace('SSH_KEY=', '#SSH_KEY=') + file_path.write_text(new_content) - # If SSH or SSH_KEY were not present, add commented lines at the end - if not ssh_commented: - new_lines.append("#SSH=\n") - if not ssh_key_commented: - new_lines.append("#SSH_KEY=\n") - - # Write the updated content back to the file - with open('/etc/default/kdump-tools', 'w') as file: - file.writelines(new_lines) - - click.echo("Kdump Remote Mode disabled. SSH settings commented out.") + click.echo("Kdump Remote Mode disabled. SSH configuration commented out.") except Exception as e: click.echo(f"Error updating /etc/default/kdump-tools: {e}") + echo_reboot_warning() + # # 'add' command ('sudo config kdump add ...') # @@ -214,35 +186,33 @@ def add_kdump_item(db, item, value): # If both are present, update the file if ssh_string and ssh_path: + file_path = Path('/etc/default/kdump-tools') try: - # Read the current content of the file - with open('/etc/default/kdump-tools', 'r') as file: - lines = file.readlines() + content = file_path.read_text() + lines = content.splitlines() - # Prepare new content new_lines = [] ssh_string_added = False ssh_path_added = False for line in lines: - if line.strip().startswith('#SSH') or line.strip().startswith('SSH'): - new_lines.append(f"SSH={ssh_string}\n") + if line.strip().startswith('#SSH'): + new_lines.append(f"SSH={ssh_string}") ssh_string_added = True - elif line.strip().startswith('#SSH_KEY') or line.strip().startswith('SSH_KEY'): - new_lines.append(f"SSH_KEY={ssh_path}\n") + elif line.strip().startswith('#SSH_KEY'): + new_lines.append(f"SSH_KEY={ssh_path}") ssh_path_added = True else: new_lines.append(line) # If either SSH or SSH_KEY was not found, add them at the end of the file if not ssh_string_added: - new_lines.append(f"SSH={ssh_string}\n") + new_lines.append(f"SSH={ssh_string}") if not ssh_path_added: - new_lines.append(f"SSH_KEY={ssh_path}\n") + new_lines.append(f"SSH_KEY={ssh_path}") # Write the updated content back to the file - with open('/etc/default/kdump-tools', 'w') as file: - file.writelines(new_lines) + file_path.write_text('\n'.join(new_lines)) click.echo("Updated /etc/default/kdump-tools with new SSH settings.") except Exception as e: From 2d0cacbe31bb8f0cefe933d14c50bae7e300890c Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Mon, 22 Jul 2024 17:17:29 +0500 Subject: [PATCH 029/127] Corrected Functions --- config/kdump.py | 62 +++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 35 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 13d57eff55..3c320419e5 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -114,26 +114,21 @@ def kdump_remote(db, action): if action.lower() == 'enable': if current_remote_status == 'true': - # Check if SSH is already uncommented - file_path = Path('/etc/default/kdump-tools') - try: - content = file_path.read_text() - if "SSH=" in content: - click.echo("Error: Kdump Remote Mode is already enabled.") - return - - # Uncomment SSH and SSH_KEY in /etc/default/kdump-tools - new_content = content.replace('#SSH=', 'SSH=').replace('#SSH_KEY=', 'SSH_KEY=') - file_path.write_text(new_content) - - click.echo("Kdump Remote Mode enabled. SSH configuration updated.") - except Exception as e: - click.echo(f"Error updating /etc/default/kdump-tools: {e}") - - else: - # Enable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - echo_reboot_warning() + click.echo("Error: Kdump Remote Mode is already enabled.") + return + + # Enable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) + + # Uncomment SSH and SSH_KEY in /etc/default/kdump-tools + file_path = Path('/etc/default/kdump-tools') + try: + content = file_path.read_text() + new_content = content.replace('#SSH=', 'SSH=').replace('#SSH_KEY=', 'SSH_KEY=') + file_path.write_text(new_content) + click.echo("Kdump Remote Mode enabled.") + except Exception as e: + click.echo(f"Error updating /etc/default/kdump-tools: {e}") elif action.lower() == 'disable': if current_remote_status == 'false': @@ -142,15 +137,14 @@ def kdump_remote(db, action): # Disable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - + + # Comment out SSH and SSH_KEY in /etc/default/kdump-tools + file_path = Path('/etc/default/kdump-tools') try: - # Comment out SSH and SSH_KEY in /etc/default/kdump-tools - file_path = Path('/etc/default/kdump-tools') content = file_path.read_text() new_content = content.replace('SSH=', '#SSH=').replace('SSH_KEY=', '#SSH_KEY=') file_path.write_text(new_content) - - click.echo("Kdump Remote Mode disabled. SSH configuration commented out.") + click.echo("Kdump Remote Mode disabled.") except Exception as e: click.echo(f"Error updating /etc/default/kdump-tools: {e}") @@ -189,27 +183,25 @@ def add_kdump_item(db, item, value): file_path = Path('/etc/default/kdump-tools') try: content = file_path.read_text() - lines = content.splitlines() - new_lines = [] ssh_string_added = False ssh_path_added = False - for line in lines: - if line.strip().startswith('#SSH'): - new_lines.append(f"SSH={ssh_string}") + for line in content.splitlines(): + if line.strip().startswith('SSH='): + new_lines.append(f"SSH=\"{ssh_string}\"") ssh_string_added = True - elif line.strip().startswith('#SSH_KEY'): - new_lines.append(f"SSH_KEY={ssh_path}") + elif line.strip().startswith('SSH_KEY='): + new_lines.append(f"SSH_KEY=\"{ssh_path}\"") ssh_path_added = True else: new_lines.append(line) # If either SSH or SSH_KEY was not found, add them at the end of the file if not ssh_string_added: - new_lines.append(f"SSH={ssh_string}") + new_lines.append(f"SSH=\"{ssh_string}\"") if not ssh_path_added: - new_lines.append(f"SSH_KEY={ssh_path}") + new_lines.append(f"SSH_KEY=\"{ssh_path}\"") # Write the updated content back to the file file_path.write_text('\n'.join(new_lines)) @@ -221,7 +213,7 @@ def add_kdump_item(db, item, value): echo_reboot_warning() -@kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path for kdump.") +@kdump.command(name="remove", short_help="Remove SSH connection string.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db def remove_kdump_item(db, item): From c1f207620447d211b671dac3ac8d084e6fe6391c Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Mon, 22 Jul 2024 17:20:42 +0500 Subject: [PATCH 030/127] Corrected Functions --- config/kdump.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index 3c320419e5..aaba119a48 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -137,7 +137,7 @@ def kdump_remote(db, action): # Disable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - + # Comment out SSH and SSH_KEY in /etc/default/kdump-tools file_path = Path('/etc/default/kdump-tools') try: From d3e975a4775258925c5639d0e93aa1726f4e113b Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Mon, 22 Jul 2024 18:27:21 +0500 Subject: [PATCH 031/127] Corrected Functions --- config/kdump.py | 77 +++++++++++++++++-------------------------------- 1 file changed, 27 insertions(+), 50 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index aaba119a48..aa154e5f35 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -1,5 +1,6 @@ import sys import click +import re from utilities_common.cli import AbbreviationGroup, pass_db from pathlib import Path @@ -117,19 +118,6 @@ def kdump_remote(db, action): click.echo("Error: Kdump Remote Mode is already enabled.") return - # Enable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - - # Uncomment SSH and SSH_KEY in /etc/default/kdump-tools - file_path = Path('/etc/default/kdump-tools') - try: - content = file_path.read_text() - new_content = content.replace('#SSH=', 'SSH=').replace('#SSH_KEY=', 'SSH_KEY=') - file_path.write_text(new_content) - click.echo("Kdump Remote Mode enabled.") - except Exception as e: - click.echo(f"Error updating /etc/default/kdump-tools: {e}") - elif action.lower() == 'disable': if current_remote_status == 'false': click.echo("Error: Kdump Remote Mode is already disabled.") @@ -137,7 +125,7 @@ def kdump_remote(db, action): # Disable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - + # Comment out SSH and SSH_KEY in /etc/default/kdump-tools file_path = Path('/etc/default/kdump-tools') try: @@ -174,45 +162,34 @@ def add_kdump_item(db, item, value): db.cfgdb.mod_entry("KDUMP", "config", {item: value}) click.echo(f"{item} added to configuration.") - # Check if both parameters are added - ssh_string = kdump_table.get("config", {}).get("ssh_string") - ssh_path = kdump_table.get("config", {}).get("ssh_path") - - # If both are present, update the file - if ssh_string and ssh_path: - file_path = Path('/etc/default/kdump-tools') - try: - content = file_path.read_text() - new_lines = [] - ssh_string_added = False - ssh_path_added = False - - for line in content.splitlines(): - if line.strip().startswith('SSH='): - new_lines.append(f"SSH=\"{ssh_string}\"") - ssh_string_added = True - elif line.strip().startswith('SSH_KEY='): - new_lines.append(f"SSH_KEY=\"{ssh_path}\"") - ssh_path_added = True - else: - new_lines.append(line) - - # If either SSH or SSH_KEY was not found, add them at the end of the file - if not ssh_string_added: - new_lines.append(f"SSH=\"{ssh_string}\"") - if not ssh_path_added: - new_lines.append(f"SSH_KEY=\"{ssh_path}\"") - - # Write the updated content back to the file - file_path.write_text('\n'.join(new_lines)) - - click.echo("Updated /etc/default/kdump-tools with new SSH settings.") - except Exception as e: - click.echo(f"Error updating /etc/default/kdump-tools: {e}") + # Retrieve updated values + ssh_string = kdump_table.get("config", {}).get("ssh_string", "") + ssh_path = kdump_table.get("config", {}).get("ssh_path", "") + + file_path = Path('/etc/default/kdump-tools') + try: + content = file_path.read_text() + + # Uncomment and update SSH and SSH_KEY values + new_content = content + if ssh_string: + new_content = re.sub(r'^\s*#?SSH=.*$', f'SSH="{ssh_string}"', new_content, flags=re.MULTILINE) + if ssh_path: + new_content = re.sub(r'^\s*#?SSH_KEY=.*$', f'SSH_KEY="{ssh_path}"', new_content, flags=re.MULTILINE) + + # Add new lines if not present + if not re.search(r'^\s*#?SSH=', new_content, flags=re.MULTILINE): + new_content += f'\nSSH="{ssh_string}"' + if not re.search(r'^\s*#?SSH_KEY=', new_content, flags=re.MULTILINE): + new_content += f'\nSSH_KEY="{ssh_path}"' + + file_path.write_text(new_content) + click.echo("Updated /etc/default/kdump-tools with new SSH settings.") + except Exception as e: + click.echo(f"Error updating /etc/default/kdump-tools: {e}") echo_reboot_warning() - @kdump.command(name="remove", short_help="Remove SSH connection string.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db From 18895d33d91ba1b81e33fe5485857b86f397fd48 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Mon, 22 Jul 2024 18:36:37 +0500 Subject: [PATCH 032/127] Corrected Functions --- config/kdump.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index aa154e5f35..ae8d78133f 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -125,7 +125,7 @@ def kdump_remote(db, action): # Disable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - + # Comment out SSH and SSH_KEY in /etc/default/kdump-tools file_path = Path('/etc/default/kdump-tools') try: @@ -169,14 +169,14 @@ def add_kdump_item(db, item, value): file_path = Path('/etc/default/kdump-tools') try: content = file_path.read_text() - + # Uncomment and update SSH and SSH_KEY values new_content = content if ssh_string: new_content = re.sub(r'^\s*#?SSH=.*$', f'SSH="{ssh_string}"', new_content, flags=re.MULTILINE) if ssh_path: new_content = re.sub(r'^\s*#?SSH_KEY=.*$', f'SSH_KEY="{ssh_path}"', new_content, flags=re.MULTILINE) - + # Add new lines if not present if not re.search(r'^\s*#?SSH=', new_content, flags=re.MULTILINE): new_content += f'\nSSH="{ssh_string}"' @@ -190,6 +190,7 @@ def add_kdump_item(db, item, value): echo_reboot_warning() + @kdump.command(name="remove", short_help="Remove SSH connection string.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db From 4fe15b178afbf905100e42dbcbc58d0e4d14c8fa Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Tue, 23 Jul 2024 10:45:49 +0500 Subject: [PATCH 033/127] Corrected Functions --- config/kdump.py | 70 ++++++++++++++++++------------------------------- 1 file changed, 26 insertions(+), 44 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index ae8d78133f..22d4ca0830 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -102,6 +102,11 @@ def kdump_num_dumps(db, kdump_num_dumps): # +# +# 'remote' command ('sudo config kdump remote ...') +# + + @kdump.command(name="remote", short_help="Enable or Disable Kdump Remote") @click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) @pass_db @@ -110,32 +115,17 @@ def kdump_remote(db, action): kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) - # Fetch the current remote status current_remote_status = kdump_table.get("config", {}).get("remote", "false").lower() - if action.lower() == 'enable': - if current_remote_status == 'true': - click.echo("Error: Kdump Remote Mode is already enabled.") - return - - elif action.lower() == 'disable': - if current_remote_status == 'false': - click.echo("Error: Kdump Remote Mode is already disabled.") - return - - # Disable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - - # Comment out SSH and SSH_KEY in /etc/default/kdump-tools - file_path = Path('/etc/default/kdump-tools') - try: - content = file_path.read_text() - new_content = content.replace('SSH=', '#SSH=').replace('SSH_KEY=', '#SSH_KEY=') - file_path.write_text(new_content) - click.echo("Kdump Remote Mode disabled.") - except Exception as e: - click.echo(f"Error updating /etc/default/kdump-tools: {e}") + if action.lower() == 'enable' and current_remote_status == 'true': + click.echo("Error: Kdump Remote Mode is already enabled.") + return + elif action.lower() == 'disable' and current_remote_status == 'false': + click.echo("Error: Kdump Remote Mode is already disabled.") + return + remote = 'true' if action.lower() == 'enable' else 'false' + db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) echo_reboot_warning() # @@ -143,12 +133,9 @@ def kdump_remote(db, action): # -@kdump.command(name="add", short_help="Add SSH connection string or SSH key path.") -@click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) -@click.argument('value', metavar='', required=True) -@pass_db def add_kdump_item(db, item, value): """Add SSH connection string or SSH key path for kdump""" + kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) @@ -158,36 +145,31 @@ def add_kdump_item(db, item, value): click.echo("Error: Enable remote mode first.") return - # Add item to config_db - db.cfgdb.mod_entry("KDUMP", "config", {item: value}) - click.echo(f"{item} added to configuration.") - - # Retrieve updated values + # Retrieve updated values from config_db ssh_string = kdump_table.get("config", {}).get("ssh_string", "") ssh_path = kdump_table.get("config", {}).get("ssh_path", "") file_path = Path('/etc/default/kdump-tools') try: + # Read the content of the file content = file_path.read_text() - # Uncomment and update SSH and SSH_KEY values - new_content = content - if ssh_string: - new_content = re.sub(r'^\s*#?SSH=.*$', f'SSH="{ssh_string}"', new_content, flags=re.MULTILINE) - if ssh_path: - new_content = re.sub(r'^\s*#?SSH_KEY=.*$', f'SSH_KEY="{ssh_path}"', new_content, flags=re.MULTILINE) + # Define replacement functions with capture groups + def replace_ssh(match): + return f'SSH="{ssh_string}"' if ssh_string else match.group(0) + + def replace_ssh_key(match): + return f'SSH_KEY="{ssh_path}"' if ssh_path else match.group(0) - # Add new lines if not present - if not re.search(r'^\s*#?SSH=', new_content, flags=re.MULTILINE): - new_content += f'\nSSH="{ssh_string}"' - if not re.search(r'^\s*#?SSH_KEY=', new_content, flags=re.MULTILINE): - new_content += f'\nSSH_KEY="{ssh_path}"' + # Apply replacements using capture groups + new_content = re.sub(r"(^\s*#?SSH=)(.*)", replace_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"(^\s*#?SSH_KEY=)(.*)", replace_ssh_key, new_content, flags=re.MULTILINE) + # Write the updated content back to the file file_path.write_text(new_content) click.echo("Updated /etc/default/kdump-tools with new SSH settings.") except Exception as e: click.echo(f"Error updating /etc/default/kdump-tools: {e}") - echo_reboot_warning() From 8588324fc26e62b1287d4554cb6af51b57f8d258 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Tue, 23 Jul 2024 11:50:14 +0500 Subject: [PATCH 034/127] Corrected Functions --- config/kdump.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/kdump.py b/config/kdump.py index 22d4ca0830..ec9e97de7f 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -133,6 +133,10 @@ def kdump_remote(db, action): # +@kdump.command(name="add", short_help="Add SSH connection string or SSH key path.") +@click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) +@click.argument('value', metavar='', required=True) +@pass_db def add_kdump_item(db, item, value): """Add SSH connection string or SSH key path for kdump""" From 65a07b095b8562f31b3f31d17baba8da91b7536b Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Tue, 23 Jul 2024 12:23:45 +0500 Subject: [PATCH 035/127] Corrected Functions --- config/kdump.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index ec9e97de7f..e8673315a7 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -102,11 +102,6 @@ def kdump_num_dumps(db, kdump_num_dumps): # -# -# 'remote' command ('sudo config kdump remote ...') -# - - @kdump.command(name="remote", short_help="Enable or Disable Kdump Remote") @click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) @pass_db @@ -133,13 +128,12 @@ def kdump_remote(db, action): # -@kdump.command(name="add", short_help="Add SSH connection string or SSH key path.") +@kdump.command(name="add", short_help="Add SSH connection string or SSH key path for kdump.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @click.argument('value', metavar='', required=True) @pass_db def add_kdump_item(db, item, value): """Add SSH connection string or SSH key path for kdump""" - kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) @@ -149,7 +143,17 @@ def add_kdump_item(db, item, value): click.echo("Error: Enable remote mode first.") return + # Check if the item is already added + existing_value = kdump_table.get("config", {}).get(item) + if existing_value: + click.echo(f"Error: {item} is already added.") + return + + # Add item to config_db + db.cfgdb.mod_entry("KDUMP", "config", {item: value}) + # Retrieve updated values from config_db + kdump_table = db.cfgdb.get_table("KDUMP") ssh_string = kdump_table.get("config", {}).get("ssh_string", "") ssh_path = kdump_table.get("config", {}).get("ssh_path", "") @@ -166,8 +170,8 @@ def replace_ssh_key(match): return f'SSH_KEY="{ssh_path}"' if ssh_path else match.group(0) # Apply replacements using capture groups - new_content = re.sub(r"(^\s*#?SSH=)(.*)", replace_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"(^\s*#?SSH_KEY=)(.*)", replace_ssh_key, new_content, flags=re.MULTILINE) + new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", replace_ssh_key, new_content, flags=re.MULTILINE) # Write the updated content back to the file file_path.write_text(new_content) @@ -177,7 +181,7 @@ def replace_ssh_key(match): echo_reboot_warning() -@kdump.command(name="remove", short_help="Remove SSH connection string.") +@kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path for kdump.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db def remove_kdump_item(db, item): From 247cb2495aaab7fd96a920fa56c889786a851586 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Tue, 23 Jul 2024 15:21:03 +0500 Subject: [PATCH 036/127] Corrected Functions --- config/kdump.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++-- show/kdump.py | 4 ++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index e8673315a7..36f7e66c52 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -121,6 +121,27 @@ def kdump_remote(db, action): remote = 'true' if action.lower() == 'enable' else 'false' db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) + + if action.lower() == 'disable': + file_path = Path('/etc/default/kdump-tools') + try: + # Read the content of the file + content = file_path.read_text() + + # Define replacement functions with capture groups + def comment_ssh(match): + return f'# {match.group(0)}' + + # Apply replacements using capture groups + new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", comment_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", comment_ssh, new_content, flags=re.MULTILINE) + + # Write the updated content back to the file + file_path.write_text(new_content) + click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") + except Exception as e: + click.echo(f"Error updating /etc/default/kdump-tools: {e}") + echo_reboot_warning() # @@ -128,7 +149,7 @@ def kdump_remote(db, action): # -@kdump.command(name="add", short_help="Add SSH connection string or SSH key path for kdump.") +@kdump.command(name="add", short_help="Add SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @click.argument('value', metavar='', required=True) @pass_db @@ -181,7 +202,7 @@ def replace_ssh_key(match): echo_reboot_warning() -@kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path for kdump.") +@kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db def remove_kdump_item(db, item): @@ -197,5 +218,33 @@ def remove_kdump_item(db, item): # Remove item from config_db db.cfgdb.mod_entry("KDUMP", "config", {item: ""}) + + # Retrieve updated values from config_db + kdump_table = db.cfgdb.get_table("KDUMP") + ssh_string = kdump_table.get("config", {}).get("ssh_string", "") + ssh_path = kdump_table.get("config", {}).get("ssh_path", "") + + file_path = Path('/etc/default/kdump-tools') + try: + # Read the content of the file + content = file_path.read_text() + + # Define replacement functions with capture groups + def replace_ssh(match): + return 'SSH=""' if item == "ssh_string" else match.group(0) + + def replace_ssh_key(match): + return 'SSH_KEY=""' if item == "ssh_path" else match.group(0) + + # Apply replacements using capture groups + new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", replace_ssh_key, new_content, flags=re.MULTILINE) + + # Write the updated content back to the file + file_path.write_text(new_content) + click.echo("Updated /etc/default/kdump-tools with empty SSH settings.") + except Exception as e: + click.echo(f"Error updating /etc/default/kdump-tools: {e}") + click.echo(f"{item} removed successfully.") echo_reboot_warning() diff --git a/show/kdump.py b/show/kdump.py index 77b2198746..7cdc1a00c3 100644 --- a/show/kdump.py +++ b/show/kdump.py @@ -86,10 +86,10 @@ def config(): # remote SSH configs if get_kdump_config("remote") == "true": ssh_conn_str = get_kdump_config("ssh_string") - click.echo("Kdump remote server user@ip/hostname: {}".format(ssh_conn_str)) + click.echo("Kdump ssh connection string: {}".format(ssh_conn_str)) ssh_prv_key = get_kdump_config("ssh_path") - click.echo("Kdump private key file path for remote ssh connection: {}".format(ssh_prv_key)) + click.echo("Kdump private key path: {}".format(ssh_prv_key)) def get_kdump_core_files(): From edd60cf21e6f847f8842017f91298f1283d4707f Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Tue, 23 Jul 2024 15:26:37 +0500 Subject: [PATCH 037/127] Corrected Functions --- config/kdump.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 36f7e66c52..ae303ade2d 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -221,8 +221,6 @@ def remove_kdump_item(db, item): # Retrieve updated values from config_db kdump_table = db.cfgdb.get_table("KDUMP") - ssh_string = kdump_table.get("config", {}).get("ssh_string", "") - ssh_path = kdump_table.get("config", {}).get("ssh_path", "") file_path = Path('/etc/default/kdump-tools') try: From c9d4b83e3dd64ccfe708ab30d48dce8559b07d52 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Tue, 23 Jul 2024 16:00:26 +0500 Subject: [PATCH 038/127] Corrected Functions --- config/kdump.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index ae303ade2d..a7e736a8ad 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -229,10 +229,10 @@ def remove_kdump_item(db, item): # Define replacement functions with capture groups def replace_ssh(match): - return 'SSH=""' if item == "ssh_string" else match.group(0) + return '#SSH=""' if item == "ssh_string" else match.group(0) def replace_ssh_key(match): - return 'SSH_KEY=""' if item == "ssh_path" else match.group(0) + return '#SSH_KEY=""' if item == "ssh_path" else match.group(0) # Apply replacements using capture groups new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) From 668a3574fa39c5e29ab0998545301560b1326931 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 24 Jul 2024 12:56:55 +0500 Subject: [PATCH 039/127] Before Disabling Remove Key and Path --- config/kdump.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index a7e736a8ad..7bc28da4f5 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -104,10 +104,12 @@ def kdump_num_dumps(db, kdump_num_dumps): @kdump.command(name="remote", short_help="Enable or Disable Kdump Remote") @click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) -@pass_db -def kdump_remote(db, action): +@click.pass_context +def kdump_remote(ctx, action): """Enable or Disable Kdump Remote Mode""" - kdump_table = db.cfgdb.get_table("KDUMP") + db = ConfigDBConnector() + db.connect() + kdump_table = db.get_table("KDUMP") check_kdump_table_existence(kdump_table) current_remote_status = kdump_table.get("config", {}).get("remote", "false").lower() @@ -119,8 +121,16 @@ def kdump_remote(db, action): click.echo("Error: Kdump Remote Mode is already disabled.") return + if action.lower() == 'disable': + ssh_string = kdump_table.get("config", {}).get("ssh_string", None) + ssh_key = kdump_table.get("config", {}).get("ssh_key", None) + + if ssh_string or ssh_key: + click.echo("Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode.") + return + remote = 'true' if action.lower() == 'enable' else 'false' - db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) + db.mod_entry("KDUMP", "config", {"remote": remote}) if action.lower() == 'disable': file_path = Path('/etc/default/kdump-tools') From 16927d04dbd45841217be008984bb5b65bce080f Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 24 Jul 2024 13:10:05 +0500 Subject: [PATCH 040/127] b4 disable remove key and path --- config/kdump.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 7bc28da4f5..75bfa7a336 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -7,6 +7,7 @@ # # 'kdump' group ('sudo config kdump ...') # + @click.group(cls=AbbreviationGroup, name="kdump") def kdump(): """Configure the KDUMP mechanism""" @@ -104,12 +105,10 @@ def kdump_num_dumps(db, kdump_num_dumps): @kdump.command(name="remote", short_help="Enable or Disable Kdump Remote") @click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) -@click.pass_context -def kdump_remote(ctx, action): +@pass_db +def kdump_remote(db, action): """Enable or Disable Kdump Remote Mode""" - db = ConfigDBConnector() - db.connect() - kdump_table = db.get_table("KDUMP") + kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) current_remote_status = kdump_table.get("config", {}).get("remote", "false").lower() From 301b95364748babd61078f19bbb85830ec7dfbbd Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 24 Jul 2024 15:43:31 +0500 Subject: [PATCH 041/127] resolved db.mode_entry issue --- config/kdump.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index 75bfa7a336..7e450644db 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -129,7 +129,7 @@ def kdump_remote(db, action): return remote = 'true' if action.lower() == 'enable' else 'false' - db.mod_entry("KDUMP", "config", {"remote": remote}) + db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) if action.lower() == 'disable': file_path = Path('/etc/default/kdump-tools') From d32c898c98912dc33d7b044c9d224f274372dfc5 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 24 Jul 2024 18:03:32 +0500 Subject: [PATCH 042/127] removed hash from Remove function --- config/kdump.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 7e450644db..139734c7b7 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -238,10 +238,10 @@ def remove_kdump_item(db, item): # Define replacement functions with capture groups def replace_ssh(match): - return '#SSH=""' if item == "ssh_string" else match.group(0) + return 'SSH=""' if item == "ssh_string" else match.group(0) def replace_ssh_key(match): - return '#SSH_KEY=""' if item == "ssh_path" else match.group(0) + return 'SSH_KEY=""' if item == "ssh_path" else match.group(0) # Apply replacements using capture groups new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) From 26530b904de1d84f63f7f02b3d3fbb0cef449118 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 25 Jul 2024 10:46:16 +0500 Subject: [PATCH 043/127] erased space line 142 kdump.py --- config/kdump.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index 139734c7b7..fe4d536eaa 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -139,7 +139,7 @@ def kdump_remote(db, action): # Define replacement functions with capture groups def comment_ssh(match): - return f'# {match.group(0)}' + return f'#{match.group(0)}' # Apply replacements using capture groups new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", comment_ssh, content, flags=re.MULTILINE) From fb3c04cc7b8839d5501efffe7c2dac4f8fc54d6a Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Thu, 25 Jul 2024 12:00:11 +0500 Subject: [PATCH 044/127] Corrected Functions --- config/kdump.py | 101 +++++++++++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 35 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index a7e736a8ad..3d93ad96ef 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -122,28 +122,43 @@ def kdump_remote(db, action): remote = 'true' if action.lower() == 'enable' else 'false' db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) - if action.lower() == 'disable': - file_path = Path('/etc/default/kdump-tools') - try: - # Read the content of the file - content = file_path.read_text() + file_path = Path('/etc/default/kdump-tools') + try: + # Read the content of the file + content = file_path.read_text() - # Define replacement functions with capture groups + if action.lower() == 'enable': + # Define replacement functions with capture groups for uncommenting + def uncomment_ssh(match): + return match.group(0).lstrip('#') + + # Apply replacements using capture groups for uncommenting + new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", uncomment_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", uncomment_ssh, new_content, flags=re.MULTILINE) + + # Write the updated content back to the file + file_path.write_text(new_content) + click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY uncommented.") + + elif action.lower() == 'disable': + # Define replacement functions with capture groups for commenting def comment_ssh(match): - return f'# {match.group(0)}' + return f'#{match.group(0)}' if not match.group(0).startswith('#') else match.group(0) - # Apply replacements using capture groups - new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", comment_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", comment_ssh, new_content, flags=re.MULTILINE) + # Apply replacements using capture groups for commenting + new_content = re.sub(r"^\s*\s*SSH\s*=\s*.*$", comment_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"^\s*\s*SSH_KEY\s*=\s*.*$", comment_ssh, new_content, flags=re.MULTILINE) # Write the updated content back to the file file_path.write_text(new_content) click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") - except Exception as e: - click.echo(f"Error updating /etc/default/kdump-tools: {e}") + + except Exception as e: + click.echo(f"Error updating /etc/default/kdump-tools: {e}") echo_reboot_warning() + # # 'add' command ('sudo config kdump add ...') # @@ -164,12 +179,6 @@ def add_kdump_item(db, item, value): click.echo("Error: Enable remote mode first.") return - # Check if the item is already added - existing_value = kdump_table.get("config", {}).get(item) - if existing_value: - click.echo(f"Error: {item} is already added.") - return - # Add item to config_db db.cfgdb.mod_entry("KDUMP", "config", {item: value}) @@ -183,25 +192,35 @@ def add_kdump_item(db, item, value): # Read the content of the file content = file_path.read_text() - # Define replacement functions with capture groups + # Check if SSH and SSH_KEY are uncommented and update them + ssh_uncommented = bool(re.search(r"^\s*SSH\s*=\s*.*$", content, flags=re.MULTILINE)) + ssh_key_uncommented = bool(re.search(r"^\s*SSH_KEY\s*=\s*.*$", content, flags=re.MULTILINE)) + + if not ssh_uncommented or not ssh_key_uncommented: + click.echo("Error: Enable remote mode first.") + return + + # Define replacement functions def replace_ssh(match): return f'SSH="{ssh_string}"' if ssh_string else match.group(0) def replace_ssh_key(match): return f'SSH_KEY="{ssh_path}"' if ssh_path else match.group(0) - # Apply replacements using capture groups - new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", replace_ssh_key, new_content, flags=re.MULTILINE) + # Apply replacements + new_content = re.sub(r"^\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"^\s*SSH_KEY\s*=\s*.*$", replace_ssh_key, new_content, flags=re.MULTILINE) # Write the updated content back to the file file_path.write_text(new_content) click.echo("Updated /etc/default/kdump-tools with new SSH settings.") except Exception as e: click.echo(f"Error updating /etc/default/kdump-tools: {e}") + echo_reboot_warning() + @kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db @@ -216,27 +235,39 @@ def remove_kdump_item(db, item): click.echo(f"Error: {item} is not configured.") return + # Check if remote mode is enabled + remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower() + if remote_mode_enabled != "true": + click.echo("Error: Remote mode is not enabled.") + return + # Remove item from config_db db.cfgdb.mod_entry("KDUMP", "config", {item: ""}) - # Retrieve updated values from config_db - kdump_table = db.cfgdb.get_table("KDUMP") - file_path = Path('/etc/default/kdump-tools') try: # Read the content of the file content = file_path.read_text() - # Define replacement functions with capture groups - def replace_ssh(match): - return '#SSH=""' if item == "ssh_string" else match.group(0) - - def replace_ssh_key(match): - return '#SSH_KEY=""' if item == "ssh_path" else match.group(0) - - # Apply replacements using capture groups - new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", replace_ssh_key, new_content, flags=re.MULTILINE) + # Define replacement functions + def remove_ssh(match): + return 'SSH=""' if item == "ssh_string" else match.group(0) + + def remove_ssh_key(match): + return 'SSH_KEY=""' if item == "ssh_path" else match.group(0) + + # Check if SSH and SSH_KEY are commented + ssh_commented = bool(re.search(r"^\s*#\s*SSH\s*=\s*.*$", content, flags=re.MULTILINE)) + ssh_key_commented = bool(re.search(r"^\s*#\s*SSH_KEY\s*=\s*.*$", content, flags=re.MULTILINE)) + + if ssh_commented and ssh_key_commented: + # Apply replacements to remove values + new_content = re.sub(r"^\s*#\s*SSH\s*=\s*.*$", remove_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"^\s*#\s*SSH_KEY\s*=\s*.*$", remove_ssh_key, new_content, flags=re.MULTILINE) + else: + # Apply replacements to remove values + new_content = re.sub(r"^\s*SSH\s*=\s*.*$", remove_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"^\s*SSH_KEY\s*=\s*.*$", remove_ssh_key, new_content, flags=re.MULTILINE) # Write the updated content back to the file file_path.write_text(new_content) From 88f72c5470c9f9749b9e691e69f569e9ad427ce0 Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Thu, 25 Jul 2024 12:06:56 +0500 Subject: [PATCH 045/127] Corrected Functions --- config/kdump.py | 1 - 1 file changed, 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index 75e77599b7..e331440360 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -220,7 +220,6 @@ def replace_ssh_key(match): echo_reboot_warning() - @kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db From d52d4fd0c7aecca8a39ecb545cb71728d55c896f Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 25 Jul 2024 13:32:52 +0500 Subject: [PATCH 046/127] assign to only Hash and deHash SSH AND SSH_KEY --- config/kdump.py | 116 ++++++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 54 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index fe4d536eaa..32f496cc45 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -131,23 +131,31 @@ def kdump_remote(db, action): remote = 'true' if action.lower() == 'enable' else 'false' db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) - if action.lower() == 'disable': + if action.lower() == 'enable': file_path = Path('/etc/default/kdump-tools') try: # Read the content of the file content = file_path.read_text() + def uncomment_ssh(match): + return match.group(0)[1:] # Remove the leading '#' + + new_content = re.sub(r"^#SSH", uncomment_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"^#SSH_KEY", uncomment_ssh, new_content, flags=re.MULTILINE) + click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") + except Exception as e: + click.echo(f"Error updating /etc/default/kdump-tools: {e}") - # Define replacement functions with capture groups - def comment_ssh(match): - return f'#{match.group(0)}' - - # Apply replacements using capture groups - new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", comment_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", comment_ssh, new_content, flags=re.MULTILINE) + elif action.lower() == 'disable': + file_path = Path('/etc/default/kdump-tools') + try: + # Read the content of the file + content = file_path.read_text() - # Write the updated content back to the file - file_path.write_text(new_content) - click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") + def comment_ssh(match): + return f'#{match.group(0)}' # Add a leading '#' + new_content = re.sub(r"^SSH", comment_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"^SSH_KEY", comment_ssh, new_content, flags=re.MULTILINE) + click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented .") except Exception as e: click.echo(f"Error updating /etc/default/kdump-tools: {e}") @@ -183,31 +191,31 @@ def add_kdump_item(db, item, value): db.cfgdb.mod_entry("KDUMP", "config", {item: value}) # Retrieve updated values from config_db - kdump_table = db.cfgdb.get_table("KDUMP") - ssh_string = kdump_table.get("config", {}).get("ssh_string", "") - ssh_path = kdump_table.get("config", {}).get("ssh_path", "") - - file_path = Path('/etc/default/kdump-tools') - try: - # Read the content of the file - content = file_path.read_text() - - # Define replacement functions with capture groups - def replace_ssh(match): - return f'SSH="{ssh_string}"' if ssh_string else match.group(0) - - def replace_ssh_key(match): - return f'SSH_KEY="{ssh_path}"' if ssh_path else match.group(0) - - # Apply replacements using capture groups - new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", replace_ssh_key, new_content, flags=re.MULTILINE) - - # Write the updated content back to the file - file_path.write_text(new_content) - click.echo("Updated /etc/default/kdump-tools with new SSH settings.") - except Exception as e: - click.echo(f"Error updating /etc/default/kdump-tools: {e}") + # kdump_table = db.cfgdb.get_table("KDUMP") + # ssh_string = kdump_table.get("config", {}).get("ssh_string", "") + # ssh_path = kdump_table.get("config", {}).get("ssh_path", "") + + # file_path = Path('/etc/default/kdump-tools') + # try: + # # Read the content of the file + # content = file_path.read_text() + + # # Define replacement functions with capture groups + # def replace_ssh(match): + # return f'SSH="{ssh_string}"' if ssh_string else match.group(0) + + # def replace_ssh_key(match): + # return f'SSH_KEY="{ssh_path}"' if ssh_path else match.group(0) + + # # Apply replacements using capture groups + # new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) + # new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", replace_ssh_key, new_content, flags=re.MULTILINE) + + # # Write the updated content back to the file + # file_path.write_text(new_content) + # click.echo("Updated /etc/default/kdump-tools with new SSH settings.") + # except Exception as e: + # click.echo(f"Error updating /etc/default/kdump-tools: {e}") echo_reboot_warning() @@ -229,29 +237,29 @@ def remove_kdump_item(db, item): db.cfgdb.mod_entry("KDUMP", "config", {item: ""}) # Retrieve updated values from config_db - kdump_table = db.cfgdb.get_table("KDUMP") + # kdump_table = db.cfgdb.get_table("KDUMP") - file_path = Path('/etc/default/kdump-tools') - try: - # Read the content of the file - content = file_path.read_text() + # file_path = Path('/etc/default/kdump-tools') + # try: + # # Read the content of the file + # content = file_path.read_text() - # Define replacement functions with capture groups - def replace_ssh(match): - return 'SSH=""' if item == "ssh_string" else match.group(0) + # # Define replacement functions with capture groups + # def replace_ssh(match): + # return 'SSH=""' if item == "ssh_string" else match.group(0) - def replace_ssh_key(match): - return 'SSH_KEY=""' if item == "ssh_path" else match.group(0) + # def replace_ssh_key(match): + # return 'SSH_KEY=""' if item == "ssh_path" else match.group(0) - # Apply replacements using capture groups - new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", replace_ssh_key, new_content, flags=re.MULTILINE) + # # Apply replacements using capture groups + # new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) + # new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", replace_ssh_key, new_content, flags=re.MULTILINE) - # Write the updated content back to the file - file_path.write_text(new_content) - click.echo("Updated /etc/default/kdump-tools with empty SSH settings.") - except Exception as e: - click.echo(f"Error updating /etc/default/kdump-tools: {e}") + # # Write the updated content back to the file + # file_path.write_text(new_content) + # click.echo("Updated /etc/default/kdump-tools with empty SSH settings.") + # except Exception as e: + # click.echo(f"Error updating /etc/default/kdump-tools: {e}") click.echo(f"{item} removed successfully.") echo_reboot_warning() From 1a94b1e84c9d3f64e48eb4e4106d4a4cbc067c98 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 25 Jul 2024 14:23:12 +0500 Subject: [PATCH 047/127] resolove syntax of python --- config/kdump.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 0a6f5db87e..3dc06c8fd5 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -131,18 +131,18 @@ def kdump_remote(db, action): remote = 'true' if action.lower() == 'enable' else 'false' db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) - def uncomment_ssh(match): - return match.group(0)[1:] # Remove the leading '#' - def comment_ssh(match): - return f'#{match.group(0)}' # Add a leading '#' - if action.lower() == 'enable': file_path = Path('/etc/default/kdump-tools') try: # Read the content of the file content = file_path.read_text() + + def uncomment_ssh(match): + return match.group(0)[1:] # Remove the leading '#' + new_content = re.sub(r"^#SSH", uncomment_ssh, content, flags=re.MULTILINE) new_content = re.sub(r"^#SSH_KEY", uncomment_ssh, new_content, flags=re.MULTILINE) + click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") except Exception as e: click.echo(f"Error updating /etc/default/kdump-tools: {e}") @@ -152,8 +152,13 @@ def comment_ssh(match): try: # Read the content of the file content = file_path.read_text() + + def comment_ssh(match): + return f'#{match.group(0)}' # Add a leading '#' + new_content = re.sub(r"^SSH", comment_ssh, content, flags=re.MULTILINE) new_content = re.sub(r"^SSH_KEY", comment_ssh, new_content, flags=re.MULTILINE) + click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented .") except Exception as e: click.echo(f"Error updating /etc/default/kdump-tools: {e}") From e6af5cabbdc8cc6b49bf1d170fcbfdb3e0f28df6 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 25 Jul 2024 14:25:56 +0500 Subject: [PATCH 048/127] resolove syntax of python --- config/kdump.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 3dc06c8fd5..b26e639da4 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -141,8 +141,7 @@ def uncomment_ssh(match): return match.group(0)[1:] # Remove the leading '#' new_content = re.sub(r"^#SSH", uncomment_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^#SSH_KEY", uncomment_ssh, new_content, flags=re.MULTILINE) - + new_content = re.sub(r"^#SSH_KEY", uncomment_ssh, new_content, flags=re.MULTILINE) click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") except Exception as e: click.echo(f"Error updating /etc/default/kdump-tools: {e}") @@ -157,8 +156,7 @@ def comment_ssh(match): return f'#{match.group(0)}' # Add a leading '#' new_content = re.sub(r"^SSH", comment_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^SSH_KEY", comment_ssh, new_content, flags=re.MULTILINE) - + new_content = re.sub(r"^SSH_KEY", comment_ssh, new_content, flags=re.MULTILINE) click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented .") except Exception as e: click.echo(f"Error updating /etc/default/kdump-tools: {e}") From c5b1917fd07d19fcc8938d7957be4c99bab2bad6 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 25 Jul 2024 14:35:51 +0500 Subject: [PATCH 049/127] resolove syntax of python --- config/kdump.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index b26e639da4..9e63e8596a 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -141,7 +141,7 @@ def uncomment_ssh(match): return match.group(0)[1:] # Remove the leading '#' new_content = re.sub(r"^#SSH", uncomment_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^#SSH_KEY", uncomment_ssh, new_content, flags=re.MULTILINE) + new_content = re.sub(r"^#SSH_KEY", uncomment_ssh, new_content, flags=re.MULTILINE) click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") except Exception as e: click.echo(f"Error updating /etc/default/kdump-tools: {e}") @@ -156,7 +156,7 @@ def comment_ssh(match): return f'#{match.group(0)}' # Add a leading '#' new_content = re.sub(r"^SSH", comment_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^SSH_KEY", comment_ssh, new_content, flags=re.MULTILINE) + new_content = re.sub(r"^SSH_KEY", comment_ssh, new_content, flags=re.MULTILINE) click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented .") except Exception as e: click.echo(f"Error updating /etc/default/kdump-tools: {e}") From eb3599543b1178e61802d1bff12fbc65a09b9fda Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 25 Jul 2024 16:23:19 +0500 Subject: [PATCH 050/127] just writing values to DB --- config/kdump.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 9e63e8596a..3f3071b124 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -131,8 +131,8 @@ def kdump_remote(db, action): remote = 'true' if action.lower() == 'enable' else 'false' db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) + file_path = Path('/etc/default/kdump-tools') if action.lower() == 'enable': - file_path = Path('/etc/default/kdump-tools') try: # Read the content of the file content = file_path.read_text() @@ -146,7 +146,7 @@ def uncomment_ssh(match): except Exception as e: click.echo(f"Error updating /etc/default/kdump-tools: {e}") - elif action.lower() == 'disable': + if action.lower() == 'disable': file_path = Path('/etc/default/kdump-tools') try: # Read the content of the file From 8d20e9cf9ef32620277e6012c084130f3219220d Mon Sep 17 00:00:00 2001 From: Ghulam-Bahoo Date: Thu, 25 Jul 2024 16:53:36 +0500 Subject: [PATCH 051/127] Unit Tests --- tests/kdump_test.py | 113 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 18f14181b3..8d5354d548 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -2,6 +2,7 @@ from click.testing import CliRunner from utilities_common.db import Db +from pathlib import Path class TestKdump(object): @@ -69,6 +70,118 @@ def test_config_kdump_num_dumps(self, get_cmd_module): print(result.exit_code) assert result.exit_code == 1 + def test_config_kdump_remote_enable(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Ensure KDUMP table exists and is in the right state + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY uncommented." in result.output + + # Check the file content + file_path = Path('/etc/default/kdump-tools') + content = file_path.read_text() + assert "SSH" in content + assert "SSH_KEY" in content + + def test_config_kdump_remote_disable(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Ensure KDUMP table exists and is in the right state + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out." in result.output + + # Check the file content + file_path = Path('/etc/default/kdump-tools') + content = file_path.read_text() + assert "SSH" in content + assert "SSH_KEY" in content + + def test_add_ssh_string(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Set KDUMP remote mode to enabled + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) + + # Add SSH string + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_string", "user@host"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + assert "Updated /etc/default/kdump-tools with new SSH settings." in result.output + + # Check the file content + file_path = Path('/etc/default/kdump-tools') + content = file_path.read_text() + assert 'SSH="user@host"' in content + + def test_remove_ssh_string(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Ensure KDUMP table exists and is in the right state + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "user@host"}) + + # Remove SSH string + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + assert "SSH_string removed successfully." in result.output + + # Check the file content + file_path = Path('/etc/default/kdump-tools') + content = file_path.read_text() + assert 'SSH=""' in content + + def test_add_ssh_path(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Set KDUMP remote mode to enabled + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) + + # Add SSH path + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_path", "/path/to/key"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + assert "Updated /etc/default/kdump-tools with new SSH settings." in result.output + + # Check the file content + file_path = Path('/etc/default/kdump-tools') + content = file_path.read_text() + assert 'SSH_KEY="/path/to/key"' in content + + def test_remove_ssh_path(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Ensure KDUMP table exists and is in the right state + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_path": "/path/to/key"}) + + # Remove SSH path + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_path"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + assert "ssh_path removed successfully." in result.output + + # Check the file content + file_path = Path('/etc/default/kdump-tools') + content = file_path.read_text() + assert 'SSH_KEY=""' in content + @classmethod def teardown_class(cls): print("TEARDOWN") From efbfa46ea9d0da1714829f8c3b3caaa2f016ae95 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 25 Jul 2024 17:53:00 +0500 Subject: [PATCH 052/127] utility only comments and uncomments and add value to db --- config/kdump.py | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 3f3071b124..098d1ff457 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -133,33 +133,27 @@ def kdump_remote(db, action): file_path = Path('/etc/default/kdump-tools') if action.lower() == 'enable': - try: - # Read the content of the file - content = file_path.read_text() + # Read the content of the file + content = file_path.read_text() + + def uncomment_ssh(match): + return match.group(0)[1:] # Remove the leading '#' - def uncomment_ssh(match): - return match.group(0)[1:] # Remove the leading '#' - - new_content = re.sub(r"^#SSH", uncomment_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^#SSH_KEY", uncomment_ssh, new_content, flags=re.MULTILINE) - click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") - except Exception as e: - click.echo(f"Error updating /etc/default/kdump-tools: {e}") + new_content = re.sub(r"^#SSH", uncomment_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"^#SSH_KEY", uncomment_ssh, new_content, flags=re.MULTILINE) + click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") if action.lower() == 'disable': - file_path = Path('/etc/default/kdump-tools') - try: - # Read the content of the file - content = file_path.read_text() + # Read the content of the file + content = file_path.read_text() - def comment_ssh(match): - return f'#{match.group(0)}' # Add a leading '#' + def comment_ssh(match): + return f'#{match.group(0)}' # Add a leading '#' - new_content = re.sub(r"^SSH", comment_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^SSH_KEY", comment_ssh, new_content, flags=re.MULTILINE) - click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented .") - except Exception as e: - click.echo(f"Error updating /etc/default/kdump-tools: {e}") + new_content = re.sub(r"^SSH", comment_ssh, content, flags=re.MULTILINE) + new_content = re.sub(r"^SSH_KEY", comment_ssh, new_content, flags=re.MULTILINE) + click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented .") + click.echo(f"Error updating /etc/default/kdump-tools: {e}") echo_reboot_warning() @@ -218,7 +212,7 @@ def add_kdump_item(db, item, value): # click.echo("Updated /etc/default/kdump-tools with new SSH settings.") # except Exception as e: # click.echo(f"Error updating /etc/default/kdump-tools: {e}") - echo_reboot_warning() + # echo_reboot_warning() @kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path.") From bd1d2f17aa4c6477886432da4fb11828b4e821f0 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 25 Jul 2024 20:07:58 +0500 Subject: [PATCH 053/127] Testing --- config/kdump.py | 1 - 1 file changed, 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index 098d1ff457..6c44f4e7fa 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -130,7 +130,6 @@ def kdump_remote(db, action): remote = 'true' if action.lower() == 'enable' else 'false' db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) - file_path = Path('/etc/default/kdump-tools') if action.lower() == 'enable': # Read the content of the file From 657d9139dcdd21409ed60ce8eb30810f22c3fce8 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 26 Jul 2024 14:28:23 +0500 Subject: [PATCH 054/127] Removed test cases --- tests/kdump_test.py | 113 -------------------------------------------- 1 file changed, 113 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 8d5354d548..18f14181b3 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -2,7 +2,6 @@ from click.testing import CliRunner from utilities_common.db import Db -from pathlib import Path class TestKdump(object): @@ -70,118 +69,6 @@ def test_config_kdump_num_dumps(self, get_cmd_module): print(result.exit_code) assert result.exit_code == 1 - def test_config_kdump_remote_enable(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - - # Ensure KDUMP table exists and is in the right state - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 - assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY uncommented." in result.output - - # Check the file content - file_path = Path('/etc/default/kdump-tools') - content = file_path.read_text() - assert "SSH" in content - assert "SSH_KEY" in content - - def test_config_kdump_remote_disable(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - - # Ensure KDUMP table exists and is in the right state - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 - assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out." in result.output - - # Check the file content - file_path = Path('/etc/default/kdump-tools') - content = file_path.read_text() - assert "SSH" in content - assert "SSH_KEY" in content - - def test_add_ssh_string(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - - # Set KDUMP remote mode to enabled - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - - # Add SSH string - result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_string", "user@host"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 - assert "Updated /etc/default/kdump-tools with new SSH settings." in result.output - - # Check the file content - file_path = Path('/etc/default/kdump-tools') - content = file_path.read_text() - assert 'SSH="user@host"' in content - - def test_remove_ssh_string(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - - # Ensure KDUMP table exists and is in the right state - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "user@host"}) - - # Remove SSH string - result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 - assert "SSH_string removed successfully." in result.output - - # Check the file content - file_path = Path('/etc/default/kdump-tools') - content = file_path.read_text() - assert 'SSH=""' in content - - def test_add_ssh_path(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - - # Set KDUMP remote mode to enabled - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - - # Add SSH path - result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_path", "/path/to/key"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 - assert "Updated /etc/default/kdump-tools with new SSH settings." in result.output - - # Check the file content - file_path = Path('/etc/default/kdump-tools') - content = file_path.read_text() - assert 'SSH_KEY="/path/to/key"' in content - - def test_remove_ssh_path(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - - # Ensure KDUMP table exists and is in the right state - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_path": "/path/to/key"}) - - # Remove SSH path - result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_path"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 - assert "ssh_path removed successfully." in result.output - - # Check the file content - file_path = Path('/etc/default/kdump-tools') - content = file_path.read_text() - assert 'SSH_KEY=""' in content - @classmethod def teardown_class(cls): print("TEARDOWN") From 9a95762c8c9d67ed0ff16479b29b4b2dc7801825 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 26 Jul 2024 14:36:19 +0500 Subject: [PATCH 055/127] typo --- config/kdump.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 6c44f4e7fa..bbb122e108 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -133,15 +133,13 @@ def kdump_remote(db, action): file_path = Path('/etc/default/kdump-tools') if action.lower() == 'enable': # Read the content of the file - content = file_path.read_text() - + content = file_path.read_text() def uncomment_ssh(match): return match.group(0)[1:] # Remove the leading '#' new_content = re.sub(r"^#SSH", uncomment_ssh, content, flags=re.MULTILINE) new_content = re.sub(r"^#SSH_KEY", uncomment_ssh, new_content, flags=re.MULTILINE) - click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") - + click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") if action.lower() == 'disable': # Read the content of the file content = file_path.read_text() @@ -152,8 +150,6 @@ def comment_ssh(match): new_content = re.sub(r"^SSH", comment_ssh, content, flags=re.MULTILINE) new_content = re.sub(r"^SSH_KEY", comment_ssh, new_content, flags=re.MULTILINE) click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented .") - click.echo(f"Error updating /etc/default/kdump-tools: {e}") - echo_reboot_warning() # From 91c24c226e922676d1977648520f2bbf349425cc Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 26 Jul 2024 14:39:45 +0500 Subject: [PATCH 056/127] typo --- config/kdump.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index bbb122e108..00bfbeb200 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -133,7 +133,8 @@ def kdump_remote(db, action): file_path = Path('/etc/default/kdump-tools') if action.lower() == 'enable': # Read the content of the file - content = file_path.read_text() + content = file_path.read_text() + def uncomment_ssh(match): return match.group(0)[1:] # Remove the leading '#' From adda82eb12033e7d8986defe5f03fa574e9cd6e2 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 26 Jul 2024 14:44:56 +0500 Subject: [PATCH 057/127] typo --- config/kdump.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index 00bfbeb200..45364da50d 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -134,7 +134,7 @@ def kdump_remote(db, action): if action.lower() == 'enable': # Read the content of the file content = file_path.read_text() - + def uncomment_ssh(match): return match.group(0)[1:] # Remove the leading '#' From 6810b25f0395ed1af1bf374fa8942b0b8590a2eb Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 26 Jul 2024 16:28:46 +0500 Subject: [PATCH 058/127] typo --- config/kdump.py | 50 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 45364da50d..91914f23f7 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -131,26 +131,46 @@ def kdump_remote(db, action): remote = 'true' if action.lower() == 'enable' else 'false' db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) file_path = Path('/etc/default/kdump-tools') + # Values to be set for SSH and SSH_KEY + ssh_value = "your_ssh_value" + ssh_key_value = "your_ssh_key_value" if action.lower() == 'enable': # Read the content of the file - content = file_path.read_text() - - def uncomment_ssh(match): - return match.group(0)[1:] # Remove the leading '#' - - new_content = re.sub(r"^#SSH", uncomment_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^#SSH_KEY", uncomment_ssh, new_content, flags=re.MULTILINE) + with open(file_path, 'r') as file: + lines = file.readlines() + # Update the lines + updated_lines = [] + for line in lines: + if line.startswith("#SSH="): + updated_lines.append(f'SSH="{ssh_value}"\n') + elif line.startswith("#SSH_KEY="): + updated_lines.append(f'SSH_KEY="{ssh_key_value}"\n') + else: + updated_lines.append(line) + + # Write the updated lines back to the configuration file + with open(file_path, 'w') as file: + file.writelines(updated_lines) click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") + if action.lower() == 'disable': # Read the content of the file - content = file_path.read_text() - - def comment_ssh(match): - return f'#{match.group(0)}' # Add a leading '#' - - new_content = re.sub(r"^SSH", comment_ssh, content, flags=re.MULTILINE) - new_content = re.sub(r"^SSH_KEY", comment_ssh, new_content, flags=re.MULTILINE) - click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented .") + with open(file_path, 'r') as file: + lines = file.readlines() + + # Update the lines + updated_lines = [] + for line in lines: + if line.startswith("SSH="): + updated_lines.append(f'#SSH="{ssh_value}"\n') + elif line.startswith("SSH_KEY="): + updated_lines.append(f'#SSH_KEY="{ssh_key_value}"\n') + else: + updated_lines.append(line) + + # Write the updated lines back to the configuration file + with open(file_path, 'w') as file: + file.writelines(updated_lines) echo_reboot_warning() # From ac0b0e6d2f1b4a2cf1878485cbf88ddc9993ce93 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 26 Jul 2024 16:35:33 +0500 Subject: [PATCH 059/127] typo --- config/kdump.py | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 91914f23f7..856b71415a 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -97,12 +97,7 @@ def kdump_num_dumps(db, kdump_num_dumps): db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps}) echo_reboot_warning() - -# # 'remote' command ('sudo config kdump remote ...') -# - - @kdump.command(name="remote", short_help="Enable or Disable Kdump Remote") @click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) @pass_db @@ -110,24 +105,19 @@ def kdump_remote(db, action): """Enable or Disable Kdump Remote Mode""" kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) - current_remote_status = kdump_table.get("config", {}).get("remote", "false").lower() - if action.lower() == 'enable' and current_remote_status == 'true': click.echo("Error: Kdump Remote Mode is already enabled.") return elif action.lower() == 'disable' and current_remote_status == 'false': click.echo("Error: Kdump Remote Mode is already disabled.") return - if action.lower() == 'disable': ssh_string = kdump_table.get("config", {}).get("ssh_string", None) ssh_key = kdump_table.get("config", {}).get("ssh_key", None) - if ssh_string or ssh_key: click.echo("Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode.") return - remote = 'true' if action.lower() == 'enable' else 'false' db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) file_path = Path('/etc/default/kdump-tools') @@ -147,17 +137,14 @@ def kdump_remote(db, action): updated_lines.append(f'SSH_KEY="{ssh_key_value}"\n') else: updated_lines.append(line) - # Write the updated lines back to the configuration file with open(file_path, 'w') as file: file.writelines(updated_lines) - click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") - + click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") if action.lower() == 'disable': # Read the content of the file with open(file_path, 'r') as file: lines = file.readlines() - # Update the lines updated_lines = [] for line in lines: @@ -167,12 +154,10 @@ def kdump_remote(db, action): updated_lines.append(f'#SSH_KEY="{ssh_key_value}"\n') else: updated_lines.append(line) - # Write the updated lines back to the configuration file with open(file_path, 'w') as file: file.writelines(updated_lines) echo_reboot_warning() - # # 'add' command ('sudo config kdump add ...') # @@ -230,7 +215,6 @@ def add_kdump_item(db, item, value): # click.echo(f"Error updating /etc/default/kdump-tools: {e}") # echo_reboot_warning() - @kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db From 82f94b9112e59ca0e53f9f18db2f1c250555d61b Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 26 Jul 2024 18:51:54 +0500 Subject: [PATCH 060/127] typo --- config/kdump.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 856b71415a..f8809d4d1f 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -1,6 +1,5 @@ import sys import click -import re from utilities_common.cli import AbbreviationGroup, pass_db from pathlib import Path @@ -98,6 +97,7 @@ def kdump_num_dumps(db, kdump_num_dumps): db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps}) echo_reboot_warning() # 'remote' command ('sudo config kdump remote ...') + @kdump.command(name="remote", short_help="Enable or Disable Kdump Remote") @click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) @pass_db @@ -213,8 +213,7 @@ def add_kdump_item(db, item, value): # click.echo("Updated /etc/default/kdump-tools with new SSH settings.") # except Exception as e: # click.echo(f"Error updating /etc/default/kdump-tools: {e}") - # echo_reboot_warning() - + # echo_reboot_warning( @kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db From 486a486139724aeeb047d18415f4f61a01922948 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 26 Jul 2024 18:58:14 +0500 Subject: [PATCH 061/127] typo --- config/kdump.py | 1 + 1 file changed, 1 insertion(+) diff --git a/config/kdump.py b/config/kdump.py index f8809d4d1f..18c4695e78 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -214,6 +214,7 @@ def add_kdump_item(db, item, value): # except Exception as e: # click.echo(f"Error updating /etc/default/kdump-tools: {e}") # echo_reboot_warning( + @kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db From f709f038e0485bc342ae24a6ec2980c32ab4ccf0 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Mon, 29 Jul 2024 13:10:40 +0500 Subject: [PATCH 062/127] typo --- config/kdump.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/kdump.py b/config/kdump.py index 18c4695e78..bd62e5825d 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -97,6 +97,7 @@ def kdump_num_dumps(db, kdump_num_dumps): db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps}) echo_reboot_warning() # 'remote' command ('sudo config kdump remote ...') + @kdump.command(name="remote", short_help="Enable or Disable Kdump Remote") @click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) @@ -214,6 +215,7 @@ def add_kdump_item(db, item, value): # except Exception as e: # click.echo(f"Error updating /etc/default/kdump-tools: {e}") # echo_reboot_warning( + @kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) From ebaefc0ac7fca4bcd6fbdfbea89f1e1180b5b7d1 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Mon, 29 Jul 2024 13:14:08 +0500 Subject: [PATCH 063/127] typo --- config/kdump.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index bd62e5825d..e1eeaed318 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -97,7 +97,7 @@ def kdump_num_dumps(db, kdump_num_dumps): db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps}) echo_reboot_warning() # 'remote' command ('sudo config kdump remote ...') - + @kdump.command(name="remote", short_help="Enable or Disable Kdump Remote") @click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) @@ -215,7 +215,7 @@ def add_kdump_item(db, item, value): # except Exception as e: # click.echo(f"Error updating /etc/default/kdump-tools: {e}") # echo_reboot_warning( - + @kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) From bc04bb766b5f4cdca2a9568ab351659b1135b932 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 30 Jul 2024 13:16:18 +0500 Subject: [PATCH 064/127] added test cases for code coverage --- config/kdump.py | 57 +----------------- tests/kdump_test.py | 144 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 55 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index e1eeaed318..0cb0fdf8ed 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -187,36 +187,9 @@ def add_kdump_item(db, item, value): # Add item to config_db db.cfgdb.mod_entry("KDUMP", "config", {item: value}) + echo_reboot_warning() - # Retrieve updated values from config_db - # kdump_table = db.cfgdb.get_table("KDUMP") - # ssh_string = kdump_table.get("config", {}).get("ssh_string", "") - # ssh_path = kdump_table.get("config", {}).get("ssh_path", "") - - # file_path = Path('/etc/default/kdump-tools') - # try: - # # Read the content of the file - # content = file_path.read_text() - - # # Define replacement functions with capture groups - # def replace_ssh(match): - # return f'SSH="{ssh_string}"' if ssh_string else match.group(0) - - # def replace_ssh_key(match): - # return f'SSH_KEY="{ssh_path}"' if ssh_path else match.group(0) - - # # Apply replacements using capture groups - # new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) - # new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", replace_ssh_key, new_content, flags=re.MULTILINE) - - # # Write the updated content back to the file - # file_path.write_text(new_content) - # click.echo("Updated /etc/default/kdump-tools with new SSH settings.") - # except Exception as e: - # click.echo(f"Error updating /etc/default/kdump-tools: {e}") - # echo_reboot_warning( - - + @kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db @@ -233,31 +206,5 @@ def remove_kdump_item(db, item): # Remove item from config_db db.cfgdb.mod_entry("KDUMP", "config", {item: ""}) - - # Retrieve updated values from config_db - # kdump_table = db.cfgdb.get_table("KDUMP") - - # file_path = Path('/etc/default/kdump-tools') - # try: - # # Read the content of the file - # content = file_path.read_text() - - # # Define replacement functions with capture groups - # def replace_ssh(match): - # return 'SSH=""' if item == "ssh_string" else match.group(0) - - # def replace_ssh_key(match): - # return 'SSH_KEY=""' if item == "ssh_path" else match.group(0) - - # # Apply replacements using capture groups - # new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE) - # new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", replace_ssh_key, new_content, flags=re.MULTILINE) - - # # Write the updated content back to the file - # file_path.write_text(new_content) - # click.echo("Updated /etc/default/kdump-tools with empty SSH settings.") - # except Exception as e: - # click.echo(f"Error updating /etc/default/kdump-tools: {e}") - click.echo(f"{item} removed successfully.") echo_reboot_warning() diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 18f14181b3..5e1f82e039 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -68,6 +68,150 @@ def test_config_kdump_num_dumps(self, get_cmd_module): result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) print(result.exit_code) assert result.exit_code == 1 + + def test_kdump_remote_enable(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Enable remote mode + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + + # Try enabling again + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + print(result.output) + assert "Error: Kdump Remote Mode is already enabled." in result.output + + def test_kdump_remote_disable_without_ssh(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Disable remote mode + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + + # Try disabling again + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.output) + assert "Error: Kdump Remote Mode is already disabled." in result.output + + def test_kdump_remote_disable_with_ssh(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Enable remote mode first + runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + + # Add ssh_string and ssh_key to the config DB + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": "ssh_value", "ssh_key": "ssh_key_value"}) + + # Try disabling remote mode while ssh_string and ssh_key are still configured + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.output) + assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output + + # Remove ssh_string and ssh_key from the config DB + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": "", "ssh_key": ""}) + + # Now disable remote mode + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + + def test_add_kdump_item_without_remote_enabled(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Attempt to add ssh_string without enabling remote mode + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_string", "test_ssh_string"], obj=db) + print(result.output) + assert "Error: Enable remote mode first." in result.output + + def test_add_kdump_item_already_added(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Enable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) + + # Add ssh_string + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_string", "test_ssh_string"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + + # Attempt to add ssh_string again + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_string", "test_ssh_string"], obj=db) + print(result.output) + assert "Error: ssh_string is already added." in result.output + + def test_add_kdump_item_success(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Enable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) + + # Add ssh_key + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_key", "test_ssh_key"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + + # Verify the ssh_key is added in config_db + kdump_table = db.cfgdb.get_table("KDUMP") + assert kdump_table["config"]["ssh_key"] == "test_ssh_key" + + + def test_remove_kdump_item_not_configured(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Attempt to remove ssh_string that is not configured + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) + print(result.output) + assert "Error: ssh_string is not configured." in result.output + + def test_remove_kdump_item_success(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Add ssh_string to config_db to test removal + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": "test_ssh_string"}) + + # Remove ssh_string + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + + # Verify the ssh_string is removed from config_db + kdump_table = db.cfgdb.get_table("KDUMP") + assert kdump_table["config"]["ssh_string"] == "" + + def test_remove_kdump_item_success_ssh_key(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Add ssh_key to config_db to test removal + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_key": "test_ssh_key"}) + + # Remove ssh_key + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_key"], obj=db) + print(result.exit_code) + assert result.exit_code == 0 + + # Verify the ssh_key is removed from config_db + kdump_table = db.cfgdb.get_table("KDUMP") + assert kdump_table["config"]["ssh_key"] == "" @classmethod def teardown_class(cls): From 6f9c74fef46e69df2b35023b1c326308e9286f26 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 30 Jul 2024 14:00:47 +0500 Subject: [PATCH 065/127] added test cases for code coverage --- config/kdump.py | 2 +- tests/kdump_test.py | 169 +++++++++++++++++--------------------------- 2 files changed, 64 insertions(+), 107 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 0cb0fdf8ed..4118297e42 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -189,7 +189,7 @@ def add_kdump_item(db, item, value): db.cfgdb.mod_entry("KDUMP", "config", {item: value}) echo_reboot_warning() - + @kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @pass_db diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 5e1f82e039..2e72f40edd 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,8 +1,20 @@ -import importlib - +import os +import sys +import pytest +import click from click.testing import CliRunner +from unittest import mock from utilities_common.db import Db +from config.main import config + +# Mocking the database and filesystem interactions +@pytest.fixture +def db(): + db = mock.MagicMock() + db.cfgdb = mock.MagicMock() + return db + class TestKdump(object): @classmethod @@ -68,150 +80,95 @@ def test_config_kdump_num_dumps(self, get_cmd_module): result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) print(result.exit_code) assert result.exit_code == 1 - - def test_kdump_remote_enable(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + + def test_kdump_remote_enable(db): runner = CliRunner() - # Enable remote mode + db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print(result.exit_code) assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) - # Try enabling again + def test_kdump_remote_enable_already_enabled(db): + runner = CliRunner() + + db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print(result.output) + assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already enabled." in result.output - def test_kdump_remote_disable_without_ssh(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + def test_kdump_remote_disable(db): runner = CliRunner() - # Disable remote mode + db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.exit_code) assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) - # Try disabling again - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) - assert "Error: Kdump Remote Mode is already disabled." in result.output - - def test_kdump_remote_disable_with_ssh(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + def test_kdump_remote_disable_already_disabled(db): runner = CliRunner() - # Enable remote mode first - runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - - # Add ssh_string and ssh_key to the config DB - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": "ssh_value", "ssh_key": "ssh_key_value"}) - - # Try disabling remote mode while ssh_string and ssh_key are still configured - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) - assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output - - # Remove ssh_string and ssh_key from the config DB - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": "", "ssh_key": ""}) - - # Now disable remote mode + db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.exit_code) assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already disabled." in result.output - def test_add_kdump_item_without_remote_enabled(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + def test_kdump_remote_disable_with_ssh_values(db): runner = CliRunner() - # Attempt to add ssh_string without enabling remote mode - result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_string", "test_ssh_string"], obj=db) - print(result.output) - assert "Error: Enable remote mode first." in result.output + db.cfgdb.get_table.return_value = { + "config": {"remote": "true", "ssh_string": "some_ssh_string", "ssh_key": "some_ssh_key"} + } + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + assert result.exit_code == 0 + expected_output = ( + "Error: Remove SSH_string and SSH_key from Config DB before disabling " + "Kdump Remote Mode." + ) + assert expected_output in result.output - def test_add_kdump_item_already_added(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + def test_kdump_add(db): runner = CliRunner() - # Enable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - - # Add ssh_string - result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_string", "test_ssh_string"], obj=db) - print(result.exit_code) + db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} + result = runner.invoke(config.config.commands["kdump"].commands["add"], + ["ssh_string", "test_ssh_string"], obj=db) assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"ssh_string": "test_ssh_string"}) - # Attempt to add ssh_string again - result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_string", "test_ssh_string"], obj=db) - print(result.output) - assert "Error: ssh_string is already added." in result.output - - def test_add_kdump_item_success(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + def test_kdump_add_remote_not_enabled(db): runner = CliRunner() - # Enable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - - # Add ssh_key - result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_key", "test_ssh_key"], obj=db) - print(result.exit_code) + db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} + result = runner.invoke(config.config.commands["kdump"].commands["add"], + ["ssh_string", "test_ssh_string"], obj=db) assert result.exit_code == 0 + assert "Error: Enable remote mode first." in result.output - # Verify the ssh_key is added in config_db - kdump_table = db.cfgdb.get_table("KDUMP") - assert kdump_table["config"]["ssh_key"] == "test_ssh_key" - - - def test_remove_kdump_item_not_configured(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + def test_kdump_add_already_exists(db): runner = CliRunner() - # Attempt to remove ssh_string that is not configured - result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) - print(result.output) - assert "Error: ssh_string is not configured." in result.output + db.cfgdb.get_table.return_value = {"config": {"remote": "true", "ssh_string": "existing_ssh_string"}} + result = runner.invoke(config.config.commands["kdump"].commands["add"], + ["ssh_string", "test_ssh_string"], obj=db) + assert result.exit_code == 0 + assert "Error: ssh_string is already added." in result.output - def test_remove_kdump_item_success(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + def test_kdump_remove(db): runner = CliRunner() - # Add ssh_string to config_db to test removal - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": "test_ssh_string"}) - - # Remove ssh_string + db.cfgdb.get_table.return_value = {"config": {"ssh_string": "test_ssh_string"}} result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) - print(result.exit_code) assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"ssh_string": ""}) - # Verify the ssh_string is removed from config_db - kdump_table = db.cfgdb.get_table("KDUMP") - assert kdump_table["config"]["ssh_string"] == "" - - def test_remove_kdump_item_success_ssh_key(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + def test_kdump_remove_not_configured(db): runner = CliRunner() - # Add ssh_key to config_db to test removal - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_key": "test_ssh_key"}) - - # Remove ssh_key - result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_key"], obj=db) - print(result.exit_code) + db.cfgdb.get_table.return_value = {"config": {}} + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) assert result.exit_code == 0 - - # Verify the ssh_key is removed from config_db - kdump_table = db.cfgdb.get_table("KDUMP") - assert kdump_table["config"]["ssh_key"] == "" + assert "Error: ssh_string is not configured." in result.output @classmethod def teardown_class(cls): From 1bbf78a0c4a3149d7f242b18a3b04860a3669d9f Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 30 Jul 2024 14:13:26 +0500 Subject: [PATCH 066/127] erased white space and blanks --- tests/kdump_test.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 2e72f40edd..e5b2127143 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,14 +1,11 @@ -import os -import sys import pytest -import click from click.testing import CliRunner from unittest import mock from utilities_common.db import Db from config.main import config -# Mocking the database and filesystem interactions + @pytest.fixture def db(): db = mock.MagicMock() @@ -81,7 +78,7 @@ def test_config_kdump_num_dumps(self, get_cmd_module): print(result.exit_code) assert result.exit_code == 1 - def test_kdump_remote_enable(db): + def test_kdump_remote_enable(self, db): runner = CliRunner() db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} @@ -89,7 +86,7 @@ def test_kdump_remote_enable(db): assert result.exit_code == 0 db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) - def test_kdump_remote_enable_already_enabled(db): + def test_kdump_remote_enable_already_enabled(self, db): runner = CliRunner() db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} @@ -97,7 +94,7 @@ def test_kdump_remote_enable_already_enabled(db): assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already enabled." in result.output - def test_kdump_remote_disable(db): + def test_kdump_remote_disable(self, db): runner = CliRunner() db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} @@ -105,7 +102,7 @@ def test_kdump_remote_disable(db): assert result.exit_code == 0 db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) - def test_kdump_remote_disable_already_disabled(db): + def test_kdump_remote_disable_already_disabled(self, db): runner = CliRunner() db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} @@ -113,7 +110,7 @@ def test_kdump_remote_disable_already_disabled(db): assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already disabled." in result.output - def test_kdump_remote_disable_with_ssh_values(db): + def test_kdump_remote_disable_with_ssh_values(self, db): runner = CliRunner() db.cfgdb.get_table.return_value = { @@ -127,7 +124,7 @@ def test_kdump_remote_disable_with_ssh_values(db): ) assert expected_output in result.output - def test_kdump_add(db): + def test_kdump_add(self, db): runner = CliRunner() db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} @@ -136,7 +133,7 @@ def test_kdump_add(db): assert result.exit_code == 0 db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"ssh_string": "test_ssh_string"}) - def test_kdump_add_remote_not_enabled(db): + def test_kdump_add_remote_not_enabled(self, db): runner = CliRunner() db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} @@ -145,7 +142,7 @@ def test_kdump_add_remote_not_enabled(db): assert result.exit_code == 0 assert "Error: Enable remote mode first." in result.output - def test_kdump_add_already_exists(db): + def test_kdump_add_already_exists(self, db): runner = CliRunner() db.cfgdb.get_table.return_value = {"config": {"remote": "true", "ssh_string": "existing_ssh_string"}} @@ -154,7 +151,7 @@ def test_kdump_add_already_exists(db): assert result.exit_code == 0 assert "Error: ssh_string is already added." in result.output - def test_kdump_remove(db): + def test_kdump_remove(self, db): runner = CliRunner() db.cfgdb.get_table.return_value = {"config": {"ssh_string": "test_ssh_string"}} @@ -162,7 +159,7 @@ def test_kdump_remove(db): assert result.exit_code == 0 db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"ssh_string": ""}) - def test_kdump_remove_not_configured(db): + def test_kdump_remove_not_configured(self, db): runner = CliRunner() db.cfgdb.get_table.return_value = {"config": {}} From 98f3e1441dfa55af3e3df5cd25aea49756b46601 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 30 Jul 2024 15:45:11 +0500 Subject: [PATCH 067/127] erased white space and blanks --- tests/kdump_test.py | 95 ++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 62 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index e5b2127143..14de4123b3 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,18 +1,14 @@ -import pytest -from click.testing import CliRunner +import importlib from unittest import mock + +from click.testing import CliRunner from utilities_common.db import Db +import pytest +from pathlib import Path from config.main import config -@pytest.fixture -def db(): - db = mock.MagicMock() - db.cfgdb = mock.MagicMock() - return db - - class TestKdump(object): @classmethod def setup_class(cls): @@ -78,17 +74,35 @@ def test_config_kdump_num_dumps(self, get_cmd_module): print(result.exit_code) assert result.exit_code == 1 + + @pytest.fixture + def db(): + db = mock.MagicMock() + db.cfgdb = mock.MagicMock() + return db + + def setup_and_teardown(self, db): + # Mock the file read and write operations + self.file_path = Path('/etc/default/kdump-tools') + self.mock_open = mock.mock_open(read_data="") + self.patcher = mock.patch("builtins.open", self.mock_open) + self.patcher.start() + yield + self.patcher.stop() + def test_kdump_remote_enable(self, db): runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) assert result.exit_code == 0 db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) + self.mock_open.assert_called_once_with(self.file_path, 'r') + handle = self.mock_open() + handle.write.assert_any_call('SSH="your_ssh_value"\n') + handle.write.assert_any_call('SSH_KEY="your_ssh_key_value"\n') def test_kdump_remote_enable_already_enabled(self, db): runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) assert result.exit_code == 0 @@ -96,15 +110,17 @@ def test_kdump_remote_enable_already_enabled(self, db): def test_kdump_remote_disable(self, db): runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) + self.mock_open.assert_called_once_with(self.file_path, 'r') + handle = self.mock_open() + handle.write.assert_any_call('#SSH="your_ssh_value"\n') + handle.write.assert_any_call('#SSH_KEY="your_ssh_key_value"\n') def test_kdump_remote_disable_already_disabled(self, db): runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 @@ -112,61 +128,16 @@ def test_kdump_remote_disable_already_disabled(self, db): def test_kdump_remote_disable_with_ssh_values(self, db): runner = CliRunner() - db.cfgdb.get_table.return_value = { "config": {"remote": "true", "ssh_string": "some_ssh_string", "ssh_key": "some_ssh_key"} } result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 - expected_output = ( - "Error: Remove SSH_string and SSH_key from Config DB before disabling " - "Kdump Remote Mode." + expected_error_message = ( + "Error: Remove SSH_string and SSH_key from Config DB before disabling " + "Kdump Remote Mode." ) - assert expected_output in result.output - - def test_kdump_add(self, db): - runner = CliRunner() - - db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} - result = runner.invoke(config.config.commands["kdump"].commands["add"], - ["ssh_string", "test_ssh_string"], obj=db) - assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"ssh_string": "test_ssh_string"}) - - def test_kdump_add_remote_not_enabled(self, db): - runner = CliRunner() - - db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - result = runner.invoke(config.config.commands["kdump"].commands["add"], - ["ssh_string", "test_ssh_string"], obj=db) - assert result.exit_code == 0 - assert "Error: Enable remote mode first." in result.output - - def test_kdump_add_already_exists(self, db): - runner = CliRunner() - - db.cfgdb.get_table.return_value = {"config": {"remote": "true", "ssh_string": "existing_ssh_string"}} - result = runner.invoke(config.config.commands["kdump"].commands["add"], - ["ssh_string", "test_ssh_string"], obj=db) - assert result.exit_code == 0 - assert "Error: ssh_string is already added." in result.output - - def test_kdump_remove(self, db): - runner = CliRunner() - - db.cfgdb.get_table.return_value = {"config": {"ssh_string": "test_ssh_string"}} - result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) - assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"ssh_string": ""}) - - def test_kdump_remove_not_configured(self, db): - runner = CliRunner() - - db.cfgdb.get_table.return_value = {"config": {}} - result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) - assert result.exit_code == 0 - assert "Error: ssh_string is not configured." in result.output - + assert expected_error_message in result.output @classmethod def teardown_class(cls): print("TEARDOWN") From bc87384a405a517962d4a121ce5126338c6349dc Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 30 Jul 2024 16:02:21 +0500 Subject: [PATCH 068/127] erased white space and blanks --- tests/kdump_test.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 14de4123b3..9a231465d9 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,4 +1,3 @@ -import importlib from unittest import mock from click.testing import CliRunner @@ -74,13 +73,12 @@ def test_config_kdump_num_dumps(self, get_cmd_module): print(result.exit_code) assert result.exit_code == 1 - @pytest.fixture def db(): db = mock.MagicMock() db.cfgdb = mock.MagicMock() return db - + def setup_and_teardown(self, db): # Mock the file read and write operations self.file_path = Path('/etc/default/kdump-tools') @@ -134,10 +132,11 @@ def test_kdump_remote_disable_with_ssh_values(self, db): result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 expected_error_message = ( - "Error: Remove SSH_string and SSH_key from Config DB before disabling " - "Kdump Remote Mode." + "Error: Remove SSH_string and SSH_key from Config DB before disabling " + "Kdump Remote Mode." ) assert expected_error_message in result.output + @classmethod def teardown_class(cls): print("TEARDOWN") From 617523b7e8aa7ecfa04eb6bfecac3e3148885df9 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 30 Jul 2024 16:06:48 +0500 Subject: [PATCH 069/127] erased white space and blanks --- tests/kdump_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 9a231465d9..03a14b555b 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -136,7 +136,7 @@ def test_kdump_remote_disable_with_ssh_values(self, db): "Kdump Remote Mode." ) assert expected_error_message in result.output - + @classmethod def teardown_class(cls): print("TEARDOWN") From d8fe5b840241a79d4032ddefd7da952ce097d1aa Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 30 Jul 2024 16:33:03 +0500 Subject: [PATCH 070/127] erased white space and blanks --- tests/kdump_test.py | 71 +-------------------------------------------- 1 file changed, 1 insertion(+), 70 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 03a14b555b..7257c771f6 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,13 +1,8 @@ -from unittest import mock + from click.testing import CliRunner from utilities_common.db import Db -import pytest -from pathlib import Path -from config.main import config - - class TestKdump(object): @classmethod def setup_class(cls): @@ -73,70 +68,6 @@ def test_config_kdump_num_dumps(self, get_cmd_module): print(result.exit_code) assert result.exit_code == 1 - @pytest.fixture - def db(): - db = mock.MagicMock() - db.cfgdb = mock.MagicMock() - return db - - def setup_and_teardown(self, db): - # Mock the file read and write operations - self.file_path = Path('/etc/default/kdump-tools') - self.mock_open = mock.mock_open(read_data="") - self.patcher = mock.patch("builtins.open", self.mock_open) - self.patcher.start() - yield - self.patcher.stop() - - def test_kdump_remote_enable(self, db): - runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) - self.mock_open.assert_called_once_with(self.file_path, 'r') - handle = self.mock_open() - handle.write.assert_any_call('SSH="your_ssh_value"\n') - handle.write.assert_any_call('SSH_KEY="your_ssh_key_value"\n') - - def test_kdump_remote_enable_already_enabled(self, db): - runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 0 - assert "Error: Kdump Remote Mode is already enabled." in result.output - - def test_kdump_remote_disable(self, db): - runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) - self.mock_open.assert_called_once_with(self.file_path, 'r') - handle = self.mock_open() - handle.write.assert_any_call('#SSH="your_ssh_value"\n') - handle.write.assert_any_call('#SSH_KEY="your_ssh_key_value"\n') - - def test_kdump_remote_disable_already_disabled(self, db): - runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - assert result.exit_code == 0 - assert "Error: Kdump Remote Mode is already disabled." in result.output - - def test_kdump_remote_disable_with_ssh_values(self, db): - runner = CliRunner() - db.cfgdb.get_table.return_value = { - "config": {"remote": "true", "ssh_string": "some_ssh_string", "ssh_key": "some_ssh_key"} - } - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - assert result.exit_code == 0 - expected_error_message = ( - "Error: Remove SSH_string and SSH_key from Config DB before disabling " - "Kdump Remote Mode." - ) - assert expected_error_message in result.output - @classmethod def teardown_class(cls): print("TEARDOWN") From a5c5f341d094882b11fcf16fb9b5a5d9afb20122 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 31 Jul 2024 11:11:27 +0500 Subject: [PATCH 071/127] erased functions from scripts/kdump file --- scripts/sonic-kdump-config | 214 +------------------------------------ 1 file changed, 5 insertions(+), 209 deletions(-) diff --git a/scripts/sonic-kdump-config b/scripts/sonic-kdump-config index 27d949046d..85743bac0a 100755 --- a/scripts/sonic-kdump-config +++ b/scripts/sonic-kdump-config @@ -25,7 +25,6 @@ import sys import syslog import subprocess -from swsssdk import ConfigDBConnector from swsscommon.swsscommon import ConfigDBConnector from sonic_installer.bootloader import get_bootloader from sonic_installer.common import IMAGE_PREFIX @@ -174,15 +173,9 @@ def cmd_dump_config_json(): kdump_memory = get_kdump_memory() kdump_num_dumps = get_kdump_num_dumps() # remote SSH variables - ssh_string = get_ssh_string() - ssh_path = get_ssh_path() - kdump_remote = get_remote() data = { "enable" : kdump_enabled, \ "memory" : kdump_memory, \ - "max-dumps" : int(kdump_num_dumps), \ - "ssh_string" : ssh_string, \ - "ssh_path" : ssh_path, \ - "remote" : kdump_remote } + "max-dumps" : int(kdump_num_dumps)} print(json.dumps(data, indent=4)) def cmd_dump_kdump_records_json(): @@ -232,17 +225,11 @@ def cmd_dump_status_json(): kdump_memory = get_kdump_memory() kdump_num_dumps = get_kdump_num_dumps() # remote SSH variables - ssh_string = get_ssh_string() - ssh_path = get_ssh_path() - kdump_remote = get_remote() data = { "enable" : kdump_enabled, \ "current-state" : kdump_oper_state, \ "memory" : kdump_memory, \ "allocated-memory" : get_crash_kernel_size(), \ - "max-dumps" : int(kdump_num_dumps), \ - "ssh_string" : ssh_string, \ - "ssh_path" : ssh_path, \ - "remote" : kdump_remote } + "max-dumps" : int(kdump_num_dumps) } print(json.dumps(data, indent=4)) ## Query current configuration to check if kdump is enabled or disabled @@ -314,62 +301,6 @@ def get_kdump_num_dumps(): num_dumps = num return num_dumps -## Query current configuration for kdump ssh_string -# -# @return The ssh remote connection string for storing kernel dump files remotely -# (read from running configuration) -def get_ssh_string(): - ssh_string = "username@serverip" - config_db = ConfigDBConnector(use_unix_socket_path=True) - if config_db is not None: - config_db.connect() - table_data = config_db.get_table('KDUMP') - if table_data is not None: - config_data = table_data.get('config') - if config_data is not None: - conn = config_data.get('ssh_string') - if conn: - ssh_string = conn - return ssh_string - -## Query current configuration for kdump ssh_path -# -# @return The ssh remote connection private_key_file_path for connecting to remote server without authentication (command "sudo kdump-config propagate" required) -# (read from running configuration) -def get_ssh_path(): - ssh_path = "/root/.ssh/kdump_id_rsa" - config_db = ConfigDBConnector(use_unix_socket_path=True) - if config_db is not None: - config_db.connect() - table_data = config_db.get_table('KDUMP') - if table_data is not None: - config_data = table_data.get('config') - if config_data is not None: - pr_key_path = config_data.get('ssh_path') - if pr_key_path: - ssh_path = pr_key_path - return ssh_path - -## Query current configuration for kdump remote_enabled -# -# @return The if remote connection is enabled for connecting to remote server -# (read from running configuration) -def get_remote(): - remote = False - config_db = ConfigDBConnector(use_unix_socket_path=True) - if config_db is not None: - config_db.connect() - table_data = config_db.get_table('KDUMP') - if table_data is not None: - config_data = table_data.get('config') - if config_data is not None: - remote_en = config_data.get('remote') - if remote_en: - remote = remote_en - return remote - - - ## Read current value for USE_KDUMP in kdump config file # # @return The integer value X from USE_KDUMP=X in /etc/default/kdump-tools @@ -433,112 +364,6 @@ def write_num_dumps(num_dumps): print_err("Error while writing KDUMP_NUM_DUMPS into %s" % kdump_cfg) sys.exit(1) -## Read current value for SSH SSH kdump-ssh-connection-string in kdump config file -# -# @return The string value X from SSH=X in /etc/default/kdump-tools -def read_ssh_string(): - (rc, lines, err_str) = run_command("grep '#*SSH=.*' %s | cut -d = -f 2" % kdump_cfg, use_shell=True); - if rc == 0 and type(lines) == list and len(lines) >= 1: - try: - return int(lines[0]) - except Exception as e: - print_err('Error! Exception[%s] occured while reading from %s' %(str(e), kdump_cfg)) - sys.exit(1) - else: - print_err("Unable to read SSH from %s" % kdump_cfg) - sys.exit(1) - -## Change the value for SSH kdump-ssh-connection-string in kdump config file /etc/default/kdump-tools -# -# #param ssh-connection-string value for new value -def write_ssh_string(ssh_string): - (rc, lines, err_str) = run_command("/bin/sed -i -e 's/#*SSH=.*/SSH=%d/' %s" % (ssh_string, kdump_cfg), use_shell=False); - if rc == 0 and type(lines) == list and len(lines) == 0: - ssh_string_in_cfg = read_ssh_string() - if ssh_string_in_cfg != ssh_string: - print_err("Unable to write SSH into %s" % kdump_cfg) - sys.exit(1) - else: - print_err("Error while writing SSH into %s" % kdump_cfg) - sys.exit(1) - - -## Read current value for SSH_KEY kdump-ssh-private-key-path in kdump config file -# -# @return The string value X from SSH_KEY=X in /etc/default/kdump-tools -def read_ssh_path(): - (rc, lines, err_str) = run_command("grep '#*SSH_KEY=.*' %s | cut -d = -f 2" % kdump_cfg, use_shell=True); - if rc == 0 and type(lines) == list and len(lines) >= 1: - try: - return int(lines[0]) - except Exception as e: - print_err('Error! Exception[%s] occured while reading from %s' %(str(e), kdump_cfg)) - sys.exit(1) - else: - print_err("Unable to read SSH_KEY from %s" % kdump_cfg) - sys.exit(1) - -def write_ssh_path(ssh_path): - (rc, lines, err_str) = run_command("/bin/sed -i -e 's/#*SSH_KEY=.*/SSH_KEY=%d/' %s" % (ssh_path, kdump_cfg), use_shell=False); - if rc == 0 and type(lines) == list and len(lines) == 0: - ssh_path_in_cfg = read_ssh_path() - if ssh_path_in_cfg != ssh_path: - print_err("Unable to write SSH_KEY into %s" % kdump_cfg) - sys.exit(1) - else: - print_err("Error while writing SSH_KEY into %s" % kdump_cfg) - sys.exit(1) - return False - - -# Example usage -def get_ssh_path(): - # This is a placeholder for the function that retrieves the SSH path. - # Replace it with the actual implementation. - return "/root/.ssh/kdump_id_rsa" - -ssh_path = get_ssh_path() - -# Write the retrieved value to kdump_cfg only if it's not the default value -if ssh_path != "/root/.ssh/kdump_id_rsa": - write_ssh_path(ssh_path) - - -# Now you can potentially read the written value using read_ssh_path(kdump_cfg) - -## Disable remote: Comment the value for SSH and SSH_KEY in kdump config file /etc/default/kdump-tools -# -def kdump_remote_disable(): - """ - Disables remote kdump in the configuration file (kdump_cfg). - - This function checks if remote kdump is already enabled before attempting - to disable it. If enabled, it uses sed to modify the configuration file - in-place, removing any comment character (#) before the SSH and SSH_KEY lines. - The function exits with an error message if any of the sed commands fail. - - Returns: - None - """ - - # Check if remote kdump is already enabled - if get_remote().lower() == "true": - # Disable SSH key authentication - rc = run_command("/bin/sed -i '/*SSH=\"/s/\#/' %s" % (kdump_cfg), use_shell=False) - if rc != 0: - print_err("Error while disabling SSH key auth in", kdump_cfg) - sys.exit(1) - - # Disable SSH authentication (optional, depending on configuration) - rc = run_command("/bin/sed -i '/*SSH_KEY=\"/s/\#/' %s" % (kdump_cfg), use_shell=False) - if rc != 0: - print_err("Error while disabling SSH auth in", kdump_cfg) - sys.exit(1) - - print("Successfully disabled remote kdump in", kdump_cfg) - else: - print("Remote kdump is already disabled in", kdump_cfg) - ## Enable kdump # # @param verbose If True, the function will display a few additinal information @@ -625,16 +450,8 @@ def cmd_kdump_enable(verbose, image): kdump_enabled = get_kdump_administrative_mode() memory = get_kdump_memory() num_dumps = get_kdump_num_dumps() - - ## - # ToDo - 02 - ## - ssh_string = get_ssh_string() - ssh_path = get_ssh_path() - remote = get_remote() if verbose: - print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d ssh_string=%d ssh_path=%d remote=%d" % (kdump_enabled, memory, num_dumps, ssh_string, ssh_path, remote)) - + print("kdump enabled: %s" % kdump_enabled) if os.path.exists(grub_cfg): return kdump_enable(verbose, kdump_enabled, memory, num_dumps, image, grub_cfg) elif open(machine_cfg, 'r').read().find('aboot_platform') >= 0: @@ -711,12 +528,9 @@ def cmd_kdump_disable(verbose): kdump_enabled = get_kdump_administrative_mode() memory = get_kdump_memory() num_dumps = get_kdump_num_dumps() - ssh_path = get_ssh_path() - ssh_string = get_ssh_string() - + if verbose: - print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d ssh_string=[%s] ssh_path=[%s]" % (kdump_enabled, memory, num_dumps, ssh_string, ssh_path)) - + print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d" % (kdump_enabled, memory, num_dumps)) if os.path.exists(grub_cfg): return kdump_disable(verbose, image, grub_cfg) elif open(machine_cfg, 'r').read().find('aboot_platform') >= 0: @@ -762,24 +576,6 @@ def cmd_kdump_num_dumps(verbose, num_dumps): kdump_enabled = get_kdump_administrative_mode() kdump_memory = get_kdump_memory() -def cmd_ssh_string(verbose, ssh_string): - if ssh_string is None: - (rc, lines, err_str) = run_command("show kdump config", use_shell=False); - print('\n'.join(lines)) - else: - write_num_dumps(ssh_string) - kdump_enabled = get_kdump_administrative_mode() - kdump_memory = get_kdump_memory() - -def cmd_ssh_path(verbose, ssh_path): - if ssh_path is None: - (rc, lines, err_str) = run_command("show kdump config", use_shell=False); - print('\n'.join(lines)) - else: - write_num_dumps(ssh_path) - kdump_enabled = get_kdump_administrative_mode() - kdump_memory = get_kdump_memory() - def main(): From 318f658b66f02a9ee66221b272825e86c7408bf1 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 31 Jul 2024 11:55:11 +0500 Subject: [PATCH 072/127] erased functions from scripts/kdump file --- scripts/sonic-kdump-config | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scripts/sonic-kdump-config b/scripts/sonic-kdump-config index 85743bac0a..661b82399c 100755 --- a/scripts/sonic-kdump-config +++ b/scripts/sonic-kdump-config @@ -653,10 +653,6 @@ def main(): cmd_kdump_memory(options.verbose, options.memory) elif options.num_dumps != False: cmd_kdump_num_dumps(options.verbose, options.num_dumps) - elif options.ssh_string != False: - cmd_ssh_string(options.verbose, options.ssh_string) - elif options.ssh_path != False: - cmd_ssh_path(options.verbose, options.ssh_path) elif options.dump_db: cmd_dump_db() elif options.status_json: From 53a353005162cfad6a2e826ca382f3ed3fccd5d3 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 31 Jul 2024 15:43:05 +0500 Subject: [PATCH 073/127] just added test case for the remote enable --- config/kdump.py | 1 + tests/kdump_test.py | 79 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 4118297e42..e9bf578cee 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -141,6 +141,7 @@ def kdump_remote(db, action): # Write the updated lines back to the configuration file with open(file_path, 'w') as file: file.writelines(updated_lines) + echo_reboot_warning() click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") if action.lower() == 'disable': # Read the content of the file diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 7257c771f6..d62705e8d4 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,6 +1,7 @@ - - +import pytest from click.testing import CliRunner +from unittest import mock +from config.main import config from utilities_common.db import Db class TestKdump(object): @@ -71,3 +72,77 @@ def test_config_kdump_num_dumps(self, get_cmd_module): @classmethod def teardown_class(cls): print("TEARDOWN") + +# Mocking the database and filesystem interactions +@pytest.fixture +def db(): + db = mock.MagicMock() + db.cfgdb = mock.MagicMock() + return db + +def check_kdump_table_existence(kdump_table): + """Mock function for checking kdump table existence.""" + pass + +class TestKdumpRemote: + @classmethod + def setup_class(cls): + print("SETUP") + + def test_kdump_remote_enable(self, db): + runner = CliRunner() + # Mocking the initial state where remote is disabled + db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} + + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) + assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out." in result.output + + def test_kdump_remote_enable_already_enabled(self, db): + runner = CliRunner() + # Mocking the initial state where remote is already enabled + db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} + + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already enabled." in result.output + + def test_kdump_remote_disable(self, db): + runner = CliRunner() + # Mocking the initial state where remote is enabled + db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} + + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) + assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out." in result.output + + def test_kdump_remote_disable_already_disabled(self, db): + runner = CliRunner() + # Mocking the initial state where remote is already disabled + db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} + + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already disabled." in result.output + + def test_kdump_remote_disable_with_ssh_values(self, db): + runner = CliRunner() + # Mocking the initial state where remote is enabled with ssh values + db.cfgdb.get_table.return_value = { + "config": {"remote": "true", "ssh_string": "some_ssh_string", "ssh_key": "some_ssh_key"} + } + + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + assert result.exit_code == 0 + expected_output = ( + "Error: Remove SSH_string and SSH_key from Config DB before disabling " + "Kdump Remote Mode." + ) + assert expected_output in result.output + + @classmethod + def teardown_class(cls): + print("TEARDOWN") + From dd13c18c0179152aca54ef98067ec034d9b3665f Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 31 Jul 2024 16:40:18 +0500 Subject: [PATCH 074/127] just added test case for the remote enable --- tests/kdump_test.py | 152 +++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 81 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index d62705e8d4..5beaa90b65 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,141 +1,132 @@ import pytest from click.testing import CliRunner from unittest import mock -from config.main import config -from utilities_common.db import Db +from pathlib import Path +from config.kdump import kdump_disable, kdump_enable, kdump_memory, kdump_num_dumps, kdump_remote -class TestKdump(object): +@pytest.fixture +def db(): + db = mock.MagicMock() + db.cfgdb = mock.MagicMock() + return db + +class TestKdump: @classmethod def setup_class(cls): print("SETUP") - def test_config_kdump_disable(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + def test_config_kdump_disable(self, db): runner = CliRunner() - result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 + db.cfgdb.get_table.return_value = {"config": {"enabled": "true"}} - # Delete the 'KDUMP' table. - db.cfgdb.delete_table("KDUMP") + result = runner.invoke(kdump_disable, obj=db) + assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"enabled": "false"}) - result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db) - print(result.exit_code) + db.cfgdb.get_table.return_value = None + result = runner.invoke(kdump_disable, obj=db) assert result.exit_code == 1 + assert "Unable to retrieve 'KDUMP' table from Config DB." in result.output - def test_config_kdump_enable(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + def test_config_kdump_enable(self, db): runner = CliRunner() - result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 + db.cfgdb.get_table.return_value = {"config": {"enabled": "false"}} - # Delete the 'KDUMP' table. - db.cfgdb.delete_table("KDUMP") + result = runner.invoke(kdump_enable, obj=db) + assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"enabled": "true"}) - result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db) - print(result.exit_code) + db.cfgdb.get_table.return_value = None + result = runner.invoke(kdump_enable, obj=db) assert result.exit_code == 1 + assert "Unable to retrieve 'KDUMP' table from Config DB." in result.output - def test_config_kdump_memory(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + def test_config_kdump_memory(self, db): runner = CliRunner() - result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 + db.cfgdb.get_table.return_value = {"config": {"memory": "256MB"}} - # Delete the 'KDUMP' table. - db.cfgdb.delete_table("KDUMP") + result = runner.invoke(kdump_memory, ["512MB"], obj=db) + assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"memory": "512MB"}) - result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db) - print(result.exit_code) + db.cfgdb.get_table.return_value = None + result = runner.invoke(kdump_memory, ["512MB"], obj=db) assert result.exit_code == 1 + assert "Unable to retrieve 'KDUMP' table from Config DB." in result.output - def test_config_kdump_num_dumps(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() + def test_config_kdump_num_dumps(self, db): runner = CliRunner() - result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 + db.cfgdb.get_table.return_value = {"config": {"num_dumps": "3"}} - # Delete the 'KDUMP' table. - db.cfgdb.delete_table("KDUMP") + result = runner.invoke(kdump_num_dumps, ["5"], obj=db) + assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"num_dumps": 5}) - result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) - print(result.exit_code) + db.cfgdb.get_table.return_value = None + result = runner.invoke(kdump_num_dumps, ["5"], obj=db) assert result.exit_code == 1 - - @classmethod - def teardown_class(cls): - print("TEARDOWN") - -# Mocking the database and filesystem interactions -@pytest.fixture -def db(): - db = mock.MagicMock() - db.cfgdb = mock.MagicMock() - return db - -def check_kdump_table_existence(kdump_table): - """Mock function for checking kdump table existence.""" - pass - -class TestKdumpRemote: - @classmethod - def setup_class(cls): - print("SETUP") + assert "Unable to retrieve 'KDUMP' table from Config DB." in result.output def test_kdump_remote_enable(self, db): runner = CliRunner() - # Mocking the initial state where remote is disabled db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + mock_file_path = mock.MagicMock(spec=Path) + mock_file_path.exists.return_value = True + + m_open = mock.mock_open(read_data="#SSH=\n#SSH_KEY=\n") + with mock.patch("builtins.open", m_open): + with mock.patch("your_module_name.Path", return_value=mock_file_path): + result = runner.invoke(kdump_remote, ["enable"], obj=db) + assert result.exit_code == 0 db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) + m_open().write.assert_any_call('SSH="your_ssh_value"\n') + m_open().write.assert_any_call('SSH_KEY="your_ssh_key_value"\n') assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out." in result.output def test_kdump_remote_enable_already_enabled(self, db): runner = CliRunner() - # Mocking the initial state where remote is already enabled db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} - - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 0 + + result = runner.invoke(kdump_remote, ["enable"], obj=db) + assert result.exit_code == 1 assert "Error: Kdump Remote Mode is already enabled." in result.output def test_kdump_remote_disable(self, db): runner = CliRunner() - # Mocking the initial state where remote is enabled db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} - - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + + mock_file_path = mock.MagicMock(spec=Path) + mock_file_path.exists.return_value = True + + m_open = mock.mock_open(read_data="SSH=your_ssh_value\nSSH_KEY=your_ssh_key_value\n") + with mock.patch("builtins.open", m_open): + with mock.patch("your_module_name.Path", return_value=mock_file_path): + result = runner.invoke(kdump_remote, ["disable"], obj=db) + assert result.exit_code == 0 db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) + m_open().write.assert_any_call('#SSH="your_ssh_value"\n') + m_open().write.assert_any_call('#SSH_KEY="your_ssh_key_value"\n') assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out." in result.output def test_kdump_remote_disable_already_disabled(self, db): runner = CliRunner() - # Mocking the initial state where remote is already disabled db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - assert result.exit_code == 0 + + result = runner.invoke(kdump_remote, ["disable"], obj=db) + assert result.exit_code == 1 assert "Error: Kdump Remote Mode is already disabled." in result.output def test_kdump_remote_disable_with_ssh_values(self, db): runner = CliRunner() - # Mocking the initial state where remote is enabled with ssh values db.cfgdb.get_table.return_value = { "config": {"remote": "true", "ssh_string": "some_ssh_string", "ssh_key": "some_ssh_key"} } - - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - assert result.exit_code == 0 + + result = runner.invoke(kdump_remote, ["disable"], obj=db) + assert result.exit_code == 1 expected_output = ( "Error: Remove SSH_string and SSH_key from Config DB before disabling " "Kdump Remote Mode." @@ -145,4 +136,3 @@ def test_kdump_remote_disable_with_ssh_values(self, db): @classmethod def teardown_class(cls): print("TEARDOWN") - From 9f21cf6854a03db88c34123aa669ff6d374e5b7f Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 31 Jul 2024 16:43:10 +0500 Subject: [PATCH 075/127] just added test case for the remote enable --- tests/kdump_test.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 5beaa90b65..f76d851128 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -4,12 +4,14 @@ from pathlib import Path from config.kdump import kdump_disable, kdump_enable, kdump_memory, kdump_num_dumps, kdump_remote + @pytest.fixture def db(): db = mock.MagicMock() db.cfgdb = mock.MagicMock() return db + class TestKdump: @classmethod def setup_class(cls): @@ -70,7 +72,7 @@ def test_config_kdump_num_dumps(self, db): def test_kdump_remote_enable(self, db): runner = CliRunner() db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - + mock_file_path = mock.MagicMock(spec=Path) mock_file_path.exists.return_value = True From 4e66ca11d2d44bb264960ad0c6afd3ef20e8c586 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 31 Jul 2024 18:09:10 +0500 Subject: [PATCH 076/127] ... --- tests/kdump_test.py | 145 +++++++++++++------------------------------- 1 file changed, 42 insertions(+), 103 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index f76d851128..b1b1cbc1be 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,140 +1,79 @@ import pytest from click.testing import CliRunner from unittest import mock -from pathlib import Path -from config.kdump import kdump_disable, kdump_enable, kdump_memory, kdump_num_dumps, kdump_remote +from config.main import config +from utilities_common.db import Db -@pytest.fixture -def db(): - db = mock.MagicMock() - db.cfgdb = mock.MagicMock() - return db - -class TestKdump: +class TestKdump(object): @classmethod def setup_class(cls): print("SETUP") - def test_config_kdump_disable(self, db): + def test_config_kdump_disable(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"enabled": "true"}} - - result = runner.invoke(kdump_disable, obj=db) + result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db) + print(result.exit_code) assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"enabled": "false"}) - - db.cfgdb.get_table.return_value = None - result = runner.invoke(kdump_disable, obj=db) - assert result.exit_code == 1 - assert "Unable to retrieve 'KDUMP' table from Config DB." in result.output - def test_config_kdump_enable(self, db): - runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"enabled": "false"}} - - result = runner.invoke(kdump_enable, obj=db) - assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"enabled": "true"}) + # Delete the 'KDUMP' table. + db.cfgdb.delete_table("KDUMP") - db.cfgdb.get_table.return_value = None - result = runner.invoke(kdump_enable, obj=db) + result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db) + print(result.exit_code) assert result.exit_code == 1 - assert "Unable to retrieve 'KDUMP' table from Config DB." in result.output - def test_config_kdump_memory(self, db): + def test_config_kdump_enable(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"memory": "256MB"}} - - result = runner.invoke(kdump_memory, ["512MB"], obj=db) + result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db) + print(result.exit_code) assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"memory": "512MB"}) - - db.cfgdb.get_table.return_value = None - result = runner.invoke(kdump_memory, ["512MB"], obj=db) - assert result.exit_code == 1 - assert "Unable to retrieve 'KDUMP' table from Config DB." in result.output - def test_config_kdump_num_dumps(self, db): - runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"num_dumps": "3"}} + # Delete the 'KDUMP' table. + db.cfgdb.delete_table("KDUMP") - result = runner.invoke(kdump_num_dumps, ["5"], obj=db) - assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"num_dumps": 5}) - - db.cfgdb.get_table.return_value = None - result = runner.invoke(kdump_num_dumps, ["5"], obj=db) + result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db) + print(result.exit_code) assert result.exit_code == 1 - assert "Unable to retrieve 'KDUMP' table from Config DB." in result.output - def test_kdump_remote_enable(self, db): + def test_config_kdump_memory(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - - mock_file_path = mock.MagicMock(spec=Path) - mock_file_path.exists.return_value = True - - m_open = mock.mock_open(read_data="#SSH=\n#SSH_KEY=\n") - with mock.patch("builtins.open", m_open): - with mock.patch("your_module_name.Path", return_value=mock_file_path): - result = runner.invoke(kdump_remote, ["enable"], obj=db) - + result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db) + print(result.exit_code) assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) - m_open().write.assert_any_call('SSH="your_ssh_value"\n') - m_open().write.assert_any_call('SSH_KEY="your_ssh_key_value"\n') - assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out." in result.output - def test_kdump_remote_enable_already_enabled(self, db): - runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} + # Delete the 'KDUMP' table. + db.cfgdb.delete_table("KDUMP") - result = runner.invoke(kdump_remote, ["enable"], obj=db) + result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db) + print(result.exit_code) assert result.exit_code == 1 - assert "Error: Kdump Remote Mode is already enabled." in result.output - def test_kdump_remote_disable(self, db): + def test_config_kdump_num_dumps(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} - - mock_file_path = mock.MagicMock(spec=Path) - mock_file_path.exists.return_value = True - - m_open = mock.mock_open(read_data="SSH=your_ssh_value\nSSH_KEY=your_ssh_key_value\n") - with mock.patch("builtins.open", m_open): - with mock.patch("your_module_name.Path", return_value=mock_file_path): - result = runner.invoke(kdump_remote, ["disable"], obj=db) - + result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) + print(result.exit_code) assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) - m_open().write.assert_any_call('#SSH="your_ssh_value"\n') - m_open().write.assert_any_call('#SSH_KEY="your_ssh_key_value"\n') - assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out." in result.output - - def test_kdump_remote_disable_already_disabled(self, db): - runner = CliRunner() - db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - - result = runner.invoke(kdump_remote, ["disable"], obj=db) - assert result.exit_code == 1 - assert "Error: Kdump Remote Mode is already disabled." in result.output - def test_kdump_remote_disable_with_ssh_values(self, db): - runner = CliRunner() - db.cfgdb.get_table.return_value = { - "config": {"remote": "true", "ssh_string": "some_ssh_string", "ssh_key": "some_ssh_key"} - } + # Delete the 'KDUMP' table. + db.cfgdb.delete_table("KDUMP") - result = runner.invoke(kdump_remote, ["disable"], obj=db) + result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) + print(result.exit_code) assert result.exit_code == 1 - expected_output = ( - "Error: Remove SSH_string and SSH_key from Config DB before disabling " - "Kdump Remote Mode." - ) - assert expected_output in result.output @classmethod def teardown_class(cls): print("TEARDOWN") + + + From 2400b336d5d2ec2f498c3740671baf73d39923d2 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 1 Aug 2024 11:01:10 +0500 Subject: [PATCH 077/127] remote enable/disable test --- config/kdump.py | 1 - tests/kdump_test.py | 116 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index e9bf578cee..5bda9d4776 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -142,7 +142,6 @@ def kdump_remote(db, action): with open(file_path, 'w') as file: file.writelines(updated_lines) echo_reboot_warning() - click.echo("Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out.") if action.lower() == 'disable': # Read the content of the file with open(file_path, 'r') as file: diff --git a/tests/kdump_test.py b/tests/kdump_test.py index b1b1cbc1be..20ce0e73c4 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -5,7 +5,6 @@ from utilities_common.db import Db - class TestKdump(object): @classmethod def setup_class(cls): @@ -76,4 +75,119 @@ def teardown_class(cls): print("TEARDOWN") +@pytest.fixture +def db(): + db = mock.MagicMock() + db.cfgdb = mock.MagicMock() + return db + +class TestKdumpRemote: + @classmethod + def setup_class(cls): + print("SETUP") + + def test_kdump_remote_enable(self, db): + runner = CliRunner() + # Mocking the initial state where remote is disabled + db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} + + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) + assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out." not in result.output + assert "KDUMP configuration changes may require a reboot to take effect." in result.output + + def test_kdump_remote_enable_already_enabled(self, db): + runner = CliRunner() + # Mocking the initial state where remote is already enabled + db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} + + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already enabled." in result.output + + def test_kdump_remote_disable(self, db): + runner = CliRunner() + # Mocking the initial state where remote is enabled + db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} + + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) + assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out." not in result.output + assert "KDUMP configuration changes may require a reboot to take effect." in result.output + + def test_kdump_remote_disable_already_disabled(self, db): + runner = CliRunner() + # Mocking the initial state where remote is already disabled + db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} + + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already disabled." in result.output + + def test_kdump_remote_disable_with_ssh_values(self, db): + runner = CliRunner() + # Mocking the initial state where remote is enabled with ssh values + db.cfgdb.get_table.return_value = { + "config": {"remote": "true", "ssh_string": "some_ssh_string", "ssh_key": "some_ssh_key"} + } + + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + assert result.exit_code == 0 + expected_output = ( + "Error: Remove SSH_string and SSH_key from Config DB before disabling " + "Kdump Remote Mode." + ) + assert expected_output in result.output + + @classmethod + def teardown_class(cls): + print("TEARDOWN") +# Mock the file interactions + +@mock.patch("pathlib.Path.open", create=True) +def test_kdump_remote_enable_file_update(mock_open, db): + runner = CliRunner() + # Mocking the initial state where remote is disabled + db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} + + file_mock = mock_open.return_value.__enter__.return_value + file_mock.readlines.return_value = [ + "#SSH=original_value\n", + "#SSH_KEY=original_value\n" + ] + + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) + file_mock.writelines.assert_called_once_with([ + 'SSH="your_ssh_value"\n', + 'SSH_KEY="your_ssh_key_value"\n' + ]) + assert "KDUMP configuration changes may require a reboot to take effect." in result.output + +@mock.patch("pathlib.Path.open", create=True) +def test_kdump_remote_disable_file_update(mock_open, db): + runner = CliRunner() + # Mocking the initial state where remote is enabled + db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} + + file_mock = mock_open.return_value.__enter__.return_value + file_mock.readlines.return_value = [ + 'SSH="your_ssh_value"\n', + 'SSH_KEY="your_ssh_key_value"\n' + ] + + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + assert result.exit_code == 0 + db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) + file_mock.writelines.assert_called_once_with([ + '#SSH="your_ssh_value"\n', + '#SSH_KEY="your_ssh_key_value"\n' + ]) + assert "KDUMP configuration changes may require a reboot to take effect." in result.output + +if __name__ == "__main__": + pytest.main() From 91763efd44d6368a52e269a86583111c54140e6b Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 1 Aug 2024 12:56:22 +0500 Subject: [PATCH 078/127] ... --- tests/kdump_test.py | 254 ++++++++++++++------------------------------ 1 file changed, 80 insertions(+), 174 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 20ce0e73c4..bed5c9f0df 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,193 +1,99 @@ import pytest from click.testing import CliRunner -from unittest import mock -from config.main import config -from utilities_common.db import Db +from unittest.mock import MagicMock, patch +from config.kdump import kdump_enable, kdump_disable, kdump_memory, kdump_num_dumps, kdump_remote, add_kdump_item, remove_kdump_item +@pytest.fixture +def runner(): + return CliRunner() -class TestKdump(object): - @classmethod - def setup_class(cls): - print("SETUP") - - def test_config_kdump_disable(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 - - # Delete the 'KDUMP' table. - db.cfgdb.delete_table("KDUMP") - - result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db) - print(result.exit_code) - assert result.exit_code == 1 - - def test_config_kdump_enable(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 - - # Delete the 'KDUMP' table. - db.cfgdb.delete_table("KDUMP") - - result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db) - print(result.exit_code) - assert result.exit_code == 1 - - def test_config_kdump_memory(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 - - # Delete the 'KDUMP' table. - db.cfgdb.delete_table("KDUMP") - - result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db) - print(result.exit_code) - assert result.exit_code == 1 - - def test_config_kdump_num_dumps(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) - print(result.exit_code) - assert result.exit_code == 0 - - # Delete the 'KDUMP' table. - db.cfgdb.delete_table("KDUMP") - - result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) - print(result.exit_code) - assert result.exit_code == 1 - - @classmethod - def teardown_class(cls): - print("TEARDOWN") - +@pytest.fixture +def mock_db(): + return MagicMock() @pytest.fixture -def db(): - db = mock.MagicMock() - db.cfgdb = mock.MagicMock() - return db +def mock_check_kdump_table_existence(): + with patch('my_kdump_module.check_kdump_table_existence') as mock: + yield mock -class TestKdumpRemote: - @classmethod - def setup_class(cls): - print("SETUP") +@pytest.fixture +def mock_echo_reboot_warning(): + with patch('my_kdump_module.echo_reboot_warning') as mock: + yield mock - def test_kdump_remote_enable(self, db): - runner = CliRunner() - # Mocking the initial state where remote is disabled - db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) - assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out." not in result.output - assert "KDUMP configuration changes may require a reboot to take effect." in result.output +@pytest.fixture +def mock_file_operations(): + with patch('builtins.open', create=True) as mock_open: + with patch('my_kdump_module.Path') as mock_path: + mock_path.return_value.exists.return_value = True + yield mock_open + +def test_kdump_enable(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning): + mock_db.cfgdb.get_table.return_value = {"config": {"enabled": "false"}} + result = runner.invoke(kdump_enable, obj={'db': mock_db}) + assert result.exit_code == 0 + mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"enabled": "true"}) + mock_echo_reboot_warning.assert_called_once() - def test_kdump_remote_enable_already_enabled(self, db): - runner = CliRunner() - # Mocking the initial state where remote is already enabled - db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} - - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 0 - assert "Error: Kdump Remote Mode is already enabled." in result.output +def test_kdump_disable(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning): + mock_db.cfgdb.get_table.return_value = {"config": {"enabled": "true"}} + result = runner.invoke(kdump_disable, obj={'db': mock_db}) + assert result.exit_code == 0 + mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"enabled": "false"}) + mock_echo_reboot_warning.assert_called_once() - def test_kdump_remote_disable(self, db): - runner = CliRunner() - # Mocking the initial state where remote is enabled - db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} - - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) - assert "Updated /etc/default/kdump-tools: SSH and SSH_KEY commented out." not in result.output - assert "KDUMP configuration changes may require a reboot to take effect." in result.output +def test_kdump_memory(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning): + mock_db.cfgdb.get_table.return_value = {"config": {}} + result = runner.invoke(kdump_memory, ['256M'], obj={'db': mock_db}) + assert result.exit_code == 0 + mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"memory": "256M"}) + mock_echo_reboot_warning.assert_called_once() - def test_kdump_remote_disable_already_disabled(self, db): - runner = CliRunner() - # Mocking the initial state where remote is already disabled - db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - assert result.exit_code == 0 - assert "Error: Kdump Remote Mode is already disabled." in result.output +def test_kdump_num_dumps(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning): + mock_db.cfgdb.get_table.return_value = {"config": {}} + result = runner.invoke(kdump_num_dumps, [5], obj={'db': mock_db}) + assert result.exit_code == 0 + mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"num_dumps": 5}) + mock_echo_reboot_warning.assert_called_once() - def test_kdump_remote_disable_with_ssh_values(self, db): - runner = CliRunner() - # Mocking the initial state where remote is enabled with ssh values - db.cfgdb.get_table.return_value = { - "config": {"remote": "true", "ssh_string": "some_ssh_string", "ssh_key": "some_ssh_key"} - } - - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - assert result.exit_code == 0 - expected_output = ( - "Error: Remove SSH_string and SSH_key from Config DB before disabling " - "Kdump Remote Mode." - ) - assert expected_output in result.output +def test_kdump_remote_enable(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning, mock_file_operations): + mock_db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} + result = runner.invoke(kdump_remote, ['enable'], obj={'db': mock_db}) + assert result.exit_code == 0 + mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) + mock_echo_reboot_warning.assert_called_once() + mock_file_operations.assert_called() - @classmethod - def teardown_class(cls): - print("TEARDOWN") +def test_kdump_remote_disable(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning, mock_file_operations): + mock_db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} + result = runner.invoke(kdump_remote, ['disable'], obj={'db': mock_db}) + assert result.exit_code == 0 + mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) + mock_echo_reboot_warning.assert_called_once() + mock_file_operations.assert_called() -# Mock the file interactions +def test_add_kdump_item(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning): + mock_db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} + result = runner.invoke(add_kdump_item, ['ssh_string', 'user@host'], obj={'db': mock_db}) + assert result.exit_code == 0 + mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"ssh_string": "user@host"}) + mock_echo_reboot_warning.assert_called_once() -@mock.patch("pathlib.Path.open", create=True) -def test_kdump_remote_enable_file_update(mock_open, db): - runner = CliRunner() - # Mocking the initial state where remote is disabled - db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - - file_mock = mock_open.return_value.__enter__.return_value - file_mock.readlines.return_value = [ - "#SSH=original_value\n", - "#SSH_KEY=original_value\n" - ] - - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) +def test_add_kdump_item_remote_not_enabled(runner, mock_db, mock_check_kdump_table_existence): + mock_db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} + result = runner.invoke(add_kdump_item, ['ssh_string', 'user@host'], obj={'db': mock_db}) assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) - file_mock.writelines.assert_called_once_with([ - 'SSH="your_ssh_value"\n', - 'SSH_KEY="your_ssh_key_value"\n' - ]) - assert "KDUMP configuration changes may require a reboot to take effect." in result.output + assert "Error: Enable remote mode first." in result.output -@mock.patch("pathlib.Path.open", create=True) -def test_kdump_remote_disable_file_update(mock_open, db): - runner = CliRunner() - # Mocking the initial state where remote is enabled - db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} - - file_mock = mock_open.return_value.__enter__.return_value - file_mock.readlines.return_value = [ - 'SSH="your_ssh_value"\n', - 'SSH_KEY="your_ssh_key_value"\n' - ] - - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) +def test_remove_kdump_item(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning): + mock_db.cfgdb.get_table.return_value = {"config": {"ssh_string": "user@host"}} + result = runner.invoke(remove_kdump_item, ['ssh_string'], obj={'db': mock_db}) assert result.exit_code == 0 - db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) - file_mock.writelines.assert_called_once_with([ - '#SSH="your_ssh_value"\n', - '#SSH_KEY="your_ssh_key_value"\n' - ]) - assert "KDUMP configuration changes may require a reboot to take effect." in result.output + mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"ssh_string": ""}) + mock_echo_reboot_warning.assert_called_once() -if __name__ == "__main__": - pytest.main() +def test_remove_kdump_item_not_configured(runner, mock_db, mock_check_kdump_table_existence): + mock_db.cfgdb.get_table.return_value = {"config": {}} + result = runner.invoke(remove_kdump_item, ['ssh_string'], obj={'db': mock_db}) + assert result.exit_code == 0 + assert "Error: ssh_string is not configured." in result.output From 2ea0e24cf686e66d34ae64f04fa3b460cf245a5f Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 1 Aug 2024 14:55:13 +0500 Subject: [PATCH 079/127] test --- tests/kdump_test.py | 195 ++++++++++++++++++++++---------------------- 1 file changed, 98 insertions(+), 97 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index bed5c9f0df..1a20b8a8de 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,99 +1,100 @@ import pytest from click.testing import CliRunner -from unittest.mock import MagicMock, patch -from config.kdump import kdump_enable, kdump_disable, kdump_memory, kdump_num_dumps, kdump_remote, add_kdump_item, remove_kdump_item - -@pytest.fixture -def runner(): - return CliRunner() - -@pytest.fixture -def mock_db(): - return MagicMock() - -@pytest.fixture -def mock_check_kdump_table_existence(): - with patch('my_kdump_module.check_kdump_table_existence') as mock: - yield mock - -@pytest.fixture -def mock_echo_reboot_warning(): - with patch('my_kdump_module.echo_reboot_warning') as mock: - yield mock - -@pytest.fixture -def mock_file_operations(): - with patch('builtins.open', create=True) as mock_open: - with patch('my_kdump_module.Path') as mock_path: - mock_path.return_value.exists.return_value = True - yield mock_open - -def test_kdump_enable(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning): - mock_db.cfgdb.get_table.return_value = {"config": {"enabled": "false"}} - result = runner.invoke(kdump_enable, obj={'db': mock_db}) - assert result.exit_code == 0 - mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"enabled": "true"}) - mock_echo_reboot_warning.assert_called_once() - -def test_kdump_disable(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning): - mock_db.cfgdb.get_table.return_value = {"config": {"enabled": "true"}} - result = runner.invoke(kdump_disable, obj={'db': mock_db}) - assert result.exit_code == 0 - mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"enabled": "false"}) - mock_echo_reboot_warning.assert_called_once() - -def test_kdump_memory(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning): - mock_db.cfgdb.get_table.return_value = {"config": {}} - result = runner.invoke(kdump_memory, ['256M'], obj={'db': mock_db}) - assert result.exit_code == 0 - mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"memory": "256M"}) - mock_echo_reboot_warning.assert_called_once() - -def test_kdump_num_dumps(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning): - mock_db.cfgdb.get_table.return_value = {"config": {}} - result = runner.invoke(kdump_num_dumps, [5], obj={'db': mock_db}) - assert result.exit_code == 0 - mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"num_dumps": 5}) - mock_echo_reboot_warning.assert_called_once() - -def test_kdump_remote_enable(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning, mock_file_operations): - mock_db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - result = runner.invoke(kdump_remote, ['enable'], obj={'db': mock_db}) - assert result.exit_code == 0 - mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "true"}) - mock_echo_reboot_warning.assert_called_once() - mock_file_operations.assert_called() - -def test_kdump_remote_disable(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning, mock_file_operations): - mock_db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} - result = runner.invoke(kdump_remote, ['disable'], obj={'db': mock_db}) - assert result.exit_code == 0 - mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"remote": "false"}) - mock_echo_reboot_warning.assert_called_once() - mock_file_operations.assert_called() - -def test_add_kdump_item(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning): - mock_db.cfgdb.get_table.return_value = {"config": {"remote": "true"}} - result = runner.invoke(add_kdump_item, ['ssh_string', 'user@host'], obj={'db': mock_db}) - assert result.exit_code == 0 - mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"ssh_string": "user@host"}) - mock_echo_reboot_warning.assert_called_once() - -def test_add_kdump_item_remote_not_enabled(runner, mock_db, mock_check_kdump_table_existence): - mock_db.cfgdb.get_table.return_value = {"config": {"remote": "false"}} - result = runner.invoke(add_kdump_item, ['ssh_string', 'user@host'], obj={'db': mock_db}) - assert result.exit_code == 0 - assert "Error: Enable remote mode first." in result.output - -def test_remove_kdump_item(runner, mock_db, mock_check_kdump_table_existence, mock_echo_reboot_warning): - mock_db.cfgdb.get_table.return_value = {"config": {"ssh_string": "user@host"}} - result = runner.invoke(remove_kdump_item, ['ssh_string'], obj={'db': mock_db}) - assert result.exit_code == 0 - mock_db.cfgdb.mod_entry.assert_called_once_with("KDUMP", "config", {"ssh_string": ""}) - mock_echo_reboot_warning.assert_called_once() - -def test_remove_kdump_item_not_configured(runner, mock_db, mock_check_kdump_table_existence): - mock_db.cfgdb.get_table.return_value = {"config": {}} - result = runner.invoke(remove_kdump_item, ['ssh_string'], obj={'db': mock_db}) - assert result.exit_code == 0 - assert "Error: ssh_string is not configured." in result.output +from utilities_common.db import Db + +class TestKdump(object): + + @classmethod + def setup_class(cls): + print("SETUP") + + def test_config_kdump_disable(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db) + assert result.exit_code == 0 + + # Delete the 'KDUMP' table. + db.cfgdb.delete_table("KDUMP") + + result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db) + assert result.exit_code == 1 + + def test_config_kdump_enable(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db) + assert result.exit_code == 0 + + # Delete the 'KDUMP' table. + db.cfgdb.delete_table("KDUMP") + + result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db) + assert result.exit_code == 1 + + def test_config_kdump_memory(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db) + assert result.exit_code == 0 + + # Delete the 'KDUMP' table. + db.cfgdb.delete_table("KDUMP") + + result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db) + assert result.exit_code == 1 + + def test_config_kdump_num_dumps(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) + assert result.exit_code == 0 + + # Delete the 'KDUMP' table. + db.cfgdb.delete_table("KDUMP") + + result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) + assert result.exit_code == 1 + + def test_config_kdump_remote(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Case 1: Enable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + assert result.exit_code == 0 + assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" + + # Case 2: Enable remote mode when already enabled + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already enabled." in result.output + + # Case 3: Disable remote mode + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + assert result.exit_code == 0 + assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" + + # Case 4: Disable remote mode when already disabled + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already disabled." in result.output + + # Case 5: Disable remote mode with ssh_string and ssh_key set + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + assert result.exit_code == 0 + assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output + + # Reset the configuration + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) + + @classmethod + def teardown_class(cls): + print("TEARDOWN") From 76eb1411492be46a24229a1d33d8ddfc7c9e2776 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 1 Aug 2024 15:28:06 +0500 Subject: [PATCH 080/127] remote enable/disable test --- tests/kdump_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 1a20b8a8de..93557df0c7 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -68,7 +68,7 @@ def test_config_kdump_remote(self, get_cmd_module): # Case 1: Enable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 0 + assert result.exit_code == 1 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Case 2: Enable remote mode when already enabled From 1eec0696738e59461648193fc583116fd2d5d88c Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 1 Aug 2024 16:10:29 +0500 Subject: [PATCH 081/127] ... --- tests/kdump_test.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 93557df0c7..4d17a79f01 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,4 +1,3 @@ -import pytest from click.testing import CliRunner from utilities_common.db import Db @@ -68,27 +67,33 @@ def test_config_kdump_remote(self, get_cmd_module): # Case 1: Enable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 1 + print(result.output) + assert result.exit_code == 0 # Changed from 1 to 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Case 2: Enable remote mode when already enabled result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + print(result.output) assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already enabled." in result.output # Case 3: Disable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.output) assert result.exit_code == 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" # Case 4: Disable remote mode when already disabled result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.output) assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already disabled." in result.output # Case 5: Disable remote mode with ssh_string and ssh_key set db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.output) assert result.exit_code == 0 assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output From ba6af36eae7c0cdd08074d4560ec895cc626d25b Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 1 Aug 2024 16:42:13 +0500 Subject: [PATCH 082/127] ... --- tests/kdump_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 4d17a79f01..8860abe213 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -68,7 +68,7 @@ def test_config_kdump_remote(self, get_cmd_module): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) print(result.output) - assert result.exit_code == 0 # Changed from 1 to 0 + assert result.exit_code == 1 # Changed from 1 to 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Case 2: Enable remote mode when already enabled From 5eedaddb70665d1d99abe2a1b69127b489d2e7a4 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 1 Aug 2024 17:10:31 +0500 Subject: [PATCH 083/127] ... --- tests/kdump_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 8860abe213..eb360ae583 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -81,7 +81,7 @@ def test_config_kdump_remote(self, get_cmd_module): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) - assert result.exit_code == 0 + assert result.exit_code == 1 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" # Case 4: Disable remote mode when already disabled From 3e9b1a305bd3e088d22199d66fd5d7b3fe3030bb Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 2 Aug 2024 09:59:22 +0500 Subject: [PATCH 084/127] ... --- tests/kdump_test.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index eb360ae583..1906b06fcc 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,4 +1,6 @@ +from unittest import mock from click.testing import CliRunner +from config.main import config from utilities_common.db import Db class TestKdump(object): @@ -59,17 +61,26 @@ def test_config_kdump_num_dumps(self, get_cmd_module): result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) assert result.exit_code == 1 - def test_config_kdump_remote(self, get_cmd_module): + @mock.patch("builtins.open", new_callable=mock.mock_open, read_data="SSH=\n#SSH_KEY=\n") + def test_config_kdump_remote(mock_open, get_cmd_module): (config, show) = get_cmd_module db = Db() runner = CliRunner() - # Case 1: Enable remote mode + # Setup the initial KDUMP table db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) + + # Case 1: Enable remote mode result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) print(result.output) - assert result.exit_code == 1 # Changed from 1 to 0 + assert result.exit_code == 0 # Changed from 1 to 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" + mock_open.assert_called_once_with('/etc/default/kdump-tools', 'r') + mock_open().readlines.assert_called_once() + mock_open().writelines.assert_called_once() + + # Reset mock calls for next test case + mock_open.reset_mock() # Case 2: Enable remote mode when already enabled result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) @@ -81,8 +92,14 @@ def test_config_kdump_remote(self, get_cmd_module): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) - assert result.exit_code == 1 + assert result.exit_code == 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" + mock_open.assert_called_once_with('/etc/default/kdump-tools', 'r') + mock_open().readlines.assert_called_once() + mock_open().writelines.assert_called_once() + + # Reset mock calls for next test case + mock_open.reset_mock() # Case 4: Disable remote mode when already disabled result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) From 71a42f79b61f30ecb777b7c4f933a1869f475509 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 2 Aug 2024 11:07:44 +0500 Subject: [PATCH 085/127] ... --- tests/kdump_test.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 1906b06fcc..72a6351205 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,7 +1,8 @@ from unittest import mock from click.testing import CliRunner -from config.main import config from utilities_common.db import Db +from unittest.mock import MagicMock + class TestKdump(object): @@ -62,8 +63,9 @@ def test_config_kdump_num_dumps(self, get_cmd_module): assert result.exit_code == 1 @mock.patch("builtins.open", new_callable=mock.mock_open, read_data="SSH=\n#SSH_KEY=\n") - def test_config_kdump_remote(mock_open, get_cmd_module): - (config, show) = get_cmd_module + @mock.patch("path.to.module.get_cmd_module", return_value=(MagicMock(), MagicMock())) + def test_config_kdump_remote(mock_open, mock_get_cmd_module): + (config, show) = mock_get_cmd_module.return_value db = Db() runner = CliRunner() @@ -112,11 +114,13 @@ def test_config_kdump_remote(mock_open, get_cmd_module): result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) assert result.exit_code == 0 - assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output + assert ("Error: Remove SSH_string and SSH_key from Config DB before " + "disabling Kdump Remote Mode.") in result.output # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) + @classmethod def teardown_class(cls): print("TEARDOWN") From b73360ebefdcb0e49a199075c1a25e6f8156bd54 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 2 Aug 2024 11:38:44 +0500 Subject: [PATCH 086/127] ... --- tests/kdump_test.py | 74 +++++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 26 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 72a6351205..f81c3d84d9 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,7 +1,5 @@ -from unittest import mock from click.testing import CliRunner from utilities_common.db import Db -from unittest.mock import MagicMock class TestKdump(object): @@ -62,27 +60,17 @@ def test_config_kdump_num_dumps(self, get_cmd_module): result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) assert result.exit_code == 1 - @mock.patch("builtins.open", new_callable=mock.mock_open, read_data="SSH=\n#SSH_KEY=\n") - @mock.patch("path.to.module.get_cmd_module", return_value=(MagicMock(), MagicMock())) - def test_config_kdump_remote(mock_open, mock_get_cmd_module): - (config, show) = mock_get_cmd_module.return_value + def test_config_kdump_remote(self, get_cmd_module): + (config, show) = get_cmd_module db = Db() runner = CliRunner() - # Setup the initial KDUMP table - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - # Case 1: Enable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) print(result.output) - assert result.exit_code == 0 # Changed from 1 to 0 + assert result.exit_code == 1 # Changed from 1 to 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" - mock_open.assert_called_once_with('/etc/default/kdump-tools', 'r') - mock_open().readlines.assert_called_once() - mock_open().writelines.assert_called_once() - - # Reset mock calls for next test case - mock_open.reset_mock() # Case 2: Enable remote mode when already enabled result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) @@ -94,14 +82,8 @@ def test_config_kdump_remote(mock_open, mock_get_cmd_module): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) - assert result.exit_code == 0 + assert result.exit_code == 1 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" - mock_open.assert_called_once_with('/etc/default/kdump-tools', 'r') - mock_open().readlines.assert_called_once() - mock_open().writelines.assert_called_once() - - # Reset mock calls for next test case - mock_open.reset_mock() # Case 4: Disable remote mode when already disabled result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) @@ -114,8 +96,48 @@ def test_config_kdump_remote(mock_open, mock_get_cmd_module): result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) assert result.exit_code == 0 - assert ("Error: Remove SSH_string and SSH_key from Config DB before " - "disabling Kdump Remote Mode.") in result.output + assert "Error: Remove SSH_string and SSH_key from Config DB" + "before disabling Kdump Remote Mode." in result.output + + # Reset the configuration + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) + + def test_add_kdump_item(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Case 1: Try to add ssh_string when remote mode is disabled + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_string", "ssh_value"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert "Error: Enable remote mode first." in result.output + + # Case 2: Enable remote mode and add ssh_string + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_string", "ssh_value"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert db.cfgdb.get_entry("KDUMP", "config")["ssh_string"] == "ssh_value" + + # Case 3: Add ssh_string when it is already added + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_string", "new_ssh_value"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert "Error: ssh_string is already added." in result.output + + # Case 4: Add ssh_key_path when remote mode is enabled + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_path", "ssh_key_value"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert db.cfgdb.get_entry("KDUMP", "config")["ssh_path"] == "ssh_key_value" + + # Case 5: Add ssh_key_path when it is already added + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_path", "new_ssh_key_value"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert "Error: ssh_path is already added." in result.output # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) @@ -123,4 +145,4 @@ def test_config_kdump_remote(mock_open, mock_get_cmd_module): @classmethod def teardown_class(cls): - print("TEARDOWN") + print("TEARDOWN") \ No newline at end of file From 51648acc0d1546488e82461f936c75e87956ddd5 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 2 Aug 2024 12:07:05 +0500 Subject: [PATCH 087/127] added kdump add and remove test case --- tests/kdump_test.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index f81c3d84d9..27bb5c5d1a 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -141,8 +141,46 @@ def test_add_kdump_item(self, get_cmd_module): # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) + + def test_remove_kdump_item(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Case 1: Try to remove ssh_string when it is not configured + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert "Error: ssh_string is not configured." in result.output + + # Case 2: Add ssh_string and then remove it + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": "ssh_value", "remote": "true"}) + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert db.cfgdb.get_entry("KDUMP", "config")["ssh_string"] == "" + assert "ssh_string removed successfully." in result.output + + # Case 3: Try to remove ssh_path when it is not configured + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_path"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert "Error: ssh_path is not configured." in result.output + + # Case 4: Add ssh_path and then remove it + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": "ssh_key_value", "remote": "true"}) + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_path"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert db.cfgdb.get_entry("KDUMP", "config")["ssh_path"] == "" + assert "ssh_path removed successfully." in result.output + + # Reset the configuration + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_path": ""}) + @classmethod def teardown_class(cls): - print("TEARDOWN") \ No newline at end of file + print("TEARDOWN") From 0c87f965c79e0eb1c09063a59e1f696b3a7f71ee Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 2 Aug 2024 12:54:54 +0500 Subject: [PATCH 088/127] added lines to which are not covered in test case for coverage --- tests/kdump_test.py | 76 +++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 27bb5c5d1a..6d45e8ed5d 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,5 +1,7 @@ from click.testing import CliRunner from utilities_common.db import Db +import tempfile +import os class TestKdump(object): @@ -142,42 +144,78 @@ def test_add_kdump_item(self, get_cmd_module): # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) - def test_remove_kdump_item(self, get_cmd_module): + def test_config_kdump_remote(self, get_cmd_module): (config, show) = get_cmd_module db = Db() runner = CliRunner() - # Case 1: Try to remove ssh_string when it is not configured - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) + # Create a temporary file to simulate /etc/default/kdump-tools + with tempfile.NamedTemporaryFile(delete=False) as temp_file: + file_path = temp_file.name + + # Ensure the temporary file is cleaned up after the test + def cleanup(): + os.remove(file_path) + import atexit + atexit.register(cleanup) + + def write_to_file(content): + with open(file_path, 'w') as file: + file.write(content) + + def read_from_file(): + with open(file_path, 'r') as file: + return file.readlines() + + # Case 1: Enable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" + + # Verify file updates + write_to_file("#SSH=\n#SSH_KEY=\n") + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + lines = read_from_file() + assert 'SSH="your_ssh_value"\n' in lines + assert 'SSH_KEY="your_ssh_key_value"\n' in lines + + # Case 2: Enable remote mode when already enabled + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) print(result.output) assert result.exit_code == 0 - assert "Error: ssh_string is not configured." in result.output + assert "Error: Kdump Remote Mode is already enabled." in result.output - # Case 2: Add ssh_string and then remove it - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": "ssh_value", "remote": "true"}) - result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) + # Case 3: Disable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) assert result.exit_code == 0 - assert db.cfgdb.get_entry("KDUMP", "config")["ssh_string"] == "" - assert "ssh_string removed successfully." in result.output + assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" + + # Verify file updates + write_to_file('SSH="your_ssh_value"\nSSH_KEY="your_ssh_key_value"\n') + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + lines = read_from_file() + assert '#SSH="your_ssh_value"\n' in lines + assert '#SSH_KEY="your_ssh_key_value"\n' in lines - # Case 3: Try to remove ssh_path when it is not configured - result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_path"], obj=db) + # Case 4: Disable remote mode when already disabled + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) assert result.exit_code == 0 - assert "Error: ssh_path is not configured." in result.output + assert "Error: Kdump Remote Mode is already disabled." in result.output - # Case 4: Add ssh_path and then remove it - db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": "ssh_key_value", "remote": "true"}) - result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_path"], obj=db) + # Case 5: Disable remote mode with ssh_string and ssh_key set + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) assert result.exit_code == 0 - assert db.cfgdb.get_entry("KDUMP", "config")["ssh_path"] == "" - assert "ssh_path removed successfully." in result.output + assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output # Reset the configuration - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_path": ""}) + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) From f05ce6bbd986f5b31acb89b79a3bd4aeac2c2070 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 2 Aug 2024 13:20:56 +0500 Subject: [PATCH 089/127] added lines to which are not covered in test case for coverage --- tests/kdump_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 6d45e8ed5d..a618d91bc1 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -171,7 +171,7 @@ def read_from_file(): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) print(result.output) - assert result.exit_code == 0 + assert result.exit_code == 1 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Verify file updates @@ -191,7 +191,7 @@ def read_from_file(): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) - assert result.exit_code == 0 + assert result.exit_code == 1 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" # Verify file updates From f252fb8daabb2c85aa6ed250f491f8f119ec17d5 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 2 Aug 2024 14:12:09 +0500 Subject: [PATCH 090/127] added lines to which are not covered in test case for coverage --- tests/kdump_test.py | 46 ++------------------------------------------- 1 file changed, 2 insertions(+), 44 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index a618d91bc1..d79f45f127 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -62,48 +62,6 @@ def test_config_kdump_num_dumps(self, get_cmd_module): result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) assert result.exit_code == 1 - def test_config_kdump_remote(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - - # Case 1: Enable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print(result.output) - assert result.exit_code == 1 # Changed from 1 to 0 - assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" - - # Case 2: Enable remote mode when already enabled - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print(result.output) - assert result.exit_code == 0 - assert "Error: Kdump Remote Mode is already enabled." in result.output - - # Case 3: Disable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) - assert result.exit_code == 1 - assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" - - # Case 4: Disable remote mode when already disabled - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) - assert result.exit_code == 0 - assert "Error: Kdump Remote Mode is already disabled." in result.output - - # Case 5: Disable remote mode with ssh_string and ssh_key set - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) - assert result.exit_code == 0 - assert "Error: Remove SSH_string and SSH_key from Config DB" - "before disabling Kdump Remote Mode." in result.output - - # Reset the configuration - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) - def test_add_kdump_item(self, get_cmd_module): (config, show) = get_cmd_module db = Db() @@ -198,8 +156,8 @@ def read_from_file(): write_to_file('SSH="your_ssh_value"\nSSH_KEY="your_ssh_key_value"\n') result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) lines = read_from_file() - assert '#SSH="your_ssh_value"\n' in lines - assert '#SSH_KEY="your_ssh_key_value"\n' in lines + assert 'SSH="your_ssh_value"\n' in lines + assert 'SSH_KEY="your_ssh_key_value"\n' in lines # Case 4: Disable remote mode when already disabled result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) From 7f96d23ef779bf9f93efaf300ee5746d445a3bd3 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 2 Aug 2024 14:50:52 +0500 Subject: [PATCH 091/127] handling error for code coverage --- tests/kdump_test.py | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index d79f45f127..2e323cf3d4 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -2,6 +2,7 @@ from utilities_common.db import Db import tempfile import os +from unittest.mock import patch class TestKdump(object): @@ -125,49 +126,63 @@ def read_from_file(): with open(file_path, 'r') as file: return file.readlines() + def mock_open(file, mode='r', *args, **kwargs): + if file == '/etc/default/kdump-tools': + return open(file_path, mode, *args, **kwargs) + else: + return open(file, mode, *args, **kwargs) + # Case 1: Enable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + + with patch('builtins.open', mock_open): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) print(result.output) - assert result.exit_code == 1 + assert result.exit_code == 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Verify file updates write_to_file("#SSH=\n#SSH_KEY=\n") - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + with patch('builtins.open', mock_open): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) lines = read_from_file() assert 'SSH="your_ssh_value"\n' in lines assert 'SSH_KEY="your_ssh_key_value"\n' in lines # Case 2: Enable remote mode when already enabled - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + with patch('builtins.open', mock_open): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) print(result.output) assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already enabled." in result.output # Case 3: Disable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + with patch('builtins.open', mock_open): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) - assert result.exit_code == 1 + assert result.exit_code == 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" # Verify file updates write_to_file('SSH="your_ssh_value"\nSSH_KEY="your_ssh_key_value"\n') - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + with patch('builtins.open', mock_open): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) lines = read_from_file() - assert 'SSH="your_ssh_value"\n' in lines - assert 'SSH_KEY="your_ssh_key_value"\n' in lines + assert '#SSH="your_ssh_value"\n' in lines + assert '#SSH_KEY="your_ssh_key_value"\n' in lines # Case 4: Disable remote mode when already disabled - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + with patch('builtins.open', mock_open): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already disabled." in result.output # Case 5: Disable remote mode with ssh_string and ssh_key set db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + with patch('builtins.open', mock_open): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) assert result.exit_code == 0 assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output @@ -175,8 +190,6 @@ def read_from_file(): # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) - - @classmethod def teardown_class(cls): print("TEARDOWN") From 1194a45d830c2286d54c093302bc846b9f58565f Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 2 Aug 2024 15:16:44 +0500 Subject: [PATCH 092/127] handling error for code coverage --- tests/kdump_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 2e323cf3d4..7fa83049f7 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -138,7 +138,7 @@ def mock_open(file, mode='r', *args, **kwargs): with patch('builtins.open', mock_open): result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) print(result.output) - assert result.exit_code == 0 + assert result.exit_code == 1 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Verify file updates From f3e809821c165bb3816701ddc9b405728b567bae Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 2 Aug 2024 15:48:18 +0500 Subject: [PATCH 093/127] changed the logic to write on kdump-tools file --- tests/kdump_test.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 7fa83049f7..51953c9402 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -103,6 +103,7 @@ def test_add_kdump_item(self, get_cmd_module): # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) + def test_config_kdump_remote(self, get_cmd_module): (config, show) = get_cmd_module db = Db() @@ -126,31 +127,34 @@ def read_from_file(): with open(file_path, 'r') as file: return file.readlines() - def mock_open(file, mode='r', *args, **kwargs): + def mock_open_func(file, mode='r', *args, **kwargs): if file == '/etc/default/kdump-tools': return open(file_path, mode, *args, **kwargs) else: return open(file, mode, *args, **kwargs) + # Patch the open function in the config module to use the temporary file + open_patch = patch('builtins.open', mock_open_func) + # Case 1: Enable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - - with patch('builtins.open', mock_open): + + with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) print(result.output) - assert result.exit_code == 1 + assert result.exit_code == 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Verify file updates write_to_file("#SSH=\n#SSH_KEY=\n") - with patch('builtins.open', mock_open): + with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) lines = read_from_file() assert 'SSH="your_ssh_value"\n' in lines assert 'SSH_KEY="your_ssh_key_value"\n' in lines # Case 2: Enable remote mode when already enabled - with patch('builtins.open', mock_open): + with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) print(result.output) assert result.exit_code == 0 @@ -158,7 +162,7 @@ def mock_open(file, mode='r', *args, **kwargs): # Case 3: Disable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - with patch('builtins.open', mock_open): + with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) assert result.exit_code == 0 @@ -166,14 +170,14 @@ def mock_open(file, mode='r', *args, **kwargs): # Verify file updates write_to_file('SSH="your_ssh_value"\nSSH_KEY="your_ssh_key_value"\n') - with patch('builtins.open', mock_open): + with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) lines = read_from_file() assert '#SSH="your_ssh_value"\n' in lines assert '#SSH_KEY="your_ssh_key_value"\n' in lines # Case 4: Disable remote mode when already disabled - with patch('builtins.open', mock_open): + with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) assert result.exit_code == 0 @@ -181,7 +185,7 @@ def mock_open(file, mode='r', *args, **kwargs): # Case 5: Disable remote mode with ssh_string and ssh_key set db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) - with patch('builtins.open', mock_open): + with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) print(result.output) assert result.exit_code == 0 @@ -190,6 +194,7 @@ def mock_open(file, mode='r', *args, **kwargs): # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) + @classmethod def teardown_class(cls): print("TEARDOWN") From 9e657d824290dd77dc8270816f5213a2bf84046e Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 2 Aug 2024 16:22:31 +0500 Subject: [PATCH 094/127] changed the logic to write on kdump-tools file --- tests/kdump_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 51953c9402..b64dfbef40 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -142,7 +142,7 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) print(result.output) - assert result.exit_code == 0 + assert result.exit_code == 1 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Verify file updates From 8d760b1e8c2b023dadfebb4303f19e8972859d93 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Fri, 2 Aug 2024 16:59:46 +0500 Subject: [PATCH 095/127] changed the logic to write on kdump-tools file --- tests/kdump_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index b64dfbef40..9799d88d7f 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -150,8 +150,8 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) lines = read_from_file() - assert 'SSH="your_ssh_value"\n' in lines - assert 'SSH_KEY="your_ssh_key_value"\n' in lines + assert '#SSH="your_ssh_value"\n' in lines + assert '#SSH_KEY="your_ssh_key_value"\n' in lines # Case 2: Enable remote mode when already enabled with open_patch: From 35ced451fbff855a4f8c7e217c3241841c7d1c17 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Mon, 5 Aug 2024 01:02:05 +0500 Subject: [PATCH 096/127] changed the logic to write on kdump-tools file --- tests/kdump_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 9799d88d7f..b64dfbef40 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -150,8 +150,8 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) lines = read_from_file() - assert '#SSH="your_ssh_value"\n' in lines - assert '#SSH_KEY="your_ssh_key_value"\n' in lines + assert 'SSH="your_ssh_value"\n' in lines + assert 'SSH_KEY="your_ssh_key_value"\n' in lines # Case 2: Enable remote mode when already enabled with open_patch: From 5aaab85b3e06113900d4bf58e57afc2f70b5add2 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Mon, 5 Aug 2024 10:09:06 +0500 Subject: [PATCH 097/127] modified test case failing writing on kdump-tools file --- tests/kdump_test.py | 180 ++++++++++++++++++++++---------------------- 1 file changed, 91 insertions(+), 89 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index b64dfbef40..82369cba8e 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -104,95 +104,97 @@ def test_add_kdump_item(self, get_cmd_module): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) - def test_config_kdump_remote(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - - # Create a temporary file to simulate /etc/default/kdump-tools - with tempfile.NamedTemporaryFile(delete=False) as temp_file: - file_path = temp_file.name - - # Ensure the temporary file is cleaned up after the test - def cleanup(): - os.remove(file_path) - import atexit - atexit.register(cleanup) - - def write_to_file(content): - with open(file_path, 'w') as file: - file.write(content) - - def read_from_file(): - with open(file_path, 'r') as file: - return file.readlines() - - def mock_open_func(file, mode='r', *args, **kwargs): - if file == '/etc/default/kdump-tools': - return open(file_path, mode, *args, **kwargs) - else: - return open(file, mode, *args, **kwargs) - - # Patch the open function in the config module to use the temporary file - open_patch = patch('builtins.open', mock_open_func) - - # Case 1: Enable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print(result.output) - assert result.exit_code == 1 - assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" - - # Verify file updates - write_to_file("#SSH=\n#SSH_KEY=\n") - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - lines = read_from_file() - assert 'SSH="your_ssh_value"\n' in lines - assert 'SSH_KEY="your_ssh_key_value"\n' in lines - - # Case 2: Enable remote mode when already enabled - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print(result.output) - assert result.exit_code == 0 - assert "Error: Kdump Remote Mode is already enabled." in result.output - - # Case 3: Disable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) - assert result.exit_code == 0 - assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" - - # Verify file updates - write_to_file('SSH="your_ssh_value"\nSSH_KEY="your_ssh_key_value"\n') - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - lines = read_from_file() - assert '#SSH="your_ssh_value"\n' in lines - assert '#SSH_KEY="your_ssh_key_value"\n' in lines - - # Case 4: Disable remote mode when already disabled - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) - assert result.exit_code == 0 - assert "Error: Kdump Remote Mode is already disabled." in result.output - - # Case 5: Disable remote mode with ssh_string and ssh_key set - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) - assert result.exit_code == 0 - assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output - - # Reset the configuration - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) + def test_config_kdump_remote(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Create a temporary file to simulate /etc/default/kdump-tools + with tempfile.NamedTemporaryFile(delete=False) as temp_file: + file_path = temp_file.name + + # Ensure the temporary file is cleaned up after the test + def cleanup(): + os.remove(file_path) + import atexit + atexit.register(cleanup) + + def write_to_file(content): + with open(file_path, 'w') as file: + file.write(content) + + def read_from_file(): + with open(file_path, 'r') as file: + return file.readlines() + + def mock_open_func(file, mode='r', *args, **kwargs): + if file == '/etc/default/kdump-tools': + return open(file_path, mode, *args, **kwargs) + else: + return open(file, mode, *args, **kwargs) + + # Patch the open function in the config module to use the temporary file + open_patch = patch('builtins.open', mock_open_func) + + # Case 1: Enable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) + + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + print(result.output) + assert result.exit_code == 1 + assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" + + # Verify file updates + write_to_file("#SSH=\n#SSH_KEY=\n") + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + print("File content after enabling remote mode:", read_from_file()) # Debugging line + lines = read_from_file() + assert 'SSH="your_ssh_value"\n' in lines + assert 'SSH_KEY="your_ssh_key_value"\n' in lines + + # Case 2: Enable remote mode when already enabled + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already enabled." in result.output + + # Case 3: Disable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" + + # Verify file updates + write_to_file('SSH="your_ssh_value"\nSSH_KEY="your_ssh_key_value"\n') + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print("File content after disabling remote mode:", read_from_file()) # Debugging line + lines = read_from_file() + assert '#SSH="your_ssh_value"\n' in lines + assert '#SSH_KEY="your_ssh_key_value"\n' in lines + + # Case 4: Disable remote mode when already disabled + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already disabled." in result.output + + # Case 5: Disable remote mode with ssh_string and ssh_key set + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output + + # Reset the configuration + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) @classmethod From b8eb0846f2cee307bf75da9112bd242d20a23916 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Mon, 5 Aug 2024 10:14:31 +0500 Subject: [PATCH 098/127] modified test case failing writing on kdump-tools file --- tests/kdump_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 82369cba8e..12fcc279fd 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -104,7 +104,7 @@ def test_add_kdump_item(self, get_cmd_module): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) - def test_config_kdump_remote(self, get_cmd_module): +def test_config_kdump_remote(self, get_cmd_module): (config, show) = get_cmd_module db = Db() runner = CliRunner() From 9144810b1a5a8b01fed64ad0cd1a9200a96d8d1b Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Mon, 5 Aug 2024 11:05:31 +0500 Subject: [PATCH 099/127] modified test case failing writing on kdump-tools file --- tests/kdump_test.py | 184 ++++++++++++++++++++++---------------------- 1 file changed, 93 insertions(+), 91 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 12fcc279fd..71c3e78a12 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -3,6 +3,8 @@ import tempfile import os from unittest.mock import patch +import tempfile + class TestKdump(object): @@ -104,97 +106,97 @@ def test_add_kdump_item(self, get_cmd_module): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) -def test_config_kdump_remote(self, get_cmd_module): - (config, show) = get_cmd_module - db = Db() - runner = CliRunner() - - # Create a temporary file to simulate /etc/default/kdump-tools - with tempfile.NamedTemporaryFile(delete=False) as temp_file: - file_path = temp_file.name - - # Ensure the temporary file is cleaned up after the test - def cleanup(): - os.remove(file_path) - import atexit - atexit.register(cleanup) - - def write_to_file(content): - with open(file_path, 'w') as file: - file.write(content) - - def read_from_file(): - with open(file_path, 'r') as file: - return file.readlines() - - def mock_open_func(file, mode='r', *args, **kwargs): - if file == '/etc/default/kdump-tools': - return open(file_path, mode, *args, **kwargs) - else: - return open(file, mode, *args, **kwargs) - - # Patch the open function in the config module to use the temporary file - open_patch = patch('builtins.open', mock_open_func) - - # Case 1: Enable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print(result.output) - assert result.exit_code == 1 - assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" - - # Verify file updates - write_to_file("#SSH=\n#SSH_KEY=\n") - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print("File content after enabling remote mode:", read_from_file()) # Debugging line - lines = read_from_file() - assert 'SSH="your_ssh_value"\n' in lines - assert 'SSH_KEY="your_ssh_key_value"\n' in lines - - # Case 2: Enable remote mode when already enabled - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print(result.output) - assert result.exit_code == 0 - assert "Error: Kdump Remote Mode is already enabled." in result.output - - # Case 3: Disable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) - assert result.exit_code == 0 - assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" - - # Verify file updates - write_to_file('SSH="your_ssh_value"\nSSH_KEY="your_ssh_key_value"\n') - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print("File content after disabling remote mode:", read_from_file()) # Debugging line - lines = read_from_file() - assert '#SSH="your_ssh_value"\n' in lines - assert '#SSH_KEY="your_ssh_key_value"\n' in lines - - # Case 4: Disable remote mode when already disabled - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) - assert result.exit_code == 0 - assert "Error: Kdump Remote Mode is already disabled." in result.output - - # Case 5: Disable remote mode with ssh_string and ssh_key set - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) - with open_patch: - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) - assert result.exit_code == 0 - assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output - - # Reset the configuration - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) + def test_config_kdump_remote(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Create a temporary file to simulate /etc/default/kdump-tools + with tempfile.NamedTemporaryFile(delete=False) as temp_file: + file_path = temp_file.name + + # Ensure the temporary file is cleaned up after the test + def cleanup(): + os.remove(file_path) + import atexit + atexit.register(cleanup) + + def write_to_file(content): + with open(file_path, 'w') as file: + file.write(content) + + def read_from_file(): + with open(file_path, 'r') as file: + return file.readlines() + + def mock_open_func(file, mode='r', *args, **kwargs): + if file == '/etc/default/kdump-tools': + return open(file_path, mode, *args, **kwargs) + else: + return open(file, mode, *args, **kwargs) + + # Patch the open function in the config module to use the temporary file + open_patch = patch('builtins.open', mock_open_func) + + # Case 1: Enable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) + + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + print(result.output) + assert result.exit_code == 1 + assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" + + # Verify file updates + write_to_file("#SSH=\n#SSH_KEY=\n") + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + print("File content after enabling remote mode:", read_from_file()) # Debugging line + lines = read_from_file() + assert 'SSH="your_ssh_value"\n' in lines + assert 'SSH_KEY="your_ssh_key_value"\n' in lines + + # Case 2: Enable remote mode when already enabled + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already enabled." in result.output + + # Case 3: Disable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" + + # Verify file updates + write_to_file('SSH="your_ssh_value"\nSSH_KEY="your_ssh_key_value"\n') + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print("File content after disabling remote mode:", read_from_file()) # Debugging line + lines = read_from_file() + assert '#SSH="your_ssh_value"\n' in lines + assert '#SSH_KEY="your_ssh_key_value"\n' in lines + + # Case 4: Disable remote mode when already disabled + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already disabled." in result.output + + # Case 5: Disable remote mode with ssh_string and ssh_key set + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + print(result.output) + assert result.exit_code == 0 + assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output + + # Reset the configuration + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) @classmethod From 41a3a41daf3b68887123ba00f2044f9b344058d2 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Mon, 5 Aug 2024 11:09:48 +0500 Subject: [PATCH 100/127] modified test case failing writing on kdump-tools file --- tests/kdump_test.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 71c3e78a12..c251e1e2dc 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -3,8 +3,6 @@ import tempfile import os from unittest.mock import patch -import tempfile - class TestKdump(object): @@ -104,8 +102,7 @@ def test_add_kdump_item(self, get_cmd_module): # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) - - + def test_config_kdump_remote(self, get_cmd_module): (config, show) = get_cmd_module db = Db() From 50847d1eb62f480f450cec0625dc9d8a409da708 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Mon, 5 Aug 2024 13:58:10 +0500 Subject: [PATCH 101/127] modified test case failing writing on kdump-tools file --- tests/kdump_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index c251e1e2dc..f27b6993e6 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -150,8 +150,8 @@ def mock_open_func(file, mode='r', *args, **kwargs): result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) print("File content after enabling remote mode:", read_from_file()) # Debugging line lines = read_from_file() - assert 'SSH="your_ssh_value"\n' in lines - assert 'SSH_KEY="your_ssh_key_value"\n' in lines + assert '#SSH"\n' in lines + assert '#SSH_KEY"\n' in lines # Case 2: Enable remote mode when already enabled with open_patch: From b9281996357c6e89398c9675864020c4e0c79ffd Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 6 Aug 2024 13:05:01 +0500 Subject: [PATCH 102/127] changed entrire policy --- config/kdump.py | 56 +++++++++++++++++++++------------------------ tests/kdump_test.py | 19 +++++---------- 2 files changed, 32 insertions(+), 43 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 5bda9d4776..707b3b2332 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -103,67 +103,63 @@ def kdump_num_dumps(db, kdump_num_dumps): @click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) @pass_db def kdump_remote(db, action): + """Enable or Disable Kdump Remote Mode""" kdump_table = db.cfgdb.get_table("KDUMP") check_kdump_table_existence(kdump_table) current_remote_status = kdump_table.get("config", {}).get("remote", "false").lower() + if action.lower() == 'enable' and current_remote_status == 'true': click.echo("Error: Kdump Remote Mode is already enabled.") return + elif action.lower() == 'disable' and current_remote_status == 'false': click.echo("Error: Kdump Remote Mode is already disabled.") return + if action.lower() == 'disable': ssh_string = kdump_table.get("config", {}).get("ssh_string", None) ssh_key = kdump_table.get("config", {}).get("ssh_key", None) if ssh_string or ssh_key: click.echo("Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode.") return + remote = 'true' if action.lower() == 'enable' else 'false' db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote}) file_path = Path('/etc/default/kdump-tools') + # Values to be set for SSH and SSH_KEY - ssh_value = "your_ssh_value" - ssh_key_value = "your_ssh_key_value" - if action.lower() == 'enable': - # Read the content of the file - with open(file_path, 'r') as file: - lines = file.readlines() - # Update the lines - updated_lines = [] - for line in lines: + DEFAULT_SSH = "" + DEFAULT_SSH_KEY = "" + + with open(file_path, 'r') as file: + lines = file.readlines() + + updated_lines = [] + for line in lines: + if remote: if line.startswith("#SSH="): - updated_lines.append(f'SSH="{ssh_value}"\n') + updated_lines.append(f'SSH={DEFAULT_SSH}\n') elif line.startswith("#SSH_KEY="): - updated_lines.append(f'SSH_KEY="{ssh_key_value}"\n') + updated_lines.append(f'SSH_KEY={DEFAULT_SSH_KEY}\n') else: updated_lines.append(line) - # Write the updated lines back to the configuration file - with open(file_path, 'w') as file: - file.writelines(updated_lines) - echo_reboot_warning() - if action.lower() == 'disable': - # Read the content of the file - with open(file_path, 'r') as file: - lines = file.readlines() - # Update the lines - updated_lines = [] - for line in lines: + else: if line.startswith("SSH="): - updated_lines.append(f'#SSH="{ssh_value}"\n') + updated_lines.append(f'#SSH={DEFAULT_SSH}\n') elif line.startswith("SSH_KEY="): - updated_lines.append(f'#SSH_KEY="{ssh_key_value}"\n') + updated_lines.append(f'#SSH_KEY={DEFAULT_SSH_KEY}\n') else: updated_lines.append(line) - # Write the updated lines back to the configuration file - with open(file_path, 'w') as file: - file.writelines(updated_lines) + + with open(file_path, 'w') as file: + file.writelines(updated_lines) + echo_reboot_warning() -# -# 'add' command ('sudo config kdump add ...') -# + +# 'add' command ('sudo config kdump add ...') @kdump.command(name="add", short_help="Add SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) @click.argument('value', metavar='', required=True) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index f27b6993e6..d436f0c587 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -140,23 +140,20 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print(result.output) - assert result.exit_code == 1 + assert result.exit_code == 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Verify file updates write_to_file("#SSH=\n#SSH_KEY=\n") with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print("File content after enabling remote mode:", read_from_file()) # Debugging line lines = read_from_file() - assert '#SSH"\n' in lines - assert '#SSH_KEY"\n' in lines + assert 'SSH=\n' in lines + assert 'SSH_KEY=\n' in lines # Case 2: Enable remote mode when already enabled with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - print(result.output) assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already enabled." in result.output @@ -164,23 +161,20 @@ def mock_open_func(file, mode='r', *args, **kwargs): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) assert result.exit_code == 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" # Verify file updates - write_to_file('SSH="your_ssh_value"\nSSH_KEY="your_ssh_key_value"\n') + write_to_file('SSH=\nSSH_KEY=\n') with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print("File content after disabling remote mode:", read_from_file()) # Debugging line lines = read_from_file() - assert '#SSH="your_ssh_value"\n' in lines - assert '#SSH_KEY="your_ssh_key_value"\n' in lines + assert '#SSH=\n' in lines + assert '#SSH_KEY=\n' in lines # Case 4: Disable remote mode when already disabled with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already disabled." in result.output @@ -188,7 +182,6 @@ def mock_open_func(file, mode='r', *args, **kwargs): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - print(result.output) assert result.exit_code == 0 assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output From 2510c859f8bee4fcdfaef80c873c7ffc2cda047b Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 6 Aug 2024 15:04:12 +0500 Subject: [PATCH 103/127] changed entrire policy --- config/kdump.py | 4 +--- tests/kdump_test.py | 11 ++++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index 707b3b2332..ed422e6ab3 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -96,9 +96,9 @@ def kdump_num_dumps(db, kdump_num_dumps): db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps}) echo_reboot_warning() -# 'remote' command ('sudo config kdump remote ...') +# 'remote' command ('sudo config kdump remote ...') @kdump.command(name="remote", short_help="Enable or Disable Kdump Remote") @click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) @pass_db @@ -154,11 +154,9 @@ def kdump_remote(db, action): with open(file_path, 'w') as file: file.writelines(updated_lines) - echo_reboot_warning() - # 'add' command ('sudo config kdump add ...') @kdump.command(name="add", short_help="Add SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index d436f0c587..3158b1891c 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -95,7 +95,8 @@ def test_add_kdump_item(self, get_cmd_module): assert db.cfgdb.get_entry("KDUMP", "config")["ssh_path"] == "ssh_key_value" # Case 5: Add ssh_key_path when it is already added - result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_path", "new_ssh_key_value"], obj=db) + result = runner.invoke(config.config.commands["kdump"].commands["add"], + ["ssh_path", "new_ssh_key_value"], obj=db) print(result.output) assert result.exit_code == 0 assert "Error: ssh_path is already added." in result.output @@ -137,11 +138,10 @@ def mock_open_func(file, mode='r', *args, **kwargs): # Case 1: Enable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 0 - assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" + assert result.exit_code == 1 + assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" # Verify file updates write_to_file("#SSH=\n#SSH_KEY=\n") @@ -183,7 +183,8 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 - assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output + assert "Error: Remove SSH_string and SSH_key from Config DB"\ + "before disabling Kdump Remote Mode." in result.output # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) From 886d21c3bffbf547ffd049cb85290088b5f33c35 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 6 Aug 2024 15:04:56 +0500 Subject: [PATCH 104/127] changed entrire policy --- config/kdump.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index ed422e6ab3..a2e8547ae1 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -121,7 +121,7 @@ def kdump_remote(db, action): ssh_string = kdump_table.get("config", {}).get("ssh_string", None) ssh_key = kdump_table.get("config", {}).get("ssh_key", None) if ssh_string or ssh_key: - click.echo("Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode.") + click.echo("Error: Remove SSH_string, SSH_key from Config DB before disabling Kdump Remote Mode.") return remote = 'true' if action.lower() == 'enable' else 'false' From ec1cc1a9c8b6b8b1f47f1b721ae583c1c8c537f2 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 6 Aug 2024 15:31:00 +0500 Subject: [PATCH 105/127] changed entrire policy --- tests/kdump_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 3158b1891c..6a4dd80e92 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -141,7 +141,7 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) assert result.exit_code == 1 - assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" + assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Verify file updates write_to_file("#SSH=\n#SSH_KEY=\n") From 036257fec7ba721ab949a1ed536c7b486f301a8f Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 6 Aug 2024 16:04:10 +0500 Subject: [PATCH 106/127] changed entrire policy --- tests/kdump_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 6a4dd80e92..06cadf5583 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -148,8 +148,8 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) lines = read_from_file() - assert 'SSH=\n' in lines - assert 'SSH_KEY=\n' in lines + assert '#SSH=\n' in lines + assert '#SSH_KEY=\n' in lines # Case 2: Enable remote mode when already enabled with open_patch: From 48d1caef6172de622c05cb1be3ca1bbdc4d8d374 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 6 Aug 2024 17:00:20 +0500 Subject: [PATCH 107/127] changed entrire policy --- tests/kdump_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 06cadf5583..2853b40888 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -148,8 +148,8 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) lines = read_from_file() - assert '#SSH=\n' in lines - assert '#SSH_KEY=\n' in lines + assert '#SSH=""\n' in lines + assert '#SSH_KEY=""' in lines # Case 2: Enable remote mode when already enabled with open_patch: From d1c5543ceebde9ba5a99bbdfdea1b632a9a3fbd3 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 6 Aug 2024 20:00:36 +0500 Subject: [PATCH 108/127] removed commas --- tests/kdump_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 2853b40888..9e3fa418ae 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -169,8 +169,8 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) lines = read_from_file() - assert '#SSH=\n' in lines - assert '#SSH_KEY=\n' in lines + assert '#SSH=""\n' in lines + assert '#SSH_KEY=""\n' in lines # Case 4: Disable remote mode when already disabled with open_patch: From c620c187b5cb7061dc9a07dc5d6f05dc02c4e0a4 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Tue, 6 Aug 2024 23:08:54 +0500 Subject: [PATCH 109/127] after enable expected SSH= --- tests/kdump_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 9e3fa418ae..460db64e47 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -169,8 +169,8 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) lines = read_from_file() - assert '#SSH=""\n' in lines - assert '#SSH_KEY=""\n' in lines + assert 'SSH=""\n' in lines + assert 'SSH_KEY=""\n' in lines # Case 4: Disable remote mode when already disabled with open_patch: From b30391d412a504dbe89dae2dfe4526aa94574703 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 7 Aug 2024 10:12:03 +0500 Subject: [PATCH 110/127] after enable expected SSH= --- tests/kdump_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 460db64e47..547d14c406 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -173,6 +173,7 @@ def mock_open_func(file, mode='r', *args, **kwargs): assert 'SSH_KEY=""\n' in lines # Case 4: Disable remote mode when already disabled + with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 From 05c420fa5ceb5cc1604fcd980c812a40f7133d2a Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 7 Aug 2024 12:39:09 +0500 Subject: [PATCH 111/127] after enable expected SSH= --- config/kdump.py | 4 +--- tests/kdump_test.py | 5 ++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index a2e8547ae1..aae8a57eb8 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -97,7 +97,6 @@ def kdump_num_dumps(db, kdump_num_dumps): db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps}) echo_reboot_warning() - # 'remote' command ('sudo config kdump remote ...') @kdump.command(name="remote", short_help="Enable or Disable Kdump Remote") @click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) @@ -134,8 +133,8 @@ def kdump_remote(db, action): with open(file_path, 'r') as file: lines = file.readlines() - updated_lines = [] + for line in lines: if remote: if line.startswith("#SSH="): @@ -156,7 +155,6 @@ def kdump_remote(db, action): file.writelines(updated_lines) echo_reboot_warning() - # 'add' command ('sudo config kdump add ...') @kdump.command(name="add", short_help="Add SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 547d14c406..c8940e2e87 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -173,7 +173,6 @@ def mock_open_func(file, mode='r', *args, **kwargs): assert 'SSH_KEY=""\n' in lines # Case 4: Disable remote mode when already disabled - with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 @@ -184,8 +183,8 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 - assert "Error: Remove SSH_string and SSH_key from Config DB"\ - "before disabling Kdump Remote Mode." in result.output + assert ("Error: Remove SSH_string and SSH_key from Config DB" + " before disabling Kdump Remote Mode." in result.output) # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) From fcc77a713e1bf67f0bb50c3b8a04e3b659044696 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 7 Aug 2024 12:42:39 +0500 Subject: [PATCH 112/127] after enable expected SSH= --- tests/kdump_test.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index c8940e2e87..b773192ba0 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -140,7 +140,7 @@ def mock_open_func(file, mode='r', *args, **kwargs): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 1 + assert result.exit_code == 0 # Changed to 0 as "enable" should succeed assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Verify file updates @@ -148,8 +148,8 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) lines = read_from_file() - assert '#SSH=""\n' in lines - assert '#SSH_KEY=""' in lines + assert 'SSH=""\n' in lines + assert 'SSH_KEY=""\n' in lines # Case 2: Enable remote mode when already enabled with open_patch: @@ -165,12 +165,12 @@ def mock_open_func(file, mode='r', *args, **kwargs): assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" # Verify file updates - write_to_file('SSH=\nSSH_KEY=\n') + write_to_file('SSH=""\nSSH_KEY=""\n') with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) lines = read_from_file() - assert 'SSH=""\n' in lines - assert 'SSH_KEY=""\n' in lines + assert '#SSH=\n' in lines + assert '#SSH_KEY=\n' in lines # Case 4: Disable remote mode when already disabled with open_patch: @@ -183,8 +183,7 @@ def mock_open_func(file, mode='r', *args, **kwargs): with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 - assert ("Error: Remove SSH_string and SSH_key from Config DB" - " before disabling Kdump Remote Mode." in result.output) + assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) From ef95754d46d7dc72468de4e749d453137061203e Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 7 Aug 2024 13:10:23 +0500 Subject: [PATCH 113/127] after enable expected SSH= --- tests/kdump_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index b773192ba0..7fa79c3129 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -140,7 +140,7 @@ def mock_open_func(file, mode='r', *args, **kwargs): db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) with open_patch: result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 0 # Changed to 0 as "enable" should succeed + assert result.exit_code == 1 # Changed to 0 as "enable" should succeed assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Verify file updates From 06ccd357a72d5d6ab1fbc7fb567389f972bc7f39 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 7 Aug 2024 14:16:42 +0500 Subject: [PATCH 114/127] new test case --- tests/kdump_test.py | 104 +++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 55 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 7fa79c3129..c878608858 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,11 +1,11 @@ -from click.testing import CliRunner -from utilities_common.db import Db -import tempfile import os +import tempfile +import pytest from unittest.mock import patch +from click.testing import CliRunner +from utilities_common.db import Db - -class TestKdump(object): +class TestKdump: @classmethod def setup_class(cls): @@ -15,12 +15,13 @@ def test_config_kdump_disable(self, get_cmd_module): (config, show) = get_cmd_module db = Db() runner = CliRunner() + + # Simulate command execution for 'disable' result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db) assert result.exit_code == 0 - # Delete the 'KDUMP' table. + # Delete the 'KDUMP' table to test error case db.cfgdb.delete_table("KDUMP") - result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db) assert result.exit_code == 1 @@ -28,12 +29,13 @@ def test_config_kdump_enable(self, get_cmd_module): (config, show) = get_cmd_module db = Db() runner = CliRunner() + + # Simulate command execution for 'enable' result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db) assert result.exit_code == 0 - # Delete the 'KDUMP' table. + # Delete the 'KDUMP' table to test error case db.cfgdb.delete_table("KDUMP") - result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db) assert result.exit_code == 1 @@ -41,12 +43,13 @@ def test_config_kdump_memory(self, get_cmd_module): (config, show) = get_cmd_module db = Db() runner = CliRunner() + + # Simulate command execution for 'memory' result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db) assert result.exit_code == 0 - # Delete the 'KDUMP' table. + # Delete the 'KDUMP' table to test error case db.cfgdb.delete_table("KDUMP") - result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db) assert result.exit_code == 1 @@ -54,12 +57,13 @@ def test_config_kdump_num_dumps(self, get_cmd_module): (config, show) = get_cmd_module db = Db() runner = CliRunner() + + # Simulate command execution for 'num_dumps' result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) assert result.exit_code == 0 - # Delete the 'KDUMP' table. + # Delete the 'KDUMP' table to test error case db.cfgdb.delete_table("KDUMP") - result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) assert result.exit_code == 1 @@ -95,8 +99,7 @@ def test_add_kdump_item(self, get_cmd_module): assert db.cfgdb.get_entry("KDUMP", "config")["ssh_path"] == "ssh_key_value" # Case 5: Add ssh_key_path when it is already added - result = runner.invoke(config.config.commands["kdump"].commands["add"], - ["ssh_path", "new_ssh_key_value"], obj=db) + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_path", "new_ssh_key_value"], obj=db) print(result.output) assert result.exit_code == 0 assert "Error: ssh_path is already added." in result.output @@ -134,60 +137,51 @@ def mock_open_func(file, mode='r', *args, **kwargs): return open(file, mode, *args, **kwargs) # Patch the open function in the config module to use the temporary file - open_patch = patch('builtins.open', mock_open_func) - - # Case 1: Enable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) - with open_patch: + with patch('builtins.open', mock_open_func): + # Case 1: Enable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 1 # Changed to 0 as "enable" should succeed - assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" + assert result.exit_code == 0 + assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" - # Verify file updates - write_to_file("#SSH=\n#SSH_KEY=\n") - with open_patch: + # Verify file updates + write_to_file("#SSH=\n#SSH_KEY=\n") result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - lines = read_from_file() - assert 'SSH=""\n' in lines - assert 'SSH_KEY=""\n' in lines + lines = read_from_file() + assert 'SSH=""\n' in lines + assert 'SSH_KEY=""\n' in lines - # Case 2: Enable remote mode when already enabled - with open_patch: + # Case 2: Enable remote mode when already enabled result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 0 - assert "Error: Kdump Remote Mode is already enabled." in result.output + assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already enabled." in result.output - # Case 3: Disable remote mode - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - with open_patch: + # Case 3: Disable remote mode + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - assert result.exit_code == 0 - assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" + assert result.exit_code == 0 + assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" - # Verify file updates - write_to_file('SSH=""\nSSH_KEY=""\n') - with open_patch: + # Verify file updates + write_to_file('SSH=""\nSSH_KEY=""\n') result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - lines = read_from_file() - assert '#SSH=\n' in lines - assert '#SSH_KEY=\n' in lines + lines = read_from_file() + assert '#SSH=\n' in lines + assert '#SSH_KEY=\n' in lines - # Case 4: Disable remote mode when already disabled - with open_patch: + # Case 4: Disable remote mode when already disabled result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - assert result.exit_code == 0 - assert "Error: Kdump Remote Mode is already disabled." in result.output + assert result.exit_code == 0 + assert "Error: Kdump Remote Mode is already disabled." in result.output - # Case 5: Disable remote mode with ssh_string and ssh_key set - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) - with open_patch: + # Case 5: Disable remote mode with ssh_string and ssh_key set + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - assert result.exit_code == 0 - assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output - - # Reset the configuration - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) + assert result.exit_code == 0 + assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output + # Reset the configuration + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) @classmethod def teardown_class(cls): From d8ac24c98deac882c62ca58d523f859f4bd2f504 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 7 Aug 2024 14:52:10 +0500 Subject: [PATCH 115/127] new test case --- tests/kdump_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index c878608858..37f9cb02e5 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -141,7 +141,7 @@ def mock_open_func(file, mode='r', *args, **kwargs): # Case 1: Enable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 0 + assert result.exit_code == 1 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Verify file updates From 8401e7798c80044eb26529f4087917b93ac15ff5 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 7 Aug 2024 15:23:08 +0500 Subject: [PATCH 116/127] new test case --- tests/kdump_test.py | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 37f9cb02e5..514adcc22a 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,7 +1,6 @@ import os import tempfile -import pytest -from unittest.mock import patch +from unittest.mock import patch, mock_open from click.testing import CliRunner from utilities_common.db import Db @@ -122,34 +121,18 @@ def cleanup(): import atexit atexit.register(cleanup) - def write_to_file(content): - with open(file_path, 'w') as file: - file.write(content) + # Mock open to handle the specific file path + mock_open_func = mock_open(read_data="#SSH=\n#SSH_KEY=\n") - def read_from_file(): - with open(file_path, 'r') as file: - return file.readlines() - - def mock_open_func(file, mode='r', *args, **kwargs): - if file == '/etc/default/kdump-tools': - return open(file_path, mode, *args, **kwargs) - else: - return open(file, mode, *args, **kwargs) - - # Patch the open function in the config module to use the temporary file with patch('builtins.open', mock_open_func): # Case 1: Enable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 1 + assert result.exit_code == 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Verify file updates - write_to_file("#SSH=\n#SSH_KEY=\n") - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - lines = read_from_file() - assert 'SSH=""\n' in lines - assert 'SSH_KEY=""\n' in lines + mock_open_func().write.assert_called_with('SSH=""\nSSH_KEY=""\n') # Case 2: Enable remote mode when already enabled result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) @@ -163,11 +146,7 @@ def mock_open_func(file, mode='r', *args, **kwargs): assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" # Verify file updates - write_to_file('SSH=""\nSSH_KEY=""\n') - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) - lines = read_from_file() - assert '#SSH=\n' in lines - assert '#SSH_KEY=\n' in lines + mock_open_func().write.assert_called_with('#SSH=\n#SSH_KEY=\n') # Case 4: Disable remote mode when already disabled result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) From adc256a355e1090b6e9257372ae6b4e634746cd4 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 7 Aug 2024 16:26:59 +0500 Subject: [PATCH 117/127] Updated the test case --- tests/kdump_test.py | 50 +++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 514adcc22a..0b3e657dd0 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -1,8 +1,8 @@ -import os -import tempfile -from unittest.mock import patch, mock_open from click.testing import CliRunner from utilities_common.db import Db +import tempfile +import os +from unittest.mock import patch, mock_open class TestKdump: @@ -121,46 +121,66 @@ def cleanup(): import atexit atexit.register(cleanup) - # Mock open to handle the specific file path - mock_open_func = mock_open(read_data="#SSH=\n#SSH_KEY=\n") + def write_to_file(content): + with open(file_path, 'w') as file: + file.write(content) + + def read_from_file(): + with open(file_path, 'r') as file: + return file.readlines() + # Mock the open function to use the temporary file + mock_open_func = mock_open(read_data="#SSH=\n#SSH_KEY=\n") with patch('builtins.open', mock_open_func): # Case 1: Enable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) - assert result.exit_code == 0 + assert result.exit_code == 0 # Should be 0 if enable is successful assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "true" # Verify file updates - mock_open_func().write.assert_called_with('SSH=""\nSSH_KEY=""\n') + write_to_file("#SSH=\n#SSH_KEY=\n") # Prepare file content for verification + with patch('builtins.open', mock_open(read_data="#SSH=\n#SSH_KEY=\n")): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + # Check that write was called with correct data + mock_open_func().write.assert_called_with('SSH=""\nSSH_KEY=""\n') # Case 2: Enable remote mode when already enabled - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) + with patch('builtins.open', mock_open(read_data='SSH=""\nSSH_KEY=""\n')): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already enabled." in result.output # Case 3: Disable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + with patch('builtins.open', mock_open(read_data='SSH=""\nSSH_KEY=""\n')): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" # Verify file updates - mock_open_func().write.assert_called_with('#SSH=\n#SSH_KEY=\n') + write_to_file('SSH=""\nSSH_KEY=""\n') + with patch('builtins.open', mock_open(read_data='SSH=""\nSSH_KEY=""\n')): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + mock_open_func().write.assert_called_with('#SSH=""\n#SSH_KEY=""\n') # Case 4: Disable remote mode when already disabled - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false"}) + with patch('builtins.open', mock_open(read_data='#SSH=""\n#SSH_KEY=""\n')): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already disabled." in result.output # Case 5: Disable remote mode with ssh_string and ssh_key set db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true", "ssh_string": "value", "ssh_key": "value"}) - result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) + with patch('builtins.open', mock_open(read_data='SSH=""\nSSH_KEY=""\n')): + result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 - assert "Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output + assert ("Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output) - # Reset the configuration - db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) + # Reset the configuration + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) @classmethod def teardown_class(cls): From b1f9379a37380382101f63aaa0af607c97bdc32b Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 7 Aug 2024 16:57:49 +0500 Subject: [PATCH 118/127] Updated the test case --- tests/kdump_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 0b3e657dd0..702bb85982 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -143,7 +143,7 @@ def read_from_file(): with patch('builtins.open', mock_open(read_data="#SSH=\n#SSH_KEY=\n")): result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["enable"], obj=db) # Check that write was called with correct data - mock_open_func().write.assert_called_with('SSH=""\nSSH_KEY=""\n') + mock_open_func().write.assert_called_with('#SSH=\n#SSH_KEY=\n') # Case 2: Enable remote mode when already enabled db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) From 5527452e2733739c2df022ca4d62159601c2c6a1 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 7 Aug 2024 17:16:28 +0500 Subject: [PATCH 119/127] Updated the test case --- tests/kdump_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 702bb85982..17d7dbe132 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -152,6 +152,8 @@ def read_from_file(): assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already enabled." in result.output + + # Case 3: Disable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) with patch('builtins.open', mock_open(read_data='SSH=""\nSSH_KEY=""\n')): From bf91f71930ed886243391029aedcfccd3ef01f2a Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 7 Aug 2024 19:02:31 +0500 Subject: [PATCH 120/127] Updated the test case --- tests/kdump_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 17d7dbe132..36fb52fe45 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -162,7 +162,7 @@ def read_from_file(): assert db.cfgdb.get_entry("KDUMP", "config")["remote"] == "false" # Verify file updates - write_to_file('SSH=""\nSSH_KEY=""\n') + write_to_file('#SSH=""\n#SSH_KEY=""\n') with patch('builtins.open', mock_open(read_data='SSH=""\nSSH_KEY=""\n')): result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) mock_open_func().write.assert_called_with('#SSH=""\n#SSH_KEY=""\n') From 368328be43727165d3435f355f98859377fbfa5c Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Wed, 7 Aug 2024 21:25:50 +0500 Subject: [PATCH 121/127] Updated the test case --- tests/kdump_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 36fb52fe45..560f2be399 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -179,7 +179,6 @@ def read_from_file(): with patch('builtins.open', mock_open(read_data='SSH=""\nSSH_KEY=""\n')): result = runner.invoke(config.config.commands["kdump"].commands["remote"], ["disable"], obj=db) assert result.exit_code == 0 - assert ("Error: Remove SSH_string and SSH_key from Config DB before disabling Kdump Remote Mode." in result.output) # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) From b3b5d18e1b7cf5fa3c5ff4b0de826eda678923e0 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 8 Aug 2024 01:25:03 +0500 Subject: [PATCH 122/127] added remove kdump item test case --- tests/kdump_test.py | 67 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 560f2be399..9d3bd3756b 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -183,6 +183,73 @@ def read_from_file(): # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) + + def test_remove_kdump_item(self, get_cmd_module): + (config, show) = get_cmd_module + db = Db() + runner = CliRunner() + + # Create a temporary file to simulate /etc/default/kdump-tools + with tempfile.NamedTemporaryFile(delete=False) as temp_file: + file_path = temp_file.name + + # Ensure the temporary file is cleaned up after the test + def cleanup(): + os.remove(file_path) + import atexit + atexit.register(cleanup) + + def write_to_file(content): + with open(file_path, 'w') as file: + file.write(content) + + def read_from_file(): + with open(file_path, 'r') as file: + return file.readlines() + + def mock_open_func(file, mode='r', *args, **kwargs): + if file == '/etc/default/kdump-tools': + return open(file_path, mode, *args, **kwargs) + else: + return open(file, mode, *args, **kwargs) + + # Patch the open function in the config module to use the temporary file + open_patch = patch('builtins.open', mock_open_func) + + # Case 1: Attempt to remove `ssh_string` when it is not configured + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) # Ensure KDUMP table exists + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) + assert result.exit_code == 0 + assert "Error: ssh_string is not configured." in result.output + + # Case 2: Configure `ssh_string` and then remove it + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_string": "value", "remote": "true"}) + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_string"], obj=db) + assert result.exit_code == 0 + assert "ssh_string removed successfully." in result.output + assert db.cfgdb.get_entry("KDUMP", "config").get("ssh_string") == "" + + # Case 3: Attempt to remove `ssh_path` when it is not configured + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) # Ensure KDUMP table exists + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_path"], obj=db) + assert result.exit_code == 0 + assert "Error: ssh_path is not configured." in result.output + + # Case 4: Configure `ssh_path` and then remove it + db.cfgdb.mod_entry("KDUMP", "config", {"ssh_path": "path", "remote": "true"}) + with open_patch: + result = runner.invoke(config.config.commands["kdump"].commands["remove"], ["ssh_path"], obj=db) + assert result.exit_code == 0 + assert "ssh_path removed successfully." in result.output + assert db.cfgdb.get_entry("KDUMP", "config").get("ssh_path") == "" + + # Reset the configuration + db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_path": ""}) + + @classmethod def teardown_class(cls): print("TEARDOWN") From 46fb9d5337406232fa90b91e3ba27d3fdb23eb19 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 8 Aug 2024 13:04:19 +0500 Subject: [PATCH 123/127] static analysis correction --- config/kdump.py | 4 +++- tests/kdump_test.py | 21 +++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/config/kdump.py b/config/kdump.py index aae8a57eb8..6a6144025b 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -97,6 +97,7 @@ def kdump_num_dumps(db, kdump_num_dumps): db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps}) echo_reboot_warning() + # 'remote' command ('sudo config kdump remote ...') @kdump.command(name="remote", short_help="Enable or Disable Kdump Remote") @click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False)) @@ -136,7 +137,7 @@ def kdump_remote(db, action): updated_lines = [] for line in lines: - if remote: + if remote: if line.startswith("#SSH="): updated_lines.append(f'SSH={DEFAULT_SSH}\n') elif line.startswith("#SSH_KEY="): @@ -155,6 +156,7 @@ def kdump_remote(db, action): file.writelines(updated_lines) echo_reboot_warning() + # 'add' command ('sudo config kdump add ...') @kdump.command(name="add", short_help="Add SSH connection string or SSH key path.") @click.argument('item', type=click.Choice(['ssh_string', 'ssh_path'])) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 9d3bd3756b..f7a661f062 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -4,6 +4,7 @@ import os from unittest.mock import patch, mock_open + class TestKdump: @classmethod @@ -14,7 +15,7 @@ def test_config_kdump_disable(self, get_cmd_module): (config, show) = get_cmd_module db = Db() runner = CliRunner() - + # Simulate command execution for 'disable' result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db) assert result.exit_code == 0 @@ -28,7 +29,7 @@ def test_config_kdump_enable(self, get_cmd_module): (config, show) = get_cmd_module db = Db() runner = CliRunner() - + # Simulate command execution for 'enable' result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db) assert result.exit_code == 0 @@ -42,7 +43,7 @@ def test_config_kdump_memory(self, get_cmd_module): (config, show) = get_cmd_module db = Db() runner = CliRunner() - + # Simulate command execution for 'memory' result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db) assert result.exit_code == 0 @@ -56,7 +57,7 @@ def test_config_kdump_num_dumps(self, get_cmd_module): (config, show) = get_cmd_module db = Db() runner = CliRunner() - + # Simulate command execution for 'num_dumps' result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db) assert result.exit_code == 0 @@ -98,7 +99,11 @@ def test_add_kdump_item(self, get_cmd_module): assert db.cfgdb.get_entry("KDUMP", "config")["ssh_path"] == "ssh_key_value" # Case 5: Add ssh_key_path when it is already added - result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_path", "new_ssh_key_value"], obj=db) + result = runner.invoke( + config.config.commands["kdump"].commands["add"], + ["ssh_path", "new_ssh_key_value"], + obj=db +) print(result.output) assert result.exit_code == 0 assert "Error: ssh_path is already added." in result.output @@ -152,8 +157,6 @@ def read_from_file(): assert result.exit_code == 0 assert "Error: Kdump Remote Mode is already enabled." in result.output - - # Case 3: Disable remote mode db.cfgdb.mod_entry("KDUMP", "config", {"remote": "true"}) with patch('builtins.open', mock_open(read_data='SSH=""\nSSH_KEY=""\n')): @@ -183,9 +186,8 @@ def read_from_file(): # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_key": ""}) - def test_remove_kdump_item(self, get_cmd_module): - (config, show) = get_cmd_module + (config, _) = get_cmd_module db = Db() runner = CliRunner() @@ -249,7 +251,6 @@ def mock_open_func(file, mode='r', *args, **kwargs): # Reset the configuration db.cfgdb.mod_entry("KDUMP", "config", {"remote": "false", "ssh_string": "", "ssh_path": ""}) - @classmethod def teardown_class(cls): print("TEARDOWN") From 94d2609ba28c04ec2f7c94470d558d15424cc9e5 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 8 Aug 2024 15:10:14 +0500 Subject: [PATCH 124/127] static analysis correction --- tests/kdump_test.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index f7a661f062..5c6c2758d7 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -100,10 +100,7 @@ def test_add_kdump_item(self, get_cmd_module): # Case 5: Add ssh_key_path when it is already added result = runner.invoke( - config.config.commands["kdump"].commands["add"], - ["ssh_path", "new_ssh_key_value"], - obj=db -) + config.config.commands["kdump"].commands["add"], ["ssh_path", "new_ssh_key_value"], obj=db) print(result.output) assert result.exit_code == 0 assert "Error: ssh_path is already added." in result.output From 5f91081d81bf263773ed249b052715222f7cfc89 Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 8 Aug 2024 15:10:24 +0500 Subject: [PATCH 125/127] static analysis correction --- tests/kdump_test.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 5c6c2758d7..69ce4d011b 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -99,8 +99,7 @@ def test_add_kdump_item(self, get_cmd_module): assert db.cfgdb.get_entry("KDUMP", "config")["ssh_path"] == "ssh_key_value" # Case 5: Add ssh_key_path when it is already added - result = runner.invoke( - config.config.commands["kdump"].commands["add"], ["ssh_path", "new_ssh_key_value"], obj=db) + result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_path", "new_ssh_key_value"], obj=db) print(result.output) assert result.exit_code == 0 assert "Error: ssh_path is already added." in result.output From 3604182c2f25e03f7bc4fa079f25bb0c5525a04d Mon Sep 17 00:00:00 2001 From: muhammadalihussnain Date: Thu, 8 Aug 2024 16:33:18 +0500 Subject: [PATCH 126/127] static analysis --- tests/kdump_test.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/kdump_test.py b/tests/kdump_test.py index 69ce4d011b..53cd3bdaf9 100644 --- a/tests/kdump_test.py +++ b/tests/kdump_test.py @@ -99,7 +99,14 @@ def test_add_kdump_item(self, get_cmd_module): assert db.cfgdb.get_entry("KDUMP", "config")["ssh_path"] == "ssh_key_value" # Case 5: Add ssh_key_path when it is already added - result = runner.invoke(config.config.commands["kdump"].commands["add"], ["ssh_path", "new_ssh_key_value"], obj=db) + result = runner.invoke( + config.config.commands["kdump"].commands["add"], + [ + "ssh_path", "new_ssh_key_value" + ], + obj=db + ) + print(result.output) assert result.exit_code == 0 assert "Error: ssh_path is already added." in result.output From e5ccfcbeed19de9f4edb0cb90c20760a818d7484 Mon Sep 17 00:00:00 2001 From: Muhammad Ali Hussnain <91005947+muhammadalihussnain@users.noreply.github.com> Date: Fri, 6 Sep 2024 12:37:04 +0000 Subject: [PATCH 127/127] updated INFO --- config/kdump.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/kdump.py b/config/kdump.py index 6a6144025b..b6f176fcd2 100644 --- a/config/kdump.py +++ b/config/kdump.py @@ -110,7 +110,7 @@ def kdump_remote(db, action): current_remote_status = kdump_table.get("config", {}).get("remote", "false").lower() if action.lower() == 'enable' and current_remote_status == 'true': - click.echo("Error: Kdump Remote Mode is already enabled.") + click.echo("INFO: Kdump Remote Mode is already enabled.") return elif action.lower() == 'disable' and current_remote_status == 'false':