Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Fix CRS e2e helper with multiple bindings #10191

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion test/framework/clusterresourceset_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,27 @@ func WaitForClusterResourceSetToApplyResources(ctx context.Context, input WaitFo
continue
}

if len(binding.Spec.Bindings) == 0 || !binding.Spec.Bindings[0].IsApplied(resource) {
// Check relevant ResourceSetBinding to see if the resource is applied. If no ResourceSetBinding is found for
// the specified ClusterResourceSet, the resource has not applied.
resourceSetBinding := getResourceSetBindingForClusterResourceSet(binding, input.ClusterResourceSet)
if resourceSetBinding == nil || !resourceSetBinding.IsApplied(resource) {
return false
}
}
return true
}, intervals...).Should(BeTrue())
}

func getResourceSetBindingForClusterResourceSet(
clusterResourceSetBinding *addonsv1.ClusterResourceSetBinding, clusterResourceSet *addonsv1.ClusterResourceSet,
) *addonsv1.ResourceSetBinding {
if clusterResourceSetBinding == nil || clusterResourceSet == nil {
return nil
}
for _, binding := range clusterResourceSetBinding.Spec.Bindings {
if binding.ClusterResourceSetName == clusterResourceSet.Name {
return binding
}
}
return nil
}
135 changes: 135 additions & 0 deletions test/framework/clusterresourceset_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package framework

import (
"testing"

. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
)

func Test_getResourceSetBindingForClusterResourceSet(t *testing.T) {
tests := []struct {
name string
inputCRSB *addonsv1.ClusterResourceSetBinding
inputCRS *addonsv1.ClusterResourceSet
want *addonsv1.ResourceSetBinding
}{{
name: "nil inputs",
want: nil,
}, {
name: "nil CRS",
inputCRSB: &addonsv1.ClusterResourceSetBinding{},
want: nil,
}, {
name: "nil CRSB",
inputCRS: &addonsv1.ClusterResourceSet{},
want: nil,
}, {
name: "CRSB with no bindings",
inputCRSB: &addonsv1.ClusterResourceSetBinding{},
inputCRS: &addonsv1.ClusterResourceSet{},
want: nil,
}, {
name: "CRSB with no matching bindings",
inputCRSB: &addonsv1.ClusterResourceSetBinding{
Spec: addonsv1.ClusterResourceSetBindingSpec{
Bindings: []*addonsv1.ResourceSetBinding{
{ClusterResourceSetName: "bar"},
},
},
},
inputCRS: &addonsv1.ClusterResourceSet{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
want: nil,
}, {
name: "CRSB with single matching bindings",
inputCRSB: &addonsv1.ClusterResourceSetBinding{
Spec: addonsv1.ClusterResourceSetBindingSpec{
Bindings: []*addonsv1.ResourceSetBinding{
{ClusterResourceSetName: "foo"},
},
},
},
inputCRS: &addonsv1.ClusterResourceSet{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
want: &addonsv1.ResourceSetBinding{ClusterResourceSetName: "foo"},
}, {
name: "CRSB with multiple bindings with match at index 0",
inputCRSB: &addonsv1.ClusterResourceSetBinding{
Spec: addonsv1.ClusterResourceSetBindingSpec{
Bindings: []*addonsv1.ResourceSetBinding{
{ClusterResourceSetName: "foo"},
{ClusterResourceSetName: "bar"},
},
},
},
inputCRS: &addonsv1.ClusterResourceSet{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
want: &addonsv1.ResourceSetBinding{ClusterResourceSetName: "foo"},
}, {
name: "CRSB with multiple bindings with match at index 1",
inputCRSB: &addonsv1.ClusterResourceSetBinding{
Spec: addonsv1.ClusterResourceSetBindingSpec{
Bindings: []*addonsv1.ResourceSetBinding{
{ClusterResourceSetName: "bar"},
{ClusterResourceSetName: "foo"},
},
},
},
inputCRS: &addonsv1.ClusterResourceSet{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
want: &addonsv1.ResourceSetBinding{ClusterResourceSetName: "foo"},
}, {
name: "CRSB with multiple bindings with match at middle index",
inputCRSB: &addonsv1.ClusterResourceSetBinding{
Spec: addonsv1.ClusterResourceSetBindingSpec{
Bindings: []*addonsv1.ResourceSetBinding{
{ClusterResourceSetName: "bar"},
{ClusterResourceSetName: "foo"},
{ClusterResourceSetName: "baz"},
},
},
},
inputCRS: &addonsv1.ClusterResourceSet{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
want: &addonsv1.ResourceSetBinding{ClusterResourceSetName: "foo"},
}, {
name: "CRSB with multiple bindings with match at last index",
inputCRSB: &addonsv1.ClusterResourceSetBinding{
Spec: addonsv1.ClusterResourceSetBindingSpec{
Bindings: []*addonsv1.ResourceSetBinding{
{ClusterResourceSetName: "bar"},
{ClusterResourceSetName: "baz"},
{ClusterResourceSetName: "foo"},
},
},
},
inputCRS: &addonsv1.ClusterResourceSet{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
want: &addonsv1.ResourceSetBinding{ClusterResourceSetName: "foo"},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

g.Expect(
getResourceSetBindingForClusterResourceSet(
tt.inputCRSB,
tt.inputCRS,
),
).To(Equal(tt.want))
})
}
}