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

Increase code coverage #81

Merged
merged 34 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
85deeac
increase code coverage to 80%
suryagupta4 Feb 5, 2025
2f09f72
increase coverage api/v* pkgs to 90%
suryagupta4 Feb 7, 2025
2bc9da8
api package test coverage increased to 85.1%
alexemc Feb 13, 2025
6261680
remove lint errors
alikdell Feb 14, 2025
ff24adb
remove lint errors
alikdell Feb 14, 2025
3390872
remove lint errors
alikdell Feb 14, 2025
a7fd746
remove lint errors
alikdell Feb 14, 2025
3dd17a1
remove lintings
alikdell Feb 14, 2025
be2baa3
Beefing up json_encode tests to 86.3%.
anathoodell Feb 14, 2025
9e93688
Merge branch 'chore-code-coverage' of https://github.com/dell/gopower…
anathoodell Feb 14, 2025
7e3dc7b
error injection for testing api.go
ChristianAtDell Feb 14, 2025
bd185b7
linter
ChristianAtDell Feb 14, 2025
131a175
more lint
ChristianAtDell Feb 14, 2025
32226c4
added some injection hooks to jscon_decode.go
ChristianAtDell Feb 14, 2025
fa745d7
fix a UT, boost coverage to 81%
JacobGros Feb 14, 2025
356f515
Bring json_encode unit tests up to 87.8% coverage.
anathoodell Feb 15, 2025
6f877d6
gofumpt, boost gosilion pkg to 90.3%
JacobGros Feb 17, 2025
30e87a2
linting issue
JacobGros Feb 17, 2025
1028955
exports_test.go
JacobGros Feb 17, 2025
7844fc8
fix test
JacobGros Feb 17, 2025
12966ef
switch logic so error is returned first before possibly nil result is…
JacobGros Feb 17, 2025
ce760ce
try ut fix
JacobGros Feb 17, 2025
fb69d3d
ignore time zone for UT test
JacobGros Feb 17, 2025
0e15002
add comment
JacobGros Feb 17, 2025
0a2e6d5
fix comment
JacobGros Feb 17, 2025
d86cea7
fix comment
JacobGros Feb 17, 2025
f2ca27b
Increase UT code coverage of json_decode.go
WilsonRadadia20 Feb 18, 2025
f5af15a
cleaned up a test case in json_decode
ChristianAtDell Feb 18, 2025
808c0e7
added a test case to json_stream
ChristianAtDell Feb 18, 2025
6c9e451
Increased UT coverage of json_decode.test to 62.3%
WilsonRadadia20 Feb 19, 2025
6f3f4ac
Merge branch 'chore-code-coverage' of https://github.com/dell/gopower…
WilsonRadadia20 Feb 19, 2025
e09a5f9
Update UT coverage of json_decode to 66.7%
WilsonRadadia20 Feb 19, 2025
370117a
Remove private json package in favor of encoding/json (#93)
donatwork Feb 19, 2025
bfc33cc
Merge branch 'main' into chore-code-coverage
donatwork Feb 19, 2025
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
19 changes: 9 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
.PHONY: all
all: go-build
all: build

ifneq (on,$(GO111MODULE))
export GO111MODULE := on
endif

#.PHONY: go-dep
go-dep:
#.PHONY: dep
dep:
go mod download && go mod verify

.PHONY: go-build
go-build:
.PHONY: build
build:
git config core.hooksPath hooks
go build .
#
# Tests-related tasks
#
.PHONY: go-unittest
go-unittest: go-build
.PHONY: unit-test
unit-test: build
go test -json ./... -run ^Test

.PHONY: go-coverage
go-coverage: go-build
.PHONY: coverage
coverage: build
go test -json -covermode=atomic -coverpkg=./... -coverprofile goisilon_coverprofile.out ./... -run ^Test

115 changes: 24 additions & 91 deletions acls_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (c) 2022-2025 Dell Inc, or its subsidiaries.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,105 +16,38 @@ limitations under the License.
package goisilon

import (
"fmt"
"testing"

"github.com/dell/goisilon/mocks"
"github.com/stretchr/testify/assert"

api "github.com/dell/goisilon/api/v2"
)

func TestGetVolumeACL(t *testing.T) {
volumeName := "test_get_volume_acl"

// make sure the volume exists
client.CreateVolume(defaultCtx, volumeName)
volume, err := client.GetVolume(defaultCtx, volumeName, volumeName)
assertNoError(t, err)
assertNotNil(t, volume)

defer client.DeleteVolume(defaultCtx, volume.Name)

username := client.API.User()
user, err := client.GetUserByNameOrUID(defaultCtx, &username, nil)
assertNoError(t, err)
assertNotNil(t, user)

acl, err := client.GetVolumeACL(defaultCtx, volume.Name)
assertNoError(t, err)
assertNotNil(t, acl)

assertNotNil(t, acl.Owner)
assertNotNil(t, acl.Owner.Name)
assert.Equal(t, user.Name, *acl.Owner.Name)
assertNotNil(t, acl.Owner.Type)
assert.Equal(t, api.PersonaTypeUser, *acl.Owner.Type)
assertNotNil(t, acl.Owner.ID)
assert.Equal(t, user.OnDiskUserIdentity.ID, fmt.Sprintf("UID:%s", acl.Owner.ID.ID))
assert.Equal(t, api.PersonaIDTypeUID, acl.Owner.ID.Type)
client.API.(*mocks.Client).On("VolumesPath", anyArgs[0:6]...).Return("").Once()
client.API.(*mocks.Client).On("Get", anyArgs[0:6]...).Return(nil).Once()
_, err := client.GetVolumeACL(defaultCtx, "test_get_volume_acl")
assert.Nil(t, err)
}

func TestSetVolumeOwnerToCurrentUser(t *testing.T) {
volumeName := "test_set_volume_owner"

// make sure the volume exists
client.CreateVolume(defaultCtx, volumeName)
volume, err := client.GetVolume(defaultCtx, volumeName, volumeName)
assertNoError(t, err)
assertNotNil(t, volume)

defer client.DeleteVolume(defaultCtx, volume.Name)

username := client.API.User()
user, err := client.GetUserByNameOrUID(defaultCtx, &username, nil)
assertNoError(t, err)
assertNotNil(t, user)

acl, err := client.GetVolumeACL(defaultCtx, volume.Name)
assertNoError(t, err)
assertNotNil(t, acl)

assertNotNil(t, acl.Owner)
assertNotNil(t, acl.Owner.Name)
assert.Equal(t, user.Name, *acl.Owner.Name)
assertNotNil(t, acl.Owner.Type)
assert.Equal(t, api.PersonaTypeUser, *acl.Owner.Type)
assertNotNil(t, acl.Owner.ID)
assert.Equal(t, user.OnDiskUserIdentity.ID, fmt.Sprintf("UID:%s", acl.Owner.ID.ID))
assert.Equal(t, api.PersonaIDTypeUID, acl.Owner.ID.Type)

err = client.SetVolumeOwner(defaultCtx, volume.Name, "rexray")
if err != nil {
t.Skipf("Unable to change volume owner: %s - is efs.bam.chown_unrestricted set?", err)
}
assertNoError(t, err)

acl, err = client.GetVolumeACL(defaultCtx, volume.Name)
assertNoError(t, err)
assertNotNil(t, acl)

assertNotNil(t, acl.Owner)
assertNotNil(t, acl.Owner.Name)
assert.Equal(t, "rexray", *acl.Owner.Name)
assertNotNil(t, acl.Owner.Type)
assert.Equal(t, api.PersonaTypeUser, *acl.Owner.Type)
assertNotNil(t, acl.Owner.ID)
assert.Equal(t, "2000", acl.Owner.ID.ID)
assert.Equal(t, api.PersonaIDTypeUID, acl.Owner.ID.Type)

err = client.SetVolumeOwnerToCurrentUser(defaultCtx, volume.Name)
assertNoError(t, err)
client.API.(*mocks.Client).On("VolumesPath", anyArgs[0:6]...).Return("").Once()
client.API.(*mocks.Client).On("Get", anyArgs[0:6]...).Return(nil).Once()
client.API.(*mocks.Client).On("User", anyArgs[0:6]...).Return("").Once()
client.API.(*mocks.Client).On("Put", anyArgs...).Return(nil).Once()
err := client.SetVolumeOwnerToCurrentUser(defaultCtx, "test_set_volume_owner")
assert.Nil(t, err)
}

acl, err = client.GetVolumeACL(defaultCtx, volume.Name)
assertNoError(t, err)
assertNotNil(t, acl)
func TestSetVolumeOwner(t *testing.T) {
client.API.(*mocks.Client).On("VolumesPath", anyArgs[0:6]...).Return("").Once()
client.API.(*mocks.Client).On("Put", anyArgs...).Return(nil).Once()
err := client.SetVolumeOwner(defaultCtx, "test_set_volume_owner", "rexray")
assert.Nil(t, err)
}

assertNotNil(t, acl.Owner)
assertNotNil(t, acl.Owner.Name)
assert.Equal(t, client.API.User(), *acl.Owner.Name)
assertNotNil(t, acl.Owner.Type)
assert.Equal(t, api.PersonaTypeUser, *acl.Owner.Type)
assertNotNil(t, acl.Owner.ID)
assert.Equal(t, "10", acl.Owner.ID.ID)
assert.Equal(t, api.PersonaIDTypeUID, acl.Owner.ID.Type)
func TestSetVolumeMode(t *testing.T) {
client.API.(*mocks.Client).On("VolumesPath", anyArgs[0:6]...).Return("").Once()
client.API.(*mocks.Client).On("Put", anyArgs...).Return(nil).Once()
err := client.SetVolumeMode(defaultCtx, "test_set_volume_owner", 777)
assert.Nil(t, err)
}
27 changes: 22 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -35,7 +36,6 @@ import (
"github.com/sirupsen/logrus"

"github.com/PuerkitoBio/goquery"
"github.com/dell/goisilon/api/json"
)

const (
Expand Down Expand Up @@ -389,6 +389,10 @@ func (c *client) DoWithHeaders(
params OrderedValues, headers map[string]string,
body, resp interface{},
) error {
return doWithHeadersFunc(c, ctx, method, uri, id, params, headers, body, resp)
}

var doWithHeadersFunc = func(c *client, ctx context.Context, method string, uri string, id string, params OrderedValues, headers map[string]string, body, resp interface{}) error {
res, _, err := c.DoAndGetResponseBody(
ctx, method, uri, id, params, headers, body)
if err != nil {
Expand Down Expand Up @@ -425,6 +429,16 @@ func (c *client) DoAndGetResponseBody(
method, uri, id string,
params OrderedValues, headers map[string]string,
body interface{},
) (*http.Response, bool, error) {
return doAndGetResponseBodyFunc(c, ctx, method, uri, id, params, headers, body)
}

var doAndGetResponseBodyFunc = func(
c *client,
ctx context.Context,
method, uri, id string,
params OrderedValues, headers map[string]string,
body interface{},
) (*http.Response, bool, error) {
var (
err error
Expand Down Expand Up @@ -650,6 +664,10 @@ func parseJSONHTMLError(r *http.Response) error {
// Authenticate make a REST API call [/session/1/session] to PowerScale to authenticate the given credentials.
// The response contains the session Cookie, X-CSRF-Token and the client uses it for further communication.
func (c *client) authenticate(ctx context.Context, username string, password string, endpoint string) error {
return authenticateFunc(c, ctx, username, password, endpoint)
}

var authenticateFunc = func(c *client, ctx context.Context, username string, password string, endpoint string) error {
headers := make(map[string]string, 1)
headers[headerKeyContentType] = headerValContentTypeJSON
data := &setupConnection{Services: []string{"platform", "namespace"}, Username: username, Password: password}
Expand Down Expand Up @@ -722,19 +740,18 @@ func (c *client) executeWithRetryAuthenticate(ctx context.Context, method, uri s
return fmt.Errorf("authentication failure due to: %v", err)
}
return c.DoWithHeaders(ctx, method, uri, id, params, headers, body, resp)
} else {
log.Error(ctx, "Error in response. Method:%s URI:%s Error: %v JSON Error: %+v", method, uri, err, e)
}
log.Error(ctx, "Error in response. Method:%s URI:%s Error: %v JSON Error: %+v", method, uri, err, e)

case *HTMLError:
if e.StatusCode == 401 {
log.Debug(ctx, "Authentication failed. Trying to re-authenticate")
if err := c.authenticate(ctx, c.username, c.password, c.hostname); err != nil {
return fmt.Errorf("authentication failure due to: %v", err)
}
return c.DoWithHeaders(ctx, method, uri, id, params, headers, body, resp)
} else {
log.Error(ctx, "Error in response. Method:%s URI:%s Error: %v HTML Error: %+v", method, uri, err, e)
}
log.Error(ctx, "Error in response. Method:%s URI:%s Error: %v HTML Error: %+v", method, uri, err, e)
default:
log.Error(ctx, "Error is not a type of \"*JSONError or *HTMLError\". Error:", err)
}
Expand Down
Loading