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 Queue pkg under cli #258

Merged
merged 1 commit into from
Jun 29, 2019
Merged
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
191 changes: 191 additions & 0 deletions pkg/cli/queue/queue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
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 queue
asifdxtreme marked this conversation as resolved.
Show resolved Hide resolved

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

"github.com/kubernetes-sigs/kube-batch/pkg/apis/scheduling/v1alpha1"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1"
)

func getTestQueueHTTPServer(t *testing.T) *httptest.Server {

response := v1alpha1.Queue{}

response.Name = "testQueue"
response.Spec.Weight = int32(2)

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)
}
})
return httptest.NewServer(handler)
}

func getTestQueueListHTTPServer(t *testing.T) *httptest.Server {

response := v1alpha1.QueueList{}

response.Items = []v1alpha1.Queue{
{
ObjectMeta: v1.ObjectMeta{
Name: "testQueue",
},
Spec: v1alpha1.QueueSpec{
Weight: int32(2),
},
},
}

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)
}
})
return httptest.NewServer(handler)
}

func getCommonFlags(master string) commonFlags {
return commonFlags{
Master: master,
}
}

func TestCreateQueue(t *testing.T) {
InitRunFlags(&cobra.Command{})
server := getTestQueueHTTPServer(t)
defer server.Close()

createQueueFlags.commonFlags = getCommonFlags(server.URL)
createQueueFlags.Name = "testQueue"
createQueueFlags.Weight = int32(2)

testCases := []struct {
Name string
ExpectValue error
}{
{
Name: "CreateQueue",
ExpectValue: nil,
},
}
for _, testcase := range testCases {
err := CreateQueue()
if err != nil {
t.Errorf("(%s): expected: %v, got %v ", testcase.Name, testcase.ExpectValue, err)
}
}
}

func TestGetQueue(t *testing.T) {
InitGetFlags(&cobra.Command{})
server := getTestQueueHTTPServer(t)
defer server.Close()

getQueueFlags.commonFlags = getCommonFlags(server.URL)

testCases := []struct {
Name string
ExpectValue error
QueueName string
}{
{
Name: "GetQueue",
ExpectValue: nil,
QueueName: "testQueue",
},
{
Name: "",
ExpectValue: fmt.Errorf("name is mandatory to get the particular queue details"),
QueueName: "",
},
}
for _, testcase := range testCases {
getQueueFlags.Name = testcase.QueueName
err := GetQueue()
if err != nil && (err.Error() != testcase.ExpectValue.Error()) {
t.Errorf("(%s): expected: %v, got %v ", testcase.Name, testcase.ExpectValue, err)
}
}
}

func TestListQueue_empty(t *testing.T) {
InitListFlags(&cobra.Command{})
server := getTestQueueHTTPServer(t)
defer server.Close()

listQueueFlags.commonFlags = getCommonFlags(server.URL)

testCases := []struct {
Name string
ExpectValue error
QueueName string
}{
{
Name: "GetQueue",
ExpectValue: nil,
},
}
for _, testcase := range testCases {
err := ListQueue()
if err != nil && (err.Error() != testcase.ExpectValue.Error()) {
t.Errorf("(%s): expected: %v, got %v ", testcase.Name, testcase.ExpectValue, err)
}
}
}

func TestListQueue_nonempty(t *testing.T) {
InitListFlags(&cobra.Command{})
server := getTestQueueListHTTPServer(t)
defer server.Close()

listQueueFlags.commonFlags = getCommonFlags(server.URL)

testCases := []struct {
Name string
ExpectValue error
QueueName string
}{
{
Name: "GetQueue",
ExpectValue: nil,
QueueName: "testQueue",
},
{
Name: "",
ExpectValue: fmt.Errorf("name is mandatory to get the particular queue details"),
QueueName: "",
},
}
for _, testcase := range testCases {
err := ListQueue()
if err != nil && err.Error() != testcase.ExpectValue.Error() {
t.Errorf("(%s): expected: %v, got %v ", testcase.Name, testcase.ExpectValue, err)
}
}
}