tests: Convert lots of places to use run_and_expect - #20893
Conversation
Greptile SummaryThis large PR converts ~30 topotest files from immediate state-checking (or fixed
Confidence Score: 4/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Test issues config command] --> B{Old pattern}
A --> C{New pattern}
B --> D["Direct check: result = router_json_cmp(...)"]
D --> E["assert result is None"]
E --> F["β Fails if state not yet propagated"]
B --> G["sleep(N) then check"]
G --> H["β Wastes time or still too short"]
C --> I["Wrap check in partial/lambda"]
I --> J["run_and_expect(func, expected, count, wait)"]
J --> K{result == expected?}
K -->|Yes| L["β
Return (True, result) β early exit"]
K -->|No| M["sleep(wait), retry"]
M --> N{count > 0?}
N -->|Yes| J
N -->|No| O["Return (False, result) β timeout"]
Last reviewed commit: 7c03ee6 |
| _, rc = topotest.run_and_expect( | ||
| check_p2mp_non_broadcast_config, True, count=30, wait=1 | ||
| ) |
There was a problem hiding this comment.
Wrong expected value for cmd_status return code
check_p2mp_non_broadcast_config() returns the shell exit code from grep -q: 0 when the config IS present, 1 when it is NOT. run_and_expect uses result != what to compare, and in Python 0 != True is True (they are not equal), so this first call β which verifies the config is present β will never match and will always time out after 30 seconds.
The second usage at line 270 (verifying removal) works only by coincidence: grep -q returns 1 when config is absent, and 1 == True is True in Python due to bool being a subclass of int.
The expected value should be 0 for "config present" checks and 1 for "config absent" checks:
| _, rc = topotest.run_and_expect( | |
| check_p2mp_non_broadcast_config, True, count=30, wait=1 | |
| ) | |
| _, rc = topotest.run_and_expect( | |
| check_p2mp_non_broadcast_config, 0, count=30, wait=1 | |
| ) |
And similarly at line 270, change the expected value to 1:
_, rc = topotest.run_and_expect(
check_p2mp_non_broadcast_config, 1, count=30, wait=1
)And at line 283:
_, rc = topotest.run_and_expect(
check_p2mp_non_broadcast_config, 0, count=30, wait=1
)Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/topotests/ospf_p2mp/test_ospf_p2mp_non_broadcast.py
Line: 246-248
Comment:
**Wrong expected value for `cmd_status` return code**
`check_p2mp_non_broadcast_config()` returns the shell exit code from `grep -q`: **0** when the config IS present, **1** when it is NOT. `run_and_expect` uses `result != what` to compare, and in Python `0 != True` is `True` (they are not equal), so this first call β which verifies the config **is** present β will never match and will always time out after 30 seconds.
The second usage at line 270 (verifying removal) works only by coincidence: `grep -q` returns `1` when config is absent, and `1 == True` is `True` in Python due to `bool` being a subclass of `int`.
The expected value should be `0` for "config present" checks and `1` for "config absent" checks:
```suggestion
_, rc = topotest.run_and_expect(
check_p2mp_non_broadcast_config, 0, count=30, wait=1
)
```
And similarly at line 270, change the expected value to `1`:
```python
_, rc = topotest.run_and_expect(
check_p2mp_non_broadcast_config, 1, count=30, wait=1
)
```
And at line 283:
```python
_, rc = topotest.run_and_expect(
check_p2mp_non_broadcast_config, 0, count=30, wait=1
)
```
How can I resolve this? If you propose a fix, please make it concise.
riw777
left a comment
There was a problem hiding this comment.
looks good ... greptile's comments look valid, though (?)
7c03ee6 to
8750cb5
Compare
There are a large number of places where the topotests are changing state via some sort of command and then immediately checking to see if that state is reflected, without giving time for that command to be processed. Modify these places that have been identified and switch the resulting show commands( of what ever ilk ) to use run_and_expect. Signed-off-by: Donald Sharp <sharpd@nvidia.com>
8750cb5 to
e6ceee4
Compare
There are a large number of places where the topotests are changing state via some sort of command and then immediately checking to see if that state is reflected, without giving time for that command to be processed. Modify these places that have been identified and switch the resulting show commands( of what ever ilk ) to use run_and_expect.