From 45e0fdca04b264ca9f12d2ed93ca119e44034ae2 Mon Sep 17 00:00:00 2001 From: kritikapradhan <70977265+kritikapradhan@users.noreply.github.com> Date: Wed, 18 Nov 2020 18:37:30 -0500 Subject: [PATCH] Helm values picked by Sloop (#133) * fixed helm issuues being picked up by file * changed formatting of code * added unit tests * added unit tests * changes to test files * added panic tests * changes tests, moved test files * removed returning error * removed an extra line to change email * updated data files to not reflect default values --- pkg/sloop/server/internal/config/config.go | 31 ++++++++----- .../server/internal/config/config_test.go | 42 ++++++++++++++++++ .../server/internal/testFiles/testConfig.txt | 44 +++++++++++++++++++ .../server/internal/testFiles/testconfig.json | 44 +++++++++++++++++++ .../server/internal/testFiles/testconfig.yaml | 43 ++++++++++++++++++ 5 files changed, 194 insertions(+), 10 deletions(-) create mode 100644 pkg/sloop/server/internal/config/config_test.go create mode 100644 pkg/sloop/server/internal/testFiles/testConfig.txt create mode 100644 pkg/sloop/server/internal/testFiles/testconfig.json create mode 100644 pkg/sloop/server/internal/testFiles/testconfig.yaml diff --git a/pkg/sloop/server/internal/config/config.go b/pkg/sloop/server/internal/config/config.go index 93086ae..43f55c8 100644 --- a/pkg/sloop/server/internal/config/config.go +++ b/pkg/sloop/server/internal/config/config.go @@ -8,15 +8,16 @@ package config import ( + "encoding/json" "flag" "fmt" - "io/ioutil" - "os" - "time" - "github.com/ghodss/yaml" "github.com/golang/glog" "github.com/pkg/errors" + "io/ioutil" + "os" + "strings" + "time" "github.com/salesforce/sloop/pkg/sloop/webserver" ) @@ -136,10 +137,10 @@ func Init() *SloopConfig { } if configFilename != "" { newConfig = loadFromFile(configFilename) + } else { + registerFlags(flag.CommandLine, newConfig) + flag.Parse() } - - registerFlags(flag.CommandLine, newConfig) - flag.Parse() // Set this to the correct value in case we got it from envVar newConfig.ConfigFile = configFilename return newConfig @@ -172,16 +173,26 @@ func (c *SloopConfig) Validate() error { } func loadFromFile(filename string) *SloopConfig { - yamlFile, err := ioutil.ReadFile(filename) + var config SloopConfig + + configFile, err := ioutil.ReadFile(filename) if err != nil { panic(fmt.Sprintf("failed to read %v. %v", filename, err)) } - var config SloopConfig - err = yaml.Unmarshal(yamlFile, &config) + + if strings.Contains(filename, ".yaml") { + err = yaml.Unmarshal(configFile, &config) + } else if strings.Contains(filename, ".json") { + err = json.Unmarshal(configFile, &config) + } else { + panic(fmt.Sprintf("incorrect file format %v. Use json or yaml file type. ", filename)) + } + if err != nil { panic(fmt.Sprintf("failed to unmarshal %v. %v", filename, err)) } return &config + } // Pre-parse flags and return config filename without side-effects diff --git a/pkg/sloop/server/internal/config/config_test.go b/pkg/sloop/server/internal/config/config_test.go new file mode 100644 index 0000000..049199d --- /dev/null +++ b/pkg/sloop/server/internal/config/config_test.go @@ -0,0 +1,42 @@ +package config + +import ( + "encoding/json" + "github.com/ghodss/yaml" + "github.com/stretchr/testify/assert" + "io/ioutil" + "path/filepath" + "testing" +) + +func Test_loadFromJSONFile_Success(t *testing.T) { + var expectedconfig SloopConfig + configfilename, _ := filepath.Abs("../testFiles/testconfig.json") + configFile, err := ioutil.ReadFile(configfilename) + err = json.Unmarshal(configFile, &expectedconfig) + assert.Nil(t, err) + + out_config := loadFromFile(configfilename) + assert.Equal(t, out_config, &expectedconfig) +} + +func Test_loadFromYAMLFile_Success(t *testing.T) { + var expectedconfig SloopConfig + configfilename, _ := filepath.Abs("../testFiles/testconfig.yaml") + configFile, err := ioutil.ReadFile(configfilename) + err = yaml.Unmarshal(configFile, &expectedconfig) + assert.Nil(t, err) + + out_config := loadFromFile(configfilename) + assert.Equal(t, out_config, &expectedconfig) +} + +func Test_loadFromTxtFile_shouldPanic(t *testing.T) { + configfilename, _ := filepath.Abs("../testFiles/testconfig.txt") + assert.Panics(t, func() { loadFromFile(configfilename) }, "The code did not panic") +} + +func Test_loadFromNoFile_shouldPanic(t *testing.T) { + configfilename, _ := filepath.Abs("../testconfig.json") + assert.Panics(t, func() { loadFromFile(configfilename) }, "The code did not panic") +} diff --git a/pkg/sloop/server/internal/testFiles/testConfig.txt b/pkg/sloop/server/internal/testFiles/testConfig.txt new file mode 100644 index 0000000..e9c7d80 --- /dev/null +++ b/pkg/sloop/server/internal/testFiles/testConfig.txt @@ -0,0 +1,44 @@ +{ + "ConfigFile": "", + "leftBarLinks": null, + "resourceLinks": null, + "disableKubeWatch": false, + "kubeWatchResyncInterval": 1800000000000, + "webfilesPath": "./pkg/sloop/webfiles", + "bindAddress": "", + "port": 8080, + "storeRoot": "./data", + "maxLookBack": 1209600000000000, + "maxDiskMb": 32768, + "debugPlaybackFile": "", + "debugRecordFile": "", + "deletionBatchSize": 1000, + "mockBadger": false, + "disableStoreManager": false, + "cleanupFrequency": 1800000000000, + "keepMinorNodeUpdates": false, + "defaultNamespace": "default", + "defaultKind": "Pod", + "defaultLookback": "6h", + "context": "", + "displayContext": "", + "apiServerHost": "", + "watchCrds": true, + "threshold for GC": 0.8, + "restoreDatabaseFile": "", + "badgerDiscardRatio": 0.99, + "badgerVLogGCFreq": 60000000000, + "badgerMaxTableSize": 0, + "badgerLevelOneSize": 0, + "badgerLevSizeMultiplier": 0, + "badgerKeepL0InMemory": true, + "badgerVLogFileSize": 0, + "badgerVLogMaxEntries": 200000, + "badgerUseLSMOnlyOptions": true, + "badgerEnableEventLogging": false, + "badgerNumOfCompactors": 0, + "badgerSyncWrites": true, + "badgerVLogFileIOMapping": false, + "badgerVLogTruncate": true, + "enableDeleteKeys": false +} \ No newline at end of file diff --git a/pkg/sloop/server/internal/testFiles/testconfig.json b/pkg/sloop/server/internal/testFiles/testconfig.json new file mode 100644 index 0000000..30b9193 --- /dev/null +++ b/pkg/sloop/server/internal/testFiles/testconfig.json @@ -0,0 +1,44 @@ +{ + "ConfigFile": "", + "leftBarLinks": null, + "resourceLinks": null, + "disableKubeWatch": true, + "kubeWatchResyncInterval": 1800000000000, + "webfilesPath": "./pkg/sloop/webfiles", + "bindAddress": "", + "port": 8080, + "storeRoot": "./data", + "maxLookBack": 1209600000000000, + "maxDiskMb": 32768, + "debugPlaybackFile": "", + "debugRecordFile": "", + "deletionBatchSize": 1000, + "mockBadger": false, + "disableStoreManager": true, + "cleanupFrequency": 1800000000000, + "keepMinorNodeUpdates": true, + "defaultNamespace": "_all", + "defaultKind": "Pod", + "defaultLookback": "12h", + "context": "", + "displayContext": "", + "apiServerHost": "", + "watchCrds": true, + "threshold for GC": 0.8, + "restoreDatabaseFile": "", + "badgerDiscardRatio": 0.92, + "badgerVLogGCFreq": 60000000000, + "badgerMaxTableSize": 0, + "badgerLevelOneSize": 0, + "badgerLevSizeMultiplier": 0, + "badgerKeepL0InMemory": true, + "badgerVLogFileSize": 0, + "badgerVLogMaxEntries": 200000, + "badgerUseLSMOnlyOptions": true, + "badgerEnableEventLogging": true, + "badgerNumOfCompactors": 0, + "badgerSyncWrites": true, + "badgerVLogFileIOMapping": false, + "badgerVLogTruncate": true, + "enableDeleteKeys": false +} \ No newline at end of file diff --git a/pkg/sloop/server/internal/testFiles/testconfig.yaml b/pkg/sloop/server/internal/testFiles/testconfig.yaml new file mode 100644 index 0000000..21f5221 --- /dev/null +++ b/pkg/sloop/server/internal/testFiles/testconfig.yaml @@ -0,0 +1,43 @@ +--- +ConfigFile: '' +leftBarLinks: +resourceLinks: +disableKubeWatch: false +kubeWatchResyncInterval: 1800000000000 +webfilesPath: "./pkg/sloop/webfiles" +bindAddress: '' +port: 8080 +storeRoot: "./data" +maxLookBack: 1109600000000000 +maxDiskMb: 32768 +debugPlaybackFile: '' +debugRecordFile: '' +deletionBatchSize: 1000 +mockBadger: false +disableStoreManager: false +cleanupFrequency: 1800000000000 +keepMinorNodeUpdates: false +defaultNamespace: default +defaultKind: Pod +defaultLookback: 3h +context: '' +displayContext: '' +apiServerHost: '' +watchCrds: true +threshold for GC: 0.75 +restoreDatabaseFile: '' +badgerDiscardRatio: 0.80 +badgerVLogGCFreq: 60000000000 +badgerMaxTableSize: 0 +badgerLevelOneSize: 0 +badgerLevSizeMultiplier: 0 +badgerKeepL0InMemory: true +badgerVLogFileSize: 0 +badgerVLogMaxEntries: 150000 +badgerUseLSMOnlyOptions: true +badgerEnableEventLogging: false +badgerNumOfCompactors: 0 +badgerSyncWrites: true +badgerVLogFileIOMapping: true +badgerVLogTruncate: true +enableDeleteKeys: false