Skip to content

Commit e7512e0

Browse files
author
Shreyansh Sancheti
committed
controller/network: add unit tests for LCOW network controller
15 tests covering teardown cleanup chains (partial NIC failure continues, failed NICs stay tracked for retry from Invalid state), state machine transitions (idempotent teardown, no-op on unconfigured, duplicate Setup rejected), LCOW endpoint add/remove operations (host-before-guest ordering via InOrder, BuildLCOWNetworkAdapter struct verification, guest-fail still tracked for cleanup, host-fail not tracked), nil-capabilities disabling guest ops, and State.String(). Mocks generated for vmNetworkManager, linuxGuestNetworkManager, and capabilitiesProvider. GCS GuestDefinedCapabilities mock reused from internal/gcs/mock/. Signed-off-by: Shreyansh Sancheti <shsancheti@microsoft.com>
1 parent e901ab7 commit e7512e0

File tree

10 files changed

+1224
-0
lines changed

10 files changed

+1224
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//go:build windows
2+
3+
package network
4+
5+
import (
6+
"context"
7+
8+
"github.com/Microsoft/hcsshim/hcn"
9+
)
10+
11+
// SetStateForTest sets the Manager's state. Test-only.
12+
func (m *Manager) SetStateForTest(s State) {
13+
m.netState = s
14+
}
15+
16+
// StateForTest returns the Manager's current state. Test-only.
17+
func (m *Manager) StateForTest() State {
18+
return m.netState
19+
}
20+
21+
// SetNamespaceIDForTest sets the namespace ID on the Manager. Test-only.
22+
func (m *Manager) SetNamespaceIDForTest(id string) {
23+
m.namespaceID = id
24+
}
25+
26+
// AddEndpointForTest adds a NIC→endpoint mapping to the internal tracking map. Test-only.
27+
func (m *Manager) AddEndpointForTest(nicID string, ep *hcn.HostComputeEndpoint) {
28+
m.vmEndpoints[nicID] = ep
29+
}
30+
31+
// EndpointsForTest returns the current vmEndpoints map. Test-only.
32+
func (m *Manager) EndpointsForTest() map[string]*hcn.HostComputeEndpoint {
33+
return m.vmEndpoints
34+
}
35+
36+
// IsNamespaceSupportedForTest returns the cached namespace-support flag. Test-only.
37+
func (m *Manager) IsNamespaceSupportedForTest() bool {
38+
return m.isNamespaceSupportedByGuest
39+
}
40+
41+
// AddEndpointToGuestNamespaceForTest exposes addEndpointToGuestNamespace for testing.
42+
func (m *Manager) AddEndpointToGuestNamespaceForTest(ctx context.Context, nicID string, endpoint *hcn.HostComputeEndpoint, policyBasedRouting bool) error {
43+
return m.addEndpointToGuestNamespace(ctx, nicID, endpoint, policyBasedRouting)
44+
}
45+
46+
// RemoveEndpointFromGuestNamespaceForTest exposes removeEndpointFromGuestNamespace for testing.
47+
func (m *Manager) RemoveEndpointFromGuestNamespaceForTest(ctx context.Context, nicID string, endpoint *hcn.HostComputeEndpoint) error {
48+
return m.removeEndpointFromGuestNamespace(ctx, nicID, endpoint)
49+
}
50+
51+
// AddNetNSInsideGuestForTest exposes addNetNSInsideGuest for testing.
52+
func (m *Manager) AddNetNSInsideGuestForTest(ctx context.Context, ns *hcn.HostComputeNamespace) error {
53+
return m.addNetNSInsideGuest(ctx, ns)
54+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//go:build windows && wcow
2+
3+
package network_test
4+
5+
import (
6+
"errors"
7+
"testing"
8+
9+
"github.com/Microsoft/hcsshim/internal/controller/network"
10+
netmock "github.com/Microsoft/hcsshim/internal/controller/network/mock"
11+
gcsmock "github.com/Microsoft/hcsshim/internal/gcs/mock"
12+
13+
"go.uber.org/mock/gomock"
14+
)
15+
16+
var errTest = errors.New("test error")
17+
18+
// newWCOWTestManager creates a Manager wired with WCOW mock dependencies.
19+
func newWCOWTestManager(t *testing.T, ctrl *gomock.Controller, namespaceSupportEnabled bool) (
20+
*network.Manager,
21+
*netmock.MockvmNetworkManager,
22+
*netmock.MocklinuxGuestNetworkManager,
23+
*netmock.MockwindowsGuestNetworkManager,
24+
) {
25+
t.Helper()
26+
27+
mockVM := netmock.NewMockvmNetworkManager(ctrl)
28+
mockLinuxGuest := netmock.NewMocklinuxGuestNetworkManager(ctrl)
29+
mockWinGuest := netmock.NewMockwindowsGuestNetworkManager(ctrl)
30+
mockCaps := netmock.NewMockcapabilitiesProvider(ctrl)
31+
32+
if namespaceSupportEnabled {
33+
mockGuestCaps := gcsmock.NewMockGuestDefinedCapabilities(ctrl)
34+
mockGuestCaps.EXPECT().IsNamespaceAddRequestSupported().Return(true)
35+
mockCaps.EXPECT().Capabilities().Return(mockGuestCaps)
36+
} else {
37+
mockCaps.EXPECT().Capabilities().Return(nil)
38+
}
39+
40+
m := network.New(mockVM, mockLinuxGuest, mockWinGuest, mockCaps)
41+
return m, mockVM, mockLinuxGuest, mockWinGuest
42+
}

internal/controller/network/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
package network
44

5+
//go:generate go tool mockgen -source=interface.go -build_constraint=windows -package=mock -destination=mock/mock_network.go
6+
57
import (
68
"context"
79

0 commit comments

Comments
 (0)