|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package db_cluster |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "testing" |
| 19 | + |
| 20 | + ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" |
| 21 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + |
| 24 | + svcapitypes "github.com/aws-controllers-k8s/rds-controller/apis/v1alpha1" |
| 25 | +) |
| 26 | + |
| 27 | +func TestNewCustomUpdateRequestPayload_PreferredBackupWindow(t *testing.T) { |
| 28 | + // Test case to verify that PreferredBackupWindow is included in the ModifyDBCluster API call |
| 29 | + // when Spec.PreferredBackupWindow is in the delta |
| 30 | + |
| 31 | + // Setup |
| 32 | + rm := &resourceManager{} |
| 33 | + ctx := context.Background() |
| 34 | + |
| 35 | + // Create desired resource with PreferredBackupWindow set |
| 36 | + desired := &resource{ |
| 37 | + ko: &svcapitypes.DBCluster{ |
| 38 | + Spec: svcapitypes.DBClusterSpec{ |
| 39 | + DBClusterIdentifier: aws.String("test-cluster"), |
| 40 | + PreferredBackupWindow: aws.String("07:00-09:00"), |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + // Create latest resource with different PreferredBackupWindow |
| 46 | + latest := &resource{ |
| 47 | + ko: &svcapitypes.DBCluster{ |
| 48 | + Spec: svcapitypes.DBClusterSpec{ |
| 49 | + DBClusterIdentifier: aws.String("test-cluster"), |
| 50 | + PreferredBackupWindow: aws.String("05:00-07:00"), |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + // Create delta with PreferredBackupWindow difference |
| 56 | + delta := ackcompare.NewDelta() |
| 57 | + delta.Add("Spec.PreferredBackupWindow", latest.ko.Spec.PreferredBackupWindow, desired.ko.Spec.PreferredBackupWindow) |
| 58 | + |
| 59 | + // Call the function under test |
| 60 | + input, err := rm.newCustomUpdateRequestPayload(ctx, desired, latest, delta) |
| 61 | + |
| 62 | + // Assertions |
| 63 | + assert.NoError(t, err) |
| 64 | + assert.NotNil(t, input) |
| 65 | + assert.Equal(t, *desired.ko.Spec.PreferredBackupWindow, *input.PreferredBackupWindow) |
| 66 | +} |
| 67 | + |
| 68 | +func TestNewCustomUpdateRequestPayload_PreferredBackupWindowNotInDelta(t *testing.T) { |
| 69 | + // Test case to verify that PreferredBackupWindow is NOT included in the ModifyDBCluster API call |
| 70 | + // when Spec.PreferredBackupWindow is NOT in the delta |
| 71 | + |
| 72 | + // Setup |
| 73 | + rm := &resourceManager{} |
| 74 | + ctx := context.Background() |
| 75 | + |
| 76 | + // Create desired resource with PreferredBackupWindow set |
| 77 | + desired := &resource{ |
| 78 | + ko: &svcapitypes.DBCluster{ |
| 79 | + Spec: svcapitypes.DBClusterSpec{ |
| 80 | + DBClusterIdentifier: aws.String("test-cluster"), |
| 81 | + PreferredBackupWindow: aws.String("07:00-09:00"), |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + // Create latest resource with same PreferredBackupWindow |
| 87 | + latest := &resource{ |
| 88 | + ko: &svcapitypes.DBCluster{ |
| 89 | + Spec: svcapitypes.DBClusterSpec{ |
| 90 | + DBClusterIdentifier: aws.String("test-cluster"), |
| 91 | + PreferredBackupWindow: aws.String("07:00-09:00"), |
| 92 | + }, |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + // Create delta without PreferredBackupWindow difference |
| 97 | + delta := ackcompare.NewDelta() |
| 98 | + |
| 99 | + // Call the function under test |
| 100 | + input, err := rm.newCustomUpdateRequestPayload(ctx, desired, latest, delta) |
| 101 | + |
| 102 | + // Assertions |
| 103 | + assert.NoError(t, err) |
| 104 | + assert.NotNil(t, input) |
| 105 | + assert.Nil(t, input.PreferredBackupWindow) |
| 106 | +} |
0 commit comments