Skip to content

Commit 02bef05

Browse files
Merge pull request #257 from Rajadeepan/utclijob
Adding UT for cli job package
2 parents 1670bbd + 06762ca commit 02bef05

File tree

6 files changed

+409
-0
lines changed

6 files changed

+409
-0
lines changed

pkg/cli/job/delete_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 job
18+
19+
import (
20+
"encoding/json"
21+
"net/http"
22+
"net/http/httptest"
23+
"testing"
24+
25+
v1alpha1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
26+
)
27+
28+
func TestDeleteJobJob(t *testing.T) {
29+
response := v1alpha1.Job{}
30+
response.Name = "testJob"
31+
32+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33+
w.Header().Set("Content-Type", "application/json")
34+
val, err := json.Marshal(response)
35+
if err == nil {
36+
w.Write(val)
37+
}
38+
39+
})
40+
41+
server := httptest.NewServer(handler)
42+
defer server.Close()
43+
44+
deleteJobFlags.Master = server.URL
45+
deleteJobFlags.Namespace = "test"
46+
deleteJobFlags.JobName = "testJob"
47+
48+
testCases := []struct {
49+
Name string
50+
ExpectValue error
51+
}{
52+
{
53+
Name: "DeleteJob",
54+
ExpectValue: nil,
55+
},
56+
}
57+
58+
for i, testcase := range testCases {
59+
err := DeleteJob()
60+
if err != nil {
61+
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
62+
}
63+
}
64+
65+
}

pkg/cli/job/list_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 job
18+
19+
import (
20+
"encoding/json"
21+
"net/http"
22+
"net/http/httptest"
23+
"testing"
24+
25+
v1alpha1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
26+
)
27+
28+
func TestListJob(t *testing.T) {
29+
response := v1alpha1.JobList{}
30+
response.Items = append(response.Items, v1alpha1.Job{})
31+
32+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33+
w.Header().Set("Content-Type", "application/json")
34+
val, err := json.Marshal(response)
35+
if err == nil {
36+
w.Write(val)
37+
}
38+
39+
})
40+
41+
server := httptest.NewServer(handler)
42+
defer server.Close()
43+
44+
listJobFlags.Master = server.URL
45+
listJobFlags.Namespace = "test"
46+
47+
testCases := []struct {
48+
Name string
49+
ExpectValue error
50+
}{
51+
{
52+
Name: "ListJob",
53+
ExpectValue: nil,
54+
},
55+
}
56+
57+
for i, testcase := range testCases {
58+
err := ListJobs()
59+
if err != nil {
60+
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
61+
}
62+
}
63+
64+
}

pkg/cli/job/resume_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 job
18+
19+
import (
20+
"encoding/json"
21+
"net/http"
22+
"net/http/httptest"
23+
"strings"
24+
"testing"
25+
26+
v1alpha1batch "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
27+
v1alpha1 "volcano.sh/volcano/pkg/apis/bus/v1alpha1"
28+
)
29+
30+
func TestResumeJob(t *testing.T) {
31+
responsecommand := v1alpha1.Command{}
32+
responsejob := v1alpha1batch.Job{}
33+
34+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
35+
if strings.HasSuffix(r.URL.Path, "command") {
36+
w.Header().Set("Content-Type", "application/json")
37+
val, err := json.Marshal(responsecommand)
38+
if err == nil {
39+
w.Write(val)
40+
}
41+
42+
} else {
43+
w.Header().Set("Content-Type", "application/json")
44+
val, err := json.Marshal(responsejob)
45+
if err == nil {
46+
w.Write(val)
47+
}
48+
49+
}
50+
})
51+
52+
server := httptest.NewServer(handler)
53+
defer server.Close()
54+
55+
resumeJobFlags.Master = server.URL
56+
resumeJobFlags.Namespace = "test"
57+
resumeJobFlags.JobName = "testjob"
58+
59+
testCases := []struct {
60+
Name string
61+
ExpectValue error
62+
}{
63+
{
64+
Name: "ResumeJob",
65+
ExpectValue: nil,
66+
},
67+
}
68+
69+
for i, testcase := range testCases {
70+
err := ResumeJob()
71+
if err != nil {
72+
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
73+
}
74+
}
75+
76+
}

pkg/cli/job/run_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 job
18+
19+
import (
20+
"encoding/json"
21+
"net/http"
22+
"net/http/httptest"
23+
"testing"
24+
25+
v1alpha1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
26+
)
27+
28+
func TestCreateJob(t *testing.T) {
29+
response := v1alpha1.Job{}
30+
31+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
32+
w.Header().Set("Content-Type", "application/json")
33+
val, err := json.Marshal(response)
34+
if err == nil {
35+
w.Write(val)
36+
}
37+
38+
})
39+
40+
server := httptest.NewServer(handler)
41+
defer server.Close()
42+
43+
launchJobFlags.Master = server.URL
44+
launchJobFlags.Namespace = "test"
45+
46+
testCases := []struct {
47+
Name string
48+
ExpectValue error
49+
}{
50+
{
51+
Name: "CreateJob",
52+
ExpectValue: nil,
53+
},
54+
}
55+
56+
for i, testcase := range testCases {
57+
err := RunJob()
58+
if err != nil {
59+
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
60+
}
61+
}
62+
63+
}

pkg/cli/job/suspend_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 job
18+
19+
import (
20+
"encoding/json"
21+
"net/http"
22+
"net/http/httptest"
23+
"strings"
24+
"testing"
25+
26+
v1alpha1batch "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
27+
v1alpha1 "volcano.sh/volcano/pkg/apis/bus/v1alpha1"
28+
)
29+
30+
func TestSuspendJobJob(t *testing.T) {
31+
responsecommand := v1alpha1.Command{}
32+
responsejob := v1alpha1batch.Job{}
33+
34+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
35+
if strings.HasSuffix(r.URL.Path, "command") {
36+
w.Header().Set("Content-Type", "application/json")
37+
val, err := json.Marshal(responsecommand)
38+
if err == nil {
39+
w.Write(val)
40+
}
41+
42+
} else {
43+
w.Header().Set("Content-Type", "application/json")
44+
val, err := json.Marshal(responsejob)
45+
if err == nil {
46+
w.Write(val)
47+
}
48+
49+
}
50+
})
51+
52+
server := httptest.NewServer(handler)
53+
defer server.Close()
54+
55+
suspendJobFlags.Master = server.URL
56+
suspendJobFlags.Namespace = "test"
57+
suspendJobFlags.JobName = "testjob"
58+
59+
testCases := []struct {
60+
Name string
61+
ExpectValue error
62+
}{
63+
{
64+
Name: "SuspendJob",
65+
ExpectValue: nil,
66+
},
67+
}
68+
69+
for i, testcase := range testCases {
70+
err := SuspendJob()
71+
if err != nil {
72+
t.Errorf("case %d (%s): expected: %v, got %v ", i, testcase.Name, testcase.ExpectValue, err)
73+
}
74+
}
75+
76+
}

0 commit comments

Comments
 (0)