Skip to content

Commit cd3ee78

Browse files
authored
[Config] Update config command of kdump (sonic-net#1700)
Signed-off-by: Yong Zhao [email protected] What I did This PR aims to update the config command of Kdump (Linux kernel dump) mechanism. How I did it Before updating the value of a field in the table of KDUMP, we need decide whether the KDUMP table and corresponding field does exist in the table. How to verify it I verifies this on device str-msn2700-03 and unit test passed. tests/kdump_test.py::TestKdump::test_config_kdump_disable PASSED tests/kdump_test.py::TestKdump::test_config_kdump_enable PASSED tests/kdump_test.py::TestKdump::test_config_kdump_memory PASSED tests/kdump_test.py::TestKdump::test_config_kdump_num_dumps PASSED
1 parent 4cb3b72 commit cd3ee78

File tree

3 files changed

+160
-39
lines changed

3 files changed

+160
-39
lines changed

config/kdump.py

+81-39
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,86 @@
1-
import os
1+
import sys
2+
23
import click
3-
import utilities_common.cli as clicommon
4-
from swsscommon.swsscommon import ConfigDBConnector
4+
from utilities_common.cli import AbbreviationGroup, pass_db
5+
56

6-
@click.group(cls=clicommon.AbbreviationGroup, name="kdump")
7+
#
8+
# 'kdump' group ('sudo config kdump ...')
9+
#
10+
@click.group(cls=AbbreviationGroup, name="kdump")
711
def kdump():
8-
""" Configure kdump """
9-
if os.geteuid() != 0:
10-
exit("Root privileges are required for this operation")
11-
12-
@kdump.command()
13-
def disable():
14-
"""Disable kdump operation"""
15-
config_db = ConfigDBConnector()
16-
if config_db is not None:
17-
config_db.connect()
18-
config_db.mod_entry("KDUMP", "config", {"enabled": "false"})
19-
20-
@kdump.command()
21-
def enable():
22-
"""Enable kdump operation"""
23-
config_db = ConfigDBConnector()
24-
if config_db is not None:
25-
config_db.connect()
26-
config_db.mod_entry("KDUMP", "config", {"enabled": "true"})
27-
28-
@kdump.command()
12+
"""Configure the KDUMP mechanism"""
13+
pass
14+
15+
16+
def check_kdump_table_existence(kdump_table):
17+
"""Checks whether the 'KDUMP' table is configured in Config DB.
18+
19+
Args:
20+
kdump_table: A dictionary represents the key-value pair in sub-table
21+
of 'KDUMP'.
22+
23+
Returns:
24+
None.
25+
"""
26+
if not kdump_table:
27+
click.echo("Unable to retrieve 'KDUMP' table from Config DB.")
28+
sys.exit(1)
29+
30+
if "config" not in kdump_table:
31+
click.echo("Unable to retrieve key 'config' from KDUMP table.")
32+
sys.exit(2)
33+
34+
35+
#
36+
# 'disable' command ('sudo config kdump disable')
37+
#
38+
@kdump.command(name="disable", short_help="Disable the KDUMP mechanism")
39+
@pass_db
40+
def kdump_disable(db):
41+
"""Disable the KDUMP mechanism"""
42+
kdump_table = db.cfgdb.get_table("KDUMP")
43+
check_kdump_table_existence(kdump_table)
44+
45+
db.cfgdb.mod_entry("KDUMP", "config", {"enabled": "false"})
46+
47+
48+
#
49+
# 'enable' command ('sudo config kdump enable')
50+
#
51+
@kdump.command(name="enable", short_help="Enable the KDUMP mechanism")
52+
@pass_db
53+
def kdump_enable(db):
54+
"""Enable the KDUMP mechanism"""
55+
kdump_table = db.cfgdb.get_table("KDUMP")
56+
check_kdump_table_existence(kdump_table)
57+
58+
db.cfgdb.mod_entry("KDUMP", "config", {"enabled": "true"})
59+
60+
61+
#
62+
# 'memory' command ('sudo config kdump memory ...')
63+
#
64+
@kdump.command(name="memory", short_help="Configure the memory for KDUMP mechanism")
2965
@click.argument('kdump_memory', metavar='<kdump_memory>', required=True)
30-
def memory(kdump_memory):
31-
"""Set memory allocated for kdump capture kernel"""
32-
config_db = ConfigDBConnector()
33-
if config_db is not None:
34-
config_db.connect()
35-
config_db.mod_entry("KDUMP", "config", {"memory": kdump_memory})
36-
37-
@kdump.command('num-dumps')
66+
@pass_db
67+
def kdump_memory(db, kdump_memory):
68+
"""Reserve memory for kdump capture kernel"""
69+
kdump_table = db.cfgdb.get_table("KDUMP")
70+
check_kdump_table_existence(kdump_table)
71+
72+
db.cfgdb.mod_entry("KDUMP", "config", {"memory": kdump_memory})
73+
74+
75+
#
76+
# 'num_dumps' command ('sudo config keump num_dumps ...')
77+
#
78+
@kdump.command(name="num_dumps", short_help="Configure the maximum dump files of KDUMP mechanism")
3879
@click.argument('kdump_num_dumps', metavar='<kdump_num_dumps>', required=True, type=int)
39-
def num_dumps(kdump_num_dumps):
40-
"""Set max number of dump files for kdump"""
41-
config_db = ConfigDBConnector()
42-
if config_db is not None:
43-
config_db.connect()
44-
config_db.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps})
80+
@pass_db
81+
def kdump_num_dumps(db, kdump_num_dumps):
82+
"""Set maximum number of dump files for kdump"""
83+
kdump_table = db.cfgdb.get_table("KDUMP")
84+
check_kdump_table_existence(kdump_table)
85+
86+
db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps})

