|
| 1 | +package frontend |
| 2 | + |
| 3 | +// Copyright (c) Microsoft Corporation. |
| 4 | +// Licensed under the Apache License 2.0. |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "go.uber.org/mock/gomock" |
| 12 | + |
| 13 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armfeatures" |
| 14 | + |
| 15 | + mock_frontend "github.com/Azure/ARO-RP/pkg/util/mocks/frontend" |
| 16 | + "github.com/Azure/ARO-RP/pkg/util/pointerutils" |
| 17 | +) |
| 18 | + |
| 19 | +func TestValidateEncryptionAtHostFeature(t *testing.T) { |
| 20 | + ctx := context.Background() |
| 21 | + |
| 22 | + for _, tt := range []struct { |
| 23 | + name string |
| 24 | + fieldPath string |
| 25 | + mockFeatureState *string |
| 26 | + mockPropertiesNil bool |
| 27 | + mockStateNil bool |
| 28 | + mockGetErr error |
| 29 | + wantErr string |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "feature is registered - should return nil", |
| 33 | + fieldPath: "properties.masterProfile.encryptionAtHost", |
| 34 | + mockFeatureState: pointerutils.ToPtr("Registered"), |
| 35 | + wantErr: "", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "feature is not registered - should return 400 BadRequest", |
| 39 | + fieldPath: "properties.masterProfile.encryptionAtHost", |
| 40 | + mockFeatureState: pointerutils.ToPtr("NotRegistered"), |
| 41 | + wantErr: "400: InvalidParameter: properties.masterProfile.encryptionAtHost: Microsoft.Compute/EncryptionAtHost feature is not registered on subscription test-subscription. Please register the feature on your subscription before creating the cluster.", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "resp.Properties is nil - should return internal server error", |
| 45 | + fieldPath: "properties.workerProfiles[0].encryptionAtHost", |
| 46 | + mockPropertiesNil: true, |
| 47 | + wantErr: "500: InternalServerError: : Microsoft.Compute/EncryptionAtHost feature has no state for subscription test-subscription.", |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "resp.Properties.State is nil - should return internal server error", |
| 51 | + fieldPath: "properties.masterProfile.encryptionAtHost", |
| 52 | + mockStateNil: true, |
| 53 | + wantErr: "500: InternalServerError: : Microsoft.Compute/EncryptionAtHost feature has no state for subscription test-subscription.", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Get returns error", |
| 57 | + fieldPath: "properties.masterProfile.encryptionAtHost", |
| 58 | + mockGetErr: errors.New("random error"), |
| 59 | + wantErr: "random error", |
| 60 | + }, |
| 61 | + } { |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + controller := gomock.NewController(t) |
| 64 | + defer controller.Finish() |
| 65 | + |
| 66 | + featuresClient := mock_frontend.NewMockFeaturesClient(controller) |
| 67 | + |
| 68 | + var mockResponse armfeatures.ClientGetResponse |
| 69 | + if tt.mockPropertiesNil { |
| 70 | + mockResponse = armfeatures.ClientGetResponse{ |
| 71 | + FeatureResult: armfeatures.FeatureResult{ |
| 72 | + Properties: nil, |
| 73 | + }, |
| 74 | + } |
| 75 | + } else if tt.mockStateNil { |
| 76 | + mockResponse = armfeatures.ClientGetResponse{ |
| 77 | + FeatureResult: armfeatures.FeatureResult{ |
| 78 | + Properties: &armfeatures.FeatureProperties{ |
| 79 | + State: nil, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | + } else if tt.mockFeatureState != nil { |
| 84 | + mockResponse = armfeatures.ClientGetResponse{ |
| 85 | + FeatureResult: armfeatures.FeatureResult{ |
| 86 | + Properties: &armfeatures.FeatureProperties{ |
| 87 | + State: tt.mockFeatureState, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + featuresClient.EXPECT(). |
| 94 | + Get(ctx, "Microsoft.Compute", "EncryptionAtHost", nil). |
| 95 | + Return(mockResponse, tt.mockGetErr) |
| 96 | + |
| 97 | + err := validateEncryptionAtHostFeature(ctx, featuresClient, "test-subscription", tt.fieldPath) |
| 98 | + if err != nil && err.Error() != tt.wantErr || |
| 99 | + err == nil && tt.wantErr != "" { |
| 100 | + t.Errorf("expected error %q, got %q", tt.wantErr, err) |
| 101 | + } |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
0 commit comments