Skip to content

Commit

Permalink
Additional error states
Browse files Browse the repository at this point in the history
1. Consolidate error states for node, and create error states for pod
   information.
2. Add TestValidateEndpointFields to verify if the error states are
   triggered as expected.
  • Loading branch information
sawsa307 committed Mar 22, 2023
1 parent 0ad6d47 commit adedafd
Showing 1 changed file with 361 additions and 3 deletions.
364 changes: 361 additions & 3 deletions pkg/neg/syncers/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package syncers

import (
"errors"
"fmt"
"net"
"reflect"
Expand Down Expand Up @@ -549,6 +550,363 @@ func TestToZoneNetworkEndpointMapUtil(t *testing.T) {
}
}

// TestValidateEndpointFields validates if toZoneNetworkEndpointMap
// returns correct type of error with invalid endpoint information
func TestValidateEndpointFields(t *testing.T) {
t.Parallel()
zoneGetter := negtypes.NewFakeZoneGetter()
podLister := negtypes.NewTestContext().PodInformer.GetIndexer()
addPodsToLister(podLister)

instance1 := negtypes.TestInstance1
instance3 := negtypes.TestInstance3
instanceNotExist := "non-existent-node"
emptyZoneInstance := "empty-zone-instance"
zoneGetter.AddZone("", emptyZoneInstance)

emptyNamedPort := ""
emptyNodeName := ""
port80 := int32(80)
protocolTCP := v1.ProtocolTCP

testCases := []struct {
desc string
portName string
testEndpointSlice []*discovery.EndpointSlice
expectSets map[string]negtypes.NetworkEndpointSet
expectMap negtypes.EndpointPodMap
expectErr error
}{
{
desc: "endpoints no missing or invalid fields",
portName: "",
testEndpointSlice: []*discovery.EndpointSlice{
{
ObjectMeta: metav1.ObjectMeta{
Name: testServiceName + "-1",
Namespace: testServiceNamespace,
Labels: map[string]string{
discovery.LabelServiceName: testServiceName,
},
},
AddressType: "IPv4",
Endpoints: []discovery.Endpoint{
{
Addresses: []string{"10.100.1.1"},
NodeName: &instance1,
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "pod1",
},
},
{
Addresses: []string{"10.100.3.1"},
NodeName: &instance3,
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "pod4",
},
},
},
Ports: []discovery.EndpointPort{
{
Name: &emptyNamedPort,
Port: &port80,
Protocol: &protocolTCP,
},
},
},
},
expectSets: map[string]negtypes.NetworkEndpointSet{
negtypes.TestZone1: negtypes.NewNetworkEndpointSet(
networkEndpointFromEncodedEndpoint("10.100.1.1||instance1||80")),
negtypes.TestZone2: negtypes.NewNetworkEndpointSet(
networkEndpointFromEncodedEndpoint("10.100.3.1||instance3||80")),
},
expectMap: negtypes.EndpointPodMap{
networkEndpointFromEncodedEndpoint("10.100.1.1||instance1||80"): types.NamespacedName{Namespace: testServiceNamespace, Name: "pod1"},
networkEndpointFromEncodedEndpoint("10.100.3.1||instance3||80"): types.NamespacedName{Namespace: testServiceNamespace, Name: "pod4"},
},
expectErr: nil,
},
{
desc: "include one endpoint that has missing nodeName",
portName: "",
testEndpointSlice: []*discovery.EndpointSlice{
{
ObjectMeta: metav1.ObjectMeta{
Name: testServiceName + "-1",
Namespace: testServiceNamespace,
Labels: map[string]string{
discovery.LabelServiceName: testServiceName,
},
},
AddressType: "IPv4",
Endpoints: []discovery.Endpoint{
{
Addresses: []string{"10.100.1.1"},
NodeName: nil, // endpoint with missing nodeName
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "pod1",
},
},
{
Addresses: []string{"10.100.3.1"},
NodeName: &instance3,
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "pod4",
},
},
},
Ports: []discovery.EndpointPort{
{
Name: &emptyNamedPort,
Port: &port80,
Protocol: &protocolTCP,
},
},
},
},
expectSets: nil,
expectMap: nil,
expectErr: negtypes.ErrEPInvalidNodeName,
},
{
desc: "include one endpoint that has empty nodeName",
portName: "",
testEndpointSlice: []*discovery.EndpointSlice{
{
ObjectMeta: metav1.ObjectMeta{
Name: testServiceName + "-1",
Namespace: testServiceNamespace,
Labels: map[string]string{
discovery.LabelServiceName: testServiceName,
},
},
AddressType: "IPv4",
Endpoints: []discovery.Endpoint{
{
Addresses: []string{"10.100.1.1"},
NodeName: &emptyNodeName,
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "pod1",
},
},
{
Addresses: []string{"10.100.3.1"},
NodeName: &instance3,
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "pod4",
},
},
},
Ports: []discovery.EndpointPort{
{
Name: &emptyNamedPort,
Port: &port80,
Protocol: &protocolTCP,
},
},
},
},
expectSets: nil,
expectMap: nil,
expectErr: negtypes.ErrEPInvalidNodeName,
},
{
desc: "include one endpoint that has missing pod",
portName: "",
testEndpointSlice: []*discovery.EndpointSlice{
{
ObjectMeta: metav1.ObjectMeta{
Name: testServiceName + "-1",
Namespace: testServiceNamespace,
Labels: map[string]string{
discovery.LabelServiceName: testServiceName,
},
},
AddressType: "IPv4",
Endpoints: []discovery.Endpoint{
{
Addresses: []string{"10.100.1.1"},
NodeName: &instance1,
TargetRef: nil,
},
{
Addresses: []string{"10.100.3.1"},
NodeName: &instance3,
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "pod4",
},
},
},
Ports: []discovery.EndpointPort{
{
Name: &emptyNamedPort,
Port: &port80,
Protocol: &protocolTCP,
},
},
},
},
expectSets: nil,
expectMap: nil,
expectErr: negtypes.ErrEPInvalidPod,
},
{
desc: "include one endpoint that does correspond to an existing pod",
portName: "",
testEndpointSlice: []*discovery.EndpointSlice{
{
ObjectMeta: metav1.ObjectMeta{
Name: testServiceName + "-1",
Namespace: testServiceNamespace,
Labels: map[string]string{
discovery.LabelServiceName: testServiceName,
},
},
AddressType: "IPv4",
Endpoints: []discovery.Endpoint{
{
Addresses: []string{"10.100.1.1"},
NodeName: &instance1,
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "foo", // this is a non-existing pod
},
},
{
Addresses: []string{"10.100.3.1"},
NodeName: &instance3,
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "pod4",
},
},
},
Ports: []discovery.EndpointPort{
{
Name: &emptyNamedPort,
Port: &port80,
Protocol: &protocolTCP,
},
},
},
},
expectSets: nil,
expectMap: nil,
expectErr: negtypes.ErrEPInvalidPod,
},
{
desc: "include one endpoint that does correspond to an existing node",
portName: "",
testEndpointSlice: []*discovery.EndpointSlice{
{
ObjectMeta: metav1.ObjectMeta{
Name: testServiceName + "-1",
Namespace: testServiceNamespace,
Labels: map[string]string{
discovery.LabelServiceName: testServiceName,
},
},
AddressType: "IPv4",
Endpoints: []discovery.Endpoint{
{
Addresses: []string{"10.100.1.1"},
NodeName: &instanceNotExist,
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "pod1",
},
},
{
Addresses: []string{"10.100.3.1"},
NodeName: &instance3,
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "pod4",
},
},
},
Ports: []discovery.EndpointPort{
{
Name: &emptyNamedPort,
Port: &port80,
Protocol: &protocolTCP,
},
},
},
},
expectSets: nil,
expectMap: nil,
expectErr: negtypes.ErrEPInvalidNodeName,
},
{
desc: "include one endpoint that corresponds to an empty zone",
portName: "",
testEndpointSlice: []*discovery.EndpointSlice{
{
ObjectMeta: metav1.ObjectMeta{
Name: testServiceName + "-1",
Namespace: testServiceNamespace,
Labels: map[string]string{
discovery.LabelServiceName: testServiceName,
},
},
AddressType: "IPv4",
Endpoints: []discovery.Endpoint{
{
Addresses: []string{"10.100.1.1"},
NodeName: &emptyZoneInstance,
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "pod1",
},
},
{
Addresses: []string{"10.100.3.1"},
NodeName: &instance3,
TargetRef: &v1.ObjectReference{
Namespace: testServiceNamespace,
Name: "pod4",
},
},
},
Ports: []discovery.EndpointPort{
{
Name: &emptyNamedPort,
Port: &port80,
Protocol: &protocolTCP,
},
},
},
},
expectSets: nil,
expectMap: nil,
expectErr: negtypes.ErrEPInvalidZone,
},
}

