Skip to content

Commit

Permalink
Merge pull request #171 from SrinivasChilveri/UTTestCases
Browse files Browse the repository at this point in the history
moved some of admission e2e test cases as ut test cases
  • Loading branch information
volcano-sh-bot authored May 16, 2019
2 parents 00272c6 + 60c1795 commit 124d38d
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 160 deletions.
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

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

0 comments on commit 124d38d

Please sign in to comment.