Skip to content

Commit

Permalink
Adding UT for cli job package
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajadeepan D Ramesh committed Jun 26, 2019
1 parent 0c44e1a commit 99c5496
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pkg/cli/job/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package job

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

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

func TestDeleteJobJob(t *testing.T) {
response := v1alpha1.Job{}
response.Name = "testJob"

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
val, err := json.Marshal(response)
if err == nil {
w.Write(val)
}

})

server := httptest.NewServer(handler)
defer server.Close()

deleteJobFlags.Master = server.URL
deleteJobFlags.Namespace = "test"
deleteJobFlags.JobName = "testJob"

testCases := []struct {
Name string
ExpectValue error
}{
{
Name: "DeleteJob",
ExpectValue: nil,
},
}

for i, testcase := range testCases {
err := DeleteJob()
if err != nil {
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
}
}

}
48 changes: 48 additions & 0 deletions pkg/cli/job/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package job

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

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

func TestListJob(t *testing.T) {
response := v1alpha1.JobList{}
response.Items = append(response.Items, v1alpha1.Job{})

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
val, err := json.Marshal(response)
if err == nil {
w.Write(val)
}

})

server := httptest.NewServer(handler)
defer server.Close()

listJobFlags.Master = server.URL
listJobFlags.Namespace = "test"

testCases := []struct {
Name string
ExpectValue error
}{
{
Name: "ListJob",
ExpectValue: nil,
},
}

for i, testcase := range testCases {
err := ListJobs()
if err != nil {
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
}
}

}
60 changes: 60 additions & 0 deletions pkg/cli/job/resume_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package job

import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"

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

func TestResumeJob(t *testing.T) {
responsecommand := v1alpha1.Command{}
responsejob := v1alpha1batch.Job{}

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "command") {
w.Header().Set("Content-Type", "application/json")
val, err := json.Marshal(responsecommand)
if err == nil {
w.Write(val)
}

} else {
w.Header().Set("Content-Type", "application/json")
val, err := json.Marshal(responsejob)
if err == nil {
w.Write(val)
}

}
})

server := httptest.NewServer(handler)
defer server.Close()

resumeJobFlags.Master = server.URL
resumeJobFlags.Namespace = "test"
resumeJobFlags.JobName = "testjob"

testCases := []struct {
Name string
ExpectValue error
}{
{
Name: "ResumeJob",
ExpectValue: nil,
},
}

for i, testcase := range testCases {
err := ResumeJob()
if err != nil {
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
}
}

}
47 changes: 47 additions & 0 deletions pkg/cli/job/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package job

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

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

func TestCreateJob(t *testing.T) {
response := v1alpha1.Job{}

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
val, err := json.Marshal(response)
if err == nil {
w.Write(val)
}

})

server := httptest.NewServer(handler)
defer server.Close()

launchJobFlags.Master = server.URL
launchJobFlags.Namespace = "test"

testCases := []struct {
Name string
ExpectValue error
}{
{
Name: "CreateJob",
ExpectValue: nil,
},
}

for i, testcase := range testCases {
err := RunJob()
if err != nil {
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
}
}

}
60 changes: 60 additions & 0 deletions pkg/cli/job/suspend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package job

import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"

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

func TestSuspendJobJob(t *testing.T) {
responsecommand := v1alpha1.Command{}
responsejob := v1alpha1batch.Job{}

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "command") {
w.Header().Set("Content-Type", "application/json")
val, err := json.Marshal(responsecommand)
if err == nil {
w.Write(val)
}

} else {
w.Header().Set("Content-Type", "application/json")
val, err := json.Marshal(responsejob)
if err == nil {
w.Write(val)
}

}
})

server := httptest.NewServer(handler)
defer server.Close()

suspendJobFlags.Master = server.URL
suspendJobFlags.Namespace = "test"
suspendJobFlags.JobName = "testjob"

testCases := []struct {
Name string
ExpectValue error
}{
{
Name: "SuspendJob",
ExpectValue: nil,
},
}

for i, testcase := range testCases {
err := SuspendJob()
if err != nil {
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
}
}

}
49 changes: 49 additions & 0 deletions pkg/cli/job/view_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package job

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

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

func TestViewJob(t *testing.T) {
response := v1alpha1.Job{}
response.Name = "testJob"

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
val, err := json.Marshal(response)
if err == nil {
w.Write(val)
}

})

server := httptest.NewServer(handler)
defer server.Close()

viewJobFlags.Master = server.URL
viewJobFlags.Namespace = "test"
viewJobFlags.JobName = "testJob"

testCases := []struct {
Name string
ExpectValue error
}{
{
Name: "viewJob",
ExpectValue: nil,
},
}

for i, testcase := range testCases {
err := ViewJob()
if err != nil {
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
}
}

}

0 comments on commit 99c5496

Please sign in to comment.