Skip to content

Commit 7783868

Browse files
committed
feat: validate crm high and low threshold input
1 parent 67232b6 commit 7783868

File tree

2 files changed

+121
-23
lines changed

2 files changed

+121
-23
lines changed

crm/main.py

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ def get_thresholds_list(self):
4848

4949
def get_resources_list(self):
5050
return list(self.resources)
51+
52+
def get_crm_config(self):
53+
configdb = self.cfgdb
54+
if configdb is None:
55+
namespaces = multi_asic.get_namespace_list()
56+
configdb = ConfigDBConnector(namespace=namespaces[0])
57+
configdb.connect()
58+
59+
return configdb.get_entry('CRM', 'Config')
5160

5261
@multi_asic_util.run_on_multi_asic
5362
def config(self, attr, val):
@@ -63,15 +72,7 @@ def show_summary(self):
6372
CRM Handler to display general information.
6473
"""
6574

66-
configdb = self.cfgdb
67-
if configdb is None:
68-
# Get the namespace list
69-
namespaces = multi_asic.get_namespace_list()
70-
71-
configdb = ConfigDBConnector(namespace=namespaces[0])
72-
configdb.connect()
73-
74-
crm_info = configdb.get_entry('CRM', 'Config')
75+
crm_info = self.get_crm_config()
7576

7677
if crm_info:
7778
try:
@@ -86,15 +87,8 @@ def show_thresholds(self, resource):
8687
"""
8788
CRM Handler to display thresholds information.
8889
"""
89-
configdb = self.cfgdb
90-
if configdb is None:
91-
# Get the namespace list
92-
namespaces = multi_asic.get_namespace_list()
9390

94-
configdb = ConfigDBConnector(namespace=namespaces[0])
95-
configdb.connect()
96-
97-
crm_info = configdb.get_entry('CRM', 'Config')
91+
crm_info = self.get_crm_config()
9892

9993
header = ("Resource Name", "Threshold Type", "Low Threshold", "High Threshold")
10094
data = []
@@ -418,27 +412,72 @@ def type(ctx, value):
418412
@click.pass_context
419413
def low(ctx, value):
420414
"""CRM low threshold configuration"""
421-
attr = ''
422415

416+
type_attr = ''
423417
if ctx.obj["crm"].addr_family != None:
424-
attr += ctx.obj["crm"].addr_family + '_'
418+
type_attr += ctx.obj["crm"].addr_family + '_'
419+
type_attr += ctx.obj["crm"].res_type + '_' + 'threshold_type'
425420

426-
attr += ctx.obj["crm"].res_type + '_' + 'low_threshold'
421+
high_attr = ''
422+
if ctx.obj["crm"].addr_family != None:
423+
high_attr += ctx.obj["crm"].addr_family + '_'
424+
high_attr += ctx.obj["crm"].res_type + '_' + 'high_threshold'
425+
426+
crm_info = ctx.obj["crm"].get_crm_config()
427+
threshold_type = crm_info.get(type_attr, 'percentage')
428+
if threshold_type == 'percentage':
429+
if value < 0 or value > 100:
430+
raise click.ClickException(
431+
'Error: Low threshold value must be between 0 and 100 for '
432+
'percentage type.')
433+
434+
high_value = crm_info.get(high_attr, None)
435+
if high_value is not None and value >= int(high_value):
436+
raise click.ClickException(
437+
'Error: Low threshold value must be less than high threshold '
438+
f'value: {high_value} for percentage type.')
427439

440+
attr = ''
441+
if ctx.obj["crm"].addr_family != None:
442+
attr += ctx.obj["crm"].addr_family + '_'
443+
attr += ctx.obj["crm"].res_type + '_' + 'low_threshold'
428444
ctx.obj["crm"].config(attr, value)
429445

446+
430447
@click.command()
431448
@click.argument('value', type=click.INT)
432449
@click.pass_context
433450
def high(ctx, value):
434451
"""CRM high threshold configuration"""
435-
attr = ''
436452

453+
type_attr = ''
437454
if ctx.obj["crm"].addr_family != None:
438-
attr += ctx.obj["crm"].addr_family + '_'
455+
type_attr += ctx.obj["crm"].addr_family + '_'
456+
type_attr += ctx.obj["crm"].res_type + '_' + 'threshold_type'
439457

440-
attr += ctx.obj["crm"].res_type + '_' + 'high_threshold'
458+
low_attr = ''
459+
if ctx.obj["crm"].addr_family != None:
460+
low_attr += ctx.obj["crm"].addr_family + '_'
461+
low_attr += ctx.obj["crm"].res_type + '_' + 'low_threshold'
462+
463+
crm_info = ctx.obj["crm"].get_crm_config()
464+
threshold_type = crm_info.get(type_attr, 'percentage')
465+
if threshold_type == 'percentage':
466+
if value < 0 or value > 100:
467+
raise click.ClickException(
468+
'Error: High threshold value must be between 0 and 100 for '
469+
'percentage type.')
470+
471+
low_value = crm_info.get(low_attr, None)
472+
if low_value is not None and value <= int(low_value):
473+
raise click.ClickException(
474+
'Error: Low threshold value must be less than high threshold '
475+
f'value: {low_value} for percentage type.')
441476

477+
attr = ''
478+
if ctx.obj["crm"].addr_family != None:
479+
attr += ctx.obj["crm"].addr_family + '_'
480+
attr += ctx.obj["crm"].res_type + '_' + 'low_threshold'
442481
ctx.obj["crm"].config(attr, value)
443482

444483
route.add_command(type)

tests/crm_test.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,65 @@ def test_crm_multi_asic_show_resources_srv6_nexthop(self):
19221922
assert result.exit_code == 0
19231923
assert result.output == crm_multi_asic_show_resources_srv6_nexthop
19241924

1925+
def test_crm_config_thresholds_percentage_over_100(self):
1926+
runner = CliRunner()
1927+
db = Db()
1928+
1929+
result = runner.invoke(crm.cli, ['config', 'thresholds', 'ipv4', 'route', 'high', '150'], obj=db)
1930+
print(sys.stderr, result.output)
1931+
assert result.exit_code == 1
1932+
assert "Error: Invalid value for threshold: 150. Percentage threshold must be <= 100." in result.output
1933+
1934+
result = runner.invoke(crm.cli, ['config', 'thresholds', 'ipv6', 'neighbor', 'low', '101'], obj=db)
1935+
print(sys.stderr, result.output)
1936+
assert result.exit_code == 1
1937+
assert "Error: Invalid value for threshold: 101. Percentage threshold must be <= 100." in result.output
1938+
1939+
def test_crm_config_thresholds_low_greater_than_high(self):
1940+
runner = CliRunner()
1941+
db = Db()
1942+
1943+
result = runner.invoke(crm.cli, ['config', 'thresholds', 'ipv4', 'route', 'high', '90'], obj=db)
1944+
print(sys.stderr, result.output)
1945+
assert result.exit_code == 0
1946+
1947+
result = runner.invoke(crm.cli, ['config', 'thresholds', 'ipv4', 'route', 'low', '95'], obj=db)
1948+
print(sys.stderr, result.output)
1949+
assert result.exit_code == 1
1950+
assert "Error: Invalid value for threshold: 95. Low threshold must be less than high threshold (90)." in result.output
1951+
1952+
def test_crm_config_thresholds_high_less_than_low(self):
1953+
runner = CliRunner()
1954+
db = Db()
1955+
1956+
result = runner.invoke(crm.cli, ['config', 'thresholds', 'ipv6', 'route', 'low', '50'], obj=db)
1957+
print(sys.stderr, result.output)
1958+
assert result.exit_code == 0
1959+
1960+
result = runner.invoke(crm.cli, ['config', 'thresholds', 'ipv6', 'route', 'high', '40'], obj=db)
1961+
print(sys.stderr, result.output)
1962+
assert result.exit_code == 1
1963+
assert "Error: Invalid value for threshold: 40. High threshold must be greater than low threshold (50)." in result.output
1964+
1965+
def test_crm_config_thresholds_valid_values(self):
1966+
runner = CliRunner()
1967+
db = Db()
1968+
1969+
result = runner.invoke(crm.cli, ['config', 'thresholds', 'ipv4', 'route', 'high', '100'], obj=db)
1970+
print(sys.stderr, result.output)
1971+
assert result.exit_code == 0
1972+
1973+
result = runner.invoke(crm.cli, ['config', 'thresholds', 'ipv4', 'route', 'low', '50'], obj=db)
1974+
print(sys.stderr, result.output)
1975+
assert result.exit_code == 0
1976+
1977+
result = runner.invoke(crm.cli, ['config', 'thresholds', 'ipv6', 'neighbor', 'low', '30'], obj=db)
1978+
print(sys.stderr, result.output)
1979+
assert result.exit_code == 0
1980+
1981+
result = runner.invoke(crm.cli, ['config', 'thresholds', 'ipv6', 'neighbor', 'high', '80'], obj=db)
1982+
print(sys.stderr, result.output)
1983+
assert result.exit_code == 0
19251984

19261985
@classmethod
19271986
def teardown_class(cls):

0 commit comments

Comments
 (0)