Skip to content

Commit a67188a

Browse files
arista-hpandyagordon-nexthop
authored andcommitted
Fix syntax and semantic errors in kdump remote feature (#4114)
1 parent f4d83a0 commit a67188a

File tree

2 files changed

+16
-43
lines changed

2 files changed

+16
-43
lines changed

scripts/sonic-kdump-config

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,11 @@ def write_kdump_remote():
447447

448448
if remote:
449449
# Uncomment SSH and SSH_KEY in the /etc/default/kdump-tools file
450-
run_command("/bin/sed -i 's/#SSH/SSH/' %s" % kdump_cfg, use_shell=False)
451-
run_command("/bin/sed -i 's/#SSH_KEY/SSH_KEY/' %s" % kdump_cfg, use_shell=False)
450+
run_command("/bin/sed -i 's/^#SSH/SSH/' %s" % kdump_cfg, use_shell=False)
452451
print("SSH and SSH_KEY uncommented for remote configuration.")
453452
else:
454453
# Comment out SSH and SSH_KEY in the /etc/default/kdump-tools file
455-
run_command("/bin/sed -i 's/SSH/#SSH/' %s" % kdump_cfg, use_shell=False)
456-
run_command("/bin/sed -i 's/SSH_KEY/#SSH_KEY/' %s" % kdump_cfg, use_shell=False)
454+
run_command("/bin/sed -i 's/^SSH/#SSH/' %s" % kdump_cfg, use_shell=False)
457455
print("SSH and SSH_KEY commented out for local configuration.")
458456

459457
def read_ssh_string():
@@ -513,7 +511,7 @@ def read_ssh_path():
513511
sys.exit(1)
514512

515513
def write_ssh_path(ssh_path):
516-
cmd = "/bin/sed -i -e 's/#*SSH_KEY=.*/SSH_KEY=\"%s\"/' %s" % (ssh_path, kdump_cfg)
514+
cmd = "/bin/sed -i -e 's|#*SSH_KEY=.*|SSH_KEY=\"%s\"|' %s" % (ssh_path, kdump_cfg)
517515

518516
(rc, lines, err_str) = run_command(cmd, use_shell=False) # Make sure to set use_shell=True
519517
if rc == 0 and type(lines) == list and len(lines) == 0:
@@ -772,14 +770,12 @@ def cmd_kdump_remote(verbose):
772770

773771
if remote:
774772
# Uncomment SSH and SSH_KEY in the /etc/default/kdump-tools file
775-
run_command("/bin/sed -i 's/#SSH/SSH/' /etc/default/kdump-tools", use_shell=False)
776-
run_command("/bin/sed -i 's/#SSH_KEY/SSH_KEY/' /etc/default/kdump-tools", use_shell=False)
773+
run_command("/bin/sed -i 's/^#SSH/SSH/' /etc/default/kdump-tools", use_shell=False)
777774
if verbose:
778775
print("SSH and SSH_KEY uncommented for remote configuration.")
779776
else:
780777
# Comment out SSH and SSH_KEY in the /etc/default/kdump-tools file
781-
run_command("/bin/sed -i 's/SSH/#SSH/' /etc/default/kdump-tools", use_shell=False)
782-
run_command("/bin/sed -i 's/SSH_KEY/#SSH_KEY/' /etc/default/kdump-tools", use_shell=False)
778+
run_command("/bin/sed -i 's/^SSH/#SSH/' /etc/default/kdump-tools", use_shell=False)
783779
if verbose:
784780
print("SSH and SSH_KEY commented out for local configuration.")
785781

@@ -798,10 +794,8 @@ def cmd_kdump_ssh_path(verbose, ssh_path):
798794
(rc, lines, err_str) = run_command("show kdump ssh_path", use_shell=False)
799795
print('\n'.join(lines))
800796
else:
801-
current_ssh_path = read_ssh_path()
802-
if current_ssh_path != ssh_path:
803-
write_ssh_path(ssh_path)
804-
print("SSH path updated. Changes will take effect after reboot.")
797+
write_ssh_path(ssh_path)
798+
print("SSH path updated. Changes will take effect after reboot.")
805799

806800
def main():
807801

@@ -846,7 +840,7 @@ def main():
846840
help='Maximum number of kernel dump files stored')
847841

848842
parser.add_argument('--remote', action='store_true', default=False,
849-
help='Enable remote kdump via SSH')
843+
help='Update remote kdump via SSH based on CONFIG_DB')
850844

851845
parser.add_argument('--ssh_string', nargs='?', type=str, action='store', default=False,
852846
help='ssh_string for remote kdump')
@@ -892,7 +886,7 @@ def main():
892886
elif options.ssh_string:
893887
cmd_kdump_ssh_string(options.verbose, options.ssh_string)
894888
elif options.ssh_path:
895-
cmd_kdump_ssh_path(options.verbos, options.ssh_path)
889+
cmd_kdump_ssh_path(options.verbose, options.ssh_path)
896890
elif options.num_dumps != False:
897891
cmd_kdump_num_dumps(options.verbose, options.num_dumps)
898892
elif options.dump_db:

tests/sonic_kdump_config_test.py

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,8 @@ def test_write_kdump_remote_true(self, mock_read_remote, mock_run_command):
395395
sonic_kdump_config.write_kdump_remote() # Call the function
396396

397397
# Ensure the correct commands were run to uncomment SSH and SSH_KEY
398-
mock_run_command.assert_any_call("/bin/sed -i 's/#SSH/SSH/' /etc/default/kdump-tools", use_shell=False)
399-
mock_run_command.assert_any_call("/bin/sed -i 's/#SSH_KEY/SSH_KEY/' /etc/default/kdump-tools", use_shell=False)
400-
self.assertEqual(mock_run_command.call_count, 2) # Ensure both commands were called
398+
mock_run_command.assert_any_call("/bin/sed -i 's/^#SSH/SSH/' /etc/default/kdump-tools", use_shell=False)
399+
self.assertEqual(mock_run_command.call_count, 1) # Ensure the sed command was called once
401400

402401
@patch("sonic_kdump_config.run_command")
403402
@patch("sonic_kdump_config.get_kdump_remote")
@@ -408,9 +407,8 @@ def test_write_kdump_remote_false(self, mock_read_remote, mock_run_command):
408407
sonic_kdump_config.write_kdump_remote() # Call the function
409408

410409
# Ensure the correct commands were run to comment SSH and SSH_KEY
411-
mock_run_command.assert_any_call("/bin/sed -i 's/SSH/#SSH/' /etc/default/kdump-tools", use_shell=False)
412-
mock_run_command.assert_any_call("/bin/sed -i 's/SSH_KEY/#SSH_KEY/' /etc/default/kdump-tools", use_shell=False)
413-
self.assertEqual(mock_run_command.call_count, 2)
410+
mock_run_command.assert_any_call("/bin/sed -i 's/^SSH/#SSH/' /etc/default/kdump-tools", use_shell=False)
411+
self.assertEqual(mock_run_command.call_count, 1)
414412

415413
@patch("sonic_kdump_config.get_kdump_remote")
416414
@patch("sonic_kdump_config.run_command")
@@ -422,16 +420,14 @@ def test_cmd_kdump_remote(self, mock_run_command, mock_read_remote):
422420
sonic_kdump_config.cmd_kdump_remote(verbose=True)
423421

424422
# Ensure the correct commands are being run
425-
mock_run_command.assert_any_call("/bin/sed -i 's/#SSH/SSH/' /etc/default/kdump-tools", use_shell=False)
426-
mock_run_command.assert_any_call("/bin/sed -i 's/#SSH_KEY/SSH_KEY/' /etc/default/kdump-tools", use_shell=False)
423+
mock_run_command.assert_any_call("/bin/sed -i 's/^#SSH/SSH/' /etc/default/kdump-tools", use_shell=False)
427424

428425
# Test case: Remote is False
429426
mock_read_remote.return_value = False
430427
sonic_kdump_config.cmd_kdump_remote(verbose=True)
431428

432429
# Ensure the correct commands are being run
433-
mock_run_command.assert_any_call("/bin/sed -i 's/SSH/#SSH/' /etc/default/kdump-tools", use_shell=False)
434-
mock_run_command.assert_any_call("/bin/sed -i 's/SSH_KEY/#SSH_KEY/' /etc/default/kdump-tools", use_shell=False)
430+
mock_run_command.assert_any_call("/bin/sed -i 's/^SSH/#SSH/' /etc/default/kdump-tools", use_shell=False)
435431

436432
# Test case: Checking output messages
437433
with patch("builtins.print") as mock_print:
@@ -538,7 +534,7 @@ def test_write_ssh_path(self, mock_read_ssh_path, mock_run_cmd):
538534
sonic_kdump_config.write_ssh_path('/path/to/keys') # Call function with valid path
539535
# Ensure the correct command is being run
540536
expected_cmd = (
541-
"/bin/sed -i -e 's/#*SSH_KEY=.*/SSH_KEY=\"/path/to/keys\"/' %s"
537+
"/bin/sed -i -e 's|#*SSH_KEY=.*|SSH_KEY=\"/path/to/keys\"|' %s"
542538
% sonic_kdump_config.kdump_cfg
543539
)
544540
mock_run_cmd.assert_called_once_with(expected_cmd, use_shell=False)
@@ -670,23 +666,6 @@ def test_cmd_kdump_ssh_path_update(self, mock_print, mock_write, mock_read, mock
670666
# Verify that the correct message is printed
671667
mock_print.assert_called_once_with("SSH path updated. Changes will take effect after reboot.")
672668

673-
@patch('sonic_kdump_config.run_command')
674-
@patch('sonic_kdump_config.read_ssh_path')
675-
@patch('sonic_kdump_config.write_ssh_path')
676-
@patch('builtins.print') # Mock print to capture printed output
677-
def test_cmd_kdump_ssh_path_no_update(self, mock_print, mock_write, mock_read, mock_run):
678-
# Mock read_ssh_path to return the same SSH path provided
679-
mock_read.return_value = '/same/path/to/keys'
680-
681-
# Call the function with the same SSH path
682-
sonic_kdump_config.cmd_kdump_ssh_path(verbose=True, ssh_path='/same/path/to/keys')
683-
684-
# Check that write_ssh_path was not called
685-
mock_write.assert_not_called()
686-
687-
# Check that no message is printed for update
688-
mock_print.assert_not_called()
689-
690669
@patch("sonic_kdump_config.write_use_kdump")
691670
@patch("os.path.exists")
692671
def test_kdump_disable(self, mock_path_exist, mock_write_kdump):

0 commit comments

Comments
 (0)