gocustomurls/conf_test.go

155 lines
4.0 KiB
Go

package main
import (
"fmt"
"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)
}
func TestLoadMainConfigFileFromProvidedLocation(t *testing.T) {
tmpDir := t.TempDir()
tempConfigDir := fmt.Sprintf("%s/tmp", tmpDir)
tempLogsDir := fmt.Sprintf("%s/tmp/logs", tmpDir)
mkDirForTest(t, tempConfigDir)
defaultConfigJson := map[string]any{
"rulesPath": fmt.Sprintf("%s/rules.json", tempConfigDir),
"logPath": fmt.Sprintf("%s/app.log", tempLogsDir),
"port": "9005",
"compress": false,
"sizeToRotate": "50MB",
}
tempConfigFile := fmt.Sprintf("%s/confg.json", tempConfigDir)
writeJsonForTest(t, defaultConfigJson, tempConfigFile)
cfg := &Config{}
assert.Equal(t, cfg.MappingFilePath, "")
conf, err := cfg.LoadMainConfigFile(tempConfigFile)
assert.Equal(t, err, nil)
assert.NotEmpty(t, conf)
assert.NotEmpty(t, cfg.MappingFilePath)
}