// TODO(songrx1997): Add endpoint annotations for the test after calculation code is in
for _, tc := range testCases {
retSet, retMap, _, err := toZoneNetworkEndpointMap(negtypes.EndpointsDataFromEndpointSlices(tc.testEndpointSlice), podLister, zoneGetter, tc.portName, negtypes.VmIpPortEndpointType, negtypes.PodLabelPropagationConfig{})
if !errors.Is(err, tc.expectErr) {
t.Errorf("For case %q, expect %v error, but got %v.", tc.desc, tc.expectErr, err)
}
if !reflect.DeepEqual(retSet, tc.expectSets) {
t.Errorf("For case %q, expecting endpoint set %v, but got %v.", tc.desc, tc.expectSets, retSet)
}
if !reflect.DeepEqual(retMap, tc.expectMap) {
t.Errorf("For case %q, expecting endpoint map %v, but got %v.", tc.desc, tc.expectMap, retMap)
}
}
}

func TestRetrieveExistingZoneNetworkEndpointMap(t *testing.T) {
zoneGetter := negtypes.NewFakeZoneGetter()
negCloud := negtypes.NewFakeNetworkEndpointGroupCloud("test-subnetwork", "test-network")
Expand Down Expand Up @@ -1844,23 +2202,23 @@ func getTestEndpointSlices(name, namespace string) []*discovery.EndpointSlice {
NodeName: &instance3,
TargetRef: &v1.ObjectReference{
Namespace: namespace,
Name: "pod10",
Name: "pod13",
},
},
{
Addresses: []string{"aa:ab"},
NodeName: &instance4,
TargetRef: &v1.ObjectReference{
Namespace: namespace,
Name: "pod11",
Name: "pod14",
},
},
{
Addresses: []string{"aa:ac"},
NodeName: &instance4,
TargetRef: &v1.ObjectReference{
Namespace: namespace,
Name: "pod12",
Name: "pod15",
},
Conditions: discovery.EndpointConditions{Ready: &notReady},
},
Expand Down

0 comments on commit adedafd

Please sign in to comment.