diff options
author | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-10-11 15:54:04 -0700 |
---|---|---|
committer | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-10-15 10:02:40 -0700 |
commit | b3ad24f7f998bb8d907580ee8920546e0c267500 (patch) | |
tree | 512d1a4a75624a58e58e37027060431e66101684 /src/k8splugin/api/instancehandler_test.go | |
parent | 1c851d8bcbf526be6d7295b9e01a597be65cc692 (diff) |
Add unit tests
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>
Diffstat (limited to 'src/k8splugin/api/instancehandler_test.go')
-rw-r--r-- | src/k8splugin/api/instancehandler_test.go | 100 |
1 files changed, 97 insertions, 3 deletions
diff --git a/src/k8splugin/api/instancehandler_test.go b/src/k8splugin/api/instancehandler_test.go index b20575e7..7b6594cf 100644 --- a/src/k8splugin/api/instancehandler_test.go +++ b/src/k8splugin/api/instancehandler_test.go @@ -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 |