fix flake test with GetWorksByBindingID func

Signed-off-by: changzhen <changzhen5@huawei.com>
This commit is contained in:
changzhen 2024-12-31 11:07:49 +08:00
parent 492e24dd8c
commit 8c6d0a4ee7
1 changed files with 29 additions and 21 deletions

View File

@ -309,17 +309,17 @@ func TestGetWorksByBindingID(t *testing.T) {
assert.NoError(t, workv1alpha1.Install(scheme)) assert.NoError(t, workv1alpha1.Install(scheme))
bindingID := "test-binding-id" bindingID := "test-binding-id"
work1 := &workv1alpha1.Work{ workWithRBID := &workv1alpha1.Work{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "work1", Name: "workWithRBID",
Labels: map[string]string{ Labels: map[string]string{
workv1alpha2.ResourceBindingPermanentIDLabel: bindingID, workv1alpha2.ResourceBindingPermanentIDLabel: bindingID,
}, },
}, },
} }
work2 := &workv1alpha1.Work{ workWithCRBID := &workv1alpha1.Work{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "work2", Name: "workWithCRBID",
Labels: map[string]string{ Labels: map[string]string{
workv1alpha2.ClusterResourceBindingPermanentIDLabel: bindingID, workv1alpha2.ClusterResourceBindingPermanentIDLabel: bindingID,
}, },
@ -327,36 +327,44 @@ func TestGetWorksByBindingID(t *testing.T) {
} }
tests := []struct { tests := []struct {
name string name string
works []client.Object works []client.Object
bindingID string bindingID string
namespaced bool permanentIDKey string
wantWorks []string namespaced bool
wantWorks []string
}{ }{
{ {
name: "find namespaced binding works", name: "find namespaced binding works",
works: []client.Object{work1, work2}, works: []client.Object{workWithRBID, workWithCRBID},
bindingID: bindingID, bindingID: bindingID,
namespaced: true, permanentIDKey: workv1alpha2.ResourceBindingPermanentIDLabel,
wantWorks: []string{"work1"}, namespaced: true,
wantWorks: []string{"workWithRBID"},
}, },
{ {
name: "find cluster binding works", name: "find cluster binding works",
works: []client.Object{work1, work2}, works: []client.Object{workWithRBID, workWithCRBID},
bindingID: bindingID, bindingID: bindingID,
namespaced: false, permanentIDKey: workv1alpha2.ClusterResourceBindingPermanentIDLabel,
wantWorks: []string{"work2"}, namespaced: false,
wantWorks: []string{"workWithCRBID"},
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
client := fake.NewClientBuilder(). fakeClient := fake.NewClientBuilder().
WithScheme(scheme). WithScheme(scheme).
WithIndex(
&workv1alpha1.Work{},
tt.permanentIDKey,
IndexerFuncBasedOnLabel(tt.permanentIDKey),
).
WithObjects(tt.works...). WithObjects(tt.works...).
Build() Build()
workList, err := GetWorksByBindingID(context.TODO(), client, tt.bindingID, tt.namespaced) workList, err := GetWorksByBindingID(context.TODO(), fakeClient, tt.bindingID, tt.namespaced)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, len(tt.wantWorks), len(workList.Items)) assert.Equal(t, len(tt.wantWorks), len(workList.Items))