-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: adding additional test coverage for all error types
- Loading branch information
1 parent
84bcca8
commit 0c561ab
Showing
1 changed file
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package errors | ||
|
||
|
||
import ( | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
"testing" | ||
|
||
"bytes" | ||
"io" | ||
"net/http" | ||
|
||
) | ||
|
||
func TestIsNotFoundErr(t *testing.T) { | ||
err1 := &azcore.ResponseError{ErrorCode: ResourceNotFound} | ||
if !IsNotFoundErr(err1) { | ||
t.Errorf("Expected IsNotFoundErr to return true for err1, but it returned false") | ||
} | ||
|
||
err2 := &azcore.ResponseError{ErrorCode: "SomeOtherErrorCode"} | ||
if IsNotFoundErr(err2) { | ||
t.Errorf("Expected IsNotFoundErr to return false for err2, but it returned true") | ||
} | ||
|
||
if IsNotFoundErr(nil) { | ||
t.Errorf("Expected IsNotFoundErr to return false for nil input, but it returned true") | ||
} | ||
} | ||
|
||
|
||
|
||
type testCase struct { | ||
description string | ||
responseError *azcore.ResponseError | ||
expected bool | ||
} | ||
|
||
func TestSubscriptionQuotaHasBeenReached(t *testing.T) { | ||
testCases := []testCase{ | ||
{ | ||
description: "Quota Exceeded", | ||
responseError: &azcore.ResponseError{ | ||
ErrorCode: "OperationNotAllowed", | ||
StatusCode: http.StatusForbidden, | ||
RawResponse: &http.Response{ | ||
Body: io.NopCloser(bytes.NewReader([]byte(`{"error": {"code": "OperationNotAllowed", "message": "Family Cores quota exceeded"}}`))), | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
description: "Different Error Code", | ||
responseError: &azcore.ResponseError{ | ||
ErrorCode: "ResourceNotFound", | ||
StatusCode: http.StatusNotFound, | ||
RawResponse: &http.Response{ | ||
Body: io.NopCloser(bytes.NewReader([]byte(`{"error": {"code": "ResourceNotFound"}}`))), | ||
}, | ||
}, | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
if got := SubscriptionQuotaHasBeenReached(tc.responseError); got != tc.expected { | ||
t.Errorf("SubscriptionQuotaHasBeenReached() = %v, want %v", got, tc.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
||
func TestZonalAllocationFailureOccurred(t *testing.T) { | ||
testCases := []testCase{ | ||
{ | ||
description: "Zonal Allocation Failed", | ||
responseError: &azcore.ResponseError{ | ||
ErrorCode: ZoneAllocationFailed, | ||
StatusCode: http.StatusBadRequest, | ||
RawResponse: &http.Response{ | ||
Body: io.NopCloser(bytes.NewReader([]byte(`{"error": {"code": "ZonalAllocationFailed", "message": "Failed to allocate resources in the zone"}}`))), | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
description: "Different Error Code", | ||
responseError: &azcore.ResponseError{ | ||
ErrorCode: "ResourceNotFound", | ||
StatusCode: http.StatusNotFound, | ||
RawResponse: &http.Response{ | ||
Body: io.NopCloser(bytes.NewReader([]byte(`{"error": {"code": "ResourceNotFound"}}`))), | ||
}, | ||
}, | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
if got := ZonalAllocationFailureOccurred(tc.responseError); got != tc.expected { | ||
t.Errorf("ZonalAllocationFailureOccurred() = %v, want %v for %s", got, tc.expected, tc.description) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
func TestRegionalQuotaHasBeenReached(t *testing.T) { | ||
testCases := []testCase{ | ||
{ | ||
description: "Regional Quota Exceeded", | ||
responseError: &azcore.ResponseError{ | ||
ErrorCode: OperationNotAllowed, | ||
StatusCode: http.StatusForbidden, | ||
RawResponse: &http.Response{ | ||
Body: io.NopCloser(bytes.NewReader([]byte(`{"error": {"code": "OperationNotAllowed", "message": "exceeding approved Total Regional Cores quota"}}`))), | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
description: "Different Error Code", | ||
responseError: &azcore.ResponseError{ | ||
ErrorCode: "ResourceNotFound", | ||
StatusCode: http.StatusNotFound, | ||
RawResponse: &http.Response{ | ||
Body: io.NopCloser(bytes.NewReader([]byte(`{"error": {"code": "ResourceNotFound"}}`))), | ||
}, | ||
}, | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
if got := RegionalQuotaHasBeenReached(tc.responseError); got != tc.expected { | ||
t.Errorf("RegionalQuotaHasBeenReached() = %v, want %v for %s", got, tc.expected, tc.description) | ||
} | ||
}) | ||
} | ||
} | ||
|