Skip to content

Commit 6daf4ac

Browse files
authored
🌱 Fix CRS e2e helper with multiple bindings (#10191)
* test(e2e): Fix CRS helper with multiple bindings Check the relevant ResourceSetBinding to see if the resource is applied. If multiple ClusterResourceSets match a cluster, the ClusterResourceSetBinding will have multiple bindings and so only the relevant ResourceSetBinding should be checked. * fixup! refactor: Apply review suggestions * fixup! test: Add test for CRS e2e helper
1 parent 9949d93 commit 6daf4ac

File tree

2 files changed

+153
-1
lines changed

2 files changed

+153
-1
lines changed

test/framework/clusterresourceset_helpers.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,27 @@ func WaitForClusterResourceSetToApplyResources(ctx context.Context, input WaitFo
150150
continue
151151
}
152152

153-
if len(binding.Spec.Bindings) == 0 || !binding.Spec.Bindings[0].IsApplied(resource) {
153+
// Check relevant ResourceSetBinding to see if the resource is applied. If no ResourceSetBinding is found for
154+
// the specified ClusterResourceSet, the resource has not applied.
155+
resourceSetBinding := getResourceSetBindingForClusterResourceSet(binding, input.ClusterResourceSet)
156+
if resourceSetBinding == nil || !resourceSetBinding.IsApplied(resource) {
154157
return false
155158
}
156159
}
157160
return true
158161
}, intervals...).Should(BeTrue())
159162
}
163+
164+
func getResourceSetBindingForClusterResourceSet(
165+
clusterResourceSetBinding *addonsv1.ClusterResourceSetBinding, clusterResourceSet *addonsv1.ClusterResourceSet,
166+
) *addonsv1.ResourceSetBinding {
167+
if clusterResourceSetBinding == nil || clusterResourceSet == nil {
168+
return nil
169+
}
170+
for _, binding := range clusterResourceSetBinding.Spec.Bindings {
171+
if binding.ClusterResourceSetName == clusterResourceSet.Name {
172+
return binding
173+
}
174+
}
175+
return nil
176+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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

Comments
 (0)