tests/kdump_test.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import importlib
2+
3+
from click.testing import CliRunner
4+
from utilities_common.db import Db
5+
6+
7+
class TestKdump(object):
8+
@classmethod
9+
def setup_class(cls):
10+
print("SETUP")
11+
12+
def test_config_kdump_disable(self, get_cmd_module):
13+
(config, show) = get_cmd_module
14+
db = Db()
15+
runner = CliRunner()
16+
result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db)
17+
print(result.exit_code)
18+
assert result.exit_code == 0
19+
20+
# Delete the 'KDUMP' table.
21+
db.cfgdb.delete_table("KDUMP")
22+
23+
result = runner.invoke(config.config.commands["kdump"].commands["disable"], obj=db)
24+
print(result.exit_code)
25+
assert result.exit_code == 1
26+
27+
def test_config_kdump_enable(self, get_cmd_module):
28+
(config, show) = get_cmd_module
29+
db = Db()
30+
runner = CliRunner()
31+
result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db)
32+
print(result.exit_code)
33+
assert result.exit_code == 0
34+
35+
# Delete the 'KDUMP' table.
36+
db.cfgdb.delete_table("KDUMP")
37+
38+
result = runner.invoke(config.config.commands["kdump"].commands["enable"], obj=db)
39+
print(result.exit_code)
40+
assert result.exit_code == 1
41+
42+
def test_config_kdump_memory(self, get_cmd_module):
43+
(config, show) = get_cmd_module
44+
db = Db()
45+
runner = CliRunner()
46+
result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db)
47+
print(result.exit_code)
48+
assert result.exit_code == 0
49+
50+
# Delete the 'KDUMP' table.
51+
db.cfgdb.delete_table("KDUMP")
52+
53+
result = runner.invoke(config.config.commands["kdump"].commands["memory"], ["256MB"], obj=db)
54+
print(result.exit_code)
55+
assert result.exit_code == 1
56+
57+
def test_config_kdump_num_dumps(self, get_cmd_module):
58+
(config, show) = get_cmd_module
59+
db = Db()
60+
runner = CliRunner()
61+
result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db)
62+
print(result.exit_code)
63+
assert result.exit_code == 0
64+
65+
# Delete the 'KDUMP' table.
66+
db.cfgdb.delete_table("KDUMP")
67+
68+
result = runner.invoke(config.config.commands["kdump"].commands["num_dumps"], ["10"], obj=db)
69+
print(result.exit_code)
70+
assert result.exit_code == 1
71+
72+
@classmethod
73+
def teardown_class(cls):
74+
print("TEARDOWN")

tests/mock_tables/config_db.json

+5
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,11 @@
684684
"DEBUG_COUNTER_DROP_REASON|DEBUG_2|IP_HEADER_ERROR": {},
685685
"DEBUG_COUNTER_DROP_REASON|DEBUG_2|NO_L3_HEADER": {},
686686
"DEBUG_COUNTER_DROP_REASON|lowercase_counter|L2_ANY": {},
687+
"KDUMP|config": {
688+
"enabled": "false",
689+
"memory": "256MB",
690+
"num_dumps": "3"
691+
},
687692
"FEATURE|bgp": {
688693
"state": "enabled",
689694
"auto_restart": "enabled",

0 commit comments

Comments
 (0)