Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:
- go get -u golang.org/x/lint/golint
script:
- make verify
- stage: UT Tests
script:
- make unit-test
- stage: E2E Tests
before_script:
# Download kubectl
Expand Down
291 changes: 291 additions & 0 deletions pkg/admission/admit_job_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
package admission
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add header there.


import (
"strings"
"testing"

"k8s.io/api/admission/v1beta1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

v1alpha1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
)

func TestValidateExecution(t *testing.T) {

namespace := "test"
var invTtl int32 = -1

testCases := []struct {
Name string
Job v1alpha1.Job
ExpectErr bool
reviewResponse v1beta1.AdmissionResponse
ret string
}{
{
Name: "validate valid-job",
Job: v1alpha1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "valid-Job",
Namespace: namespace,
},
Spec: v1alpha1.JobSpec{
MinAvailable: 1,
Tasks: []v1alpha1.TaskSpec{
{
Name: "task-1",
Replicas: 1,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "test"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "fake-name",
Image: "busybox:1.24",
},
},
},
},
},
},
},
},
reviewResponse: v1beta1.AdmissionResponse{Allowed: true},
ret: "",
ExpectErr: false,
},
// duplicate task name
{
Name: "duplicate-task-job",
Job: v1alpha1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "duplicate-task-job",
Namespace: namespace,
},
Spec: v1alpha1.JobSpec{
MinAvailable: 1,
Tasks: []v1alpha1.TaskSpec{
{
Name: "duplicated-task-1",
Replicas: 1,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "test"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "fake-name",
Image: "busybox:1.24",
},
},
},
},
},
{
Name: "duplicated-task-1",
Replicas: 1,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "test"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "fake-name",
Image: "busybox:1.24",
},
},
},
},
},
},
},
},
reviewResponse: v1beta1.AdmissionResponse{Allowed: true},
ret: "duplicated task name duplicated-task-1",
ExpectErr: true,
},
// Duplicated Policy Event
{
Name: "job-policy-duplicated",
Job: v1alpha1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job-policy-duplicated",
Namespace: namespace,
},
Spec: v1alpha1.JobSpec{
MinAvailable: 1,
Tasks: []v1alpha1.TaskSpec{
{
Name: "task-1",
Replicas: 1,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "test"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "fake-name",
Image: "busybox:1.24",
},
},
},
},
},
},
Policies: []v1alpha1.LifecyclePolicy{
{
Event: v1alpha1.PodFailedEvent,
Action: v1alpha1.AbortJobAction,
},
{
Event: v1alpha1.PodFailedEvent,
Action: v1alpha1.RestartJobAction,
},
},
},
},
reviewResponse: v1beta1.AdmissionResponse{Allowed: true},
ret: "duplicate",
ExpectErr: true,
},
// Min Available illegal
{
Name: "Min Available illegal",
Job: v1alpha1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job-min-illegal",
Namespace: namespace,
},
Spec: v1alpha1.JobSpec{
MinAvailable: 2,
Tasks: []v1alpha1.TaskSpec{
{
Name: "task-1",
Replicas: 1,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "test"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "fake-name",
Image: "busybox:1.24",
},
},
},
},
},
},
},
},
reviewResponse: v1beta1.AdmissionResponse{Allowed: true},
ret: "'minAvailable' should not be greater than total replicas in tasks",
ExpectErr: true,
},
// Job Plugin illegal
{
Name: "Job Plugin illegal",
Job: v1alpha1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job-plugin-illegal",
Namespace: namespace,
},
Spec: v1alpha1.JobSpec{
MinAvailable: 1,
Tasks: []v1alpha1.TaskSpec{
{
Name: "task-1",
Replicas: 1,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "test"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "fake-name",
Image: "busybox:1.24",
},
},
},
},
},
},
Plugins: map[string][]string{
"big_plugin": {},
},
},
},
reviewResponse: v1beta1.AdmissionResponse{Allowed: true},
ret: "unable to find job plugin: big_plugin",
ExpectErr: true,
},
// ttl-illegal
{
Name: "job-ttl-illegal",
Job: v1alpha1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "job-ttl-illegal",
Namespace: namespace,
},
Spec: v1alpha1.JobSpec{
MinAvailable: 1,
Tasks: []v1alpha1.TaskSpec{
{
Name: "task-1",
Replicas: 1,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "test"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "fake-name",
Image: "busybox:1.24",
},
},
},
},
},
},
TTLSecondsAfterFinished: &invTtl,
},
},
reviewResponse: v1beta1.AdmissionResponse{Allowed: true},
ret: "'ttlSecondsAfterFinished' cannot be less than zero",
ExpectErr: true,
},
}

for _, testCase := range testCases {

ret := validateJob(testCase.Job, &testCase.reviewResponse)
//fmt.Printf("test-case name:%s, ret:%v testCase.reviewResponse:%v \n", testCase.Name, ret,testCase.reviewResponse)
if testCase.ExpectErr == true && ret == "" {
t.Errorf("%s: test case Expect error msg :%s, but got nil.", testCase.Name, testCase.ret)
}
if testCase.ExpectErr == true && testCase.reviewResponse.Allowed != false {
t.Errorf("%s: test case Expect Allowed as false but got true.", testCase.Name)
}
if testCase.ExpectErr == true && !strings.Contains(ret, testCase.ret) {
t.Errorf("%s: test case Expect error msg :%s, but got diff error %v", testCase.Name, testCase.ret, ret)
}

if testCase.ExpectErr == false && ret != "" {
t.Errorf("%s: test case Expect no error, but got error %v", testCase.Name, ret)
}
if testCase.ExpectErr == false && testCase.reviewResponse.Allowed != true {
t.Errorf("%s: test case Expect Allowed as true but got false. %v", testCase.Name, testCase.reviewResponse)
}

}

}
2 changes: 1 addition & 1 deletion pkg/controllers/queue/queue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (c *Controller) syncQueue(key string) error {
queue, err := c.queueLister.Get(key)
if err != nil {
if errors.IsNotFound(err) {
glog.V(2).Infof("queue %s has been deleted", queue)
glog.V(2).Infof("queue %s has been deleted", key)
return nil
}
return err
Expand Down
Loading