|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package framework |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + |
| 25 | + addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1" |
| 26 | +) |
| 27 | + |
| 28 | +func Test_getResourceSetBindingForClusterResourceSet(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + inputCRSB *addonsv1.ClusterResourceSetBinding |
| 32 | + inputCRS *addonsv1.ClusterResourceSet |
| 33 | + want *addonsv1.ResourceSetBinding |
| 34 | + }{{ |
| 35 | + name: "nil inputs", |
| 36 | + want: nil, |
| 37 | + }, { |
| 38 | + name: "nil CRS", |
| 39 | + inputCRSB: &addonsv1.ClusterResourceSetBinding{}, |
| 40 | + want: nil, |
| 41 | + }, { |
| 42 | + name: "nil CRSB", |
| 43 | + inputCRS: &addonsv1.ClusterResourceSet{}, |
| 44 | + want: nil, |
| 45 | + }, { |
| 46 | + name: "CRSB with no bindings", |
| 47 | + inputCRSB: &addonsv1.ClusterResourceSetBinding{}, |
| 48 | + inputCRS: &addonsv1.ClusterResourceSet{}, |
| 49 | + want: nil, |
| 50 | + }, { |
| 51 | + name: "CRSB with no matching bindings", |
| 52 | + inputCRSB: &addonsv1.ClusterResourceSetBinding{ |
| 53 | + Spec: addonsv1.ClusterResourceSetBindingSpec{ |
| 54 | + Bindings: []*addonsv1.ResourceSetBinding{ |
| 55 | + {ClusterResourceSetName: "bar"}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + inputCRS: &addonsv1.ClusterResourceSet{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, |
| 60 | + want: nil, |
| 61 | + }, { |
| 62 | + name: "CRSB with single matching bindings", |
| 63 | + inputCRSB: &addonsv1.ClusterResourceSetBinding{ |
| 64 | + Spec: addonsv1.ClusterResourceSetBindingSpec{ |
| 65 | + Bindings: []*addonsv1.ResourceSetBinding{ |
| 66 | + {ClusterResourceSetName: "foo"}, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + inputCRS: &addonsv1.ClusterResourceSet{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, |
| 71 | + want: &addonsv1.ResourceSetBinding{ClusterResourceSetName: "foo"}, |
| 72 | + }, { |
| 73 | + name: "CRSB with multiple bindings with match at index 0", |
| 74 | + inputCRSB: &addonsv1.ClusterResourceSetBinding{ |
| 75 | + Spec: addonsv1.ClusterResourceSetBindingSpec{ |
| 76 | + Bindings: []*addonsv1.ResourceSetBinding{ |
| 77 | + {ClusterResourceSetName: "foo"}, |
| 78 | + {ClusterResourceSetName: "bar"}, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + inputCRS: &addonsv1.ClusterResourceSet{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, |
| 83 | + want: &addonsv1.ResourceSetBinding{ClusterResourceSetName: "foo"}, |
| 84 | + }, { |
| 85 | + name: "CRSB with multiple bindings with match at index 1", |
| 86 | + inputCRSB: &addonsv1.ClusterResourceSetBinding{ |
| 87 | + Spec: addonsv1.ClusterResourceSetBindingSpec{ |
| 88 | + Bindings: []*addonsv1.ResourceSetBinding{ |
| 89 | + {ClusterResourceSetName: "bar"}, |
| 90 | + {ClusterResourceSetName: "foo"}, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + inputCRS: &addonsv1.ClusterResourceSet{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, |
| 95 | + want: &addonsv1.ResourceSetBinding{ClusterResourceSetName: "foo"}, |
| 96 | + }, { |
| 97 | + name: "CRSB with multiple bindings with match at middle index", |
| 98 | + inputCRSB: &addonsv1.ClusterResourceSetBinding{ |
| 99 | + Spec: addonsv1.ClusterResourceSetBindingSpec{ |
| 100 | + Bindings: []*addonsv1.ResourceSetBinding{ |
| 101 | + {ClusterResourceSetName: "bar"}, |
| 102 | + {ClusterResourceSetName: "foo"}, |
| 103 | + {ClusterResourceSetName: "baz"}, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + inputCRS: &addonsv1.ClusterResourceSet{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, |
| 108 | + want: &addonsv1.ResourceSetBinding{ClusterResourceSetName: "foo"}, |
| 109 | + }, { |
| 110 | + name: "CRSB with multiple bindings with match at last index", |
| 111 | + inputCRSB: &addonsv1.ClusterResourceSetBinding{ |
| 112 | + Spec: addonsv1.ClusterResourceSetBindingSpec{ |
| 113 | + Bindings: []*addonsv1.ResourceSetBinding{ |
| 114 | + {ClusterResourceSetName: "bar"}, |
| 115 | + {ClusterResourceSetName: "baz"}, |
| 116 | + {ClusterResourceSetName: "foo"}, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + inputCRS: &addonsv1.ClusterResourceSet{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, |
| 121 | + want: &addonsv1.ResourceSetBinding{ClusterResourceSetName: "foo"}, |
| 122 | + }} |
| 123 | + for _, tt := range tests { |
| 124 | + t.Run(tt.name, func(t *testing.T) { |
| 125 | + g := NewWithT(t) |
| 126 | + |
| 127 | + g.Expect( |
| 128 | + getResourceSetBindingForClusterResourceSet( |
| 129 | + tt.inputCRSB, |
| 130 | + tt.inputCRS, |
| 131 | + ), |
| 132 | + ).To(Equal(tt.want)) |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments