gocustomurls/conf_test.go

203 lines
5.2 KiB
Go
Raw Normal View History

2024-06-23 13:43:33 +00:00
package main
import (
"fmt"
2024-06-23 16:33:34 +00:00
"os"
"strconv"
2024-06-23 13:43:33 +00:00
"testing"
"github.com/stretchr/testify/assert"
)
func TestLoadMappingFile(t *testing.T) {
t.Run("load the mapping file correctly - initial load", func(t *testing.T) {
tmpDir := t.TempDir()
mkDirForTest(t, fmt.Sprintf("%s/tmp", tmpDir))
rulesJsonFp := "testData/rules.json"
expected := fmt.Sprintf("%s/tmp/rules.json", tmpDir)
cpFileForTest(t, rulesJsonFp, expected)
cfg := &Config{}
assert.Equal(t, cfg.MappingFilePath, "")
assert.Empty(t, cfg.MappingRules)
err := cfg.LoadMappingFile(expected)
assert.Equal(t, err, nil)
assert.Equal(t, cfg.MappingFilePath, expected)
assert.NotEmpty(t, cfg.MappingRules)
})
t.Run("load the mapping file correctly - reload", func(t *testing.T) {
tmpDir := t.TempDir()
mkDirForTest(t, fmt.Sprintf("%s/tmp", tmpDir))
rulesJsonFp := "testData/rules.json"
expected := fmt.Sprintf("%s/tmp/rules.json", tmpDir)
cpFileForTest(t, rulesJsonFp, expected)
cfg := &Config{}
assert.Equal(t, cfg.MappingFilePath, "")
assert.Empty(t, cfg.MappingRules)
err := cfg.LoadMappingFile(expected)
assert.Equal(t, err, nil)
assert.Equal(t, cfg.MappingFilePath, expected)
assert.NotEmpty(t, cfg.MappingRules)
oldRules := cfg.MappingRules
rulesJsonFp = "testData/rules2.json"
cpFileForTest(t, rulesJsonFp, expected)
err = cfg.LoadMappingFile(expected)
assert.Equal(t, err, nil)
assert.Equal(t, cfg.MappingFilePath, expected)
assert.NotEmpty(t, cfg.MappingRules)
newRules := cfg.MappingRules
assert.NotEqualValues(t, oldRules, newRules)
})
}
func TestGetDefaults(t *testing.T) {
actual, err := getDefaults()
assert.Equal(t, err, nil)
assert.Contains(t, actual, "rulesFp")
assert.Contains(t, actual, "logFp")
assert.Contains(t, actual, "confFp")
}
func TestSizeIsConfigured(t *testing.T) {
tests := map[string]struct {
input string
want bool
}{
"wrong input - K": {input: "5677.45K", want: false},
"wrong input - KiB": {input: "5677.45KiB", want: false},
"wrong input - M": {input: "9.45M", want: false},
"wrong input - G": {input: "9.45G", want: false},
"correct input - KB": {input: "5.45KB", want: true},
"correct input - MB": {input: "5677.45MB", want: true},
"correct input - GB": {input: "9.45GB", want: true},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got, _ := checkIfSizeIsConfigured(tc.input)
assert.Equal(t, got, tc.want)
})
}
}
func TestGenerateConfigFile(t *testing.T) {
tmpDir := t.TempDir()
tempLogsDir := fmt.Sprintf("%s/tmp/logs", tmpDir)
tempConfigDir := fmt.Sprintf("%s/tmp/config", tmpDir)
mkDirForTest(t, tempLogsDir)
mkDirForTest(t, tempConfigDir)
defaultConfig := map[string]string{
"rulesFp": fmt.Sprintf("%s/rules.json", tempConfigDir),
"confFp": fmt.Sprintf("%s/config.json", tempConfigDir),
"logFp": fmt.Sprintf("%s/app.log", tempLogsDir),
}
ok := IsDirEmpty(t, tempConfigDir)
assert.Equal(t, ok, true)
ok = IsDirEmpty(t, tempLogsDir)
assert.Equal(t, ok, true)
expected, err := generateDefaultConfigFile(defaultConfig)
assert.Equal(t, err, nil)
assert.NotEmpty(t, expected)
ok = IsDirEmpty(t, tempConfigDir)
assert.Equal(t, ok, false)
ok = IsDirEmpty(t, tempLogsDir)
assert.Equal(t, ok, true)
}
2024-06-23 16:33:34 +00:00
func TestLoadMainConfigFile(t *testing.T) {
t.Run("load the main config file correctly - default load", func(t *testing.T) {
tmpDir := t.TempDir()
tempConfigDir := fmt.Sprintf("%s/tmp/config", tmpDir)
mkDirForTest(t, tempConfigDir)
hmDir, _ := os.UserConfigDir()
oldConfigFile := fmt.Sprintf("%s/gocustomurls/config.json", hmDir)
t.Log("oldConfigFile: ", oldConfigFile)
newConfigFile := fmt.Sprintf("%s/config.json", tempConfigDir)
ok := doesFileExist(oldConfigFile)
if ok {
t.Log("cp old -> new")
cpFileForTest(t, oldConfigFile, newConfigFile)
}
t.Cleanup(func() {
found := doesFileExist(newConfigFile)
if found {
t.Log("cp new -> old")
cpFileForTest(t, newConfigFile, oldConfigFile)
} else {
// Add a loop to wait until you find the old ConfigFile
ok := doesFileExist(oldConfigFile)
if ok {
t.Log("remove old")
removeFileForTest(t, oldConfigFile)
}
}
})
cfg := &Config{}
assert.Equal(t, cfg.MappingFilePath, "")
conf, err := cfg.LoadMainConfigFile("")
// time.Sleep(4 * time.Second)
assert.Equal(t, err, nil)
assert.NotEmpty(t, conf)
assert.NotEmpty(t, cfg.MappingFilePath)
})
t.Run("load the main config file correctly - load from a location", func(t *testing.T) {
t.SkipNow()
tmpDir := t.TempDir()
tempConfigDir := fmt.Sprintf("%s/tmp", tmpDir)
tempLogsDir := fmt.Sprintf("%s/tmp/logs", tmpDir)
mkDirForTest(t, tempConfigDir)
defaultConfigJson := map[string]string{
"rulesFp": fmt.Sprintf("%s/rules.json", tempConfigDir),
"logFp": fmt.Sprintf("%s/app.log", tempLogsDir),
"Port": "9005",
"Compression": strconv.FormatBool(false),
"SizeToRotate": "50MB",
}
writeJsonForTest(t, defaultConfigJson, fmt.Sprintf("%s/confg.json", tempConfigDir))
cfg := &Config{}
assert.Equal(t, cfg.MappingFilePath, "")
conf, err := cfg.LoadMainConfigFile(fmt.Sprintf("%s/confg.json", tempConfigDir))
assert.Equal(t, err, nil)
assert.NotEmpty(t, conf)
assert.NotEmpty(t, cfg.MappingFilePath)
})
}