Merge pull request #109740 from AllenZMC/fix_test_iptables

Optimize test cases for iptables
This commit is contained in:
Kubernetes Prow Robot 2022-05-05 01:42:51 -07:00 committed by GitHub
commit 794d3ab686
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4235,6 +4235,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
// previousEndpoints and currentEndpoints are used to call appropriate // previousEndpoints and currentEndpoints are used to call appropriate
// handlers OnEndpoints* (based on whether corresponding values are nil // handlers OnEndpoints* (based on whether corresponding values are nil
// or non-nil) and must be of equal length. // or non-nil) and must be of equal length.
name string
previousEndpoints []*discovery.EndpointSlice previousEndpoints []*discovery.EndpointSlice
currentEndpoints []*discovery.EndpointSlice currentEndpoints []*discovery.EndpointSlice
oldEndpoints map[proxy.ServicePortName][]*endpointsInfo oldEndpoints map[proxy.ServicePortName][]*endpointsInfo
@ -4244,6 +4245,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedHealthchecks map[types.NamespacedName]int expectedHealthchecks map[types.NamespacedName]int
}{{ }{{
// Case[0]: nothing // Case[0]: nothing
name: "nothing",
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{}, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{},
expectedResult: map[proxy.ServicePortName][]*endpointsInfo{}, expectedResult: map[proxy.ServicePortName][]*endpointsInfo{},
expectedStaleEndpoints: []proxy.ServiceEndpoint{}, expectedStaleEndpoints: []proxy.ServiceEndpoint{},
@ -4251,6 +4253,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedHealthchecks: map[types.NamespacedName]int{}, expectedHealthchecks: map[types.NamespacedName]int{},
}, { }, {
// Case[1]: no change, named port, local // Case[1]: no change, named port, local
name: "no change, named port, local",
previousEndpoints: namedPortLocal, previousEndpoints: namedPortLocal,
currentEndpoints: namedPortLocal, currentEndpoints: namedPortLocal,
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{
@ -4270,6 +4273,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
}, { }, {
// Case[2]: no change, multiple subsets // Case[2]: no change, multiple subsets
name: "no change, multiple subsets",
previousEndpoints: multipleSubsets, previousEndpoints: multipleSubsets,
currentEndpoints: multipleSubsets, currentEndpoints: multipleSubsets,
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{
@ -4293,6 +4297,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedHealthchecks: map[types.NamespacedName]int{}, expectedHealthchecks: map[types.NamespacedName]int{},
}, { }, {
// Case[3]: no change, multiple subsets, multiple ports, local // Case[3]: no change, multiple subsets, multiple ports, local
name: "no change, multiple subsets, multiple ports, local",
previousEndpoints: multipleSubsetsMultiplePortsLocal, previousEndpoints: multipleSubsetsMultiplePortsLocal,
currentEndpoints: multipleSubsetsMultiplePortsLocal, currentEndpoints: multipleSubsetsMultiplePortsLocal,
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{
@ -4324,6 +4329,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
}, { }, {
// Case[4]: no change, multiple endpoints, subsets, IPs, and ports // Case[4]: no change, multiple endpoints, subsets, IPs, and ports
name: "no change, multiple endpoints, subsets, IPs, and ports",
previousEndpoints: multipleSubsetsIPsPorts, previousEndpoints: multipleSubsetsIPsPorts,
currentEndpoints: multipleSubsetsIPsPorts, currentEndpoints: multipleSubsetsIPsPorts,
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{
@ -4386,6 +4392,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
}, { }, {
// Case[5]: add an Endpoints // Case[5]: add an Endpoints
name: "add an Endpoints",
previousEndpoints: []*discovery.EndpointSlice{nil}, previousEndpoints: []*discovery.EndpointSlice{nil},
currentEndpoints: namedPortLocal, currentEndpoints: namedPortLocal,
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{}, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{},
@ -4403,6 +4410,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
}, { }, {
// Case[6]: remove an Endpoints // Case[6]: remove an Endpoints
name: "remove an Endpoints",
previousEndpoints: namedPortLocal, previousEndpoints: namedPortLocal,
currentEndpoints: []*discovery.EndpointSlice{nil}, currentEndpoints: []*discovery.EndpointSlice{nil},
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{
@ -4419,6 +4427,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedHealthchecks: map[types.NamespacedName]int{}, expectedHealthchecks: map[types.NamespacedName]int{},
}, { }, {
// Case[7]: add an IP and port // Case[7]: add an IP and port
name: "add an IP and port",
previousEndpoints: namedPort, previousEndpoints: namedPort,
currentEndpoints: namedPortsLocalNoLocal, currentEndpoints: namedPortsLocalNoLocal,
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{
@ -4445,6 +4454,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
}, { }, {
// Case[8]: remove an IP and port // Case[8]: remove an IP and port
name: "remove an IP and port",
previousEndpoints: namedPortsLocalNoLocal, previousEndpoints: namedPortsLocalNoLocal,
currentEndpoints: namedPort, currentEndpoints: namedPort,
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{
@ -4476,6 +4486,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedHealthchecks: map[types.NamespacedName]int{}, expectedHealthchecks: map[types.NamespacedName]int{},
}, { }, {
// Case[9]: add a subset // Case[9]: add a subset
name: "add a subset",
previousEndpoints: []*discovery.EndpointSlice{namedPort[0], nil}, previousEndpoints: []*discovery.EndpointSlice{namedPort[0], nil},
currentEndpoints: multipleSubsetsWithLocal, currentEndpoints: multipleSubsetsWithLocal,
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{
@ -4500,6 +4511,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
}, { }, {
// Case[10]: remove a subset // Case[10]: remove a subset
name: "remove a subset",
previousEndpoints: multipleSubsets, previousEndpoints: multipleSubsets,
currentEndpoints: []*discovery.EndpointSlice{namedPort[0], nil}, currentEndpoints: []*discovery.EndpointSlice{namedPort[0], nil},
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{
@ -4523,6 +4535,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedHealthchecks: map[types.NamespacedName]int{}, expectedHealthchecks: map[types.NamespacedName]int{},
}, { }, {
// Case[11]: rename a port // Case[11]: rename a port
name: "rename a port",
previousEndpoints: namedPort, previousEndpoints: namedPort,
currentEndpoints: namedPortRenamed, currentEndpoints: namedPortRenamed,
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{
@ -4545,6 +4558,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedHealthchecks: map[types.NamespacedName]int{}, expectedHealthchecks: map[types.NamespacedName]int{},
}, { }, {
// Case[12]: renumber a port // Case[12]: renumber a port
name: "renumber a port",
previousEndpoints: namedPort, previousEndpoints: namedPort,
currentEndpoints: namedPortRenumbered, currentEndpoints: namedPortRenumbered,
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{
@ -4565,6 +4579,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedHealthchecks: map[types.NamespacedName]int{}, expectedHealthchecks: map[types.NamespacedName]int{},
}, { }, {
// Case[13]: complex add and remove // Case[13]: complex add and remove
name: "complex add and remove",
previousEndpoints: complexBefore, previousEndpoints: complexBefore,
currentEndpoints: complexAfter, currentEndpoints: complexAfter,
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{
@ -4630,6 +4645,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
}, { }, {
// Case[14]: change from 0 endpoint address to 1 unnamed port // Case[14]: change from 0 endpoint address to 1 unnamed port
name: "change from 0 endpoint address to 1 unnamed port",
previousEndpoints: emptyEndpointSlices, previousEndpoints: emptyEndpointSlices,
currentEndpoints: namedPort, currentEndpoints: namedPort,
oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{}, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{},
@ -4647,6 +4663,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
} }
for tci, tc := range testCases { for tci, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ipt := iptablestest.NewFake() ipt := iptablestest.NewFake()
fp := NewFakeProxier(ipt) fp := NewFakeProxier(ipt)
fp.hostname = nodeName fp.hostname = nodeName
@ -4664,7 +4681,6 @@ func TestUpdateEndpointsMap(t *testing.T) {
// Now let's call appropriate handlers to get to state we want to be. // Now let's call appropriate handlers to get to state we want to be.
if len(tc.previousEndpoints) != len(tc.currentEndpoints) { if len(tc.previousEndpoints) != len(tc.currentEndpoints) {
t.Fatalf("[%d] different lengths of previous and current endpoints", tci) t.Fatalf("[%d] different lengths of previous and current endpoints", tci)
continue
} }
for i := range tc.previousEndpoints { for i := range tc.previousEndpoints {
@ -4713,6 +4729,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
if !reflect.DeepEqual(result.HCEndpointsLocalIPSize, tc.expectedHealthchecks) { if !reflect.DeepEqual(result.HCEndpointsLocalIPSize, tc.expectedHealthchecks) {
t.Errorf("[%d] expected healthchecks %v, got %v", tci, tc.expectedHealthchecks, result.HCEndpointsLocalIPSize) t.Errorf("[%d] expected healthchecks %v, got %v", tci, tc.expectedHealthchecks, result.HCEndpointsLocalIPSize)
} }
})
} }
} }