From a63d6da45abbc137ffbfdf627c2fa1ef2bd99975 Mon Sep 17 00:00:00 2001 From: Jared Harper Date: Wed, 12 Jun 2024 11:52:34 -0700 Subject: [PATCH] misc cleanups --- internal/oboe/file_watcher.go | 3 ++- internal/oboe/settings_lambda.go | 9 ++++----- internal/oboe/settings_lambda_test.go | 22 ++++++++++------------ 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/internal/oboe/file_watcher.go b/internal/oboe/file_watcher.go index 0e4290ef..f7a8a05a 100644 --- a/internal/oboe/file_watcher.go +++ b/internal/oboe/file_watcher.go @@ -22,7 +22,8 @@ import ( ) const ( - settingsCheckDuration time.Duration = 10 * time.Second + settingsCheckDuration = 10 * time.Second + settingsFileName = "/tmp/solarwinds-apm-settings.json" ) var exit = make(chan bool, 1) diff --git a/internal/oboe/settings_lambda.go b/internal/oboe/settings_lambda.go index e873f6bb..365ffa58 100644 --- a/internal/oboe/settings_lambda.go +++ b/internal/oboe/settings_lambda.go @@ -86,8 +86,7 @@ func newSettingLambdaNormalized(fromFile *settingLambdaFromFile) *settingLambdaN // specific path in a specific format then returns values normalized for // oboe UpdateSetting, else returns error. func newSettingLambdaFromFile() (*settingLambdaNormalized, error) { - fn := "/tmp/solarwinds-apm-settings.json" - settingFile, err := os.Open(fn) + settingFile, err := os.Open(settingsFileName) if err != nil { return nil, err } @@ -96,8 +95,8 @@ func newSettingLambdaFromFile() (*settingLambdaNormalized, error) { return nil, err } // Settings file should be an array with a single settings object - var settingLambdas []settingLambdaFromFile - if err := json.Unmarshal(settingBytes, &settingLambdas); err != nil { + var settingLambdas []*settingLambdaFromFile + if err = json.Unmarshal(settingBytes, &settingLambdas); err != nil { return nil, err } if len(settingLambdas) != 1 { @@ -106,5 +105,5 @@ func newSettingLambdaFromFile() (*settingLambdaNormalized, error) { settingLambda := settingLambdas[0] - return newSettingLambdaNormalized(&settingLambda), nil + return newSettingLambdaNormalized(settingLambda), nil } diff --git a/internal/oboe/settings_lambda_test.go b/internal/oboe/settings_lambda_test.go index a6f763d5..cf41686a 100644 --- a/internal/oboe/settings_lambda_test.go +++ b/internal/oboe/settings_lambda_test.go @@ -23,8 +23,6 @@ import ( "github.com/stretchr/testify/require" ) -const SettingsFile = "/tmp/solarwinds-apm-settings.json" - func TestNewSettingLambdaNormalized(t *testing.T) { settingArgs := settingArguments{ 1, @@ -103,41 +101,41 @@ func TestNewSettingLambdaNormalized(t *testing.T) { } func TestNewSettingLambdaFromFileErrorOpen(t *testing.T) { - require.NoFileExists(t, SettingsFile) + require.NoFileExists(t, settingsFileName) res, err := newSettingLambdaFromFile() assert.Nil(t, res) assert.Error(t, err) } func TestNewSettingLambdaFromFileErrorUnmarshal(t *testing.T) { - require.NoFileExists(t, SettingsFile) + require.NoFileExists(t, settingsFileName) content := []byte("hello\ngo\n") - require.NoError(t, os.WriteFile(SettingsFile, content, 0644)) + require.NoError(t, os.WriteFile(settingsFileName, content, 0644)) res, err := newSettingLambdaFromFile() assert.Nil(t, res) assert.Error(t, err) - os.Remove(SettingsFile) + os.Remove(settingsFileName) } func TestNewSettingLambdaFromFileErrorLen(t *testing.T) { - require.NoFileExists(t, SettingsFile) + require.NoFileExists(t, settingsFileName) content := []byte("[]") - require.NoError(t, os.WriteFile(SettingsFile, content, 0644)) + require.NoError(t, os.WriteFile(settingsFileName, content, 0644)) res, err := newSettingLambdaFromFile() assert.Nil(t, res) assert.Error(t, err) - os.Remove(SettingsFile) + os.Remove(settingsFileName) } func TestNewSettingLambdaFromFile(t *testing.T) { - require.NoFileExists(t, SettingsFile) + require.NoFileExists(t, settingsFileName) content := []byte("[{\"arguments\":{\"BucketCapacity\":1,\"BucketRate\":1,\"MetricsFlushInterval\":1,\"TriggerRelaxedBucketCapacity\":1,\"TriggerRelaxedBucketRate\":1,\"TriggerStrictBucketCapacity\":1,\"TriggerStrictBucketRate\":1},\"flags\":\"SAMPLE_START,SAMPLE_THROUGH_ALWAYS,SAMPLE_BUCKET_ENABLED,TRIGGER_TRACE\",\"layer\":\"\",\"timestamp\":1715900164,\"ttl\":120,\"type\":0,\"value\":1000000}]") - require.NoError(t, os.WriteFile(SettingsFile, content, 0644)) + require.NoError(t, os.WriteFile(settingsFileName, content, 0644)) result, err := newSettingLambdaFromFile() assert.Nil(t, err) assert.Equal(t, TypeDefault, result.sType) @@ -195,5 +193,5 @@ func TestNewSettingLambdaFromFile(t *testing.T) { result.args[constants.KvSignatureKey], ) - os.Remove(SettingsFile) + os.Remove(settingsFileName) }