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) }