Skip to content

Commit 3f2dc8b

Browse files
author
Rajadeepan D Ramesh
committed
Adding UT test cases to apis package
1 parent 3911b23 commit 3f2dc8b

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed

pkg/controllers/apis/job_info_test.go

+226
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
Copyright 2019 The Volcano 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 apis
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
v1 "k8s.io/api/core/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/types"
26+
vkbatchv1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
27+
)
28+
29+
func TestAddPod(t *testing.T) {
30+
namespace := "test"
31+
name := "pod1"
32+
33+
testCases := []struct {
34+
Name string
35+
jobinfo JobInfo
36+
pod *v1.Pod
37+
ExpectValue bool
38+
ExpectErr string
39+
}{
40+
{
41+
Name: "AddPod",
42+
pod: &v1.Pod{
43+
ObjectMeta: metav1.ObjectMeta{
44+
UID: types.UID(fmt.Sprintf("%v-%v", namespace, name)),
45+
Name: name,
46+
Namespace: namespace,
47+
Labels: nil,
48+
Annotations: map[string]string{vkbatchv1.JobNameKey: "job1",
49+
vkbatchv1.JobVersion: "0",
50+
vkbatchv1.TaskSpecKey: "task1"},
51+
},
52+
Status: v1.PodStatus{
53+
Phase: v1.PodRunning,
54+
},
55+
Spec: v1.PodSpec{
56+
Containers: []v1.Container{
57+
{
58+
Name: "nginx",
59+
Image: "nginx:latest",
60+
},
61+
},
62+
},
63+
},
64+
jobinfo: JobInfo{
65+
Pods: make(map[string]map[string]*v1.Pod),
66+
},
67+
ExpectValue: true,
68+
ExpectErr: "duplicated pod",
69+
},
70+
}
71+
72+
for i, testcase := range testCases {
73+
err := testcase.jobinfo.AddPod(testcase.pod)
74+
if err != nil {
75+
t.Fatalf("AddPod() error: %v", err)
76+
}
77+
78+
if _, ok := testcase.jobinfo.Pods["task1"][testcase.pod.Name]; ok != testcase.ExpectValue {
79+
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, ok)
80+
}
81+
82+
err = testcase.jobinfo.AddPod(testcase.pod)
83+
84+
if err == nil {
85+
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectErr, nil)
86+
}
87+
}
88+
89+
}
90+
91+
func TestDeletePod(t *testing.T) {
92+
namespace := "test"
93+
name := "pod1"
94+
95+
testCases := []struct {
96+
Name string
97+
jobinfo JobInfo
98+
pod *v1.Pod
99+
ExpectValue bool
100+
}{
101+
{
102+
Name: "DeletePod",
103+
pod: &v1.Pod{
104+
ObjectMeta: metav1.ObjectMeta{
105+
UID: types.UID(fmt.Sprintf("%v-%v", namespace, name)),
106+
Name: name,
107+
Namespace: namespace,
108+
Labels: nil,
109+
Annotations: map[string]string{vkbatchv1.JobNameKey: "job1",
110+
vkbatchv1.JobVersion: "0",
111+
vkbatchv1.TaskSpecKey: "task1"},
112+
},
113+
Status: v1.PodStatus{
114+
Phase: v1.PodRunning,
115+
},
116+
Spec: v1.PodSpec{
117+
Containers: []v1.Container{
118+
{
119+
Name: "nginx",
120+
Image: "nginx:latest",
121+
},
122+
},
123+
},
124+
},
125+
jobinfo: JobInfo{
126+
Pods: make(map[string]map[string]*v1.Pod),
127+
},
128+
ExpectValue: false,
129+
},
130+
}
131+
132+
for i, testcase := range testCases {
133+
134+
testcase.jobinfo.Pods["task1"] = make(map[string]*v1.Pod)
135+
testcase.jobinfo.Pods["task1"][testcase.pod.Name] = testcase.pod
136+
137+
err := testcase.jobinfo.DeletePod(testcase.pod)
138+
if err != nil {
139+
t.Fatalf("DeletePod() error: %v", err)
140+
}
141+
if _, ok := testcase.jobinfo.Pods["task1"][testcase.pod.Name]; ok != testcase.ExpectValue {
142+
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, ok)
143+
}
144+
}
145+
}
146+
147+
func TestUpdatePod(t *testing.T) {
148+
namespace := "test"
149+
name := "pod1"
150+
151+
testCases := []struct {
152+
Name string
153+
jobinfo JobInfo
154+
oldpod *v1.Pod
155+
newpod *v1.Pod
156+
ExpectValue v1.PodPhase
157+
}{
158+
{
159+
Name: "UpdatePod",
160+
oldpod: &v1.Pod{
161+
ObjectMeta: metav1.ObjectMeta{
162+
UID: types.UID(fmt.Sprintf("%v-%v", namespace, name)),
163+
Name: name,
164+
Namespace: namespace,
165+
Labels: nil,
166+
Annotations: map[string]string{vkbatchv1.JobNameKey: "job1",
167+
vkbatchv1.JobVersion: "0",
168+
vkbatchv1.TaskSpecKey: "task1"},
169+
},
170+
Status: v1.PodStatus{
171+
Phase: v1.PodRunning,
172+
},
173+
Spec: v1.PodSpec{
174+
Containers: []v1.Container{
175+
{
176+
Name: "nginx",
177+
Image: "nginx:latest",
178+
},
179+
},
180+
},
181+
},
182+
newpod: &v1.Pod{
183+
ObjectMeta: metav1.ObjectMeta{
184+
UID: types.UID(fmt.Sprintf("%v-%v", namespace, name)),
185+
Name: name,
186+
Namespace: namespace,
187+
Labels: nil,
188+
Annotations: map[string]string{vkbatchv1.JobNameKey: "job1",
189+
vkbatchv1.JobVersion: "0",
190+
vkbatchv1.TaskSpecKey: "task1"},
191+
},
192+
Status: v1.PodStatus{
193+
Phase: v1.PodSucceeded,
194+
},
195+
Spec: v1.PodSpec{
196+
Containers: []v1.Container{
197+
{
198+
Name: "nginx",
199+
Image: "nginx:latest",
200+
},
201+
},
202+
},
203+
},
204+
jobinfo: JobInfo{
205+
Pods: make(map[string]map[string]*v1.Pod),
206+
},
207+
ExpectValue: v1.PodSucceeded,
208+
},
209+
}
210+
211+
for i, testcase := range testCases {
212+
213+
testcase.jobinfo.Pods["task1"] = make(map[string]*v1.Pod)
214+
testcase.jobinfo.Pods["task1"][testcase.oldpod.Name] = testcase.oldpod
215+
216+
err := testcase.jobinfo.UpdatePod(testcase.newpod)
217+
if err != nil {
218+
t.Fatalf("UpdatePod() error: %v", err)
219+
}
220+
if val, ok := testcase.jobinfo.Pods["task1"][testcase.newpod.Name]; ok != true {
221+
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, true, ok)
222+
} else if val.Status.Phase != v1.PodSucceeded {
223+
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, val.Status.Phase)
224+
}
225+
}
226+
}

0 commit comments

Comments
 (0)