Add unit tests 85/96985/2
authorKiran Kamineni <kiran.k.kamineni@intel.com>
Fri, 11 Oct 2019 22:54:04 +0000 (15:54 -0700)
committerKiran Kamineni <kiran.k.kamineni@intel.com>
Tue, 15 Oct 2019 17:02:40 +0000 (10:02 -0700)
Add unit tests for the handler as well as backend status
function

Issue-ID: MULTICLOUD-675
Change-Id: I4c73e2c18f243702f3e791fec48d4bc5023cafd5
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
src/k8splugin/api/instancehandler_test.go
src/k8splugin/internal/app/instance_test.go

index b20575e..7b6594c 100644 (file)
@@ -39,9 +39,10 @@ type mockInstanceClient struct {
        app.InstanceManager
        // Items and err will be used to customize each test
        // via a localized instantiation of mockInstanceClient
-       items     []app.InstanceResponse
-       miniitems []app.InstanceMiniResponse
-       err       error
+       items      []app.InstanceResponse
+       miniitems  []app.InstanceMiniResponse
+       statusItem app.InstanceStatus
+       err        error
 }
 
 func (m *mockInstanceClient) Create(inp app.InstanceRequest) (app.InstanceResponse, error) {
@@ -60,6 +61,14 @@ func (m *mockInstanceClient) Get(id string) (app.InstanceResponse, error) {
        return m.items[0], nil
 }
 
+func (m *mockInstanceClient) Status(id string) (app.InstanceStatus, error) {
+       if m.err != nil {
+               return app.InstanceStatus{}, m.err
+       }
+
+       return m.statusItem, nil
+}
+
 func (m *mockInstanceClient) List(rbname, rbversion, profilename string) ([]app.InstanceMiniResponse, error) {
        if m.err != nil {
                return []app.InstanceMiniResponse{}, m.err
@@ -307,6 +316,91 @@ func TestInstanceGetHandler(t *testing.T) {
        }
 }
 
+func TestStatusHandler(t *testing.T) {
+       testCases := []struct {
+               label            string
+               input            string
+               expectedCode     int
+               expectedResponse *app.InstanceStatus
+               instClient       *mockInstanceClient
+       }{
+               {
+                       label:        "Fail to Get Status",
+                       input:        "HaKpys8e",
+                       expectedCode: http.StatusInternalServerError,
+                       instClient: &mockInstanceClient{
+                               err: pkgerrors.New("Internal error"),
+                       },
+               },
+               {
+                       label:        "Succesful GET Status",
+                       input:        "HaKpys8e",
+                       expectedCode: http.StatusOK,
+                       expectedResponse: &app.InstanceStatus{
+                               Request: app.InstanceRequest{
+                                       RBName:      "test-rbdef",
+                                       RBVersion:   "v1",
+                                       ProfileName: "profile1",
+                                       CloudRegion: "region1",
+                               },
+                               Ready:         true,
+                               ResourceCount: 2,
+                               PodStatuses: []app.PodStatus{
+                                       {
+                                               Name:        "test-pod1",
+                                               Namespace:   "default",
+                                               Ready:       true,
+                                               IPAddresses: []string{"192.168.1.1", "192.168.2.1"},
+                                       },
+                                       {
+                                               Name:        "test-pod2",
+                                               Namespace:   "default",
+                                               Ready:       true,
+                                               IPAddresses: []string{"192.168.3.1", "192.168.5.1"},
+                                       },
+                               },
+                       },
+                       instClient: &mockInstanceClient{
+                               statusItem: app.InstanceStatus{
+                                       Request: app.InstanceRequest{
+                                               RBName:      "test-rbdef",
+                                               RBVersion:   "v1",
+                                               ProfileName: "profile1",
+                                               CloudRegion: "region1",
+                                       },
+                                       Ready:         true,
+                                       ResourceCount: 2,
+                                       PodStatuses: []app.PodStatus{
+                                               {
+                                                       Name:        "test-pod1",
+                                                       Namespace:   "default",
+                                                       Ready:       true,
+                                                       IPAddresses: []string{"192.168.1.1", "192.168.2.1"},
+                                               },
+                                               {
+                                                       Name:        "test-pod2",
+                                                       Namespace:   "default",
+                                                       Ready:       true,
+                                                       IPAddresses: []string{"192.168.3.1", "192.168.5.1"},
+                                               },
+                                       },
+                               },
+                       },
+               },
+       }
+
+       for _, testCase := range testCases {
+               t.Run(testCase.label, func(t *testing.T) {
+                       request := httptest.NewRequest("GET", "/v1/instance/"+testCase.input+"/status", 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)
+                       }
+               })
+       }
+}
+
 func TestInstanceListHandler(t *testing.T) {
        testCases := []struct {
                label            string
index b79cf38..1b84b44 100644 (file)
@@ -318,6 +318,128 @@ func TestInstanceGet(t *testing.T) {
        })
 }
 
+func TestInstanceStatus(t *testing.T) {
+       oldkrdPluginData := utils.LoadedPlugins
+
+       defer func() {
+               utils.LoadedPlugins = oldkrdPluginData
+       }()
+
+       err := LoadMockPlugins(utils.LoadedPlugins)
+       if err != nil {
+               t.Fatalf("LoadMockPlugins returned an error (%s)", err)
+       }
+
+       t.Run("Successfully Get Instance Status", func(t *testing.T) {
+               db.DBconn = &db.MockDB{
+                       Items: map[string]map[string][]byte{
+                               InstanceKey{ID: "HaKpys8e"}.String(): {
+                                       "instanceStatus": []byte(
+                                               `{
+                                                       "request": {
+                                                               "profile-name":"profile1",
+                                                               "rb-name":"test-rbdef",
+                                                               "rb-version":"v1",
+                                                               "cloud-region":"region1"
+                                                       },
+                                                       "ready": true,
+                                                       "resourceCount": 2,
+                                                       "podStatuses": [
+                                                               {
+                                                                       "name":        "test-pod1",
+                                                                       "namespace":   "default",
+                                                                       "ready":       true,
+                                                                       "ipaddresses": ["192.168.1.1", "192.168.2.1"]
+                                                               },
+                                                               {
+                                                                       "name":        "test-pod2",
+                                                                       "namespace":   "default",
+                                                                       "ready":       true,
+                                                                       "ipaddresses": ["192.168.4.1", "192.168.5.1"]
+                                                               }
+                                                       ]
+                                               }`),
+                               },
+                       },
+               }
+
+               expected := InstanceStatus{
+                       Request: InstanceRequest{
+                               RBName:      "test-rbdef",
+                               RBVersion:   "v1",
+                               ProfileName: "profile1",
+                               CloudRegion: "region1",
+                       },
+                       Ready:         true,
+                       ResourceCount: 2,
+                       PodStatuses: []PodStatus{
+                               {
+                                       Name:        "test-pod1",
+                                       Namespace:   "default",
+                                       Ready:       true,
+                                       IPAddresses: []string{"192.168.1.1", "192.168.2.1"},
+                               },
+                               {
+                                       Name:        "test-pod2",
+                                       Namespace:   "default",
+                                       Ready:       true,
+                                       IPAddresses: []string{"192.168.4.1", "192.168.5.1"},
+                               },
+                       },
+               }
+               ic := NewInstanceClient()
+               id := "HaKpys8e"
+               data, err := ic.Status(id)
+               if err != nil {
+                       t.Fatalf("TestInstanceStatus returned an error (%s)", err)
+               }
+               if !reflect.DeepEqual(expected, data) {
+                       t.Fatalf("TestInstanceStatus returned:\n result=%v\n expected=%v",
+                               data, expected)
+               }
+       })
+
+       t.Run("Get non-existing Instance", func(t *testing.T) {
+               db.DBconn = &db.MockDB{
+                       Items: map[string]map[string][]byte{
+                               InstanceKey{ID: "HaKpys8e"}.String(): {
+                                       "instanceStatus": []byte(
+                                               `{
+                                                       "request": {
+                                                               "profile-name":"profile1",
+                                                               "rb-name":"test-rbdef",
+                                                               "rb-version":"v1",
+                                                               "cloud-region":"region1"
+                                                       },
+                                                       "ready": true,
+                                                       "resourceCount": 2,
+                                                       "podStatuses": [
+                                                               {
+                                                                       "name":        "test-pod1",
+                                                                       "namespace":   "default",
+                                                                       "ready":       true,
+                                                                       "ipaddresses": ["192.168.1.1", "192.168.2.1"]
+                                                               },
+                                                               {
+                                                                       "name":        "test-pod2",
+                                                                       "namespace":   "default",
+                                                                       "ready":       true,
+                                                                       "ipaddresses": ["192.168.4.1", "192.168.5.1"]
+                                                               }
+                                                       ]
+                                               }`),
+                               },
+                       },
+               }
+
+               ic := NewInstanceClient()
+               _, err := ic.Get("non-existing")
+               if err == nil {
+                       t.Fatal("Expected error, got pass", err)
+               }
+       })
+}
+
 func TestInstanceFind(t *testing.T) {
        oldkrdPluginData := utils.LoadedPlugins