1
1
import os
2
2
import json
3
3
import filecmp
4
+ import importlib
4
5
import config .main as config
5
6
6
7
from click .testing import CliRunner
20
21
RUNNING_CONFIG_YANG_FAILURE = os .path .join (DATA_DIR , "running_config_yang_failure.json" )
21
22
GOLDEN_INPUT_YANG_FAILURE = os .path .join (DATA_DIR , "golden_input_yang_failure.json" )
22
23
FINAL_CONFIG_YANG_FAILURE = os .path .join (DATA_DIR , "final_config_yang_failure.json" )
24
+ MULTI_ASIC_MACSEC_OV = os .path .join (DATA_DIR , "multi_asic_macsec_ov.json" )
25
+ MULTI_ASIC_DEVICE_METADATA_RM = os .path .join (DATA_DIR , "multi_asic_dm_rm.json" )
23
26
24
27
# Load sonic-cfggen from source since /usr/local/bin/sonic-cfggen does not have .py extension.
25
28
sonic_cfggen = load_module_from_source ('sonic_cfggen' , '/usr/local/bin/sonic-cfggen' )
@@ -173,7 +176,7 @@ def test_yang_verification_enabled(self):
173
176
def is_yang_config_validation_enabled_side_effect (filename ):
174
177
return True
175
178
176
- def config_mgmt_side_effect ():
179
+ def config_mgmt_side_effect (configdb ):
177
180
return config_mgmt .ConfigMgmt (source = CONFIG_DB_JSON_FILE )
178
181
179
182
db = Db ()
@@ -232,7 +235,7 @@ def check_yang_verification_failure(self, db, config, running_config,
232
235
def read_json_file_side_effect (filename ):
233
236
return golden_config
234
237
235
- def config_mgmt_side_effect ():
238
+ def config_mgmt_side_effect (configdb ):
236
239
return config_mgmt .ConfigMgmt (source = CONFIG_DB_JSON_FILE )
237
240
238
241
# ConfigMgmt will call ConfigDBConnector to load default config_db.json.
@@ -257,3 +260,73 @@ def teardown_class(cls):
257
260
print ("TEARDOWN" )
258
261
os .environ ["UTILITIES_UNIT_TESTING" ] = "0"
259
262
return
263
+
264
+
265
+ class TestConfigOverrideMultiasic (object ):
266
+ @classmethod
267
+ def setup_class (cls ):
268
+ print ("SETUP" )
269
+ os .environ ["UTILITIES_UNIT_TESTING" ] = "1"
270
+ os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] = "multi_asic"
271
+ # change to multi asic config
272
+ from .mock_tables import dbconnector
273
+ from .mock_tables import mock_multi_asic
274
+ importlib .reload (mock_multi_asic )
275
+ dbconnector .load_namespace_config ()
276
+ return
277
+
278
+ def test_macsec_override (self ):
279
+ def read_json_file_side_effect (filename ):
280
+ with open (MULTI_ASIC_MACSEC_OV , "r" ) as f :
281
+ macsec_profile = json .load (f )
282
+ return macsec_profile
283
+ db = Db ()
284
+ cfgdb_clients = db .cfgdb_clients
285
+
286
+ # The profile_content was copied from MULTI_ASIC_MACSEC_OV, where all
287
+ # ns sharing the same content: {"profile": {"key": "value"}}
288
+ profile_content = {"profile" : {"key" : "value" }}
289
+
290
+ with mock .patch ('config.main.read_json_file' ,
291
+ mock .MagicMock (side_effect = read_json_file_side_effect )):
292
+ runner = CliRunner ()
293
+ result = runner .invoke (config .config .commands ["override-config-table" ],
294
+ ['golden_config_db.json' ], obj = db )
295
+ assert result .exit_code == 0
296
+
297
+ for ns , config_db in cfgdb_clients .items ():
298
+ assert config_db .get_config ()['MACSEC_PROFILE' ] == profile_content
299
+
300
+ def test_device_metadata_table_rm (self ):
301
+ def read_json_file_side_effect (filename ):
302
+ with open (MULTI_ASIC_DEVICE_METADATA_RM , "r" ) as f :
303
+ device_metadata = json .load (f )
304
+ return device_metadata
305
+ db = Db ()
306
+ cfgdb_clients = db .cfgdb_clients
307
+
308
+ for ns , config_db in cfgdb_clients .items ():
309
+ assert 'DEVICE_METADATA' in config_db .get_config ()
310
+
311
+ with mock .patch ('config.main.read_json_file' ,
312
+ mock .MagicMock (side_effect = read_json_file_side_effect )):
313
+ runner = CliRunner ()
314
+ result = runner .invoke (config .config .commands ["override-config-table" ],
315
+ ['golden_config_db.json' ], obj = db )
316
+ assert result .exit_code == 0
317
+
318
+ for ns , config_db in cfgdb_clients .items ():
319
+ assert 'DEVICE_METADATA' not in config_db .get_config ()
320
+
321
+
322
+ @classmethod
323
+ def teardown_class (cls ):
324
+ print ("TEARDOWN" )
325
+ os .environ ["UTILITIES_UNIT_TESTING" ] = "0"
326
+ os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] = ""
327
+ # change back to single asic config
328
+ from .mock_tables import dbconnector
329
+ from .mock_tables import mock_single_asic
330
+ importlib .reload (mock_single_asic )
331
+ dbconnector .load_namespace_config ()
332
+ return
0 commit comments