7
7
import generic_config_updater .gu_common
8
8
9
9
10
+ def mock_get_running_config_side_effect (scope ):
11
+ print (f"mocked_value_for_{ scope } " )
12
+ return {
13
+ "tables" : {
14
+ "ACL_TABLE" : {
15
+ "services_to_validate" : ["aclservice" ],
16
+ "validate_commands" : ["acl_loader show table" ]
17
+ },
18
+ "PORT" : {
19
+ "services_to_validate" : ["portservice" ],
20
+ "validate_commands" : ["show interfaces status" ]
21
+ }
22
+ },
23
+ "services" : {
24
+ "aclservice" : {
25
+ "validate_commands" : ["acl_loader show table" ]
26
+ },
27
+ "portservice" : {
28
+ "validate_commands" : ["show interfaces status" ]
29
+ }
30
+ }
31
+ }
32
+
33
+
10
34
class TestMultiAsicChangeApplier (unittest .TestCase ):
11
35
12
36
@patch ('sonic_py_common.multi_asic.is_multi_asic' )
@@ -137,34 +161,15 @@ def test_extract_scope_singleasic(self, mock_is_multi_asic):
137
161
except Exception :
138
162
assert (not result )
139
163
140
- @patch ('generic_config_updater.change_applier.ChangeApplier._get_running_config ' , autospec = True )
164
+ @patch ('generic_config_updater.change_applier.get_config_db_as_json ' , autospec = True )
141
165
@patch ('generic_config_updater.change_applier.ConfigDBConnector' , autospec = True )
142
166
def test_apply_change_default_scope (self , mock_ConfigDBConnector , mock_get_running_config ):
143
167
# Setup mock for ConfigDBConnector
144
168
mock_db = MagicMock ()
145
169
mock_ConfigDBConnector .return_value = mock_db
146
170
147
171
# Setup mock for json.load to return some running configuration
148
- mock_get_running_config .return_value = {
149
- "tables" : {
150
- "ACL_TABLE" : {
151
- "services_to_validate" : ["aclservice" ],
152
- "validate_commands" : ["acl_loader show table" ]
153
- },
154
- "PORT" : {
155
- "services_to_validate" : ["portservice" ],
156
- "validate_commands" : ["show interfaces status" ]
157
- }
158
- },
159
- "services" : {
160
- "aclservice" : {
161
- "validate_commands" : ["acl_loader show table" ]
162
- },
163
- "portservice" : {
164
- "validate_commands" : ["show interfaces status" ]
165
- }
166
- }
167
- }
172
+ mock_get_running_config .side_effect = mock_get_running_config_side_effect
168
173
169
174
# Instantiate ChangeApplier with the default scope
170
175
applier = generic_config_updater .change_applier .ChangeApplier ()
@@ -178,34 +183,13 @@ def test_apply_change_default_scope(self, mock_ConfigDBConnector, mock_get_runni
178
183
# Assert ConfigDBConnector called with the correct namespace
179
184
mock_ConfigDBConnector .assert_called_once_with (use_unix_socket_path = True , namespace = "" )
180
185
181
- @patch ('generic_config_updater.change_applier.ChangeApplier._get_running_config ' , autospec = True )
186
+ @patch ('generic_config_updater.change_applier.get_config_db_as_json ' , autospec = True )
182
187
@patch ('generic_config_updater.change_applier.ConfigDBConnector' , autospec = True )
183
188
def test_apply_change_given_scope (self , mock_ConfigDBConnector , mock_get_running_config ):
184
189
# Setup mock for ConfigDBConnector
185
190
mock_db = MagicMock ()
186
191
mock_ConfigDBConnector .return_value = mock_db
187
-
188
- # Setup mock for json.load to return some running configuration
189
- mock_get_running_config .return_value = {
190
- "tables" : {
191
- "ACL_TABLE" : {
192
- "services_to_validate" : ["aclservice" ],
193
- "validate_commands" : ["acl_loader show table" ]
194
- },
195
- "PORT" : {
196
- "services_to_validate" : ["portservice" ],
197
- "validate_commands" : ["show interfaces status" ]
198
- }
199
- },
200
- "services" : {
201
- "aclservice" : {
202
- "validate_commands" : ["acl_loader show table" ]
203
- },
204
- "portservice" : {
205
- "validate_commands" : ["show interfaces status" ]
206
- }
207
- }
208
- }
192
+ mock_get_running_config .side_effect = mock_get_running_config_side_effect
209
193
210
194
# Instantiate ChangeApplier with the default scope
211
195
applier = generic_config_updater .change_applier .ChangeApplier (scope = "asic0" )
@@ -219,7 +203,7 @@ def test_apply_change_given_scope(self, mock_ConfigDBConnector, mock_get_running
219
203
# Assert ConfigDBConnector called with the correct scope
220
204
mock_ConfigDBConnector .assert_called_once_with (use_unix_socket_path = True , namespace = "asic0" )
221
205
222
- @patch ('generic_config_updater.change_applier.ChangeApplier._get_running_config ' , autospec = True )
206
+ @patch ('generic_config_updater.change_applier.get_config_db_as_json ' , autospec = True )
223
207
@patch ('generic_config_updater.change_applier.ConfigDBConnector' , autospec = True )
224
208
def test_apply_change_failure (self , mock_ConfigDBConnector , mock_get_running_config ):
225
209
# Setup mock for ConfigDBConnector
@@ -241,22 +225,25 @@ def test_apply_change_failure(self, mock_ConfigDBConnector, mock_get_running_con
241
225
242
226
self .assertTrue ('Failed to get running config' in str (context .exception ))
243
227
244
- @patch ('generic_config_updater.change_applier.ChangeApplier._get_running_config ' , autospec = True )
228
+ @patch ('generic_config_updater.change_applier.get_config_db_as_json ' , autospec = True )
245
229
@patch ('generic_config_updater.change_applier.ConfigDBConnector' , autospec = True )
246
230
def test_apply_patch_with_empty_tables_failure (self , mock_ConfigDBConnector , mock_get_running_config ):
247
231
# Setup mock for ConfigDBConnector
248
232
mock_db = MagicMock ()
249
233
mock_ConfigDBConnector .return_value = mock_db
250
234
251
235
# Setup mock for json.load to simulate configuration where crucial tables are unexpectedly empty
252
- mock_get_running_config .return_value = {
253
- "tables" : {
254
- # Simulate empty tables or missing crucial configuration
255
- },
256
- "services" : {
257
- # Normally, services would be listed here
236
+ def mock_get_empty_running_config_side_effect ():
237
+ return {
238
+ "tables" : {
239
+ # Simulate empty tables or missing crucial configuration
240
+ },
241
+ "services" : {
242
+ # Normally, services would be listed here
243
+ }
258
244
}
259
- }
245
+
246
+ mock_get_running_config .side_effect = mock_get_empty_running_config_side_effect
260
247
261
248
# Instantiate ChangeApplier with a specific scope to simulate applying changes in a multi-asic environment
262
249
applier = generic_config_updater .change_applier .ChangeApplier (scope = "asic0" )
0 commit comments