Skip to content

Commit

Permalink
Merge pull request #2133 from wpeng102/ut
Browse files Browse the repository at this point in the history
fix dynamic provision ut case error
  • Loading branch information
volcano-sh-bot authored Mar 30, 2022
2 parents 1949803 + 93be23f commit b2b1100
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions pkg/scheduler/actions/allocate/allocate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"volcano.sh/volcano/pkg/scheduler/plugins/gang"
"volcano.sh/volcano/pkg/scheduler/plugins/priority"

storagev1 "k8s.io/api/storage/v1"
schedulingv1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
"volcano.sh/volcano/cmd/scheduler/app/options"
"volcano.sh/volcano/pkg/scheduler/api"
Expand Down Expand Up @@ -340,20 +341,12 @@ func TestAllocateWithDynamicPVC(t *testing.T) {
Phase: schedulingv1.PodGroupInqueue,
},
}
kubeClient := fake.NewSimpleClientset()
pvc, pv, sc := util.BuildDynamicPVC("c1", "pvc1", v1.ResourceList{

pvc, pv, sc := util.BuildDynamicPVC("c1", "pvc", v1.ResourceList{
v1.ResourceStorage: resource.MustParse("1Gi"),
})
kubeClient.StorageV1().StorageClasses().Create(context.TODO(), sc, metav1.CreateOptions{})
kubeClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Create(context.TODO(), pvc, metav1.CreateOptions{})
pvcs := []*v1.PersistentVolumeClaim{pvc}
for i := 1; i <= 5; i++ {
tmp := pvc.DeepCopy()
tmp.Name = fmt.Sprintf("pvc%d", i)
pvcs = append(pvcs, tmp)
kubeClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Create(context.TODO(), tmp, metav1.CreateOptions{})
}
fakeVolumeBinder := util.NewFakeVolumeBinder(kubeClient)
pvc1 := pvc.DeepCopy()
pvc1.Name = fmt.Sprintf("pvc%d", 1)

allocate := New()

Expand All @@ -362,18 +355,22 @@ func TestAllocateWithDynamicPVC(t *testing.T) {
pods []*v1.Pod
nodes []*v1.Node
pvs []*v1.PersistentVolume
pvcs []*v1.PersistentVolumeClaim
sc *storagev1.StorageClass
expectedBind map[string]string
expectedActions map[string][]string
}{
{
name: "resource not match",
pods: []*v1.Pod{
util.BuildPodWithPVC("c1", "p1", "", v1.PodPending, util.BuildResourceList("1", "1G"), pvc, "pg1", make(map[string]string), make(map[string]string)),
util.BuildPodWithPVC("c1", "p2", "", v1.PodPending, util.BuildResourceList("1", "1G"), pvcs[2], "pg1", make(map[string]string), make(map[string]string)),
util.BuildPodWithPVC("c1", "p2", "", v1.PodPending, util.BuildResourceList("1", "1G"), pvc1, "pg1", make(map[string]string), make(map[string]string)),
},
nodes: []*v1.Node{
util.BuildNode("n1", util.BuildResourceList("1", "4Gi"), make(map[string]string)),
},
sc: sc,
pvcs: []*v1.PersistentVolumeClaim{pvc, pvc1},
expectedBind: map[string]string{},
expectedActions: map[string][]string{
"c1/p1": {"GetPodVolumes", "AllocateVolumes", "RevertVolumes"},
Expand All @@ -383,11 +380,13 @@ func TestAllocateWithDynamicPVC(t *testing.T) {
name: "node changed with enough resource",
pods: []*v1.Pod{
util.BuildPodWithPVC("c1", "p1", "", v1.PodPending, util.BuildResourceList("1", "1G"), pvc, "pg1", make(map[string]string), make(map[string]string)),
util.BuildPodWithPVC("c1", "p2", "", v1.PodPending, util.BuildResourceList("1", "1G"), pvcs[2], "pg1", make(map[string]string), make(map[string]string)),
util.BuildPodWithPVC("c1", "p2", "", v1.PodPending, util.BuildResourceList("1", "1G"), pvc1, "pg1", make(map[string]string), make(map[string]string)),
},
nodes: []*v1.Node{
util.BuildNode("n2", util.BuildResourceList("2", "4Gi"), make(map[string]string)),
},
sc: sc,
pvcs: []*v1.PersistentVolumeClaim{pvc, pvc1},
expectedBind: map[string]string{
"c1/p1": "n2",
"c1/p2": "n2",
Expand All @@ -400,15 +399,17 @@ func TestAllocateWithDynamicPVC(t *testing.T) {
{
name: "pvc with matched pv",
pods: []*v1.Pod{
util.BuildPodWithPVC("c1", "p3", "", v1.PodPending, util.BuildResourceList("1", "1G"), pvcs[3], "pg1", make(map[string]string), make(map[string]string)),
util.BuildPodWithPVC("c1", "p4", "", v1.PodPending, util.BuildResourceList("1", "1G"), pvcs[4], "pg1", make(map[string]string), make(map[string]string)),
util.BuildPodWithPVC("c1", "p3", "", v1.PodPending, util.BuildResourceList("1", "1G"), pvc, "pg1", make(map[string]string), make(map[string]string)),
util.BuildPodWithPVC("c1", "p4", "", v1.PodPending, util.BuildResourceList("1", "1G"), pvc1, "pg1", make(map[string]string), make(map[string]string)),
},
pvs: []*v1.PersistentVolume{
pv,
},
nodes: []*v1.Node{
util.BuildNode("n3", util.BuildResourceList("2", "4Gi"), make(map[string]string)),
},
sc: sc,
pvcs: []*v1.PersistentVolumeClaim{pvc, pvc1},
expectedBind: map[string]string{
"c1/p3": "n3",
"c1/p4": "n3",
Expand All @@ -422,6 +423,16 @@ func TestAllocateWithDynamicPVC(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
kubeClient := fake.NewSimpleClientset()
kubeClient.StorageV1().StorageClasses().Create(context.TODO(), test.sc, metav1.CreateOptions{})
for _, pv := range test.pvs {
kubeClient.CoreV1().PersistentVolumes().Create(context.TODO(), pv, metav1.CreateOptions{})
}
for _, pvc := range test.pvcs {
kubeClient.CoreV1().PersistentVolumeClaims(pvc.Namespace).Create(context.TODO(), pvc, metav1.CreateOptions{})
}

fakeVolumeBinder := util.NewFakeVolumeBinder(kubeClient)
binder := &util.FakeBinder{
Binds: map[string]string{},
Channel: make(chan string),
Expand All @@ -435,17 +446,13 @@ func TestAllocateWithDynamicPVC(t *testing.T) {
VolumeBinder: fakeVolumeBinder,
Recorder: record.NewFakeRecorder(100),
}

schedulerCache.AddQueueV1beta1(queue)
schedulerCache.AddPodGroupV1beta1(pg)
for i, pod := range test.pods {
priority := int32(-i)
pod.Spec.Priority = &priority
schedulerCache.AddPod(pod)
}
for _, pv := range test.pvs {
kubeClient.CoreV1().PersistentVolumes().Create(context.TODO(), pv, metav1.CreateOptions{})
}
for _, node := range test.nodes {
schedulerCache.AddNode(node)
}
Expand Down Expand Up @@ -474,9 +481,6 @@ func TestAllocateWithDynamicPVC(t *testing.T) {
defer framework.CloseSession(ssn)

allocate.Execute(ssn)
for _, pv := range test.pvs {
kubeClient.CoreV1().PersistentVolumes().Delete(context.TODO(), pv.Name, metav1.DeleteOptions{})
}
if !reflect.DeepEqual(test.expectedBind, binder.Binds) {
t.Errorf("expected: %v, got %v ", test.expectedBind, binder.Binds)
}
Expand Down

0 comments on commit b2b1100

Please sign in to comment.