Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for code coverage #153

Merged
merged 10 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
88 changes: 86 additions & 2 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import (
)

const (
nasMockURL = APIMockURL + nasURL
fsMockURL = APIMockURL + fsURL
nasMockURL = APIMockURL + nasURL
fsMockURL = APIMockURL + fsURL
nfsMockServerURL = APIMockURL + nfsServerURL
)

var (
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
22 changes: 22 additions & 0 deletions ip_pool_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
35 changes: 35 additions & 0 deletions nfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
const (
nfsMockURL = APIMockURL + nfsURL
nfsServerMockURL = APIMockURL + nfsServerURL
fileMockURL = APIMockURL + fileInterfaceURL
)

var (
Expand Down Expand Up @@ -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)
}
Loading