Skip to content

Commit fab20d6

Browse files
committed
add some more tests
Signed-off-by: Vamshi Maskuri <[email protected]>
1 parent ffe6a0b commit fab20d6

File tree

1 file changed

+139
-5
lines changed

1 file changed

+139
-5
lines changed

pkg/scheduler/scheduler_test.go

Lines changed: 139 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,34 @@ import (
2929
"k8s.io/client-go/kubernetes/fake"
3030
"k8s.io/client-go/tools/record"
3131

32+
clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
3233
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
3334
karmadafake "github.com/karmada-io/karmada/pkg/generated/clientset/versioned/fake"
3435
"github.com/karmada-io/karmada/pkg/util"
35-
// "github.com/stretchr/testify/assert"
36+
"github.com/karmada-io/karmada/pkg/util/grpcconnection"
3637
)
3738

3839
func TestCreateScheduler(t *testing.T) {
3940
dynamicClient := dynamicfake.NewSimpleDynamicClient(runtime.NewScheme())
4041
karmadaClient := karmadafake.NewSimpleClientset()
4142
kubeClient := fake.NewSimpleClientset()
4243
port := 10025
44+
servicePrefix := "test-service-prefix"
45+
schedulerName := "test-scheduler"
46+
timeout := metav1.Duration{Duration: 5 * time.Second}
4347

4448
testcases := []struct {
45-
name string
46-
opts []Option
47-
enableSchedulerEstimator bool
48-
schedulerEstimatorPort int
49+
name string
50+
opts []Option
51+
enableSchedulerEstimator bool
52+
schedulerEstimatorPort int
53+
disableSchedulerEstimatorInPullMode bool
54+
schedulerEstimatorTimeout metav1.Duration
55+
schedulerEstimatorServicePrefix string
56+
schedulerName string
57+
schedulerEstimatorClientConfig *grpcconnection.ClientConfig
58+
enableEmptyWorkloadPropagation bool
59+
plugins []string
4960
}{
5061
{
5162
name: "scheduler with default configuration",
@@ -69,6 +80,53 @@ func TestCreateScheduler(t *testing.T) {
6980
},
7081
enableSchedulerEstimator: false,
7182
},
83+
{
84+
name: "scheduler with disableSchedulerEstimatorInPullMode enabled",
85+
opts: []Option{
86+
WithEnableSchedulerEstimator(true),
87+
WithSchedulerEstimatorConnection(port, "", "", "", false),
88+
WithDisableSchedulerEstimatorInPullMode(true),
89+
},
90+
enableSchedulerEstimator: true,
91+
schedulerEstimatorPort: port,
92+
disableSchedulerEstimatorInPullMode: true,
93+
},
94+
{
95+
name: "scheduler with SchedulerEstimatorServicePrefix enabled",
96+
opts: []Option{
97+
WithEnableSchedulerEstimator(true),
98+
WithSchedulerEstimatorConnection(port, "", "", "", false),
99+
WithSchedulerEstimatorServicePrefix(servicePrefix),
100+
},
101+
enableSchedulerEstimator: true,
102+
schedulerEstimatorPort: port,
103+
schedulerEstimatorServicePrefix: servicePrefix,
104+
},
105+
{
106+
name: "scheduler with SchedulerName enabled",
107+
opts: []Option{
108+
WithSchedulerName(schedulerName),
109+
},
110+
schedulerName: schedulerName,
111+
},
112+
{
113+
name: "scheduler with EnableEmptyWorkloadPropagation enabled",
114+
opts: []Option{
115+
WithEnableEmptyWorkloadPropagation(true),
116+
},
117+
enableEmptyWorkloadPropagation: true,
118+
},
119+
{
120+
name: "scheduler with SchedulerEstimatorTimeout enabled",
121+
opts: []Option{
122+
WithEnableSchedulerEstimator(true),
123+
WithSchedulerEstimatorConnection(port, "", "", "", false),
124+
WithSchedulerEstimatorTimeout(timeout),
125+
},
126+
enableSchedulerEstimator: true,
127+
schedulerEstimatorPort: port,
128+
schedulerEstimatorTimeout: timeout,
129+
},
72130
}
73131

74132
for _, tc := range testcases {
@@ -85,6 +143,82 @@ func TestCreateScheduler(t *testing.T) {
85143
if tc.enableSchedulerEstimator && tc.schedulerEstimatorPort != sche.schedulerEstimatorClientConfig.TargetPort {
86144
t.Errorf("unexpected schedulerEstimatorPort want %v, got %v", tc.schedulerEstimatorPort, sche.schedulerEstimatorClientConfig.TargetPort)
87145
}
146+
147+
if tc.disableSchedulerEstimatorInPullMode != sche.disableSchedulerEstimatorInPullMode {
148+
t.Errorf("unexpected disableSchedulerEstimatorInPullMode want %v, got %v", tc.disableSchedulerEstimatorInPullMode, sche.disableSchedulerEstimatorInPullMode)
149+
}
150+
151+
if tc.schedulerEstimatorServicePrefix != sche.schedulerEstimatorServicePrefix {
152+
t.Errorf("unexpected schedulerEstimatorServicePrefix want %v, got %v", tc.schedulerEstimatorServicePrefix, sche.schedulerEstimatorServicePrefix)
153+
}
154+
155+
if tc.schedulerName != sche.schedulerName {
156+
t.Errorf("unexpected schedulerName want %v, got %v", tc.schedulerName, sche.schedulerName)
157+
}
158+
159+
if tc.enableEmptyWorkloadPropagation != sche.enableEmptyWorkloadPropagation {
160+
t.Errorf("unexpected enableEmptyWorkloadPropagation want %v, got %v", tc.enableEmptyWorkloadPropagation, sche.enableEmptyWorkloadPropagation)
161+
}
162+
})
163+
}
164+
}
165+
166+
func TestReconcileEstimatorConnection(t *testing.T) {
167+
dynamicClient := dynamicfake.NewSimpleDynamicClient(runtime.NewScheme())
168+
karmadaClient := karmadafake.NewSimpleClientset()
169+
kubeClient := fake.NewSimpleClientset()
170+
scheduler, err := NewScheduler(dynamicClient, karmadaClient, kubeClient, WithEnableSchedulerEstimator(true))
171+
if err != nil {
172+
t.Fatalf("Failed to create scheduler: %v", err)
173+
}
174+
175+
clusterName := "test-cluster"
176+
cluster := &clusterv1alpha1.Cluster{
177+
ObjectMeta: metav1.ObjectMeta{Name: clusterName},
178+
Spec: clusterv1alpha1.ClusterSpec{SyncMode: clusterv1alpha1.Pull},
179+
}
180+
_, err = karmadaClient.ClusterV1alpha1().Clusters().Create(context.TODO(), cluster, metav1.CreateOptions{})
181+
if err != nil {
182+
t.Fatalf("Failed to create cluster: %v", err)
183+
}
184+
185+
tests := []struct {
186+
name string
187+
key util.QueueKey
188+
disableSchedulerEstimatorInPullMode bool
189+
expectedError bool
190+
}{
191+
{
192+
name: "Invalid key",
193+
key: 123,
194+
expectedError: true,
195+
},
196+
{
197+
name: "Cluster not found",
198+
key: "non-existent-cluster",
199+
expectedError: false,
200+
},
201+
{
202+
name: "Cluster in pull mode, estimator disabled",
203+
key: clusterName,
204+
disableSchedulerEstimatorInPullMode: true,
205+
expectedError: false,
206+
},
207+
{
208+
name: "Cluster in pull mode, estimator enabled",
209+
key: clusterName,
210+
disableSchedulerEstimatorInPullMode: false,
211+
expectedError: false,
212+
},
213+
}
214+
215+
for _, tt := range tests {
216+
t.Run(tt.name, func(t *testing.T) {
217+
scheduler.disableSchedulerEstimatorInPullMode = tt.disableSchedulerEstimatorInPullMode
218+
err := scheduler.reconcileEstimatorConnection(tt.key)
219+
if (err != nil) != tt.expectedError {
220+
t.Errorf("reconcileEstimatorConnection() error = %v, expectedError %v", err, tt.expectedError)
221+
}
88222
})
89223
}
90224
}

0 commit comments

Comments
 (0)