From b3ad24f7f998bb8d907580ee8920546e0c267500 Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Fri, 11 Oct 2019 15:54:04 -0700 Subject: 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 --- src/k8splugin/internal/app/instance_test.go | 122 ++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) (limited to 'src/k8splugin/internal/app/instance_test.go') 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 -- cgit 1.2.3-korg