pillar: add unit tests for NIM-related packages#5901
pillar: add unit tests for NIM-related packages#5901eriknordmark wants to merge 6 commits intolf-edge:masterfrom
Conversation
Add Go unit tests for three dpcreconciler item types that previously had no coverage: - genericitems/wpa_supplicant_test.go: tests for WpaSupplicant metadata methods, renderWifiConfig, renderPNACConfig, and all WifiConfig/PNAC8021XConfig helpers. Covers 12 new functions. - linuxitems/bond_vlan_test.go: tests for Bond and Vlan metadata methods, Equal, GetMTU, NeedsRecreate, Dependencies, and the isFailoverBondMode helper. Covers 10 new functions. Statement coverage improvements: genericitems 7.1%→16.9% (+9.8pp), linuxitems 0.0%→6.5% (+6.5pp). Signed-off-by: eriknordmark <erik@zededa.com> Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
| {SSID: "HomeNet", KeyScheme: types.KeySchemeWpaPsk}, | ||
| }, | ||
| } | ||
| s := wifi.String() |
There was a problem hiding this comment.
The String() method is really only used for logging purposes.
Not sure if unit test is really valuable here (aside from increasing code coverage).
Every time we change some of these non-crucial methods (e.g. to change the logging output), we will have to fix potentially multiple unit tests.
| } | ||
| ethAdapter2 := mockAdapter{ifName: "eth0", wType: types.WirelessTypeNone} | ||
| wifiAdapter2 := mockAdapter{ifName: "eth0", wType: types.WirelessTypeWifi} | ||
| if !pnacDeps[0].MustSatisfy(ethAdapter2) { |
There was a problem hiding this comment.
So even if we just change the order of dependencies (which does not matter), we will have to fix these slice indices in unit tests..
Add Go unit tests for six genericitems types (AdapterAddrs, Dhcpcd metadata, NetIO, ResolvConf, SSHAuthKeys, Wwan), six linuxitems types (Adapter, Arp, IPRule, PhysIf, RFKill, Route), and netmonitor pure-logic methods (Route.IsDefaultRoute, IfAttrs.Equal, IfChange.Equal, isNetworkEvent markers). Coverage improvements: genericitems 16.9% -> 28.2% (+11.3 pp) linuxitems 6.5% -> 24.5% (+18.0 pp) netmonitor 6.2% -> 7.0% (+0.8 pp) All tests are pure Go with no OS or hardware dependencies. Signed-off-by: eriknordmark <erik@zededa.com> Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
FWIW, I have prepared proper bond and vlan integration tests using my new test framework, which I will present next week. |
|
These coverage-focused AI-generated tests remind of the infamous config properties count-check: https://github.com/lf-edge/eve/blob/master/pkg/pillar/types/global_test.go#L146-L262 |
Add white-box unit tests for all depgraph Item types in nireconciler/genericitems (Dnsmasq, HTTPServer, IPReserve, Port, Radvd) and nireconciler/linuxitems (BPDUGuard, BridgeFwdMask, Bridge, BridgePort, DummyIf, IPRule, IPSet, Route, Sysctl, TCIngress, TCMirror, VIF, VLANBridge, VLANPort, VLANSubIf). Coverage: genericitems 16.7%→32.9%, linuxitems 0%→26.6%. Signed-off-by: eriknordmark <erik@zededa.com> Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Allow parseProcBondInfo and parseProcBondMemberMetrics to accept a bondingDir parameter instead of hardcoding /proc/net/bonding, enabling unit tests to supply a temp directory with synthetic bond files. Signed-off-by: eriknordmark <erik@zededa.com> Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Add unit tests for parseChurnState, isDhcpcdNotRunningErr, parseProcBondInfo, and parseProcBondMemberMetrics. The first two are pure functions; the latter two write synthetic /proc/net/bonding files to t.TempDir() and pass the temp directory via the newly injected bondingDir parameter. Signed-off-by: eriknordmark <erik@zededa.com> Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
That was actually going to be my question to you, but you answered it before I could ask it. FWIW I have another thing to explore which is to produce eden tests which drive nim, including the startup code and handling of /config/ input. But if I do this to drive coverage it might not be that useful - checking for the semantics is more important. Working on that a bit for zedagent. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5901 +/- ##
==========================================
- Coverage 19.52% 18.90% -0.63%
==========================================
Files 19 474 +455
Lines 3021 85692 +82671
==========================================
+ Hits 590 16196 +15606
- Misses 2310 67955 +65645
- Partials 121 1541 +1420 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Two issues flagged by yetus: 1. revive: rename `new` variable in TestWpaSupplicantNeedsRecreate to `updated` to avoid shadowing the built-in function. 2. codespell: add `nd` to .codespellrc ignore-words-list. The netmonitor linux.go uses `nd` as the dhcpcd neighbor-discovery key prefix (e.g. nd0_prefix_information0_prefix); it is not a misspelling but codespell flags it as "and" or "2nd". Signed-off-by: eriknordmark <erik@zededa.com> Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Description
Commits 1–3 and 5 are test-only. The only production code change is commit 4
(c1dd0c9), which is a mechanical refactor to enable testing.
Add Go unit tests for NIM-related packages in
pkg/pillarthat previouslyhad no or minimal test coverage. Five commits, covering five packages:
Commit 1 (487661e) —
dpcreconcilerwpa/bond/vlan items:genericitems/wpa_supplicant_test.go:WpaSupplicantmetadata,renderWifiConfig,renderPNACConfig, path helpers,WifiConfig/PNAC8021XConfighelperslinuxitems/bond_vlan_test.go:Bond,Vlan,isFailoverBondMode,GetMTU,NeedsRecreate,DependenciesCommit 2 (70a6a51) — remaining
dpcreconcilerandnetmonitoritems:genericitems/items_test.go:AdapterAddrs,Dhcpcd,NetIO,ResolvConf,SSHAuthKeys,Wwanlinuxitems/items_test.go:Adapter,Arp,IPRule,PhysIf,RFKill,Routenetmonitor/netmonitor_test.go:IsDefaultRoute,IfAttrs.Equal,IfChange.Equal, event marker methodsCommit 3 (573e688) —
nireconcileritem types:nireconciler/genericitems/items_test.go:Dnsmasq,HTTPServer,IPReserve,Port,Radvdplus helpers (equalUpstreamDNSServer,equalMACToIP,EqualIPRoutes,isECONNREFUSED, path helpers,dhcpTagForHost)nireconciler/linuxitems/items_test.go:BPDUGuard,BridgeFwdMask,Bridge,BridgePort,DummyIf,IPRule,IPSet,Sysctl,TCIngress,TCMirror,VIF,VLANBridge,VLANPort,VLANSubIf,Routeplus helpers (toMaskValue,equalBoolPtr,boolPtrToString,ipVersionStr,hasDefaultDst)Commit 4 (c1dd0c9) —
netmonitorproc parser refactor (minimal production change):/proc/net/bondingpath into aprocNetBondingDirconstant and add it as a parameter toparseProcBondInfoandparseProcBondMemberMetrics, making them unit-testable without/proc.Commit 5 (075f5f5) —
netmonitorproc bond parser tests:netmonitor/netmonitor_test.go:parseChurnState,isDhcpcdNotRunningErr,parseProcBondInfo,parseProcBondMemberMetricsusing synthetic temp-dir bond files.Statement coverage improvements:
dpcreconciler/genericitems: 7.1% → ~30%dpcreconciler/linuxitems: 0.0% → ~20%netmonitor: 0% → ~15%nireconciler/genericitems: 16.7% → 32.9%nireconciler/linuxitems: 0.0% → 26.6%All new unit tests have no OS or hardware dependencies.
PR dependencies
None.
How to test and validate this PR
All packages pass all tests.
Changelog notes
No user-facing changes.
PR Backports
Checklist