diff options
author | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-04-03 16:07:26 -0700 |
---|---|---|
committer | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-04-03 16:07:30 -0700 |
commit | e88bb7c01cbc6940817850037b31db090f39f3d6 (patch) | |
tree | 9efc423488acc80042638083faaacd2b5e3988f6 /src/k8splugin/api/profilehandler_test.go | |
parent | e97d3fddc255b73f87a8b3dbd8fa505e34247504 (diff) |
Add url path tests to profile handler
Improve the profile tests by testing the
url path parameters as well.
Issue-ID: MULTICLOUD-547
Change-Id: Ib31a42cc6c3622d17ddc37eb0506ca991d2334c0
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
Diffstat (limited to 'src/k8splugin/api/profilehandler_test.go')
-rw-r--r-- | src/k8splugin/api/profilehandler_test.go | 94 |
1 files changed, 45 insertions, 49 deletions
diff --git a/src/k8splugin/api/profilehandler_test.go b/src/k8splugin/api/profilehandler_test.go index e6a87129..02325487 100644 --- a/src/k8splugin/api/profilehandler_test.go +++ b/src/k8splugin/api/profilehandler_test.go @@ -70,19 +70,19 @@ func TestRBProfileCreateHandler(t *testing.T) { reader io.Reader expected rb.Profile expectedCode int - rbDefClient *mockRBProfile + rbProClient *mockRBProfile }{ { label: "Missing Body Failure", expectedCode: http.StatusBadRequest, - rbDefClient: &mockRBProfile{}, + rbProClient: &mockRBProfile{}, reader: nil, }, { label: "Create New Profile for Definition", expectedCode: http.StatusCreated, reader: bytes.NewBuffer([]byte(`{ - "rb-name":"testresource_bundle_definition", + "rb-name":"test-rbdef", "rb-version":"v1", "profile-name":"profile1", "release-name":"testprofilereleasename", @@ -90,18 +90,18 @@ func TestRBProfileCreateHandler(t *testing.T) { "kubernetes-version":"1.12.3" }`)), expected: rb.Profile{ - RBName: "testresource_bundle_definition", + RBName: "test-rbdef", RBVersion: "v1", ProfileName: "profile1", ReleaseName: "testprofilereleasename", Namespace: "default", KubernetesVersion: "1.12.3", }, - rbDefClient: &mockRBProfile{ + rbProClient: &mockRBProfile{ //Items that will be returned by the mocked Client Items: []rb.Profile{ { - RBName: "testresource_bundle_definition", + RBName: "test-rbdef", RBVersion: "v1", ProfileName: "profile1", ReleaseName: "testprofilereleasename", @@ -115,12 +115,11 @@ func TestRBProfileCreateHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { - vh := rbProfileHandler{client: testCase.rbDefClient} - req := httptest.NewRequest("POST", "/v1/rb/profile/testresource_bundle_definition/v1/profile", + request := httptest.NewRequest("POST", "/v1/rb/definition/test-rbdef/v1/profile", testCase.reader) - rr := httptest.NewRecorder() - vh.createHandler(rr, req) - resp := rr.Result() + recorder := httptest.NewRecorder() + NewRouter("", nil, testCase.rbProClient).ServeHTTP(recorder, request) + resp := recorder.Result() //Check returned code if resp.StatusCode != testCase.expectedCode { @@ -146,27 +145,27 @@ func TestRBProfileGetHandler(t *testing.T) { testCases := []struct { label string expected rb.Profile - inpUUID string + prname string expectedCode int - rbDefClient *mockRBProfile + rbProClient *mockRBProfile }{ { label: "Get Bundle Profile", expectedCode: http.StatusOK, expected: rb.Profile{ - RBName: "testresource_bundle_definition", + RBName: "test-rbdef", RBVersion: "v1", ProfileName: "profile1", ReleaseName: "testprofilereleasename", Namespace: "default", KubernetesVersion: "1.12.3", }, - inpUUID: "123e4567-e89b-12d3-a456-426655441111", - rbDefClient: &mockRBProfile{ + prname: "profile1", + rbProClient: &mockRBProfile{ // Profile that will be returned by the mockclient Items: []rb.Profile{ { - RBName: "testresource_bundle_definition", + RBName: "test-rbdef", RBVersion: "v1", ProfileName: "profile1", ReleaseName: "testprofilereleasename", @@ -179,8 +178,8 @@ func TestRBProfileGetHandler(t *testing.T) { { label: "Get Non-Exiting Bundle Profile", expectedCode: http.StatusInternalServerError, - inpUUID: "123e4567-e89b-12d3-a456-426655440000", - rbDefClient: &mockRBProfile{ + prname: "non-existing-profile", + rbProClient: &mockRBProfile{ // list of Profiles that will be returned by the mockclient Items: []rb.Profile{}, Err: pkgerrors.New("Internal Error"), @@ -190,12 +189,11 @@ func TestRBProfileGetHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { - vh := rbProfileHandler{client: testCase.rbDefClient} - req := httptest.NewRequest("GET", "/v1/rb/profile/"+testCase.inpUUID, nil) - rr := httptest.NewRecorder() - vh.getHandler(rr, req) + request := httptest.NewRequest("GET", "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname, nil) + recorder := httptest.NewRecorder() + NewRouter("", nil, testCase.rbProClient).ServeHTTP(recorder, request) + resp := recorder.Result() - resp := rr.Result() //Check returned code if resp.StatusCode != testCase.expectedCode { t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) @@ -219,21 +217,21 @@ func TestRBProfileDeleteHandler(t *testing.T) { testCases := []struct { label string - inpUUID string + prname string expectedCode int - rbDefClient *mockRBProfile + rbProClient *mockRBProfile }{ { label: "Delete Bundle Profile", expectedCode: http.StatusNoContent, - inpUUID: "123e4567-e89b-12d3-a456-426655441111", - rbDefClient: &mockRBProfile{}, + prname: "profile1", + rbProClient: &mockRBProfile{}, }, { label: "Delete Non-Exiting Bundle Profile", expectedCode: http.StatusInternalServerError, - inpUUID: "123e4567-e89b-12d3-a456-426655440000", - rbDefClient: &mockRBProfile{ + prname: "non-existing", + rbProClient: &mockRBProfile{ Err: pkgerrors.New("Internal Error"), }, }, @@ -241,12 +239,11 @@ func TestRBProfileDeleteHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { - vh := rbProfileHandler{client: testCase.rbDefClient} - req := httptest.NewRequest("GET", "/v1/rb/profile/"+testCase.inpUUID, nil) - rr := httptest.NewRecorder() - vh.deleteHandler(rr, req) + request := httptest.NewRequest("DELETE", "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname, nil) + recorder := httptest.NewRecorder() + NewRouter("", nil, testCase.rbProClient).ServeHTTP(recorder, request) + resp := recorder.Result() - resp := rr.Result() //Check returned code if resp.StatusCode != testCase.expectedCode { t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) @@ -259,50 +256,49 @@ func TestRBProfileUploadHandler(t *testing.T) { testCases := []struct { label string - inpUUID string + prname string body io.Reader expectedCode int - rbDefClient *mockRBProfile + rbProClient *mockRBProfile }{ { label: "Upload Bundle Profile Content", expectedCode: http.StatusOK, - inpUUID: "123e4567-e89b-12d3-a456-426655441111", + prname: "profile1", body: bytes.NewBuffer([]byte{ 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xf2, 0x48, 0xcd, }), - rbDefClient: &mockRBProfile{}, + rbProClient: &mockRBProfile{}, }, { label: "Upload Invalid Bundle Profile Content", expectedCode: http.StatusInternalServerError, - inpUUID: "123e4567-e89b-12d3-a456-426655440000", + prname: "profile1", body: bytes.NewBuffer([]byte{ 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xf2, 0x48, 0xcd, }), - rbDefClient: &mockRBProfile{ + rbProClient: &mockRBProfile{ Err: pkgerrors.New("Internal Error"), }, }, { label: "Upload Empty Body Content", expectedCode: http.StatusBadRequest, - inpUUID: "123e4567-e89b-12d3-a456-426655440000", - rbDefClient: &mockRBProfile{}, + prname: "profile1", + rbProClient: &mockRBProfile{}, }, } for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { - vh := rbProfileHandler{client: testCase.rbDefClient} - req := httptest.NewRequest("POST", - "/v1/rb/profile/"+testCase.inpUUID+"/content", testCase.body) - rr := httptest.NewRecorder() - vh.uploadHandler(rr, req) + request := httptest.NewRequest("POST", + "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname+"/content", testCase.body) + recorder := httptest.NewRecorder() + NewRouter("", nil, testCase.rbProClient).ServeHTTP(recorder, request) + resp := recorder.Result() - resp := rr.Result() //Check returned code if resp.StatusCode != testCase.expectedCode { t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) |