Files
81c526d7d0 [kubectl-plugin][Test] Use client-go reactors for FieldSelector filtering in fake client tests (#4361)
* feat(kubectl-plugin): add FieldSelector reactor helper for fake client tests

Add AddRayClusterFieldSelectorReactor helper that simulates server-side
FieldSelector filtering in fake client tests. This addresses issue #4337
by allowing tests to verify filtering behavior without manual name checks.

Refs: #4337

* test(kubectl-plugin): apply FieldSelector reactor to completion tests

Use the new AddRayClusterFieldSelectorReactor in workergroup completion
tests to properly simulate server-side filtering behavior.

Refs: #4337

* refactor(kubectl-plugin): remove manual name filtering in completion

Remove the workaround that manually filtered clusters by name since the
fake client now properly supports FieldSelector filtering via reactors.

Refs: #4337

* chore(kubectl-plugin): migrate from NewSimpleClientset to NewClientset

NewSimpleClientset is deprecated in favor of NewClientset for better
server-side apply testing support.

Note: scale_cluster_test.go is not migrated because it uses Update
operations that require schema definitions missing from the generated
applyconfiguration internal schema.

Refs: #4337

* refactor(kubectl-plugin): add NewRayClientset wrapper for simpler test setup

Add a convenience wrapper that creates a fake Ray clientset with
FieldSelector reactor pre-configured. This simplifies test setup
and ensures consistent behavior across tests.

Also applies reactor to get_cluster_test.go and fixes test data
to match actual cluster names now that FieldSelector properly filters.

Refs: #4337

* Update kubectl-plugin/pkg/util/client/testing/reactor.go

Co-authored-by: JustinYeh <[email protected]>
Signed-off-by: Ikenna <[email protected]>

* fix(kubectl-plugin): update references to renamed reactor function

Update clientset.go and reactor_test.go to use the renamed function
AddRayClusterListFieldSelectorReactor.

---------

Signed-off-by: Ikenna <[email protected]>
Co-authored-by: JustinYeh <[email protected]>
2026-01-12 22:18:46 +08:00

94 lines
2.3 KiB
Go

package testing
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
rayClientFake "github.com/ray-project/kuberay/ray-operator/pkg/client/clientset/versioned/fake"
)
func TestAddRayClusterListFieldSelectorReactor(t *testing.T) {
cluster1 := &rayv1.RayCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-1",
Namespace: "default",
},
}
cluster2 := &rayv1.RayCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-2",
Namespace: "default",
},
}
cluster3 := &rayv1.RayCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-3",
Namespace: "other",
},
}
tests := []struct {
name string
namespace string
fieldSelector string
expectedNames []string
expectedCount int
}{
{
name: "no field selector returns all clusters",
namespace: "default",
fieldSelector: "",
expectedCount: 2,
expectedNames: []string{"cluster-1", "cluster-2"},
},
{
name: "field selector filters by name",
namespace: "default",
fieldSelector: "metadata.name=cluster-1",
expectedCount: 1,
expectedNames: []string{"cluster-1"},
},
{
name: "field selector with non-existent name returns empty",
namespace: "default",
fieldSelector: "metadata.name=non-existent",
expectedCount: 0,
expectedNames: []string{},
},
{
name: "namespace filtering returns only clusters in specified namespace",
namespace: "other",
fieldSelector: "",
expectedCount: 1,
expectedNames: []string{"cluster-3"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
rayClient := rayClientFake.NewClientset(cluster1, cluster2, cluster3)
AddRayClusterListFieldSelectorReactor(rayClient)
listOpts := metav1.ListOptions{}
if tc.fieldSelector != "" {
listOpts.FieldSelector = tc.fieldSelector
}
result, err := rayClient.RayV1().RayClusters(tc.namespace).List(context.Background(), listOpts)
require.NoError(t, err)
assert.Len(t, result.Items, tc.expectedCount)
names := make([]string, len(result.Items))
for i, cluster := range result.Items {
names[i] = cluster.Name
}
assert.ElementsMatch(t, tc.expectedNames, names)
})
}
}