@@ -29,23 +29,34 @@ import (
29
29
"k8s.io/client-go/kubernetes/fake"
30
30
"k8s.io/client-go/tools/record"
31
31
32
+ clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
32
33
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
33
34
karmadafake "github.com/karmada-io/karmada/pkg/generated/clientset/versioned/fake"
34
35
"github.com/karmada-io/karmada/pkg/util"
35
- // "github.com/stretchr/testify/assert "
36
+ "github.com/karmada-io/karmada/pkg/util/grpcconnection "
36
37
)
37
38
38
39
func TestCreateScheduler (t * testing.T ) {
39
40
dynamicClient := dynamicfake .NewSimpleDynamicClient (runtime .NewScheme ())
40
41
karmadaClient := karmadafake .NewSimpleClientset ()
41
42
kubeClient := fake .NewSimpleClientset ()
42
43
port := 10025
44
+ servicePrefix := "test-service-prefix"
45
+ schedulerName := "test-scheduler"
46
+ timeout := metav1.Duration {Duration : 5 * time .Second }
43
47
44
48
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
49
60
}{
50
61
{
51
62
name : "scheduler with default configuration" ,
@@ -69,6 +80,53 @@ func TestCreateScheduler(t *testing.T) {
69
80
},
70
81
enableSchedulerEstimator : false ,
71
82
},
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
+ },
72
130
}
73
131
74
132
for _ , tc := range testcases {
@@ -85,6 +143,82 @@ func TestCreateScheduler(t *testing.T) {
85
143
if tc .enableSchedulerEstimator && tc .schedulerEstimatorPort != sche .schedulerEstimatorClientConfig .TargetPort {
86
144
t .Errorf ("unexpected schedulerEstimatorPort want %v, got %v" , tc .schedulerEstimatorPort , sche .schedulerEstimatorClientConfig .TargetPort )
87
145
}
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
+ }
88
222
})
89
223
}
90
224
}
0 commit comments