diff options
Diffstat (limited to 'src/k8splugin/api/instancehandler_test.go')
-rw-r--r-- | src/k8splugin/api/instancehandler_test.go | 158 |
1 files changed, 97 insertions, 61 deletions
diff --git a/src/k8splugin/api/instancehandler_test.go b/src/k8splugin/api/instancehandler_test.go index 418054ec..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 @@ -488,61 +582,3 @@ func TestDeleteHandler(t *testing.T) { }) } } - -// TODO: Update this test when the UpdateVNF endpoint is fixed. -/* -func TestVNFInstanceUpdate(t *testing.T) { - t.Run("Succesful update a VNF", func(t *testing.T) { - payload := []byte(`{ - "cloud_region_id": "region1", - "csar_id": "UUID-1", - "oof_parameters": [{ - "key1": "value1", - "key2": "value2", - "key3": {} - }], - "network_parameters": { - "oam_ip_address": { - "connection_point": "string", - "ip_address": "string", - "workload_name": "string" - } - } - }`) - expected := &UpdateVnfResponse{ - DeploymentID: "1", - } - - var result UpdateVnfResponse - - req := httptest.NewRequest("PUT", "/v1/vnf_instances/1", bytes.NewBuffer(payload)) - - GetVNFClient = func(configPath string) (krd.VNFInstanceClientInterface, error) { - return &mockClient{ - update: func() error { - return nil - }, - }, nil - } - utils.ReadCSARFromFileSystem = func(csarID string) (*krd.KubernetesData, error) { - kubeData := &krd.KubernetesData{ - Deployment: &appsV1.Deployment{}, - Service: &coreV1.Service{}, - } - return kubeData, nil - } - - response := executeRequest(req) - checkResponseCode(t, http.StatusCreated, response.Code) - - err := json.NewDecoder(response.Body).Decode(&result) - if err != nil { - t.Fatalf("TestVNFInstanceUpdate returned:\n result=%v\n expected=%v", err, expected.DeploymentID) - } - - if resp.DeploymentID != expected.DeploymentID { - t.Fatalf("TestVNFInstanceUpdate returned:\n result=%v\n expected=%v", resp.DeploymentID, expected.DeploymentID) - } - }) -} -*/ |