Skip to content

Commit 9351b76

Browse files
authored
Merge pull request #5459 from anujagrawal699/addedTests-cmd/scheduler
Added tests for cmd/scheduler
2 parents e579560 + b64a76c commit 9351b76

File tree

2 files changed

+255
-0
lines changed

2 files changed

+255
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Copyright 2024 The Karmada Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package options
18+
19+
import (
20+
"testing"
21+
"time"
22+
23+
"github.com/spf13/pflag"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestNewOptions(t *testing.T) {
28+
opts := NewOptions()
29+
30+
assert.True(t, opts.LeaderElection.LeaderElect, "Expected default LeaderElect to be true")
31+
assert.Equal(t, "karmada-system", opts.LeaderElection.ResourceNamespace, "Unexpected default ResourceNamespace")
32+
assert.Equal(t, 15*time.Second, opts.LeaderElection.LeaseDuration.Duration, "Unexpected default LeaseDuration")
33+
assert.Equal(t, "karmada-scheduler", opts.LeaderElection.ResourceName, "Unexpected default ResourceName")
34+
}
35+
36+
func TestAddFlags(t *testing.T) {
37+
opts := NewOptions()
38+
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
39+
opts.AddFlags(fs)
40+
41+
testCases := []struct {
42+
name string
43+
expectedType string
44+
expectedDefault string
45+
}{
46+
{"kubeconfig", "string", ""},
47+
{"leader-elect", "bool", "true"},
48+
{"enable-scheduler-estimator", "bool", "false"},
49+
{"scheduler-estimator-port", "int", "10352"},
50+
{"plugins", "stringSlice", "[*]"},
51+
{"scheduler-name", "string", "default-scheduler"},
52+
}
53+
54+
for _, tc := range testCases {
55+
flag := fs.Lookup(tc.name)
56+
assert.NotNil(t, flag, "Flag %s not found", tc.name)
57+
assert.Equal(t, tc.expectedType, flag.Value.Type(), "Unexpected type for flag %s", tc.name)
58+
assert.Equal(t, tc.expectedDefault, flag.DefValue, "Unexpected default value for flag %s", tc.name)
59+
}
60+
}
61+
62+
func TestOptionsComplete(t *testing.T) {
63+
testCases := []struct {
64+
name string
65+
bindAddress string
66+
securePort int
67+
expectedMetrics string
68+
expectedHealth string
69+
}{
70+
{
71+
name: "Default values",
72+
bindAddress: defaultBindAddress,
73+
securePort: defaultPort,
74+
expectedMetrics: "0.0.0.0:10351",
75+
expectedHealth: "0.0.0.0:10351",
76+
},
77+
{
78+
name: "Custom values",
79+
bindAddress: "127.0.0.1",
80+
securePort: 8080,
81+
expectedMetrics: "127.0.0.1:8080",
82+
expectedHealth: "127.0.0.1:8080",
83+
},
84+
}
85+
86+
for _, tc := range testCases {
87+
t.Run(tc.name, func(t *testing.T) {
88+
opts := &Options{
89+
BindAddress: tc.bindAddress,
90+
SecurePort: tc.securePort,
91+
}
92+
err := opts.Complete()
93+
assert.NoError(t, err)
94+
assert.Equal(t, tc.expectedMetrics, opts.MetricsBindAddress)
95+
assert.Equal(t, tc.expectedHealth, opts.HealthProbeBindAddress)
96+
})
97+
}
98+
}
99+
100+
func TestOptionsFlagParsing(t *testing.T) {
101+
opts := NewOptions()
102+
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
103+
opts.AddFlags(fs)
104+
105+
testArgs := []string{
106+
"--leader-elect=false",
107+
"--enable-scheduler-estimator=true",
108+
"--plugins=*,-foo,bar",
109+
"--scheduler-name=custom-scheduler",
110+
}
111+
112+
err := fs.Parse(testArgs)
113+
assert.NoError(t, err)
114+
115+
assert.False(t, opts.LeaderElection.LeaderElect)
116+
assert.True(t, opts.EnableSchedulerEstimator)
117+
assert.Equal(t, []string{"*", "-foo", "bar"}, opts.Plugins)
118+
assert.Equal(t, "custom-scheduler", opts.SchedulerName)
119+
}

cmd/scheduler/app/scheduler_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
Copyright 2024 The Karmada Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package app
18+
19+
import (
20+
"net/http"
21+
"testing"
22+
"time"
23+
24+
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/require"
26+
27+
"github.com/karmada-io/karmada/cmd/scheduler/app/options"
28+
)
29+
30+
func TestNewSchedulerCommand(t *testing.T) {
31+
stopCh := make(chan struct{})
32+
cmd := NewSchedulerCommand(stopCh)
33+
assert.NotNil(t, cmd)
34+
assert.Equal(t, "karmada-scheduler", cmd.Use)
35+
assert.NotEmpty(t, cmd.Long)
36+
}
37+
38+
func TestSchedulerCommandFlagParsing(t *testing.T) {
39+
testCases := []struct {
40+
name string
41+
args []string
42+
expectError bool
43+
}{
44+
{"Default flags", []string{}, false},
45+
{"With custom health probe bind address", []string{"--health-probe-bind-address=127.0.0.1:8080"}, false},
46+
{"With custom metrics bind address", []string{"--metrics-bind-address=127.0.0.1:8081"}, false},
47+
{"With leader election enabled", []string{"--leader-elect=true"}, false},
48+
{"With invalid flag", []string{"--invalid-flag=value"}, true},
49+
}
50+
51+
for _, tc := range testCases {
52+
t.Run(tc.name, func(t *testing.T) {
53+
stopCh := make(chan struct{})
54+
cmd := NewSchedulerCommand(stopCh)
55+
cmd.SetArgs(tc.args)
56+
err := cmd.ParseFlags(tc.args)
57+
if tc.expectError {
58+
assert.Error(t, err)
59+
} else {
60+
assert.NoError(t, err)
61+
}
62+
})
63+
}
64+
}
65+
66+
func TestServeHealthzAndMetrics(t *testing.T) {
67+
healthAddress := "127.0.0.1:8082"
68+
metricsAddress := "127.0.0.1:8083"
69+
70+
go serveHealthzAndMetrics(healthAddress, metricsAddress)
71+
72+
// For servers to start
73+
time.Sleep(100 * time.Millisecond)
74+
75+
t.Run("Healthz endpoint", func(t *testing.T) {
76+
resp, err := http.Get("http://" + healthAddress + "/healthz")
77+
require.NoError(t, err)
78+
assert.Equal(t, http.StatusOK, resp.StatusCode)
79+
})
80+
81+
t.Run("Metrics endpoint", func(t *testing.T) {
82+
resp, err := http.Get("http://" + metricsAddress + "/metrics")
83+
require.NoError(t, err)
84+
assert.Equal(t, http.StatusOK, resp.StatusCode)
85+
})
86+
}
87+
88+
func TestSchedulerOptionsValidation(t *testing.T) {
89+
testCases := []struct {
90+
name string
91+
setupOpts func(*options.Options)
92+
expectError bool
93+
}{
94+
{
95+
name: "Default options",
96+
setupOpts: func(o *options.Options) {
97+
o.SchedulerName = "default-scheduler"
98+
},
99+
expectError: false,
100+
},
101+
{
102+
name: "Empty scheduler name",
103+
setupOpts: func(o *options.Options) {
104+
o.SchedulerName = ""
105+
},
106+
expectError: true,
107+
},
108+
{
109+
name: "Invalid kube API QPS",
110+
setupOpts: func(o *options.Options) {
111+
o.KubeAPIQPS = -1
112+
},
113+
expectError: true,
114+
},
115+
{
116+
name: "Invalid kube API burst",
117+
setupOpts: func(o *options.Options) {
118+
o.KubeAPIBurst = -1
119+
},
120+
expectError: true,
121+
},
122+
}
123+
124+
for _, tc := range testCases {
125+
t.Run(tc.name, func(t *testing.T) {
126+
opts := options.NewOptions()
127+
tc.setupOpts(opts)
128+
errs := opts.Validate()
129+
if tc.expectError {
130+
assert.NotEmpty(t, errs)
131+
} else {
132+
assert.Empty(t, errs)
133+
}
134+
})
135+
}
136+
}

0 commit comments

Comments
 (0)