Commit cfc954b
authored
[passw_hardening] Snapshot/restore policies in teardown to avoid spurious config_reload (sonic-net#25116)
### Description of PR
The `clean_passw_policies` teardown in `tests/passw_hardening` reset the
password hardening policies to a set of hard-coded &sonic-net#34;default&sonic-net#34;
values. Whenever those hard-coded values drifted from the real SONiC
boot defaults (defined in `init_cfg.json.j2`), the module-scoped config
check detected a `CONFIG_DB` diff and ran `config_reload`. That
`config_reload` restarts BGP, which produces spurious `bgpd memory
increased` alarms on the **next, unrelated test** (e.g.
`test_snmp_memory`), causing flaky failures.
This PR replaces the hard-coded reset with a **snapshot/restore**
approach:
- `get_passw_policies()` captures the DUT&sonic-net#39;s actual
`PASSW_HARDENING|POLICIES` values once per module.
- `restore_passw_policies()` in teardown re-applies **only the fields
that changed** during the test.
A test that does not touch the policies now issues **zero** CLI commands
in teardown, so no `CONFIG_DB` diff is created and no `config_reload` is
triggered.
Summary:
Fixes spurious `test_snmp_memory` (and other downstream) failures caused
by password hardening teardown triggering `config_reload`.
ADO: https://msazure.visualstudio.com/One/_workitems/edit/38230412
### Type of change
- [x] Bug fix
- [ ] Testbed and Framework(new/improvement)
- [ ] New Test case
- [ ] Skipped for non-supported platforms
- [x] Test case improvement
### Back port request
- [ ] 202205
- [ ] 202305
- [ ] 202311
- [ ] 202405
- [ ] 202411
- [ ] 202505
- [ ] 202511
### Approach
#### What is the motivation for this PR?
`test_snmp_memory` (and other tests) intermittently fail with `bgpd
memory increased` alarms. Root cause: the password hardening test
teardown resets policies to hard-coded &sonic-net#34;default&sonic-net#34; values that
drift from the image&sonic-net#39;s real boot defaults. The resulting `CONFIG_DB`
diff triggers `config_reload` → BGP restart → memory alarm on the
subsequent test.
#### How did you do it?
- Added `get_passw_policies(duthost)` to snapshot the live
`PASSW_HARDENING|POLICIES` hash from `CONFIG_DB` (parsed with
`ast.literal_eval`; returns `None` and logs a warning on read/parse
failure).
- Added `restore_passw_policies(duthost, snapshot)` which reads the live
state at teardown and re-applies only the fields whose value differs
from the snapshot. `state` is applied last. If the live state cannot be
read, it conservatively restores every snapshot field. CONFIG_DB field
names (underscores) are mapped to the `config passw-hardening policies`
CLI subcommands (hyphens) via `field.replace(&sonic-net#39;_&sonic-net#39;, &sonic-net#39;-&sonic-net#39;)`,
removing the previous hand-maintained key map.
- Replaced the module fixture so it snapshots the actual values
(`passw_policies_snapshot`); `clean_passw_policies` now restores from
that snapshot.
#### How did you verify/test it?
- Verified `sonic-db-cli CONFIG_DB hgetall
&sonic-net#34;PASSW_HARDENING|POLICIES&sonic-net#34;` output and `ast.literal_eval`
parsing on a live DUT (Arista-7050CX3).
- Confirmed all 10 DB fields map 1:1 to `config passw-hardening
policies` CLI subcommands via pure `_`→`-` transform.
- Unit-simulated the restore diff logic: unchanged → 0 commands; changed
fields → only those re-applied with `state` last; live-read `None` →
restore all; snapshot `None` → skip.
- flake8 (max-line-length=120) clean; `py_compile` passes.
#### Any platform specific information?
None. The fix is platform-agnostic.
#### Supported testbed topology if it&sonic-net#39;s a new test case?
N/A — existing test improvement.
### Documentation
N/A
### Elastic Test Jobs
- testbed-bjw3-can-7050c-11:
https://elastictest.org/scheduler/testplan/6a28e79d2047c3c4a9f924b1
Signed-off-by: Liping Xu <108326363+lipxu@users.noreply.github.com>1 parent 5e48416 commit cfc954b
2 files changed
Lines changed: 68 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
7 | 18 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
| 19 | + | |
22 | 20 | | |
23 | 21 | | |
24 | 22 | | |
| |||
36 | 34 | | |
37 | 35 | | |
38 | 36 | | |
39 | | - | |
| 37 | + | |
40 | 38 | | |
41 | | - | |
| 39 | + | |
| 40 | + | |
42 | 41 | | |
43 | 42 | | |
44 | 43 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
53 | 105 | | |
54 | 106 | | |
55 | 107 | | |
| |||
0 commit comments