Skip to content

Commit

Permalink
Additional error states
Browse files Browse the repository at this point in the history
1. Add additional error states, including EPMissingPod, PodNotFound, and
   NodeNotFound
2. Update error from syncInternalImpl and add setErrorState logging
3. Add TestValidateEndpointBatch and TestValidateEndpointFields to test
   if the existing error states are triggered as expected.
  • Loading branch information
sawsa307 committed Mar 21, 2023
1 parent 8d0a99d commit 5070d74
Show file tree
Hide file tree
Showing 2 changed files with 460 additions and 35 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.zoneGetter, ts.NegSyncerKey, mode, klog.TODO(), negtypes.PodLabelPropagationConfig{})
Expand Down
Loading

0 comments on commit 5070d74

Please sign in to comment.