Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add UTs for ip clean up fix #2367

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cni/network/multitenancy_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
localIPPrefixLen = 17
multiTenancyVlan1 = 1
multiTenancyVlan2 = 2
windows = "windows"
)

var errMockMulAdd = errors.New("multitenancy fail")
Expand Down Expand Up @@ -120,7 +121,8 @@ func (m *MockMultitenancy) GetAllNetworkContainers(
}

// TODO: add dual nic test cases for windows
if runtime.GOOS == "windows" {
// adhoc solution for dual stack tests
if runtime.GOOS == windows && nwCfg.Name == "DualStackTest" { //
cnsResponseTwo := &cns.GetNetworkContainerResponse{
IPConfiguration: cns.IPConfiguration{
IPSubnet: cns.IPSubnet{
Expand Down
91 changes: 91 additions & 0 deletions cni/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@

// test v4 and v6 address allocation from ipam
func TestAddDualStack(t *testing.T) {
nwCfg.Name = "DualStackTest"
nwCfg.IPV6Mode = "ipv6nat"
args.StdinData = nwCfg.Serialize()
cniPlugin, _ := cni.NewPlugin("test", "0.3.0")
Expand Down Expand Up @@ -685,6 +686,96 @@
}
}

func TestIpCleanUpWhenIpamAddFail(t *testing.T) {
endpointErrMsg := "Endpoint Error"
plugin, _ := cni.NewPlugin("test", "0.3.0")

mockIpamInvokerSingleTenancy := NewMockIpamInvoker(false, false, false, false, false)
// nw config with MultiTenancy=false
localNwCfg1 := cni.NetworkConfig{
CNIVersion: "0.3.0",
Name: "mulnet",
MultiTenancy: false,
EnableExactMatchForPodName: true,
Master: "eth0",
}

// nw config with MultiTenancy=true and IPAM.Type="azure-cns"
localNwCfg2 := cni.NetworkConfig{
CNIVersion: "0.3.0",
Name: "mulnet",
MultiTenancy: true,
EnableExactMatchForPodName: true,
Master: "eth0",
IPAM: cni.IPAM{
Type: acnnetwork.AzureCNS,
},
}
mockIpamInvokerMultiTenancy := NewMockIpamInvoker(false, false, false, false, false)
// for Multitenancy, we did not insert ip to this ipmap. Pre-insert this multitenancy ip so we know if
// the deletion call get invoked or not after the test by checking the existent of this ip.
mockIpamInvokerMultiTenancy.ipMap["20.0.0.10/24"] = true

tests := []struct {
name string
plugin *NetPlugin
args *cniSkel.CmdArgs
expectedIPMapLength int
}{
{
name: "SingleTenancy: endpoint creation failed, proceed to clean IPs",
plugin: &NetPlugin{
Plugin: plugin,
tb: &telemetry.TelemetryBuffer{},
report: &telemetry.CNIReport{},
multitenancyClient: NewMockMultitenancy(false),
ipamInvoker: mockIpamInvokerSingleTenancy,
nm: acnnetwork.NewMockNetworkmanager(acnnetwork.NewMockEndpointClient(func(*acnnetwork.EndpointInfo) error {
return acnnetwork.NewErrorMockEndpointClient(endpointErrMsg) //nolint:wrapcheck // ignore wrapping for test
})),
},
args: &cniSkel.CmdArgs{
StdinData: localNwCfg1.Serialize(),
ContainerID: "test-container1",
Netns: "test-container1",
Args: fmt.Sprintf("K8S_POD_NAME=%v;K8S_POD_NAMESPACE=%v", "test-pod", "test-pod-ns"),
IfName: eth0IfName,
},
expectedIPMapLength: 0,
},
{
name: "MultiTenancy: endpoint creation failed, skip cleaning IPs",
plugin: &NetPlugin{
Plugin: plugin,
tb: &telemetry.TelemetryBuffer{},
report: &telemetry.CNIReport{},
multitenancyClient: NewMockMultitenancy(false),
ipamInvoker: mockIpamInvokerMultiTenancy,
nm: acnnetwork.NewMockNetworkmanager(acnnetwork.NewMockEndpointClient(func(*acnnetwork.EndpointInfo) error {
return acnnetwork.NewErrorMockEndpointClient(endpointErrMsg) //nolint:wrapcheck // ignore wrapping for test
})),
},
args: &cniSkel.CmdArgs{
StdinData: localNwCfg2.Serialize(),
ContainerID: "test-container2",
Netns: "test-container2",
Args: fmt.Sprintf("K8S_POD_NAME=%v;K8S_POD_NAMESPACE=%v", "test-pod2", "test-pod-ns2"),
IfName: eth0IfName,
},
expectedIPMapLength: 1,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
err := tt.plugin.Add(tt.args)
require.Error(t, err)
assert.Equal(t, tt.expectedIPMapLength, len(tt.plugin.ipamInvoker.(*MockIpamInvoker).ipMap))

Check failure on line 774 in cni/network/network_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.21.x, ubuntu-latest)

len: use assert.Len (testifylint)

Check failure on line 774 in cni/network/network_test.go

View workflow job for this annotation

GitHub Actions / Lint (1.21.x, windows-latest)

len: use assert.Len (testifylint)
})
}
}

func TestPluginMultitenancyDelete(t *testing.T) {
plugin := GetTestResources()
plugin.multitenancyClient = NewMockMultitenancy(false)
Expand Down
Loading