|
| 1 | +/* |
| 2 | +Copyright (c) Microsoft Corporation. |
| 3 | +Licensed under the MIT license. |
| 4 | +*/ |
| 5 | + |
| 6 | +package customizations |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + |
| 13 | + "github.com/go-logr/logr" |
| 14 | + |
| 15 | + "github.com/Azure/azure-service-operator/v2/internal/genericarmclient" |
| 16 | + "github.com/Azure/azure-service-operator/v2/pkg/genruntime/core" |
| 17 | +) |
| 18 | + |
| 19 | +func Test_VirtualNetworksSubnetExtension_ClassifyError_NetcfgSubnetRangeOutsideVnet_IsRetryable(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + cases := map[string]struct { |
| 23 | + errorCode string |
| 24 | + errorMessage string |
| 25 | + mockReturns core.ErrorClassification |
| 26 | + expected core.ErrorClassification |
| 27 | + }{ |
| 28 | + "Fatal error is still fatal": { |
| 29 | + errorCode: "BadRequest", |
| 30 | + errorMessage: "Invalid parameter value", |
| 31 | + mockReturns: core.ErrorFatal, |
| 32 | + expected: core.ErrorFatal, |
| 33 | + }, |
| 34 | + "Other error is still retryable": { |
| 35 | + errorCode: "OtherError", |
| 36 | + errorMessage: "Some other error occurred", |
| 37 | + mockReturns: core.ErrorRetryable, |
| 38 | + expected: core.ErrorRetryable, |
| 39 | + }, |
| 40 | + "SubnetRangeOutsideVnet error is retryable": { |
| 41 | + errorCode: "NetcfgSubnetRangeOutsideVnet", |
| 42 | + errorMessage: "Subnet 'testsubnet' is not valid because its IP address range is outside the IP address range of virtual network 'testvnet'.", |
| 43 | + mockReturns: core.ErrorRetryable, |
| 44 | + expected: core.ErrorRetryable, |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + for name, c := range cases { |
| 49 | + t.Run(name, func(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + g := NewGomegaWithT(t) |
| 52 | + |
| 53 | + extension := &VirtualNetworksSubnetExtension{} |
| 54 | + |
| 55 | + // Create a test error with the specific code |
| 56 | + err := genericarmclient.NewTestCloudError(c.errorCode, c.errorMessage) |
| 57 | + |
| 58 | + // Mock the next classifier function - this would normally classify as fatal |
| 59 | + mockCalled := false |
| 60 | + next := func(cloudError *genericarmclient.CloudError) (core.CloudErrorDetails, error) { |
| 61 | + mockCalled = true |
| 62 | + return core.CloudErrorDetails{ |
| 63 | + Classification: c.mockReturns, |
| 64 | + Code: cloudError.Code(), |
| 65 | + Message: cloudError.Message(), |
| 66 | + }, nil |
| 67 | + } |
| 68 | + |
| 69 | + details, classifyErr := extension.ClassifyError(err, "2024-03-01", logr.Discard(), next) |
| 70 | + |
| 71 | + g.Expect(classifyErr).ToNot(HaveOccurred()) |
| 72 | + g.Expect(details.Classification).To(Equal(c.expected)) |
| 73 | + g.Expect(details.Code).To(Equal(c.errorCode)) |
| 74 | + g.Expect(details.Message).To(ContainSubstring(c.errorMessage)) |
| 75 | + g.Expect(mockCalled).To(BeTrue()) |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments