Skip to content

controller/network: add unit tests for network controller#2647

Open
shreyanshjain7174 wants to merge 2 commits intomicrosoft:mainfrom
shreyanshjain7174:controller/network-tests
Open

controller/network: add unit tests for network controller#2647
shreyanshjain7174 wants to merge 2 commits intomicrosoft:mainfrom
shreyanshjain7174:controller/network-tests

Conversation

@shreyanshjain7174
Copy link
Copy Markdown
Contributor

@shreyanshjain7174 shreyanshjain7174 commented Mar 25, 2026

Depends on #2633 — must merge first.

Adds unit tests for the network controller (internal/controller/network/). 22 tests across LCOW and WCOW, biased toward resource cleanup chains and state corruption rather than guard-check permutations.

Teardown: continue-on-error when one NIC removal fails (remaining NICs still attempted, failed NICs stay tracked for retry), retry from Invalid state, idempotent when TornDown, no-op when NotConfigured, full Configured→TornDown lifecycle. Setup rejects duplicate calls.

LCOW endpoint ops: host AddNIC before guest AddLCOWNetworkInterface (ordering enforced via gomock.InOrder), BuildLCOWNetworkAdapter struct verified, host-fail→not tracked, guest-fail→still tracked for Teardown cleanup, guest-remove-fails→host RemoveNIC not called, nil capabilities disables guest ops.

WCOW endpoint ops: 3-phase add (PreAdd → host AddNIC → guest Add), PreAdd failure→not tracked, guest-finalize failure→still tracked, remove ordering (guest before host), namespace add with/without guest support.

Mocks generated for vmNetworkManager, linuxGuestNetworkManager, windowsGuestNetworkManager, and capabilitiesProvider. GuestDefinedCapabilities mock reused from internal/gcs/mock/.

Copy link
Copy Markdown
Contributor

@rawahars rawahars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the tests. There are various scenarios which are missing such as-

  • Half setup and then teardown
  • Half setup, failure in teardown and then success teardown
  • Teardown called again after Teardown
  • Setup called again after Setup

Comment thread internal/controller/network/network_lcow.go Outdated
Comment thread internal/controller/network/network_lcow_test.go Outdated
Comment thread internal/controller/network/network_lcow_test.go Outdated
Comment thread internal/controller/network/network_test.go Outdated
Comment thread internal/controller/network/network_wcow.go Outdated
Comment thread internal/controller/network/network_wcow_test.go Outdated
Comment thread internal/controller/network/network_wcow_test.go
Comment thread internal/controller/network/state_test.go Outdated
@shreyanshjain7174
Copy link
Copy Markdown
Contributor Author

@rawahars all 8 review items addressed in 91976f0 (force-pushed). Summary:

Removed (per "use existing repo convention"):

  • //go:generate directive in network_lcow.go and network_wcow.go
  • state_test.go (over-testing)
  • containerd may retry RunPodSandbox comment in network_test.go

Fixed:

  • "Map iteration order" comment now describes what the test actually asserts.

Added (the 4 missing teardown-after-failure scenarios you flagged):

  • TestLCOW_AddEndpoint_HostOK_GuestFails_TeardownUnwindsHost
  • TestLCOW_Teardown_GuestFails_RetryFromInvalid
  • TestWCOW_AddEndpoint_FinalAddFails_TeardownUnwindsHost
  • TestWCOW_Teardown_GuestFails_RetryFromInvalid

Filed #2707 to track the go:generate standardisation + CI drift validation across all controllers.

All checks: gofmt clean, go vet clean, golangci-lint --build-tags "windows lcow" and ... wcow both 0 issues, all tests pass. Test count: 18 LCOW + 16 WCOW.

Comment thread internal/controller/network/network_lcow_test.go Outdated
Comment thread internal/controller/network/network_lcow_test.go Outdated
Comment thread internal/controller/network/network_lcow_test.go Outdated
Comment thread internal/controller/network/network_wcow_test.go Outdated
Comment thread internal/controller/network/network_wcow_test.go Outdated
Comment thread internal/controller/network/network_wcow_test.go Outdated
Adds gomock mocks (in mocks/) and unit tests for the narrow
vmNetworkManager, capabilitiesProvider, and platform-specific guestNetwork
interfaces. Mocks are committed without a //go:generate directive to match
existing repo convention; standardised mock generation and CI drift
validation will be addressed in a separate PR.

Tests cover the production failure paths:

Shared (network_test.go):
- New(): namespace-add capability is queried once and cached; nil
  capabilities do not panic the constructor.
- Setup(): rejects calls in any non-NotConfigured state without mutating
  state; an empty namespace ID transitions the controller to StateInvalid
  via the deferred error handler.
- Teardown(): no-ops from StateNotConfigured and StateTornDown so
  containerd's StopPodSandbox retries are safe.

LCOW (network_lcow_test.go):
- addEndpointToGuestNamespace: host-then-guest ordering, host-only when
  caps say no, host-failure leaves NIC untracked, guest-failure-after-host
  keeps the NIC tracked so Teardown can unwind.
- removeEndpointFromGuestNamespace: guest-then-host ordering; guest
  failure short-circuits host removal; host-only path when guest lacks
  namespace support.
- Teardown: full happy path, partial-failure cleanup chain (failed NIC
  stays tracked, others are removed, state moves to Invalid), host-side
  RemoveNIC failure path.
- Half-setup recovery: host AddNIC succeeds + guest Add fails + Teardown
  unwinds the host-side device end-to-end; first Teardown fails on guest
  removal -> StateInvalid -> retry from StateInvalid drains the endpoint
  and reaches StateTornDown.

WCOW (network_wcow_test.go):
- addEndpointToGuestNamespace: full PreAdd -> AddNIC -> Add sequence,
  pre-add failure short-circuits, host failure leaves NIC untracked,
  final-add failure leaves NIC tracked.
- removeEndpointFromGuestNamespace: guest-then-host ordering; guest
  failure short-circuits host removal.
- Teardown: happy path (no-namespace variant) and partial-failure
  cleanup chain.
- Half-setup recovery: finalize-Add failure + Teardown unwinds host;
  retry-from-StateInvalid succeeds.

Signed-off-by: Shreyansh Sancheti <shsancheti@microsoft.com>
@shreyanshjain7174 shreyanshjain7174 force-pushed the controller/network-tests branch from 91976f0 to 530518b Compare April 27, 2026 16:40
…ntract test

Each platform had two tests covering the same half-setup recovery path:

  TestLCOW_AddEndpoint_HostOK_GuestFails_StillTracked
  TestLCOW_AddEndpoint_HostOK_GuestFails_TeardownUnwindsHost

  TestWCOW_AddEndpoint_FinalAddFails_StillTracked
  TestWCOW_AddEndpoint_FinalAddFails_TeardownUnwindsHost

The "_StillTracked" tests asserted the in-memory contract that a NIC remains
tracked after a guest-side failure. The "_TeardownUnwindsHost" tests proved
the same contract end-to-end by then running Teardown and verifying the
host-side device gets unwound.

These are not independent contracts: tracking the NIC is the precondition
for Teardown unwinding. Splitting them into two tests duplicates setup and
hides the relationship. Merge by removing each "_StillTracked" test; the
"_TeardownUnwindsHost" tests already perform the tracked-state assertion as
their precondition before exercising Teardown.

Net result: identical coverage, 2 fewer tests, 1 file with the full
half-setup recovery story per platform.

Signed-off-by: Shreyansh Sancheti <shsancheti@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants