Skip to content

Commit 12f881b

Browse files
authored
Merge pull request #5445 from anujagrawal699/addedTests-cmd/descheduler
Added tests for cmd/descheduler
2 parents 47cea6a + 9d5e86e commit 12f881b

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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/descheduler/app/options"
28+
)
29+
30+
func TestNewDeschedulerCommand(t *testing.T) {
31+
stopCh := make(chan struct{})
32+
cmd := NewDeschedulerCommand(stopCh)
33+
34+
assert.NotNil(t, cmd)
35+
assert.Equal(t, "karmada-descheduler", cmd.Use)
36+
assert.NotEmpty(t, cmd.Long)
37+
}
38+
39+
func TestDeschedulerCommandFlagParsing(t *testing.T) {
40+
testCases := []struct {
41+
name string
42+
args []string
43+
expectError bool
44+
}{
45+
{"Default flags", []string{}, false},
46+
{"With custom health probe bind address", []string{"--health-probe-bind-address=127.0.0.1:8080"}, false},
47+
{"With custom metrics bind address", []string{"--metrics-bind-address=127.0.0.1:8081"}, false},
48+
{"With leader election enabled", []string{"--leader-elect=true"}, false},
49+
{"With invalid flag", []string{"--invalid-flag=value"}, true},
50+
}
51+
for _, tc := range testCases {
52+
t.Run(tc.name, func(t *testing.T) {
53+
stopCh := make(chan struct{})
54+
cmd := NewDeschedulerCommand(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 TestDeschedulerOptionsValidation(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(_ *options.Options) {
97+
// Default options are valid
98+
},
99+
expectError: false,
100+
},
101+
{
102+
name: "Invalid descheduling interval",
103+
setupOpts: func(o *options.Options) {
104+
o.DeschedulingInterval.Duration = -1 * time.Second
105+
},
106+
expectError: true,
107+
},
108+
{
109+
name: "Invalid unschedulable threshold",
110+
setupOpts: func(o *options.Options) {
111+
o.UnschedulableThreshold.Duration = -1 * time.Second
112+
},
113+
expectError: true,
114+
},
115+
}
116+
for _, tc := range testCases {
117+
t.Run(tc.name, func(t *testing.T) {
118+
opts := options.NewOptions()
119+
tc.setupOpts(opts)
120+
errs := opts.Validate()
121+
if tc.expectError {
122+
assert.NotEmpty(t, errs)
123+
} else {
124+
assert.Empty(t, errs)
125+
}
126+
})
127+
}
128+
}

0 commit comments

Comments
 (0)