Skip to content

Commit

Permalink
misc cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
swi-jared committed Jun 12, 2024
1 parent 250af6d commit a63d6da
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
3 changes: 2 additions & 1 deletion internal/oboe/file_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions internal/oboe/settings_lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -106,5 +105,5 @@ func newSettingLambdaFromFile() (*settingLambdaNormalized, error) {

settingLambda := settingLambdas[0]

return newSettingLambdaNormalized(&settingLambda), nil
return newSettingLambdaNormalized(settingLambda), nil
}
22 changes: 10 additions & 12 deletions internal/oboe/settings_lambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -195,5 +193,5 @@ func TestNewSettingLambdaFromFile(t *testing.T) {
result.args[constants.KvSignatureKey],
)

os.Remove(SettingsFile)
os.Remove(settingsFileName)
}

0 comments on commit a63d6da

Please sign in to comment.