Skip to content

Commit

Permalink
Add TestValidateEndpointBatch to verify invalid endpoint batch error …
Browse files Browse the repository at this point in the history
…states

Add TestValidateEndpointBatch to verify invalid endpoint batch error states
  • Loading branch information
sawsa307 committed Mar 23, 2023
1 parent 8762d21 commit a480de6
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions pkg/neg/syncers/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ package syncers

import (
context2 "context"
"errors"
"fmt"
"net"
"net/http"
"reflect"
"strconv"
"testing"
"time"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -1388,6 +1392,66 @@ func TestUnknownNodes(t *testing.T) {
}
}

func TestValidateEndpointBatch(t *testing.T) {
fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues())
fakeCloud := negtypes.NewAdapter(fakeGCE)
zone := "us-central1-a"
networkEndpoints := []*composite.NetworkEndpoint{}

testCases := []struct {
desc string
returnError error
endpointOperation transactionOp
expect error
}{
{
desc: "Not googleapi error",
returnError: errors.New("Not googleapi.Error"),
endpointOperation: attachOp,
expect: negtypes.ErrInvalidAPIResponse,
},
{
desc: "Server error, status code 500",
returnError: &googleapi.Error{
Code: http.StatusInternalServerError,
},
endpointOperation: attachOp,
expect: nil,
},
{
desc: "Invalid endpoint batch for endpoint attach, status code 400",
returnError: &googleapi.Error{
Code: http.StatusBadRequest,
},
endpointOperation: attachOp,
expect: negtypes.ErrInvalidEPAttach,
},
{
desc: "Invalid endpoint batch for endpoint detach, status code 400",
returnError: &googleapi.Error{
Code: http.StatusBadRequest,
},
endpointOperation: detachOp,
expect: negtypes.ErrInvalidEPDetach,
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
mockGCE := fakeGCE.Compute().(*cloud.MockGCE)
mockGCE.MockNetworkEndpointGroups.AttachNetworkEndpointsHook = func(ctx context2.Context, key *meta.Key, arg0 *compute.NetworkEndpointGroupsAttachEndpointsRequest, neg *cloud.MockNetworkEndpointGroups) error {
return tc.returnError
}
_, transactionSyncer := newTestTransactionSyncer(fakeCloud, negtypes.VmIpPortEndpointType, false)

err := transactionSyncer.cloud.AttachNetworkEndpoints(transactionSyncer.NegSyncerKey.NegName, zone, networkEndpoints, transactionSyncer.NegSyncerKey.GetAPIVersion())
if got := transactionSyncer.ValidateEndpointBatch(err, tc.endpointOperation); !errors.Is(got, tc.expect) {
t.Errorf("ValidateEndpointBatch() = %t, expected %t", got, tc.expect)
}
})
}
}

func newL4ILBTestTransactionSyncer(fakeGCE negtypes.NetworkEndpointGroupCloud, mode negtypes.EndpointsCalculatorMode) (negtypes.NegSyncer, *transactionSyncer) {
negsyncer, ts := newTestTransactionSyncer(fakeGCE, negtypes.VmIpEndpointType, false)
ts.endpointsCalculator = GetEndpointsCalculator(ts.nodeLister, ts.podLister, ts.serviceLister, ts.zoneGetter, ts.NegSyncerKey, mode, false, klog.TODO(), negtypes.PodLabelPropagationConfig{})
Expand Down

0 comments on commit a480de6

Please sign in to comment.