Skip to content

Commit 53ee2e3

Browse files
authored
[dvslib] Improve support for assert logging (#1258)
Signed-off-by: Danny Allen <[email protected]>
1 parent 25097d2 commit 53ee2e3

File tree

2 files changed

+109
-17
lines changed

2 files changed

+109
-17
lines changed

tests/dvslib/dvs_common.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,26 @@ def wait_for_result(polling_function, polling_config):
3737
not, as well as some return value.
3838
3939
Returns:
40-
Any: The output of the polling function, if it is succesful,
41-
None otherwise.
40+
(bool, Any): If the polling function succeeds, then this method
41+
will return True and the output of the polling function. If it
42+
does not succeed within the provided timeout, it will return False
43+
and whatever the output of the polling function was on the final
44+
attempt.
4245
"""
4346
if polling_config.polling_interval == 0:
4447
iterations = 1
4548
else:
4649
iterations = int(polling_config.timeout // polling_config.polling_interval) + 1
4750

4851
for _ in range(iterations):
49-
(status, result) = polling_function()
52+
status, result = polling_function()
5053

5154
if status:
52-
return result
55+
return (True, result)
5356

5457
time.sleep(polling_config.polling_interval)
5558

5659
if polling_config.strict:
57-
assert False
60+
assert False, "Operation timed out after {}s".format(polling_config.timeout)
5861

59-
return None
62+
return (False, result)

tests/dvslib/dvs_database.py

+100-11
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,14 @@ def _access_function():
119119
fv_pairs = self.get_entry(table_name, key)
120120
return (bool(fv_pairs), fv_pairs)
121121

122-
return wait_for_result(_access_function, polling_config)
122+
status, result = wait_for_result(_access_function,
123+
self._disable_strict_polling(polling_config))
124+
125+
if not status:
126+
assert not polling_config.strict, \
127+
"Entry not found: key=\"{}\", table=\"{}\"".format(key, table_name)
128+
129+
return result
123130

124131
def wait_for_field_match(self,
125132
table_name,
@@ -131,9 +138,6 @@ def wait_for_field_match(self,
131138
at `key` in the specified table. This method will wait for the
132139
fields to exist.
133140
134-
Note:
135-
This method does not check for an exact match.
136-
137141
Args:
138142
table_name (str): The name of the table where the entry is
139143
stored.
@@ -152,12 +156,57 @@ def _access_function():
152156
fv_pairs = self.get_entry(table_name, key)
153157
return (all(fv_pairs.get(k) == v for k, v in expected_fields.items()), fv_pairs)
154158

155-
return wait_for_result(_access_function, polling_config)
159+
status, result = wait_for_result(_access_function,
160+
self._disable_strict_polling(polling_config))
161+
162+
if not status:
163+
assert not polling_config.strict, \
164+
"Expected fields not found: expected={}, received={}, \
165+
key=\"{}\", table=\"{}\"".format(expected_fields, result, key, table_name)
156166

157-
def wait_for_empty_entry(self,
167+
return result
168+
169+
def wait_for_exact_match(self,
158170
table_name,
159171
key,
172+
expected_entry,
160173
polling_config=DEFAULT_POLLING_CONFIG):
174+
"""
175+
Checks if the provided entry matches the entry stored at `key`
176+
in the specified table. This method will wait for the exact entry
177+
to exist.
178+
179+
Args:
180+
table_name (str): The name of the table where the entry is
181+
stored.
182+
key (str): The key that maps to the entry being checked.
183+
expected_entry (dict): The entry we expect to see.
184+
polling_config (PollingConfig): The parameters to use to poll
185+
the db.
186+
187+
Returns:
188+
Dict[str, str]: The entry stored at `key`. If no entry is found,
189+
then an empty Dict will be returned.
190+
"""
191+
192+
def _access_function():
193+
fv_pairs = self.get_entry(table_name, key)
194+
return (fv_pairs == expected_entry, fv_pairs)
195+
196+
status, result = wait_for_result(_access_function,
197+
self._disable_strict_polling(polling_config))
198+
199+
if not status:
200+
assert not polling_config.strict, \
201+
"Exact match not found: expected={}, received={}, \
202+
key=\"{}\", table=\"{}\"".format(expected_entry, result, key, table_name)
203+
204+
return result
205+
206+
def wait_for_deleted_entry(self,
207+
table_name,
208+
key,
209+
polling_config=DEFAULT_POLLING_CONFIG):
161210
"""
162211
Checks if there is any entry stored at `key` in the specified
163212
table. This method will wait for the entry to be empty.
@@ -169,14 +218,23 @@ def wait_for_empty_entry(self,
169218
the db.
170219
171220
Returns:
172-
bool: True if no entry exists at `key`, False otherwise.
221+
Dict[str, str]: The entry stored at `key`. If no entry is found,
222+
then an empty Dict will be returned.
173223
"""
174224

175225
def _access_function():
176226
fv_pairs = self.get_entry(table_name, key)
177227
return (not bool(fv_pairs), fv_pairs)
178228

179-
return wait_for_result(_access_function, polling_config)
229+
status, result = wait_for_result(_access_function,
230+
self._disable_strict_polling(polling_config))
231+
232+
if not status:
233+
assert not polling_config.strict, \
234+
"Entry still exists: entry={}, key=\"{}\", table=\"{}\""\
235+
.format(result, key, table_name)
236+
237+
return result
180238

181239
def wait_for_n_keys(self,
182240
table_name,
@@ -203,7 +261,15 @@ def _access_function():
203261
keys = self.get_keys(table_name)
204262
return (len(keys) == num_keys, keys)
205263

206-
return wait_for_result(_access_function, polling_config)
264+
status, result = wait_for_result(_access_function,
265+
self._disable_strict_polling(polling_config))
266+
267+
if not status:
268+
assert not polling_config.strict, \
269+
"Unexpected number of keys: expected={}, received={} ({}), table=\"{}\""\
270+
.format(num_keys, len(result), result, table_name)
271+
272+
return result
207273

208274
def wait_for_matching_keys(self,
209275
table_name,
@@ -230,7 +296,15 @@ def _access_function():
230296
keys = self.get_keys(table_name)
231297
return (all(key in keys for key in expected_keys), keys)
232298

233-
return wait_for_result(_access_function, polling_config)
299+
status, result = wait_for_result(_access_function,
300+
self._disable_strict_polling(polling_config))
301+
302+
if not status:
303+
assert not polling_config.strict, \
304+
"Expected keys not found: expected={}, received={}, table=\"{}\""\
305+
.format(expected_keys, result, table_name)
306+
307+
return result
234308

235309
def wait_for_deleted_keys(self,
236310
table_name,
@@ -257,4 +331,19 @@ def _access_function():
257331
keys = self.get_keys(table_name)
258332
return (all(key not in keys for key in deleted_keys), keys)
259333

260-
return wait_for_result(_access_function, polling_config)
334+
status, result = wait_for_result(_access_function,
335+
self._disable_strict_polling(polling_config))
336+
337+
if not status:
338+
assert not polling_config.strict, \
339+
"Unexpected keys found: expected={}, received={}, table=\"{}\""\
340+
.format(deleted_keys, result, table_name)
341+
342+
return result
343+
344+
@staticmethod
345+
def _disable_strict_polling(polling_config):
346+
disabled_config = PollingConfig(polling_interval=polling_config.polling_interval,
347+
timeout=polling_config.timeout,
348+
strict=False)
349+
return disabled_config

0 commit comments

Comments
 (0)