Skip to content

Commit

Permalink
fix flake test with GetWorksByBindingID func
Browse files Browse the repository at this point in the history
Signed-off-by: changzhen <[email protected]>
  • Loading branch information
XiShanYongYe-Chang committed Dec 31, 2024
1 parent 492e24d commit 0820578
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions pkg/util/helper/work_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,54 +309,62 @@ func TestGetWorksByBindingID(t *testing.T) {
assert.NoError(t, workv1alpha1.Install(scheme))

bindingID := "test-binding-id"
work1 := &workv1alpha1.Work{
workWitRBID := &workv1alpha1.Work{
ObjectMeta: metav1.ObjectMeta{
Name: "work1",
Name: "workWitRBID",
Labels: map[string]string{
workv1alpha2.ResourceBindingPermanentIDLabel: bindingID,
},
},
}
work2 := &workv1alpha1.Work{
workWithCRBID := &workv1alpha1.Work{
ObjectMeta: metav1.ObjectMeta{
Name: "work2",
Name: "workWithCRBID",
Labels: map[string]string{
workv1alpha2.ClusterResourceBindingPermanentIDLabel: bindingID,
},
},
}

tests := []struct {
name string
works []client.Object
bindingID string
namespaced bool
wantWorks []string
name string
works []client.Object
bindingID string
permanentIDKey string
namespaced bool
wantWorks []string
}{
{
name: "find namespaced binding works",
works: []client.Object{work1, work2},
bindingID: bindingID,
namespaced: true,
wantWorks: []string{"work1"},
name: "find namespaced binding works",
works: []client.Object{workWitRBID, workWithCRBID},
bindingID: bindingID,
permanentIDKey: workv1alpha2.ResourceBindingPermanentIDLabel,
namespaced: true,
wantWorks: []string{"workWitRBID"},
},
{
name: "find cluster binding works",
works: []client.Object{work1, work2},
bindingID: bindingID,
namespaced: false,
wantWorks: []string{"work2"},
name: "find cluster binding works",
works: []client.Object{workWitRBID, workWithCRBID},
bindingID: bindingID,
permanentIDKey: workv1alpha2.ClusterResourceBindingPermanentIDLabel,
namespaced: false,
wantWorks: []string{"workWithCRBID"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := fake.NewClientBuilder().
fakeClient := fake.NewClientBuilder().
WithScheme(scheme).
WithIndex(
&workv1alpha1.Work{},
tt.permanentIDKey,
IndexerFuncBasedOnLabel(tt.permanentIDKey),
).
WithObjects(tt.works...).
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.Equal(t, len(tt.wantWorks), len(workList.Items))
Expand Down

0 comments on commit 0820578

Please sign in to comment.