Skip to content

Commit 6fa3a4b

Browse files
committed
fix golint and unit test
1 parent dbb5eca commit 6fa3a4b

4 files changed

Lines changed: 38 additions & 16 deletions

File tree

pkg/controllers/job/helpers/helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func MakePodName(jobName string, taskName string, index int) string {
4949
return fmt.Sprintf(PodNameFmt, jobName, taskName, index)
5050
}
5151

52+
// GenRandomStr generate random str with specified length l
5253
func GenRandomStr(l int) string {
5354
str := "0123456789abcdefghijklmnopqrstuvwxyz"
5455
bytes := []byte(str)

pkg/controllers/job/job_controller_actions.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ func (cc *Controller) createJobIOIfNotExist(job *vkv1.Job) (*vkv1.Job, error) {
339339
// If PVC does not exist, create them for Job.
340340
var needUpdate bool
341341
volumes := job.Spec.Volumes
342+
if job.Status.ControlledResources == nil {
343+
job.Status.ControlledResources = make(map[string]string)
344+
}
342345
for index, volume := range volumes {
343346
vcName := volume.VolumeClaimName
344347
if len(vcName) == 0 {
@@ -375,7 +378,7 @@ func (cc *Controller) createJobIOIfNotExist(job *vkv1.Job) (*vkv1.Job, error) {
375378
if exist {
376379
job.Status.ControlledResources["volume-pvc-"+vcName] = vcName
377380
} else {
378-
msg := fmt.Sprintf("PVC %s is not found, the job will be in the Pending state until the PVC is created.", vcName)
381+
msg := fmt.Sprintf("pvc %s is not found, the job will be in the Pending state until the PVC is created", vcName)
379382
glog.Error(msg)
380383
return job, errors.New(msg)
381384
}
@@ -387,12 +390,9 @@ func (cc *Controller) createJobIOIfNotExist(job *vkv1.Job) (*vkv1.Job, error) {
387390
glog.Errorf("Failed to update Job %v/%v for volume claim name: %v ",
388391
job.Namespace, job.Name, err)
389392
return job, err
390-
} else {
391-
newJob.Status = job.Status
392-
return newJob, err
393393
}
394-
newJob.Status = job.Status
395394

395+
newJob.Status = job.Status
396396
return newJob, err
397397
}
398398
return job, nil

pkg/controllers/job/job_controller_actions_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package job
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"testing"
2223

@@ -295,7 +296,7 @@ func TestCreateJobIOIfNotExistFunc(t *testing.T) {
295296
ExpextVal error
296297
}{
297298
{
298-
Name: "Create Job IO success case",
299+
Name: "Create Job IO case",
299300
Job: &v1alpha1.Job{
300301
ObjectMeta: metav1.ObjectMeta{
301302
Name: "job1",
@@ -309,7 +310,7 @@ func TestCreateJobIOIfNotExistFunc(t *testing.T) {
309310
},
310311
},
311312
},
312-
ExpextVal: nil,
313+
ExpextVal: errors.New("pvc pvc1 is not found, the job will be in the Pending state until the PVC is created"),
313314
},
314315
}
315316

@@ -319,8 +320,14 @@ func TestCreateJobIOIfNotExistFunc(t *testing.T) {
319320
fakeController := newFakeController()
320321

321322
job, err := fakeController.createJobIOIfNotExist(testcase.Job)
322-
if err != testcase.ExpextVal {
323-
t.Errorf("Expected Return value to be : %s, but got: %s in testcase %d", testcase.ExpextVal, err, i)
323+
if testcase.ExpextVal == nil {
324+
if err != nil {
325+
t.Errorf("Expected Return value to be : %v, but got: %v in testcase %d", testcase.ExpextVal, err, i)
326+
}
327+
} else {
328+
if err == nil || err.Error() != testcase.ExpextVal.Error() {
329+
t.Errorf("Expected Return value to be : %v, but got: %v in testcase %d", testcase.ExpextVal.Error(), err.Error(), i)
330+
}
324331
}
325332

326333
if len(job.Spec.Volumes) == 0 {

test/e2e/job_controlled_resource.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ var _ = Describe("Job E2E Test: Test Job PVCs", func() {
125125

126126
_, err1 := context.kubeclient.CoreV1().PersistentVolumeClaims(namespace).Create(&pvc)
127127
Expect(err1).NotTo(HaveOccurred(), "pvc creation")
128+
129+
pvSpec := &v12.PersistentVolumeClaimSpec{
130+
Resources: v12.ResourceRequirements{
131+
Requests: v12.ResourceList{
132+
v12.ResourceName(v12.ResourceStorage): resource.MustParse("1Gi"),
133+
},
134+
},
135+
AccessModes: []v12.PersistentVolumeAccessMode{
136+
v12.ReadWriteOnce,
137+
},
138+
}
128139
job := createJob(context, &jobSpec{
129140
namespace: namespace,
130141
name: jobName,
@@ -139,9 +150,12 @@ var _ = Describe("Job E2E Test: Test Job PVCs", func() {
139150
},
140151
volumes: []v1alpha1.VolumeSpec{
141152
{
142-
MountPath: "/mounttwo",
153+
MountPath: "/mountone",
143154
VolumeClaimName: pvcName,
144-
VolumeClaim: &pvc.Spec,
155+
},
156+
{
157+
MountPath: "/mounttwo",
158+
VolumeClaim: pvSpec,
145159
},
146160
},
147161
})
@@ -152,12 +166,12 @@ var _ = Describe("Job E2E Test: Test Job PVCs", func() {
152166
job, err = context.vcclient.BatchV1alpha1().Jobs(namespace).Get(jobName, metav1.GetOptions{})
153167
Expect(err).NotTo(HaveOccurred())
154168

155-
Expect(len(job.Spec.Volumes)).To(Equal(1),
169+
Expect(len(job.Spec.Volumes)).To(Equal(2),
156170
" volume should be created")
157-
for _, volume := range job.Spec.Volumes {
158-
Expect(volume.VolumeClaimName).Should((Equal(pvcName)),
159-
"PVC name should not be generated .")
160-
}
171+
Expect(job.Spec.Volumes[0].VolumeClaimName).Should(Equal(pvcName),
172+
"volume 1 PVC name should not be generated .")
173+
Expect(job.Spec.Volumes[1].VolumeClaimName).Should(Not(Equal("")),
174+
"volume 0 PVC name should be generated.")
161175
})
162176

163177
It("Generate PodGroup and valid minResource when creating job", func() {

0 commit comments

Comments
 (0)