|
| 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 compare_test |
| 15 | + |
| 16 | +import ( |
| 17 | + "reflect" |
| 18 | + "testing" |
| 19 | + |
| 20 | + k8scorev1 "k8s.io/api/core/v1" |
| 21 | + |
| 22 | + ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" |
| 23 | + ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" |
| 24 | +) |
| 25 | + |
| 26 | +// newSecretReference is used a instantiate a secret reference used for testing purposes. |
| 27 | +func newSecretReference(name string) *ackv1alpha1.SecretKeyReference { |
| 28 | + return &ackv1alpha1.SecretKeyReference{ |
| 29 | + SecretReference: k8scorev1.SecretReference{ |
| 30 | + Namespace: "default", |
| 31 | + Name: name, |
| 32 | + }, |
| 33 | + Key: "password", |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestSliceSecretKeyReferenceEqual(t *testing.T) { |
| 38 | + type args struct { |
| 39 | + a []*ackv1alpha1.SecretKeyReference |
| 40 | + b []*ackv1alpha1.SecretKeyReference |
| 41 | + } |
| 42 | + tests := []struct { |
| 43 | + name string |
| 44 | + args args |
| 45 | + wantEqual bool |
| 46 | + wantAdded []*ackv1alpha1.SecretKeyReference |
| 47 | + wantRemoved []*ackv1alpha1.SecretKeyReference |
| 48 | + }{ |
| 49 | + { |
| 50 | + name: "empty slices", |
| 51 | + args: args{ |
| 52 | + a: nil, |
| 53 | + b: nil, |
| 54 | + }, |
| 55 | + wantEqual: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "empty slices - only one non nil slice", |
| 59 | + args: args{ |
| 60 | + a: nil, |
| 61 | + b: []*ackv1alpha1.SecretKeyReference{}, |
| 62 | + }, |
| 63 | + wantEqual: true, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "empty slices - two non nil slices", |
| 67 | + args: args{ |
| 68 | + a: []*ackv1alpha1.SecretKeyReference{}, |
| 69 | + b: []*ackv1alpha1.SecretKeyReference{}, |
| 70 | + }, |
| 71 | + wantEqual: true, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "added secrets", |
| 75 | + args: args{ |
| 76 | + a: []*ackv1alpha1.SecretKeyReference{}, |
| 77 | + b: []*ackv1alpha1.SecretKeyReference{ |
| 78 | + newSecretReference("secret1"), |
| 79 | + newSecretReference("secret2"), |
| 80 | + }, |
| 81 | + }, |
| 82 | + wantEqual: false, |
| 83 | + wantAdded: []*ackv1alpha1.SecretKeyReference{ |
| 84 | + newSecretReference("secret1"), |
| 85 | + newSecretReference("secret2"), |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "removed secrets", |
| 90 | + args: args{ |
| 91 | + a: []*ackv1alpha1.SecretKeyReference{ |
| 92 | + newSecretReference("secret1"), |
| 93 | + newSecretReference("secret2"), |
| 94 | + }, |
| 95 | + b: []*ackv1alpha1.SecretKeyReference{}, |
| 96 | + }, |
| 97 | + wantEqual: false, |
| 98 | + wantRemoved: []*ackv1alpha1.SecretKeyReference{ |
| 99 | + newSecretReference("secret1"), |
| 100 | + newSecretReference("secret2"), |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "added and removed secrets", |
| 105 | + args: args{ |
| 106 | + a: []*ackv1alpha1.SecretKeyReference{ |
| 107 | + newSecretReference("secret1"), |
| 108 | + }, |
| 109 | + b: []*ackv1alpha1.SecretKeyReference{ |
| 110 | + newSecretReference("secret2"), |
| 111 | + }, |
| 112 | + }, |
| 113 | + wantEqual: false, |
| 114 | + wantAdded: []*ackv1alpha1.SecretKeyReference{ |
| 115 | + newSecretReference("secret2"), |
| 116 | + }, |
| 117 | + wantRemoved: []*ackv1alpha1.SecretKeyReference{ |
| 118 | + newSecretReference("secret1"), |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "equal slices with duplicate elements", |
| 123 | + args: args{ |
| 124 | + a: []*ackv1alpha1.SecretKeyReference{ |
| 125 | + newSecretReference("secret1"), |
| 126 | + newSecretReference("secret1"), |
| 127 | + newSecretReference("secret1"), |
| 128 | + newSecretReference("secret2"), |
| 129 | + }, |
| 130 | + b: []*ackv1alpha1.SecretKeyReference{ |
| 131 | + newSecretReference("secret2"), |
| 132 | + newSecretReference("secret2"), |
| 133 | + newSecretReference("secret2"), |
| 134 | + newSecretReference("secret1"), |
| 135 | + }, |
| 136 | + }, |
| 137 | + wantEqual: true, |
| 138 | + wantAdded: nil, |
| 139 | + wantRemoved: nil, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "added and removed secrets with duplicate elements", |
| 143 | + args: args{ |
| 144 | + a: []*ackv1alpha1.SecretKeyReference{ |
| 145 | + newSecretReference("secret1"), |
| 146 | + newSecretReference("secret2"), |
| 147 | + newSecretReference("secret2"), |
| 148 | + newSecretReference("secret3"), |
| 149 | + }, |
| 150 | + b: []*ackv1alpha1.SecretKeyReference{ |
| 151 | + newSecretReference("secret3"), |
| 152 | + newSecretReference("secret4"), |
| 153 | + newSecretReference("secret4"), |
| 154 | + }, |
| 155 | + }, |
| 156 | + wantEqual: false, |
| 157 | + wantAdded: []*ackv1alpha1.SecretKeyReference{ |
| 158 | + newSecretReference("secret4"), |
| 159 | + }, |
| 160 | + wantRemoved: []*ackv1alpha1.SecretKeyReference{ |
| 161 | + newSecretReference("secret1"), |
| 162 | + newSecretReference("secret2"), |
| 163 | + }, |
| 164 | + }, |
| 165 | + } |
| 166 | + for _, tt := range tests { |
| 167 | + t.Run(tt.name, func(t *testing.T) { |
| 168 | + gotEqual, gotAdded, gotRemoved := ackcompare.CompareSecretKeyReferences(tt.args.a, tt.args.b) |
| 169 | + if gotEqual != tt.wantEqual { |
| 170 | + t.Errorf("SliceSecretKeyReferenceEqual() gotEqual = %v, want %v", gotEqual, tt.wantEqual) |
| 171 | + } |
| 172 | + if !reflect.DeepEqual(gotAdded, tt.wantAdded) { |
| 173 | + t.Errorf("SliceSecretKeyReferenceEqual() gotAdded = %v, want %v", gotAdded, tt.wantAdded) |
| 174 | + } |
| 175 | + if !reflect.DeepEqual(gotRemoved, tt.wantRemoved) { |
| 176 | + t.Errorf("SliceSecretKeyReferenceEqual() gotRemoved = %v, want %v", gotRemoved, tt.wantRemoved) |
| 177 | + } |
| 178 | + }) |
| 179 | + } |
| 180 | +} |
0 commit comments