Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding UT for cli job package #257

Merged
merged 2 commits into from
Jun 27, 2019
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
65 changes: 65 additions & 0 deletions pkg/cli/job/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2019 The Volcano Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

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)
}
}

}
64 changes: 64 additions & 0 deletions pkg/cli/job/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2019 The Volcano Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

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)
}
}

}
76 changes: 76 additions & 0 deletions pkg/cli/job/resume_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2019 The Volcano Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

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)
}
}

}
63 changes: 63 additions & 0 deletions pkg/cli/job/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2019 The Volcano Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

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)
}
}

}
76 changes: 76 additions & 0 deletions pkg/cli/job/suspend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2019 The Volcano Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

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)
}
}

}
Loading