Skip to content

Commit 8729283

Browse files
committed
Add more tests + introduce the option to pass the DB to crm main
1 parent 98000a4 commit 8729283

File tree

2 files changed

+430
-23
lines changed

2 files changed

+430
-23
lines changed

crm/main.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,35 @@
77
from sonic_py_common import multi_asic
88

99
class Crm:
10-
def __init__(self):
10+
def __init__(self, db=None):
1111
self.cli_mode = None
1212
self.addr_family = None
1313
self.res_type = None
1414
self.db = None
15-
self.config_db = None
15+
self.cfgdb = db
1616
self.multi_asic = multi_asic_util.MultiAsic()
1717

1818
@multi_asic_util.run_on_multi_asic
1919
def config(self, attr, val):
2020
"""
2121
CRM handler for 'config' CLI commands.
2222
"""
23+
if self.cfgdb:
24+
self.config_db = self.cfgdb
2325
self.config_db.mod_entry("CRM", 'Config', {attr: val})
2426

2527
def show_summary(self):
2628
"""
2729
CRM Handler to display general information.
2830
"""
2931

30-
# Get the namespace list
31-
namespaces = multi_asic.get_namespace_list()
32+
configdb = self.cfgdb
33+
if configdb is None:
34+
# Get the namespace list
35+
namespaces = multi_asic.get_namespace_list()
3236

33-
configdb = swsssdk.ConfigDBConnector(namespace=namespaces[0])
34-
configdb.connect()
37+
configdb = swsssdk.ConfigDBConnector(namespace=namespaces[0])
38+
configdb.connect()
3539

3640
crm_info = configdb.get_entry('CRM', 'Config')
3741

@@ -45,11 +49,13 @@ def show_thresholds(self, resource):
4549
CRM Handler to display thresholds information.
4650
"""
4751

48-
# Get the namespace list
49-
namespaces = multi_asic.get_namespace_list()
52+
configdb = self.cfgdb
53+
if configdb is None:
54+
# Get the namespace list
55+
namespaces = multi_asic.get_namespace_list()
5056

51-
configdb = swsssdk.ConfigDBConnector(namespace=namespaces[0])
52-
configdb.connect()
57+
configdb = swsssdk.ConfigDBConnector(namespace=namespaces[0])
58+
configdb.connect()
5359

5460
crm_info = configdb.get_entry('CRM', 'Config')
5561

@@ -190,8 +196,11 @@ def cli(ctx):
190196
"""
191197
Utility entry point.
192198
"""
199+
# Use the db object if given as input.
200+
db = None if ctx.obj is None else ctx.obj.cfgdb
201+
193202
context = {
194-
"crm": Crm()
203+
"crm": Crm(db)
195204
}
196205

197206
ctx.obj = context

0 commit comments

Comments
 (0)