@@ -21,6 +21,10 @@ import (
2121 "strings"
2222 "testing"
2323
24+ gocmp "github.com/google/go-cmp/cmp"
25+
26+ schedconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
27+
2428 "sigs.k8s.io/scheduler-plugins/apis/config"
2529)
2630
@@ -67,3 +71,126 @@ func TestValidateNodeResourceTopologyMatchArgs(t *testing.T) {
6771 })
6872 }
6973}
74+
75+ func TestValidateCoschedulingArgs (t * testing.T ) {
76+ testCases := []struct {
77+ args * config.CoschedulingArgs
78+ expectedErr error
79+ description string
80+ }{
81+ {
82+ description : "correct config with valid values" ,
83+ args : & config.CoschedulingArgs {
84+ PermitWaitingTimeSeconds : 30 ,
85+ PodGroupBackoffSeconds : 60 ,
86+ },
87+ expectedErr : nil ,
88+ },
89+ {
90+ description : "invalid PermitWaitingTimeSeconds (negative value)" ,
91+ args : & config.CoschedulingArgs {
92+ PermitWaitingTimeSeconds : - 10 ,
93+ PodGroupBackoffSeconds : 60 ,
94+ },
95+ expectedErr : fmt .Errorf ("permitWaitingTimeSeconds: Invalid value: %v: must be greater than 0" , - 10 ),
96+ },
97+ {
98+ description : "invalid PodGroupBackoffSeconds (negative value)" ,
99+ args : & config.CoschedulingArgs {
100+ PermitWaitingTimeSeconds : 30 ,
101+ PodGroupBackoffSeconds : - 20 ,
102+ },
103+ expectedErr : fmt .Errorf ("podGroupBackoffSeconds: Invalid value: %v: must be greater than 0" , - 20 ),
104+ },
105+ {
106+ description : "both PermitWaitingTimeSeconds and PodGroupBackoffSeconds are negative" ,
107+ args : & config.CoschedulingArgs {
108+ PermitWaitingTimeSeconds : - 30 ,
109+ PodGroupBackoffSeconds : - 20 ,
110+ },
111+ expectedErr : fmt .Errorf (
112+ "[permitWaitingTimeSeconds: Invalid value: %v: must be greater than 0, podGroupBackoffSeconds: Invalid value: %v: must be greater than 0]" ,
113+ - 30 , - 20 ,
114+ ),
115+ },
116+ }
117+
118+ for _ , testCase := range testCases {
119+ t .Run (testCase .description , func (t * testing.T ) {
120+ err := ValidateCoschedulingArgs (testCase .args , nil )
121+ if testCase .expectedErr != nil {
122+ if err == nil {
123+ t .Fatalf ("expected err to equal %v not nil" , testCase .expectedErr )
124+ }
125+ if diff := gocmp .Diff (err .Error (), testCase .expectedErr .Error ()); diff != "" {
126+ fmt .Println (diff )
127+ t .Fatalf ("expected err to contain %s in error message: %s" , testCase .expectedErr .Error (), err .Error ())
128+
129+ }
130+ }
131+ if testCase .expectedErr == nil && err != nil {
132+ t .Fatalf ("unexpected error: %v" , err )
133+ }
134+ })
135+ }
136+ }
137+
138+ func TestValidateNodeResourcesAllocatableArgs (t * testing.T ) {
139+ testCases := []struct {
140+ args * config.NodeResourcesAllocatableArgs
141+ expectedErr error
142+ description string
143+ }{
144+ {
145+ description : "correct config with valid resources and mode" ,
146+ args : & config.NodeResourcesAllocatableArgs {
147+ Resources : []schedconfig.ResourceSpec {
148+ {Name : "cpu" , Weight : 1 },
149+ {Name : "memory" , Weight : 2 },
150+ },
151+ Mode : config .Least ,
152+ },
153+ expectedErr : nil ,
154+ },
155+ {
156+ description : "invalid resource weight (non-positive value)" ,
157+ args : & config.NodeResourcesAllocatableArgs {
158+ Resources : []schedconfig.ResourceSpec {
159+ {Name : "cpu" , Weight : 0 },
160+ {Name : "memory" , Weight : - 1 },
161+ },
162+ Mode : config .Least ,
163+ },
164+ expectedErr : fmt .Errorf ("[resources[0].weight: Invalid value: %v: resource weight of cpu should be a positive value, got :%v, resources[1].weight: Invalid value: %v: resource weight of memory should be a positive value, got :%v]" , 0 , 0 , - 1 , - 1 ), // 期望错误信息包含该内容
165+ },
166+ {
167+ description : "invalid ModeType" ,
168+ args : & config.NodeResourcesAllocatableArgs {
169+ Resources : []schedconfig.ResourceSpec {
170+ {Name : "cpu" , Weight : 1 },
171+ {Name : "memory" , Weight : 2 },
172+ },
173+ Mode : "not existent" ,
174+ },
175+ expectedErr : fmt .Errorf ("mode: Invalid value: \" %s\" : invalid support ModeType" , "not existent" ), // 期望错误信息包含该内容
176+ },
177+ }
178+
179+ for _ , testCase := range testCases {
180+ t .Run (testCase .description , func (t * testing.T ) {
181+ err := ValidateNodeResourcesAllocatableArgs (testCase .args , nil )
182+ if testCase .expectedErr != nil {
183+ if err == nil {
184+ t .Fatalf ("expected err to equal %v not nil" , testCase .expectedErr )
185+ }
186+ if diff := gocmp .Diff (err .Error (), testCase .expectedErr .Error ()); diff != "" {
187+ fmt .Println (diff )
188+ t .Fatalf ("expected err to contain %s in error message: %s" , testCase .expectedErr .Error (), err .Error ())
189+ }
190+ }
191+ if testCase .expectedErr == nil && err != nil {
192+ t .Fatalf ("unexpected error: %v" , err )
193+ }
194+ })
195+ }
196+ }
0 commit comments