aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKiran Kamineni <kiran.k.kamineni@intel.com>2019-10-11 15:54:04 -0700
committerKiran Kamineni <kiran.k.kamineni@intel.com>2019-10-15 10:02:40 -0700
commitb3ad24f7f998bb8d907580ee8920546e0c267500 (patch)
tree512d1a4a75624a58e58e37027060431e66101684 /src
parent1c851d8bcbf526be6d7295b9e01a597be65cc692 (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')
-rw-r--r--src/k8splugin/api/instancehandler_test.go100
-rw-r--r--src/k8splugin/internal/app/instance_test.go122
2 files changed, 219 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
diff --git a/src/k8splugin/internal/app/instance_test.go b/src/k8splugin/internal/app/instance_test.go
index b79cf388..1b84b449 100644
--- a/src/k8splugin/internal/app/instance_test.go
+++ b/src/k8splugin/internal/app/instance_test.go
@@ -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