diff --git a/api/api_test.go b/api/api_test.go index 4e1325c..9b0432b 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -98,6 +98,8 @@ type testResp struct { } func TestClient_Query(t *testing.T) { + os.Setenv("GOPOWERSTORE_DEBUG", "true") + defer os.Unsetenv("GOPOWERSTORE_DEBUG") apiURL := "https://foo" testURL := "mock" action := "attach" @@ -122,6 +124,82 @@ func TestClient_Query(t *testing.T) { assert.Equal(t, resp.Name, "Foo") } +func TestClient_Query_Forbidden(t *testing.T) { + os.Setenv("GOPOWERSTORE_DEBUG", "true") + defer os.Unsetenv("GOPOWERSTORE_DEBUG") + apiURL := "https://foo" + testURL := "mock" + action := "attach" + login := "login_session" + id := "5bfebae3-a278-4c50-af16-011a1dfc1b6f" + c := testClient(t, apiURL) + ctx := context.Background() + httpmock.Activate() + defer httpmock.DeactivateAndReset() + respData := `{"name": "Foo"}` + loginData := `[{"id": "id"}]` + qp := QueryParams{} + qp.RawArg("foo", "bar") + + requestCount := -1 + statusCodeFn := func() int { + requestCount++ + switch requestCount { + case 0: + return http.StatusForbidden + default: + return http.StatusCreated + } + } + + httpmock.RegisterResponder("POST", fmt.Sprintf("%s/%s/%s/%s?foo=bar", apiURL, testURL, id, action), + func(_ *http.Request) (*http.Response, error) { + code := statusCodeFn() + return httpmock.NewStringResponse(code, respData), nil + }) + httpmock.RegisterResponder("GET", fmt.Sprintf("%s/%s", apiURL, login), + httpmock.NewStringResponder(http.StatusOK, loginData)) + + reqBody := make(map[string]string) + reqBody["foo"] = "bar" + resp := &testResp{} + _, err := c.Query(ctx, RequestConfig{ + Method: "POST", Endpoint: testURL, ID: id, Action: action, QueryParams: &qp, Body: reqBody, + }, resp) + assert.Nil(t, err) + assert.Equal(t, "Foo", resp.Name) +} + +func TestClient_Query_Login_Error(t *testing.T) { + os.Setenv("GOPOWERSTORE_DEBUG", "true") + defer os.Unsetenv("GOPOWERSTORE_DEBUG") + apiURL := "https://foo" + testURL := "mock" + action := "attach" + login := "login_session" + id := "5bfebae3-a278-4c50-af16-011a1dfc1b6f" + c := testClient(t, apiURL) + ctx := context.Background() + httpmock.Activate() + defer httpmock.DeactivateAndReset() + respData := `{"name": "Foo"}` + qp := QueryParams{} + qp.RawArg("foo", "bar") + + httpmock.RegisterResponder("POST", fmt.Sprintf("%s/%s/%s/%s?foo=bar", apiURL, testURL, id, action), + httpmock.NewStringResponder(http.StatusForbidden, respData)) + httpmock.RegisterResponder("GET", fmt.Sprintf("%s/%s", apiURL, login), + httpmock.NewStringResponder(http.StatusUnauthorized, respData)) + + reqBody := make(map[string]string) + reqBody["foo"] = "bar" + resp := &testResp{} + _, err := c.Query(ctx, RequestConfig{ + Method: "POST", Endpoint: testURL, ID: id, Action: action, QueryParams: &qp, Body: reqBody, + }, resp) + assert.NotNil(t, err) +} + func TestClientIMPL_prepareRequestURL(t *testing.T) { apiURL := "https://foo.com" endpoint := "node" @@ -167,6 +245,25 @@ func TestClientIMPL_updatePaginationInfoInMeta(t *testing.T) { assert.False(t, meta.Pagination.IsPaginate) } +func TestClientIMPL_SetLogger(t *testing.T) { + log := &defaultLogger{} + c := ClientIMPL{apiThrottle: NewTimeoutSemaphore(10, 10, log)} + c.SetLogger(&defaultLogger{}) + assert.NotNil(t, c.logger) +} + +func TestClientIMPL_GetCustomHTTPHeaders(t *testing.T) { + c := ClientIMPL{} + + want := http.Header{ + "foo": {"bar"}, + } + c.SetCustomHTTPHeaders(want) + + got := c.GetCustomHTTPHeaders() + assert.Equal(t, want, got) +} + type qpTest struct{} func (qp *qpTest) Fields() []string { diff --git a/client_options_test.go b/client_options_test.go index 5604cad..a49c2a5 100644 --- a/client_options_test.go +++ b/client_options_test.go @@ -42,3 +42,10 @@ func TestClientOptions_RequestIDKey(t *testing.T) { co.SetRequestIDKey("foobar") assert.Equal(t, "foobar", string(co.RequestIDKey())) } + +func TestClientOptions_RateLimit(t *testing.T) { + co := NewClientOptions() + value := 10 + co.SetRateLimit(value) + assert.Equal(t, value, co.RateLimit()) +} diff --git a/fs_test.go b/fs_test.go index 9f1760c..3e5a2bb 100644 --- a/fs_test.go +++ b/fs_test.go @@ -28,8 +28,9 @@ import ( ) const ( - nasMockURL = APIMockURL + nasURL - fsMockURL = APIMockURL + fsURL + nasMockURL = APIMockURL + nasURL + fsMockURL = APIMockURL + fsURL + nfsMockServerURL = APIMockURL + nfsServerURL ) var ( @@ -164,6 +165,17 @@ func TestClientIMPL_CreateNAS(t *testing.T) { assert.Equal(t, nasID, nas.ID) } +func TestClientIMPL_GetNAS(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + respData := fmt.Sprintf(`{"id": "%s"}`, nasID) + httpmock.RegisterResponder("GET", fmt.Sprintf("%s/%s", nasMockURL, nasID), + httpmock.NewStringResponder(200, respData)) + nas, err := C.GetNAS(context.Background(), nasID) + assert.Nil(t, err) + assert.Equal(t, nasID, nas.ID) +} + func TestClientIMPL_DeleteNAS(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() @@ -173,3 +185,75 @@ func TestClientIMPL_DeleteNAS(t *testing.T) { assert.Nil(t, err) assert.Len(t, string(resp), 0) } + +func TestClientIMPL_GetNfsServer(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + respData := fmt.Sprintf(`{"id": "%s"}`, nasID) + httpmock.RegisterResponder("GET", fmt.Sprintf("%s/%s", nfsMockServerURL, nfsID), + httpmock.NewStringResponder(200, respData)) + resp, err := C.GetNfsServer(context.Background(), nfsID) + assert.Nil(t, err) + assert.Equal(t, nasID, resp.ID) +} + +func TestClientIMPL_CreateFsSnapshot(t *testing.T) { + id := "5e8d8e8e-671b-336f-db4e-cee0fbdc981e" + httpmock.Activate() + defer httpmock.DeactivateAndReset() + respData := fmt.Sprintf(`{"id": "%s"}`, id) + httpmock.RegisterResponder("POST", fmt.Sprintf("%s/%s/snapshot", fsMockURL, id), + httpmock.NewStringResponder(200, respData)) + resp, err := C.CreateFsSnapshot(context.Background(), &SnapshotFSCreate{}, id) + assert.Nil(t, err) + assert.Equal(t, id, resp.ID) +} + +func TestClientIMPL_GetFsSnapshot(t *testing.T) { + id := "5e8d8e8e-671b-336f-db4e-cee0fbdc981e" + httpmock.Activate() + defer httpmock.DeactivateAndReset() + respData := fmt.Sprintf(`{"id": "%s"}`, id) + httpmock.RegisterResponder("GET", fmt.Sprintf("%s/%s", fsMockURL, id), + httpmock.NewStringResponder(200, respData)) + resp, err := C.GetFsSnapshot(context.Background(), id) + assert.Nil(t, err) + assert.Equal(t, id, resp.ID) +} + +func TestClientIMPL_GetFsSnapshots(t *testing.T) { + id := "5e8d8e8e-671b-336f-db4e-cee0fbdc981e" + httpmock.Activate() + defer httpmock.DeactivateAndReset() + respData := fmt.Sprintf(`[{"id": "%s"}]`, id) + httpmock.RegisterResponder("GET", fsMockURL, + httpmock.NewStringResponder(200, respData)) + resp, err := C.GetFsSnapshots(context.Background()) + assert.Nil(t, err) + assert.Equal(t, id, resp[0].ID) +} + +func TestClientIMPL_GetFsSnapshotsByVolumeID(t *testing.T) { + id := "5e8d8e8e-671b-336f-db4e-cee0fbdc981e" + httpmock.Activate() + defer httpmock.DeactivateAndReset() + respData := fmt.Sprintf(`[{"id": "%s"}]`, id) + httpmock.RegisterResponder("GET", fsMockURL, + httpmock.NewStringResponder(200, respData)) + resp, err := C.GetFsSnapshotsByVolumeID(context.Background(), id) + assert.Nil(t, err) + assert.Equal(t, id, resp[0].ID) +} + +func TestClientIMPL_CreateFsFromSnapshot(t *testing.T) { + id := "5e8d8e8e-671b-336f-db4e-cee0fbdc981e" + name := "test" + httpmock.Activate() + defer httpmock.DeactivateAndReset() + respData := fmt.Sprintf(`{"id": "%s"}`, id) + httpmock.RegisterResponder("POST", fmt.Sprintf("%s/%s/clone", fsMockURL, id), + httpmock.NewStringResponder(200, respData)) + resp, err := C.CreateFsFromSnapshot(context.Background(), &FsClone{Name: &name}, id) + assert.Nil(t, err) + assert.Equal(t, id, resp.ID) +} diff --git a/ip_pool_address_test.go b/ip_pool_address_test.go index 71a5fbc..a824376 100644 --- a/ip_pool_address_test.go +++ b/ip_pool_address_test.go @@ -58,3 +58,25 @@ func TestClientIMPL_GetIPPoolAddress_NotFound(t *testing.T) { _, err := C.GetStorageISCSITargetAddresses(context.Background()) assert.NotNil(t, err) } + +func TestClientIMPL_GetStorageNVMETCPTargetAddresses(t *testing.T) { + want := []IPPoolAddress{ + {ID: "ip1"}, + } + httpmock.Activate() + defer httpmock.DeactivateAndReset() + setResponder := func(respData string) { + httpmock.RegisterResponder("GET", ipPoolAddressMockURL, + httpmock.NewStringResponder(200, respData)) + } + respData := `[{"id": "ip1"}]` + setResponder(respData) + resp, err := C.GetStorageNVMETCPTargetAddresses(context.Background()) + assert.Nil(t, err) + assert.Equal(t, resp, want) + respData = `[]` + setResponder(respData) + _, err = C.GetStorageNVMETCPTargetAddresses(context.Background()) + assert.NotNil(t, err) + httpmock.Reset() +} diff --git a/nfs_test.go b/nfs_test.go index 905724c..432d177 100644 --- a/nfs_test.go +++ b/nfs_test.go @@ -30,6 +30,7 @@ import ( const ( nfsMockURL = APIMockURL + nfsURL nfsServerMockURL = APIMockURL + nfsServerURL + fileMockURL = APIMockURL + fileInterfaceURL ) var ( @@ -119,3 +120,37 @@ func TestClientIMPL_CreateNFSServer(t *testing.T) { assert.Nil(t, err) assert.Equal(t, nfsServerID, nfsServer.ID) } + +func TestClientIMPL_GetNFSExportByFileSystemID(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + setResponder := func(respData string) { + httpmock.RegisterResponder("GET", nfsMockURL, + httpmock.NewStringResponder(200, respData)) + } + respData := fmt.Sprintf(`[{"id": "%s"}]`, nfsID) + setResponder(respData) + nfs, err := C.GetNFSExportByFileSystemID(context.Background(), "test") + assert.Nil(t, err) + assert.Equal(t, nfsID, nfs.ID) + httpmock.Reset() + setResponder("") + _, err = C.GetNFSExportByFileSystemID(context.Background(), "test") + assert.NotNil(t, err) + apiError := err.(APIError) + assert.True(t, apiError.NotFound()) +} + +func TestClientIMPL_GetFileInterface(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + setResponder := func(respData string) { + httpmock.RegisterResponder("GET", fmt.Sprintf("%s/%s", fileMockURL, "test"), + httpmock.NewStringResponder(200, respData)) + } + respData := fmt.Sprintf(`{"id": "%s"}`, nfsID) + setResponder(respData) + fileInterface, err := C.GetFileInterface(context.Background(), "test") + assert.Nil(t, err) + assert.Equal(t, nfsID, fileInterface.ID) +}