Skip to content

Commit

Permalink
#5 more tests for multiple logfile tailer
Browse files Browse the repository at this point in the history
  • Loading branch information
fstab committed May 26, 2019
1 parent b6b5e1a commit a1cf7bd
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions tailer/fswatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ const tests = `
- [logrotate, logdir/logfile.log, logdir/logfile.log.1]
- [log, line 3, logdir/logfile.log]
- [expect, line 3, logdir/logfile.log]
- name: overwrite
param_filters:
logrotateMvCfg: [mv]
commands:
- [mkdir, logdir]
- [log, file 1 line 1, logdir/logfile1.log]
- [log, file 2 line 1, logdir/logfile2.log]
- [start file tailer, readall=true, fail_on_missing_logfile=false, logdir/*]
- [expect, file 1 line 1, logdir/logfile1.log]
- [expect, file 2 line 1, logdir/logfile2.log]
- [logrotate, logdir/logfile1.log, logdir/logfile2.log]
- [log, file 2 line 2, logdir/logfile2.log]
- [log, file 1 line 2, logdir/logfile1.log]
- [expect, file 1 line 2, logdir/logfile1.log]
- [expect, file 2 line 2, logdir/logfile2.log]
`

// // The following test fails on Windows in tearDown() when removing logdir.
Expand All @@ -146,13 +162,14 @@ const tests = `
// `

type testConfigType struct {
Name string
Commands [][]string
Name string
ParamFilters map[string][]string `yaml:"param_filters"`
Commands [][]string
}

func TestAll(t *testing.T) {
testConfigs := make([]testConfigType, 0)
err := yaml.Unmarshal([]byte(tests), &testConfigs)
err := yaml.UnmarshalStrict([]byte(tests), &testConfigs)
if err != nil {
t.Fatal(err)
}
Expand All @@ -162,15 +179,19 @@ func TestAll(t *testing.T) {
// All logratate configs except for copy and copytruncate can be combined with logratateMoveConfig.
for _, logrotateCfg := range []logrotateConfig{_create, _nocreate, _create_from_temp} {
for _, logrotateMvCfg := range []logrotateMoveConfig{mv, cp, rm} {
runTest(t, testConfig.Name, loggerCfg, tailerOpt, logrotateCfg, logrotateMvCfg, testConfig.Commands)
if !skip(testConfig, loggerCfg, logrotateCfg, logrotateMvCfg) {
runTest(t, testConfig.Name, loggerCfg, tailerOpt, logrotateCfg, logrotateMvCfg, testConfig.Commands)
}
}
}
for _, loggerCfg := range []loggerConfig{keepOpen, closeFileAfterEachLine} {
// When the logger keeps the file open, only the logrotate options 'copy' and 'copytruncate' make sense.
for _, logrotateCfg := range []logrotateConfig{_copy, _copytruncate} {
for _, logrotateMvCfg := range []logrotateMoveConfig{none} {
// The logroatate configs copytruncate and copy require logroatateMoveConfig == none.
runTest(t, testConfig.Name, loggerCfg, tailerOpt, logrotateCfg, logrotateMvCfg, testConfig.Commands)
if !skip(testConfig, loggerCfg, logrotateCfg, logrotateMvCfg) {
runTest(t, testConfig.Name, loggerCfg, tailerOpt, logrotateCfg, logrotateMvCfg, testConfig.Commands)
}
}
}
}
Expand Down Expand Up @@ -225,6 +246,28 @@ func TestFileMissingOnStartup(t *testing.T) {
runTest(t, "fail on missing startup", closeFileAfterEachLine, fseventTailer, _nocreate, mv, test)
}

func skip(config testConfigType, loggerCfg loggerConfig, logrotateCfg logrotateConfig, logrotateMvCfg logrotateMoveConfig) bool {
if len(config.ParamFilters["loggerCfg"]) > 0 && !containsAsString(loggerCfg, config.ParamFilters["loggerCfg"]) {
return true
}
if len(config.ParamFilters["logrotateCfg"]) > 0 && !containsAsString(logrotateCfg, config.ParamFilters["logrotateCfg"]) {
return true
}
if len(config.ParamFilters["logrotateMvCfg"]) > 0 && !containsAsString(logrotateMvCfg, config.ParamFilters["logrotateMvCfg"]) {
return true
}
return false
}

func containsAsString(item fmt.Stringer, slice []string) bool {
for _, entry := range slice {
if item.String() == entry {
return true
}
}
return false
}

func runTest(t *testing.T, testName string, loggerCfg loggerConfig, tailerCfg fileTailerConfig, logrotateCfg logrotateConfig, logrotateMvCfg logrotateMoveConfig, cmds [][]string) {
params := []fmt.Stringer{loggerCfg, tailerCfg, logrotateCfg, logrotateMvCfg}
join(params, ",")
Expand Down

0 comments on commit a1cf7bd

Please sign in to comment.