Skip to content

Commit

Permalink
Adding log to print all configuration flags at start of gcs fuse mount (
Browse files Browse the repository at this point in the history
#1565)

* logs flags while mounting gcsfuse

* updates log message for mount flag

* fixes import lint issue

* refactors and renames toString method to support marshalling of nested struct

* refactor stringify to use json marshal

* adds method for error handling in stringify

* updates logger message for flags | remove unnecessary trim space from test

* changes stringify error log to warn log

* makes customType private

* removes extra line from TestStringifyShouldReturnEmptyStringWhenMarshalErrorsOut

* moves Stringify to util package

* adds doc for Stringify

* removes unnecessary TrimSpace

* updates Stringify docs to Go format
  • Loading branch information
ankitaluthra1 authored and ashmeenkaur committed Jan 9, 2024
1 parent 93d94ca commit ec11621
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package util

import (
"encoding/json"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -80,3 +81,14 @@ func ResolveConfigFilePaths(config *config.MountConfig) (err error) {
}
return
}

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

if err != nil {
logger.Warnf("Error in Stringify %v", err)
return ""
}
return string(inputBytes)
}
50 changes: 50 additions & 0 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package util

import (
"errors"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -157,3 +158,52 @@ func (t *UtilTest) TestResolveConfigFilePaths() {
AssertEq(nil, err)
ExpectEq(filepath.Join(homeDir, "test.txt"), mountConfig.LogConfig.FilePath)
}

func (t *UtilTest) TestStringifyShouldReturnAllFieldsPassedInCustomObjectAsMarshalledString() {
sampleMap := map[string]int{
"1": 1,
"2": 2,
"3": 3,
}
sampleNestedValue := nestedCustomType{
SomeField: 10,
SomeOther: sampleMap,
}
customObject := &customTypeForSuccess{
Value: "test_value",
NestedValue: sampleNestedValue,
}

actual := Stringify(customObject)

expected := "{\"Value\":\"test_value\",\"NestedValue\":{\"SomeField\":10,\"SomeOther\":{\"1\":1,\"2\":2,\"3\":3}}}"
AssertEq(expected, actual)
}

func (t *UtilTest) TestStringifyShouldReturnEmptyStringWhenMarshalErrorsOut() {
customInstance := customTypeForError{
value: "example",
}

actual := Stringify(customInstance)

expected := ""
AssertEq(expected, actual)
}

type customTypeForSuccess struct {
Value string
NestedValue nestedCustomType
}
type nestedCustomType struct {
SomeField int
SomeOther map[string]int
}
type customTypeForError struct {
value string
}

// MarshalJSON returns an error to simulate a failure during JSON marshaling
func (c customTypeForError) MarshalJSON() ([]byte, error) {
return nil, errors.New("intentional error during JSON marshaling")
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ func runCLIApp(c *cli.Context) (err error) {
}

logger.Infof("Start gcsfuse/%s for app %q using mount point: %s\n", getVersion(), flags.AppName, mountPoint)
logger.Infof("GCSFuse mount command flags: %s", util.Stringify(*flags))
logger.Infof("GCSFuse mount config flags: %s", util.Stringify(*mountConfig))

// If we haven't been asked to run in foreground mode, we should run a daemon
// with the foreground flag set and wait for it to mount.
Expand Down
43 changes: 43 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"strings"
"testing"

"github.com/googlecloudplatform/gcsfuse/internal/config"
"github.com/googlecloudplatform/gcsfuse/internal/util"

mountpkg "github.com/googlecloudplatform/gcsfuse/internal/mount"
. "github.com/jacobsa/ogletest"
)
Expand Down Expand Up @@ -72,3 +75,43 @@ func (t *MainTest) TestGetUserAgentWhenMetadataImageTypeEnvVarSetAndAppNameNotSe

ExpectEq(expectedUserAgent, userAgent)
}

func (t *MainTest) TestStringifyShouldReturnAllFlagsPassedInMountConfigAsMarshalledString() {
mountConfig := &config.MountConfig{
WriteConfig: config.WriteConfig{
CreateEmptyFile: false,
},
LogConfig: config.LogConfig{
Severity: config.TRACE,
FilePath: "\"path\"to\"file\"",
LogRotateConfig: config.LogRotateConfig{
MaxFileSizeMB: 2,
BackupFileCount: 2,
Compress: true,
},
},
}

actual := util.Stringify(mountConfig)

expected := "{\"CreateEmptyFile\":false,\"Severity\":\"TRACE\",\"Format\":\"\",\"FilePath\":\"\\\"path\\\"to\\\"file\\\"\",\"LogRotateConfig\":{\"MaxFileSizeMB\":2,\"BackupFileCount\":2,\"Compress\":true}}"
AssertEq(expected, actual)
}

func (t *MainTest) TestStringifyShouldReturnAllFlagsPassedInFlagStorageAsMarshalledString() {
mountOptions := map[string]string{
"1": "one",
"2": "two",
"3": "three",
}
flags := &flagStorage{
SequentialReadSizeMb: 10,
ClientProtocol: mountpkg.ClientProtocol("http4"),
MountOptions: mountOptions,
}

actual := util.Stringify(flags)

expected := "{\"AppName\":\"\",\"Foreground\":false,\"ConfigFile\":\"\",\"MountOptions\":{\"1\":\"one\",\"2\":\"two\",\"3\":\"three\"},\"DirMode\":0,\"FileMode\":0,\"Uid\":0,\"Gid\":0,\"ImplicitDirs\":false,\"OnlyDir\":\"\",\"RenameDirLimit\":0,\"CustomEndpoint\":null,\"BillingProject\":\"\",\"KeyFile\":\"\",\"TokenUrl\":\"\",\"ReuseTokenFromUrl\":false,\"EgressBandwidthLimitBytesPerSecond\":0,\"OpRateLimitHz\":0,\"SequentialReadSizeMb\":10,\"MaxRetrySleep\":0,\"StatCacheCapacity\":0,\"StatCacheTTL\":0,\"TypeCacheTTL\":0,\"HttpClientTimeout\":0,\"MaxRetryDuration\":0,\"RetryMultiplier\":0,\"LocalFileCache\":false,\"TempDir\":\"\",\"ClientProtocol\":\"http4\",\"MaxConnsPerHost\":0,\"MaxIdleConnsPerHost\":0,\"EnableNonexistentTypeCache\":false,\"StackdriverExportInterval\":0,\"OtelCollectorAddress\":\"\",\"LogFile\":\"\",\"LogFormat\":\"\",\"ExperimentalEnableJsonRead\":false,\"DebugFuseErrors\":false,\"DebugFuse\":false,\"DebugFS\":false,\"DebugGCS\":false,\"DebugHTTP\":false,\"DebugInvariants\":false,\"DebugMutex\":false}"
AssertEq(expected, actual)
}

0 comments on commit ec11621

Please sign in to comment.