-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c44e1a
commit c3f4785
Showing
1 changed file
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package queue | ||
|
||
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) | ||
} | ||
} | ||
} |