Skip to content

Commit

Permalink
Add TestParseEndpointBatchErr to verify invalid endpoint batch error …
Browse files Browse the repository at this point in the history
…state.
  • Loading branch information
sawsa307 committed Jun 7, 2023
1 parent 79c6af9 commit 984c67b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
6 changes: 3 additions & 3 deletions pkg/neg/syncers/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (s *transactionSyncer) syncInternal() error {
err := s.syncInternalImpl()
if err != nil {
if syncErr := negtypes.ClassifyError(err); syncErr.IsErrorState {
s.logger.V(3).Info("Updating error state", "error state", syncErr.Reason)
s.logger.Info("Updating error state", "error state", syncErr.Reason)
s.setErrorState()
}
}
Expand Down Expand Up @@ -523,9 +523,9 @@ func (s *transactionSyncer) operationInternal(operation transactionOp, zone stri
// we would set error state and retry. For successful calls, we won't update
// error state, so its value won't be overwritten within API call go routines.
if syncErr.IsErrorState {
s.logger.Info("Detected unexpected error when checking endpoint update response", "operation", operation, "error", err)
s.logger.Error(err, "Detected unexpected error when checking endpoint update response", "operation", operation)
s.syncLock.Lock()
s.logger.V(3).Info("Updating error state", "error state", syncErr.Reason)
s.logger.Info("Updating error state", "error state", syncErr.Reason)
s.setErrorState()
s.syncLock.Unlock()
}
Expand Down
51 changes: 51 additions & 0 deletions pkg/neg/syncers/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1869,6 +1869,57 @@ func TestEnableDegradedMode(t *testing.T) {
})
}
}

func TestCheckEndpointBatchErr(t *testing.T) {
requestError := &googleapi.Error{
Code: http.StatusBadRequest,
}
serverError := &googleapi.Error{
Code: http.StatusInternalServerError,
}

testCases := []struct {
desc string
err error
endpointOperation transactionOp
expectErr error
}{
{
desc: "Not googleapi error",
err: errors.New("Not googleapi.Error"),
endpointOperation: attachOp,
expectErr: negtypes.ErrInvalidAPIResponse,
},
{
desc: "Server error, status code 500",
err: serverError,
endpointOperation: attachOp,
expectErr: serverError,
},
{
desc: "Invalid endpoint batch for endpoint attach, status code 400",
err: requestError,
endpointOperation: attachOp,
expectErr: negtypes.ErrInvalidEPAttach,
},
{
desc: "Invalid endpoint batch for endpoint detach, status code 400",
err: requestError,
endpointOperation: detachOp,
expectErr: negtypes.ErrInvalidEPDetach,
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
endpointBatchErr := checkEndpointBatchErr(tc.err, tc.endpointOperation)
if !errors.Is(endpointBatchErr, tc.expectErr) {
t.Errorf("checkEndpointBatchErr() = %t, expected %t", endpointBatchErr, tc.expectErr)
}
})
}
}

func TestGetEndpointPodLabelMap(t *testing.T) {
testContext := negtypes.NewTestContext()
podLister := testContext.PodInformer.GetIndexer()
Expand Down
2 changes: 1 addition & 1 deletion pkg/neg/syncers/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ func TestValidateEndpointFields(t *testing.T) {
},
expectSets: nil,
expectMap: nil,
expectErr: negtypes.ErrEPNodeMissing,
expectErr: negtypes.ErrEPNodeNotFound,
},
{
desc: "include one endpoint that corresponds to an empty zone",
Expand Down

0 comments on commit 984c67b

Please sign in to comment.