generated from kubernetes/kubernetes-template-project
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit tests for the priority package
- Loading branch information
1 parent
597938e
commit 0acb185
Showing
3 changed files
with
182 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
Copyright 2022 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package priority | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
schedulingv1 "k8s.io/api/scheduling/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
|
||
kueue "sigs.k8s.io/kueue/apis/kueue/v1alpha1" | ||
"sigs.k8s.io/kueue/pkg/constants" | ||
utiltesting "sigs.k8s.io/kueue/pkg/util/testing" | ||
) | ||
|
||
func TestPriority(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
workload *kueue.Workload | ||
expected int32 | ||
}{ | ||
{ | ||
desc: "priority is specified", | ||
workload: utiltesting.MakeWorkload("name", "ns").Priority(100).Obj(), | ||
expected: 100, | ||
}, | ||
{ | ||
desc: "priority is empty", | ||
workload: &kueue.Workload{ | ||
Spec: kueue.WorkloadSpec{}, | ||
}, | ||
expected: constants.DefaultPriority, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
got := Priority(tt.workload) | ||
if got != tt.expected { | ||
t.Errorf("Priority does not match: got: %d, expected: %d", got, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetPriorityFromPriorityClass(t *testing.T) { | ||
scheme := runtime.NewScheme() | ||
if err := schedulingv1.AddToScheme(scheme); err != nil { | ||
t.Fatalf("Failed adding scheduling scheme: %v", err) | ||
} | ||
|
||
tests := []struct { | ||
desc string | ||
priorityClassList *schedulingv1.PriorityClassList | ||
priorityClassName string | ||
expectedPriorityClassName string | ||
expectedPriorityClassValue int32 | ||
expectedErr string | ||
}{ | ||
{ | ||
desc: "priorityClass is specified and it exists", | ||
priorityClassList: &schedulingv1.PriorityClassList{ | ||
Items: []schedulingv1.PriorityClass{ | ||
{ | ||
ObjectMeta: v1.ObjectMeta{Name: "test"}, | ||
Value: 50, | ||
}, | ||
}, | ||
}, | ||
priorityClassName: "test", | ||
expectedPriorityClassName: "test", | ||
expectedPriorityClassValue: 50, | ||
}, | ||
{ | ||
desc: "priorityClass is specified and it does not exist", | ||
priorityClassList: &schedulingv1.PriorityClassList{ | ||
Items: []schedulingv1.PriorityClass{}, | ||
}, | ||
priorityClassName: "test", | ||
expectedErr: `priorityclasses.scheduling.k8s.io "test" not found`, | ||
}, | ||
{ | ||
desc: "priorityClass is unspecified and one global default exists", | ||
priorityClassList: &schedulingv1.PriorityClassList{ | ||
Items: []schedulingv1.PriorityClass{ | ||
{ | ||
ObjectMeta: v1.ObjectMeta{Name: "globalDefault"}, | ||
GlobalDefault: true, | ||
Value: 40, | ||
}, | ||
}, | ||
}, | ||
expectedPriorityClassName: "globalDefault", | ||
expectedPriorityClassValue: 40, | ||
}, | ||
{ | ||
desc: "priorityClass is unspecified and multiple global defaults exist", | ||
priorityClassList: &schedulingv1.PriorityClassList{ | ||
Items: []schedulingv1.PriorityClass{ | ||
{ | ||
ObjectMeta: v1.ObjectMeta{Name: "globalDefault1"}, | ||
GlobalDefault: true, | ||
Value: 90, | ||
}, | ||
{ | ||
ObjectMeta: v1.ObjectMeta{Name: "globalDefault2"}, | ||
GlobalDefault: true, | ||
Value: 20, | ||
}, | ||
{ | ||
ObjectMeta: v1.ObjectMeta{Name: "globalDefault3"}, | ||
GlobalDefault: true, | ||
Value: 50, | ||
}, | ||
}, | ||
}, | ||
expectedPriorityClassName: "globalDefault2", | ||
expectedPriorityClassValue: 20, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.desc, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
builder := fake.NewClientBuilder().WithScheme(scheme).WithLists(tt.priorityClassList) | ||
client := builder.Build() | ||
|
||
name, value, err := GetPriorityFromPriorityClass(context.Background(), client, tt.priorityClassName) | ||
if tt.expectedErr != "" { | ||
if err == nil { | ||
t.Fatalf("expected error but error is nil") | ||
} | ||
|
||
if !strings.Contains(err.Error(), tt.expectedErr) { | ||
t.Fatalf("unexpecter error message: got: %s, expected: %s", err.Error(), tt.expectedErr) | ||
} | ||
return | ||
} | ||
|
||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
if name != tt.expectedPriorityClassName { | ||
t.Errorf("unexpected name: got: %s, expected: %s", name, tt.expectedPriorityClassName) | ||
} | ||
|
||
if value != tt.expectedPriorityClassValue { | ||
t.Errorf("unexpected value: got: %d, expected: %d", value, tt.expectedPriorityClassValue) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters