Skip to content

Commit

Permalink
Add test for log input and more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
belimawr committed Oct 18, 2024
1 parent e2f5031 commit 8405aed
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
3 changes: 2 additions & 1 deletion filebeat/input/systemlogs/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ func useJournald(c *conf.C) (bool, error) {
"using log input because file(s) was(were) found when testing glob '%s'",
g)
return false, nil

}
}

// if no system log files are found, then use jounrald
logger.Info("no files were found, using journald input")

return true, nil
}

Expand Down
62 changes: 49 additions & 13 deletions filebeat/tests/integration/systemlogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
_ "embed"
"fmt"
"os"
"path"
"path/filepath"
"testing"
"time"
Expand All @@ -36,35 +37,70 @@ var systemModuleCfg string
// TestSystemLogsCanUseJournald aims to ensure the system-logs input can
// correctly choose and start a journald input when the globs defined in
// var.paths do not resolve to any file.
func TestSystemLogsCanUseJournald(t *testing.T) {
func TestSystemLogsCanUseJournaldInput(t *testing.T) {
filebeat := integration.NewBeat(
t,
"filebeat",
"../../filebeat.test",
)
workDir := filebeat.TempDir()
copyModulesDir(t, workDir)

// As the name says, we want this folder to exist bu t be empty
emptyTempFolder := t.TempDir()
yamlCfg := fmt.Sprintf(systemModuleCfg, emptyTempFolder, filebeat.TempDir())
globWithoutFiles := filepath.Join(t.TempDir(), "*")
yamlCfg := fmt.Sprintf(systemModuleCfg, globWithoutFiles, workDir)

filebeat.WriteConfigFile(yamlCfg)
filebeat.Start()

filebeat.WaitForLogs(
"no files were found, using journald input",
10*time.Second,
"system-logs did not select journald input")
filebeat.WaitForLogs(
"journalctl started with PID",
10*time.Second,
"system-logs did not start journald input")
}

func TestSystemLogsCanUseLogInput(t *testing.T) {
filebeat := integration.NewBeat(
t,
"filebeat",
"../../filebeat.test",
)
workDir := filebeat.TempDir()
copyModulesDir(t, workDir)

logFilePath := path.Join(workDir, "syslog")
integration.GenerateLogFile(t, logFilePath, 5, false)
yamlCfg := fmt.Sprintf(systemModuleCfg, logFilePath, workDir)

filebeat.WriteConfigFile(yamlCfg)
filebeat.Start()

filebeat.WaitForLogs(
"using log input because file(s) was(were) found",
10*time.Second,
"system-logs did not select the log input")
filebeat.WaitForLogs(
"Harvester started for paths:",
10*time.Second,
"system-logs did not start the log input")
}

func copyModulesDir(t *testing.T, dst string) {
pwd, err := os.Getwd()
if err != nil {
t.Fatal("cannot get the current directory: %s", err)
t.Fatalf("cannot get the current directory: %s", err)
}
localModules := os.DirFS(filepath.Join(pwd, "../", "../", "module"))
localModulesD := os.DirFS(filepath.Join(pwd, "../", "../", "modules.d"))

if err := os.CopyFS(filepath.Join(filebeat.TempDir(), "module"), localModules); err != nil {
if err := os.CopyFS(filepath.Join(dst, "module"), localModules); err != nil {
t.Fatalf("cannot copy 'module' folder to test folder: %s", err)
}
if err := os.CopyFS(filepath.Join(filebeat.TempDir(), "modules.d"), localModulesD); err != nil {
if err := os.CopyFS(filepath.Join(dst, "modules.d"), localModulesD); err != nil {
t.Fatalf("cannot copy 'modules.d' folder to test folder: %s", err)
}

filebeat.WriteConfigFile(yamlCfg)
filebeat.Start()
filebeat.WaitForLogs(
"journalctl started with PID",
10*time.Second,
"system-logs did not start journald")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ filebeat.modules:
syslog:
enabled: true
var.paths:
- "%s/*"
- "%s"

path.home: %s

Expand Down

0 comments on commit 8405aed

Please sign in to comment.