|
| 1 | +/* |
| 2 | +Copyright (c) Microsoft Corporation. |
| 3 | +Licensed under the MIT license. |
| 4 | +*/ |
| 5 | + |
| 6 | +package drain |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/google/go-cmp/cmp" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/runtime" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 18 | + |
| 19 | + placementv1beta1 "go.goms.io/fleet/apis/placement/v1beta1" |
| 20 | + "go.goms.io/fleet/pkg/utils" |
| 21 | +) |
| 22 | + |
| 23 | +func TestFetchClusterResourcePlacementToEvict(t *testing.T) { |
| 24 | + tests := []struct { |
| 25 | + name string |
| 26 | + targetCluster string |
| 27 | + bindings []placementv1beta1.ClusterResourceBinding |
| 28 | + works []placementv1beta1.Work |
| 29 | + wantErr error |
| 30 | + wantMap map[string][]placementv1beta1.ResourceIdentifier |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "successfully collected resources for CRP", |
| 34 | + targetCluster: "test-cluster1", |
| 35 | + bindings: []placementv1beta1.ClusterResourceBinding{ |
| 36 | + { |
| 37 | + ObjectMeta: metav1.ObjectMeta{ |
| 38 | + Name: "test-crb1", |
| 39 | + Labels: map[string]string{ |
| 40 | + placementv1beta1.CRPTrackingLabel: "test-crp1", |
| 41 | + }, |
| 42 | + }, |
| 43 | + Spec: placementv1beta1.ResourceBindingSpec{ |
| 44 | + TargetCluster: "test-cluster1", |
| 45 | + State: placementv1beta1.BindingStateBound, |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + ObjectMeta: metav1.ObjectMeta{ |
| 50 | + Name: "test-crb2", |
| 51 | + Labels: map[string]string{ |
| 52 | + placementv1beta1.CRPTrackingLabel: "test-crp1", |
| 53 | + }, |
| 54 | + }, |
| 55 | + Spec: placementv1beta1.ResourceBindingSpec{ |
| 56 | + TargetCluster: "test-cluster2", |
| 57 | + State: placementv1beta1.BindingStateBound, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + works: []placementv1beta1.Work{ |
| 62 | + { |
| 63 | + ObjectMeta: metav1.ObjectMeta{ |
| 64 | + Name: "test-work1", |
| 65 | + Namespace: fmt.Sprintf(utils.NamespaceNameFormat, "test-cluster1"), |
| 66 | + Labels: map[string]string{ |
| 67 | + placementv1beta1.CRPTrackingLabel: "test-crp1", |
| 68 | + }, |
| 69 | + }, |
| 70 | + Status: placementv1beta1.WorkStatus{ |
| 71 | + ManifestConditions: []placementv1beta1.ManifestCondition{ |
| 72 | + { |
| 73 | + Identifier: placementv1beta1.WorkResourceIdentifier{ |
| 74 | + Group: "test-group1", |
| 75 | + Version: "test-version1", |
| 76 | + Kind: "test-kind1", |
| 77 | + Name: "test-name1", |
| 78 | + }, |
| 79 | + Conditions: []metav1.Condition{ |
| 80 | + { |
| 81 | + Type: placementv1beta1.WorkConditionTypeApplied, |
| 82 | + Status: metav1.ConditionTrue, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + ObjectMeta: metav1.ObjectMeta{ |
| 91 | + Name: "test-work2", |
| 92 | + Namespace: fmt.Sprintf(utils.NamespaceNameFormat, "test-cluster1"), |
| 93 | + Labels: map[string]string{ |
| 94 | + placementv1beta1.CRPTrackingLabel: "test-crp1", |
| 95 | + }, |
| 96 | + }, |
| 97 | + Status: placementv1beta1.WorkStatus{ |
| 98 | + ManifestConditions: []placementv1beta1.ManifestCondition{ |
| 99 | + { |
| 100 | + Identifier: placementv1beta1.WorkResourceIdentifier{ |
| 101 | + Group: "test-group2", |
| 102 | + Version: "test-version2", |
| 103 | + Kind: "test-kind2", |
| 104 | + Name: "test-name2", |
| 105 | + }, |
| 106 | + Conditions: []metav1.Condition{ |
| 107 | + { |
| 108 | + Type: placementv1beta1.WorkConditionTypeApplied, |
| 109 | + Status: metav1.ConditionTrue, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + { |
| 117 | + ObjectMeta: metav1.ObjectMeta{ |
| 118 | + Name: "test-work3", |
| 119 | + Namespace: fmt.Sprintf(utils.NamespaceNameFormat, "test-cluster2"), |
| 120 | + Labels: map[string]string{ |
| 121 | + placementv1beta1.CRPTrackingLabel: "test-crp1", |
| 122 | + }, |
| 123 | + }, |
| 124 | + Status: placementv1beta1.WorkStatus{ |
| 125 | + ManifestConditions: []placementv1beta1.ManifestCondition{ |
| 126 | + { |
| 127 | + Identifier: placementv1beta1.WorkResourceIdentifier{ |
| 128 | + Group: "test-group3", |
| 129 | + Version: "test-version3", |
| 130 | + Kind: "test-kind3", |
| 131 | + Name: "test-name3", |
| 132 | + }, |
| 133 | + Conditions: []metav1.Condition{ |
| 134 | + { |
| 135 | + Type: placementv1beta1.WorkConditionTypeApplied, |
| 136 | + Status: metav1.ConditionTrue, |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + wantErr: nil, |
| 145 | + wantMap: map[string][]placementv1beta1.ResourceIdentifier{ |
| 146 | + "test-crp1": { |
| 147 | + { |
| 148 | + Group: "test-group1", |
| 149 | + Version: "test-version1", |
| 150 | + Kind: "test-kind1", |
| 151 | + Name: "test-name1", |
| 152 | + }, |
| 153 | + { |
| 154 | + Group: "test-group2", |
| 155 | + Version: "test-version2", |
| 156 | + Kind: "test-kind2", |
| 157 | + Name: "test-name2", |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "failed to wait for placement to be present", |
| 164 | + targetCluster: "test-cluster1", |
| 165 | + bindings: []placementv1beta1.ClusterResourceBinding{ |
| 166 | + { |
| 167 | + ObjectMeta: metav1.ObjectMeta{ |
| 168 | + Name: "test-crb1", |
| 169 | + Labels: map[string]string{ |
| 170 | + placementv1beta1.CRPTrackingLabel: "test-crp1", |
| 171 | + }, |
| 172 | + }, |
| 173 | + Spec: placementv1beta1.ResourceBindingSpec{ |
| 174 | + TargetCluster: "test-cluster1", |
| 175 | + State: placementv1beta1.BindingStateScheduled, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + wantMap: map[string][]placementv1beta1.ResourceIdentifier{}, |
| 180 | + wantErr: fmt.Errorf("failed to wait for placement to be present on member cluster: timed out waiting for the condition"), |
| 181 | + }, |
| 182 | + } |
| 183 | + |
| 184 | + for _, tc := range tests { |
| 185 | + t.Run(tc.name, func(t *testing.T) { |
| 186 | + var objects []client.Object |
| 187 | + scheme := serviceScheme(t) |
| 188 | + for i := range tc.bindings { |
| 189 | + objects = append(objects, &tc.bindings[i]) |
| 190 | + } |
| 191 | + for i := range tc.works { |
| 192 | + objects = append(objects, &tc.works[i]) |
| 193 | + } |
| 194 | + fakeClient := fake.NewClientBuilder(). |
| 195 | + WithScheme(scheme). |
| 196 | + WithObjects(objects...). |
| 197 | + Build() |
| 198 | + d := &Helper{ |
| 199 | + HubClient: fakeClient, |
| 200 | + ClusterName: tc.targetCluster, |
| 201 | + ClusterResourcePlacementResourcesMap: make(map[string][]placementv1beta1.ResourceIdentifier), |
| 202 | + } |
| 203 | + gotErr := d.fetchClusterResourcePlacementToEvict(context.Background()) |
| 204 | + if tc.wantErr == nil { |
| 205 | + if gotErr != nil { |
| 206 | + t.Errorf("fetchClusterResourcePlacementToEvict() test %s failed, got error %v, want error %v", tc.name, gotErr, tc.wantErr) |
| 207 | + } |
| 208 | + } else if gotErr == nil || gotErr.Error() != tc.wantErr.Error() { |
| 209 | + t.Errorf("fetchClusterResourcePlacementToEvict() test %s failed, got error %v, want error %v", tc.name, gotErr, tc.wantErr) |
| 210 | + } |
| 211 | + if diff := cmp.Diff(d.ClusterResourcePlacementResourcesMap, tc.wantMap); diff != "" { |
| 212 | + t.Errorf("fetchClusterResourcePlacementToEvict() test %s failed, got %v, want %v", tc.name, d.ClusterResourcePlacementResourcesMap, tc.wantMap) |
| 213 | + } |
| 214 | + }) |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +func serviceScheme(t *testing.T) *runtime.Scheme { |
| 219 | + scheme := runtime.NewScheme() |
| 220 | + if err := placementv1beta1.AddToScheme(scheme); err != nil { |
| 221 | + t.Fatalf("failed to add placement v1beta1 scheme: %v", err) |
| 222 | + } |
| 223 | + return scheme |
| 224 | +} |
0 commit comments