Skip to content

Commit

Permalink
Add tests for YAMLStringify
Browse files Browse the repository at this point in the history
  • Loading branch information
kislaykishore committed Aug 8, 2024
1 parent e4ffc6b commit 5da619f
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 13 deletions.
236 changes: 231 additions & 5 deletions cfg/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,257 @@
package cfg

import (
"fmt"
"os"
"path"
"testing"

"github.com/googlecloudplatform/gcsfuse/v2/internal/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestOctalTypeInConfigStringify(t *testing.T) {
func TestOctalTypeInConfigMarshalling(t *testing.T) {
c := Config{
FileSystem: FileSystemConfig{
DirMode: 0755,
},
}
expected :=
`file-system:
dir-mode: "755"
`

str, err := util.Stringify(&c)
str, err := util.YAMLStringify(&c)

if assert.NoError(t, err) {
assert.Equal(t, "file-system:\n dir-mode: \"755\"\n", str)
assert.Equal(t, expected, str)
}
}
func TestOctalStringify(t *testing.T) {
func TestOctalMarshalling(t *testing.T) {
o := Octal(0765)

str, err := util.Stringify(&o)
str, err := util.YAMLStringify(&o)

if assert.NoError(t, err) {
assert.Equal(t, "\"765\"\n", str)
}
}

func TestOctalUnmarshalling(t *testing.T) {
t.Parallel()
tests := []struct {
str string
expected Octal
wantErr bool
}{
{
str: "753",
expected: 0753,
wantErr: false,
},
{
str: "644",
expected: 0644,
wantErr: false,
},
{
str: "945",
wantErr: true,
},
{
str: "abc",
wantErr: true,
},
}

for idx, tc := range tests {
tc := tc
t.Run(fmt.Sprintf("octal-unmarshalling: %d", idx), func(t *testing.T) {
t.Parallel()
var o Octal

err := (&o).UnmarshalText([]byte(tc.str))

if tc.wantErr {
assert.Error(t, err)
} else if assert.NoError(t, err) {
assert.Equal(t, tc.expected, o)
}
})
}
}

func TestLogSeverityUnmarshalling(t *testing.T) {
t.Parallel()
tests := []struct {
str string
expected LogSeverity
wantErr bool
}{
{
str: "TRACE",
expected: "TRACE",
wantErr: false,
},
{
str: "info",
expected: "INFO",
wantErr: false,
},
{
str: "debUG",
expected: "DEBUG",
wantErr: false,
},
{
str: "waRniNg",
expected: "WARNING",
wantErr: false,
},
{
str: "OFF",
expected: "OFF",
wantErr: false,
},
{
str: "ERROR",
expected: "ERROR",
wantErr: false,
},
{
str: "EMPEROR",
wantErr: true,
},
}

for idx, tc := range tests {
tc := tc
t.Run(fmt.Sprintf("log-severity-unmarshalling: %d", idx), func(t *testing.T) {
t.Parallel()
var l LogSeverity

err := (&l).UnmarshalText([]byte(tc.str))

if tc.wantErr {
assert.Error(t, err)
} else if assert.NoError(t, err) {
assert.Equal(t, tc.expected, l)
}
})
}
}

func TestProtocolUnmarshalling(t *testing.T) {
t.Parallel()
tests := []struct {
str string
expected Protocol
wantErr bool
}{
{
str: "http1",
expected: "http1",
wantErr: false,
},
{
str: "HTtp1",
expected: "http1",
wantErr: false,
},
{
str: "gRPC",
expected: "grpc",
wantErr: false,
},
{
str: "HTTP2",
expected: "http2",
wantErr: false,
},
{
str: "http100",
wantErr: true,
},
}

for idx, tc := range tests {
tc := tc
t.Run(fmt.Sprintf("protocol-unmarshalling: %d", idx), func(t *testing.T) {
t.Parallel()
var p Protocol

err := (&p).UnmarshalText([]byte(tc.str))

if tc.wantErr {
assert.Error(t, err)
} else if assert.NoError(t, err) {
assert.Equal(t, tc.expected, p)
}
})
}
}

func TestResolvedPathUnmarshalling(t *testing.T) {
t.Parallel()
h, err := os.UserHomeDir()
require.NoError(t, err)
tests := []struct {
str string
expected ResolvedPath
}{
{
str: "~/test.txt",
expected: ResolvedPath(path.Join(h, "test.txt")),
},
{
str: "/a/test.txt",
expected: "/a/test.txt",
},
}

for idx, tc := range tests {
tc := tc
t.Run(fmt.Sprintf("resolved-path-unmarshalling: %d", idx), func(t *testing.T) {
t.Parallel()
var p ResolvedPath

err := (&p).UnmarshalText([]byte(tc.str))

if assert.NoError(t, err) {
assert.Equal(t, tc.expected, p)
}
})
}
}

func TestConfigMarshalling(t *testing.T) {
t.Parallel()
c := Config{
FileSystem: FileSystemConfig{
FileMode: 0732,
},
GcsConnection: GcsConnectionConfig{
ClientProtocol: "grpc",
BillingProject: "abc",
},
GcsRetries: GcsRetriesConfig{
MaxRetryAttempts: 45,
},
}

actual, err := util.YAMLStringify(c)

expected :=
`file-system:
file-mode: "732"
gcs-connection:
billing-project: abc
client-protocol: grpc
gcs-retries:
max-retry-attempts: 45
`
if assert.NoError(t, err) {
assert.Equal(t, expected, actual)
}
}
6 changes: 3 additions & 3 deletions cmd/legacy_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ func getUserAgent(appName string, config string) string {
}

func logNewConfiguration(newConfig *cfg.Config) {
cfgStr, err := util.Stringify(newConfig)
cfgStr, err := util.YAMLStringify(newConfig)
if err != nil {
logger.Warnf("failed to stringify configuration: %v", err)
return
}
logger.Infof("GCSFuse mount config: %s", cfgStr)
}
func logLegacyConfiguration(flags *flagStorage, mountConfig *config.MountConfig) {
flagsStringified, err := util.Stringify(*flags)
flagsStringified, err := util.JSONStringify(*flags)

Check failure on line 101 in cmd/legacy_main.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: util.JSONStringify is deprecated: use YAMLStringify (staticcheck)

Check failure on line 101 in cmd/legacy_main.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: util.JSONStringify is deprecated: use YAMLStringify (staticcheck)
if err != nil {
logger.Warnf("failed to stringify cli flags: %v", err)
} else {
logger.Infof("GCSFuse mount command flags: %s", flagsStringified)
}

mountConfigStringified, err := util.Stringify(*mountConfig)
mountConfigStringified, err := util.JSONStringify(*mountConfig)

Check failure on line 108 in cmd/legacy_main.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: util.JSONStringify is deprecated: use YAMLStringify (staticcheck)

Check failure on line 108 in cmd/legacy_main.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: util.JSONStringify is deprecated: use YAMLStringify (staticcheck)
if err != nil {
logger.Warnf("failed to stringify config-file: %v", err)
} else {
Expand Down
20 changes: 17 additions & 3 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"path/filepath"
"strings"
"time"

"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -75,12 +77,24 @@ func GetResolvedPath(filePath string) (resolvedPath string, err error) {
}
}

// Stringify marshals an object (only exported attribute) to a JSON string. If marshalling fails, it returns an empty string.
func Stringify(input any) (string, error) {
// JSONStringify marshals an object (only exported attribute) to a JSON string. If marshalling fails, it returns an empty string.
//
// Deprecated: use YAMLStringify
func JSONStringify(input any) (string, error) {
inputBytes, err := json.Marshal(input)

if err != nil {
return "", fmt.Errorf("error in Stringify %w", err)
return "", fmt.Errorf("error in JSONStringify %w", err)
}
return string(inputBytes), nil
}

// YAMLStringify marshals an object to a YAML string. If marshalling fails, it returns an error.
func YAMLStringify(input any) (string, error) {
inputBytes, err := yaml.Marshal(input)

if err != nil {
return "", fmt.Errorf("error in YAMLStringify %w", err)
}
return string(inputBytes), nil
}
Expand Down
41 changes: 39 additions & 2 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,43 @@ func (ts *UtilTest) ResolveWhenParentProcDirEnvSetAndAbsoluteFilePath() {
assert.Equal(ts.T(), "/var/dir/test.txt", resolvedPath)
}

func TestYAMLStringify(t *testing.T) {
tests := []struct {
name string
obj any
expected string
}{
{
name: "Test Map",
obj: map[string]int{
"1": 1,
"2": 2,
"3": 3,
},
expected: "\"1\": 1\n\"2\": 2\n\"3\": 3\n",
},
{
name: "Test Nested Map",
obj: map[string]any{
"1": 1,
"2": map[string]int{
"3": 1,
},
},
expected: "\"1\": 1\n\"2\":\n \"3\": 1\n",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
s, err := YAMLStringify(tc.obj)

if assert.NoError(t, err) {
assert.Equal(t, tc.expected, s)
}
})
}
}
func (ts *UtilTest) TestStringifyShouldReturnAllFieldsPassedInCustomObjectAsMarshalledString() {
sampleMap := map[string]int{
"1": 1,
Expand All @@ -163,7 +200,7 @@ func (ts *UtilTest) TestStringifyShouldReturnAllFieldsPassedInCustomObjectAsMars
NestedValue: sampleNestedValue,
}

actual, _ := Stringify(customObject)
actual, _ := JSONStringify(customObject)

expected := "{\"Value\":\"test_value\",\"NestedValue\":{\"SomeField\":10,\"SomeOther\":{\"1\":1,\"2\":2,\"3\":3}}}"
assert.Equal(ts.T(), expected, actual)
Expand All @@ -174,7 +211,7 @@ func (ts *UtilTest) TestStringifyShouldReturnEmptyStringWhenMarshalErrorsOut() {
value: "example",
}

actual, _ := Stringify(customInstance)
actual, _ := JSONStringify(customInstance)

expected := ""
assert.Equal(ts.T(), expected, actual)
Expand Down

0 comments on commit 5da619f

Please sign in to comment.