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/internal/app | |
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/internal/app')
-rw-r--r-- | src/k8splugin/internal/app/instance_test.go | 122 |
1 files changed, 122 insertions, 0 deletions
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 |