summaryrefslogtreecommitdiffstats
path: root/src/k8splugin/api/instancehandler_test.go
diff options
context:
space:
mode:
authorBin Yang <bin.yang@windriver.com>2019-07-23 07:30:16 +0000
committerGerrit Code Review <gerrit@onap.org>2019-07-23 07:30:16 +0000
commitf83526881ac7758638b1e7e0e1bcf2ab8cd13133 (patch)
tree83e38d253bf746e39b38d026fd9ba0717f626cad /src/k8splugin/api/instancehandler_test.go
parent40014c64df3250db72b41e05da3dd40746d3d6d3 (diff)
parente04fd6fa1f0abe6b18787b192e01164db1db06db (diff)
Merge "Add a list api for instances"
Diffstat (limited to 'src/k8splugin/api/instancehandler_test.go')
-rw-r--r--src/k8splugin/api/instancehandler_test.go118
1 files changed, 116 insertions, 2 deletions
diff --git a/src/k8splugin/api/instancehandler_test.go b/src/k8splugin/api/instancehandler_test.go
index 35cef531..83fa3d2b 100644
--- a/src/k8splugin/api/instancehandler_test.go
+++ b/src/k8splugin/api/instancehandler_test.go
@@ -21,6 +21,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
+ "sort"
"testing"
"github.com/onap/multicloud-k8s/src/k8splugin/internal/app"
@@ -38,8 +39,9 @@ type mockInstanceClient struct {
app.InstanceManager
// Items and err will be used to customize each test
// via a localized instantiation of mockInstanceClient
- items []app.InstanceResponse
- err error
+ items []app.InstanceResponse
+ miniitems []app.InstanceMiniResponse
+ err error
}
func (m *mockInstanceClient) Create(inp app.InstanceRequest) (app.InstanceResponse, error) {
@@ -58,6 +60,14 @@ func (m *mockInstanceClient) Get(id string) (app.InstanceResponse, error) {
return m.items[0], nil
}
+func (m *mockInstanceClient) List() ([]app.InstanceMiniResponse, error) {
+ if m.err != nil {
+ return []app.InstanceMiniResponse{}, m.err
+ }
+
+ return m.miniitems, nil
+}
+
func (m *mockInstanceClient) Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]app.InstanceResponse, error) {
if m.err != nil {
return nil, m.err
@@ -297,6 +307,110 @@ func TestInstanceGetHandler(t *testing.T) {
}
}
+func TestInstanceListHandler(t *testing.T) {
+ testCases := []struct {
+ label string
+ input string
+ expectedCode int
+ expectedResponse []app.InstanceMiniResponse
+ instClient *mockInstanceClient
+ }{
+ {
+ label: "Fail to List Instance",
+ input: "HaKpys8e",
+ expectedCode: http.StatusInternalServerError,
+ instClient: &mockInstanceClient{
+ err: pkgerrors.New("Internal error"),
+ },
+ },
+ {
+ label: "Succesful List Instances",
+ expectedCode: http.StatusOK,
+ expectedResponse: []app.InstanceMiniResponse{
+ {
+ ID: "HaKpys8e",
+ Request: app.InstanceRequest{
+ RBName: "test-rbdef",
+ RBVersion: "v1",
+ ProfileName: "profile1",
+ CloudRegion: "region1",
+ },
+ Namespace: "testnamespace",
+ },
+ {
+ ID: "HaKpys8f",
+ Request: app.InstanceRequest{
+ RBName: "test-rbdef-two",
+ RBVersion: "versionsomething",
+ ProfileName: "profile3",
+ CloudRegion: "region1",
+ },
+ Namespace: "testnamespace-two",
+ },
+ },
+ instClient: &mockInstanceClient{
+ miniitems: []app.InstanceMiniResponse{
+ {
+ ID: "HaKpys8e",
+ Request: app.InstanceRequest{
+ RBName: "test-rbdef",
+ RBVersion: "v1",
+ ProfileName: "profile1",
+ CloudRegion: "region1",
+ },
+ Namespace: "testnamespace",
+ },
+ {
+ ID: "HaKpys8f",
+ Request: app.InstanceRequest{
+ RBName: "test-rbdef-two",
+ RBVersion: "versionsomething",
+ ProfileName: "profile3",
+ CloudRegion: "region1",
+ },
+ Namespace: "testnamespace-two",
+ },
+ },
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("GET", "/v1/instance", nil)
+ resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil))
+
+ if testCase.expectedCode != resp.StatusCode {
+ t.Fatalf("Request method returned: %v and it was expected: %v",
+ resp.StatusCode, testCase.expectedCode)
+ }
+ if resp.StatusCode == http.StatusOK {
+ var response []app.InstanceMiniResponse
+ err := json.NewDecoder(resp.Body).Decode(&response)
+ if err != nil {
+ t.Fatalf("Parsing the returned response got an error (%s)", err)
+ }
+
+ // Since the order of returned slice is not guaranteed
+ // Sort them first and then do deepequal
+ // Check both and return error if both don't match
+ sort.Slice(response, func(i, j int) bool {
+ return response[i].ID < response[j].ID
+ })
+
+ sort.Slice(testCase.expectedResponse, func(i, j int) bool {
+ return testCase.expectedResponse[i].ID < testCase.expectedResponse[j].ID
+ })
+
+ if reflect.DeepEqual(testCase.expectedResponse, response) == false {
+ t.Fatalf("TestGetHandler returned:\n result=%v\n expected=%v",
+ &response, testCase.expectedResponse)
+ }
+ }
+ })
+ }
+}
+
func TestDeleteHandler(t *testing.T) {
testCases := []struct {
label string