From 3c07756cbaa9b1d2db98b5747759f977f5508420 Mon Sep 17 00:00:00 2001 From: bricerisingalgorand <60147418+bricerisingalgorand@users.noreply.github.com> Date: Wed, 20 Jan 2021 13:42:40 -0500 Subject: [PATCH] diagcfg : Read from global logging config if data dir not provided (#1844) When a datadir is not provided, this will have diagcfg read from the default logging config in the global config path. It will then generate a default config if that file does not exist --- cmd/diagcfg/telemetry.go | 4 ++-- logging/telemetry.go | 9 +++++---- logging/telemetry_test.go | 23 ++++++++++++++++++++++- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/cmd/diagcfg/telemetry.go b/cmd/diagcfg/telemetry.go index a54289e268..c7df1bc83c 100644 --- a/cmd/diagcfg/telemetry.go +++ b/cmd/diagcfg/telemetry.go @@ -55,7 +55,7 @@ func maybeUpdateDataDirFromEnv() { func readTelemetryConfigOrExit() logging.TelemetryConfig { maybeUpdateDataDirFromEnv() - cfg, err := logging.ReadTelemetryConfigOrDefault(&dataDir, "") + cfg, err := logging.ReadTelemetryConfigOrDefault(dataDir, "") if err != nil { fmt.Fprintf(os.Stderr, telemetryConfigReadError, err) os.Exit(1) @@ -112,7 +112,7 @@ var telemetryStatusCmd = &cobra.Command{ Long: `Print the node's telemetry status`, Run: func(cmd *cobra.Command, args []string) { maybeUpdateDataDirFromEnv() - cfg, err := logging.ReadTelemetryConfigOrDefault(&dataDir, "") + cfg, err := logging.ReadTelemetryConfigOrDefault(dataDir, "") // If error loading config, can't disable / no need to disable if err != nil { diff --git a/logging/telemetry.go b/logging/telemetry.go index 50be5a0ca8..7258b9ca61 100644 --- a/logging/telemetry.go +++ b/logging/telemetry.go @@ -90,13 +90,14 @@ func makeTelemetryState(cfg TelemetryConfig, hookFactory hookFactory) (*telemetr } // ReadTelemetryConfigOrDefault reads telemetry config from file or defaults if no config file found. -func ReadTelemetryConfigOrDefault(dataDir *string, genesisID string) (cfg TelemetryConfig, err error) { +func ReadTelemetryConfigOrDefault(dataDir string, genesisID string) (cfg TelemetryConfig, err error) { err = nil - if dataDir != nil && *dataDir != "" { - configPath := filepath.Join(*dataDir, TelemetryConfigFilename) + dataDirProvided := dataDir != "" + if dataDirProvided { + configPath := filepath.Join(dataDir, TelemetryConfigFilename) cfg, err = LoadTelemetryConfig(configPath) } - if err != nil && os.IsNotExist(err) { + if (err != nil && os.IsNotExist(err)) || !dataDirProvided { var configPath string configPath, err = config.GetConfigFilePath(TelemetryConfigFilename) if err != nil { diff --git a/logging/telemetry_test.go b/logging/telemetry_test.go index dbfa04c0fc..fce2e6d9e1 100644 --- a/logging/telemetry_test.go +++ b/logging/telemetry_test.go @@ -18,13 +18,16 @@ package logging import ( "fmt" + "os" "testing" "time" - "github.com/algorand/go-deadlock" "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" + "github.com/algorand/go-deadlock" + + "github.com/algorand/go-algorand/config" "github.com/algorand/go-algorand/logging/telemetryspec" ) @@ -315,3 +318,21 @@ func TestLogHistoryLevels(t *testing.T) { a.Nil(data[5]["log"]) // Panic - this is stack trace a.NotNil(data[6]["log"]) // Panic } + +func TestReadTelemetryConfigOrDefaultNoDataDir(t *testing.T) { + a := require.New(t) + tempDir := os.TempDir() + originalGlobalConfigFileRoot, _ := config.GetGlobalConfigFileRoot() + config.SetGlobalConfigFileRoot(tempDir) + + cfg, err := ReadTelemetryConfigOrDefault("", "") + defaultCfgSettings := createTelemetryConfig() + config.SetGlobalConfigFileRoot(originalGlobalConfigFileRoot) + + a.Nil(err) + a.NotNil(cfg) + a.NotEqual(TelemetryConfig{}, cfg) + a.Equal(defaultCfgSettings.UserName, cfg.UserName) + a.Equal(defaultCfgSettings.Password, cfg.Password) + a.Equal(len(defaultCfgSettings.GUID), len(cfg.GUID)) +}