-
Notifications
You must be signed in to change notification settings - Fork 5k
system-logs input ignores folders and add tests #41296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
belimawr
merged 6 commits into
elastic:main
from
belimawr:41259-add-tests-for-system-logs
Oct 22, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b8fbb9d
Implement tests for system-logs input
belimawr ab90dc1
Ignore directories when searching for files
belimawr d751183
Add integration tests for system-logs input
belimawr 2997727
Improve comments and build tags
belimawr edabd1c
Add test for log input and more logging
belimawr 511d658
Use `github.com/otiai10/copy` instead of `os.CopyFS`
belimawr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||
| // the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //go:build linux | ||
|
|
||
| package systemlogs | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| conf "github.com/elastic/elastic-agent-libs/config" | ||
| ) | ||
|
|
||
| func TestJournaldInputIsCreated(t *testing.T) { | ||
| c := map[string]any{ | ||
| "files.paths": []string{"/file/does/not/exist"}, | ||
| // The 'journald' object needs to exist for the input to be instantiated | ||
| "journald.enabled": true, | ||
| } | ||
|
|
||
| cfg := conf.MustNewConfigFrom(c) | ||
|
|
||
| _, inp, err := configure(cfg) | ||
| if err != nil { | ||
| t.Fatalf("did not expect an error calling newV1Input: %s", err) | ||
| } | ||
|
|
||
| type namer interface { | ||
| Name() string | ||
| } | ||
|
|
||
| i, isNamer := inp.(namer) | ||
| if !isNamer { | ||
| t.Fatalf("expecting an instance of *log.Input, got '%T' instead", inp) | ||
| } | ||
|
|
||
| if got, expected := i.Name(), "journald"; got != expected { | ||
| t.Fatalf("expecting '%s' input, got '%s'", expected, got) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||
| // the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package systemlogs | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/elastic/beats/v7/filebeat/channel" | ||
| "github.com/elastic/beats/v7/filebeat/input" | ||
| "github.com/elastic/beats/v7/filebeat/input/log" | ||
| "github.com/elastic/beats/v7/libbeat/beat" | ||
| conf "github.com/elastic/elastic-agent-libs/config" | ||
| ) | ||
|
|
||
| func generateFile(t *testing.T) string { | ||
| // Create a know file for testing, the content is not relevant | ||
| // it just needs to exist | ||
| knwonFile, err := os.CreateTemp(t.TempDir(), t.Name()+"knwonFile*") | ||
| if err != nil { | ||
| t.Fatalf("cannot create temporary file: %s", err) | ||
| } | ||
|
|
||
| if _, err := knwonFile.WriteString("Bowties are cool"); err != nil { | ||
| t.Fatalf("cannot write to temporary file '%s': %s", knwonFile.Name(), err) | ||
| } | ||
| knwonFile.Close() | ||
|
|
||
| return knwonFile.Name() | ||
| } | ||
|
|
||
| func TestUseJournald(t *testing.T) { | ||
| filename := generateFile(t) | ||
|
|
||
| testCases := map[string]struct { | ||
| cfg map[string]any | ||
| useJournald bool | ||
| expectErr bool | ||
| }{ | ||
| "No files found": { | ||
| cfg: map[string]any{ | ||
| "files.paths": []string{"/file/does/not/exist"}, | ||
| }, | ||
| useJournald: true, | ||
| }, | ||
| "File exists": { | ||
| cfg: map[string]any{ | ||
| "files.paths": []string{filename}, | ||
| }, | ||
| useJournald: false, | ||
| }, | ||
| "use_journald is true": { | ||
| cfg: map[string]any{ | ||
| "use_journald": true, | ||
| "journald": struct{}{}, | ||
| }, | ||
| useJournald: true, | ||
| }, | ||
| "use_files is true": { | ||
| cfg: map[string]any{ | ||
| "use_files": true, | ||
| "journald": nil, | ||
| "files": struct{}{}, | ||
| }, | ||
| useJournald: false, | ||
| }, | ||
| "use_journald and use_files are true": { | ||
| cfg: map[string]any{ | ||
| "use_files": true, | ||
| "use_journald": true, | ||
| "journald": struct{}{}, | ||
| }, | ||
| useJournald: false, | ||
| expectErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range testCases { | ||
| t.Run(name, func(t *testing.T) { | ||
| cfg := conf.MustNewConfigFrom(tc.cfg) | ||
|
|
||
| useJournald, err := useJournald(cfg) | ||
| if !tc.expectErr && err != nil { | ||
| t.Fatalf("did not expect an error calling 'useJournald': %s", err) | ||
| } | ||
| if tc.expectErr && err == nil { | ||
| t.Fatal("expecting an error when calling 'userJournald', got none") | ||
| } | ||
|
|
||
| if useJournald != tc.useJournald { | ||
| t.Fatalf("expecting 'useJournald' to be %t, got %t", | ||
| tc.useJournald, useJournald) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestLogInputIsInstantiated(t *testing.T) { | ||
| filename := generateFile(t) | ||
| c := map[string]any{ | ||
| "files.paths": []string{filename}, | ||
| } | ||
|
|
||
| cfg := conf.MustNewConfigFrom(c) | ||
|
|
||
| inp, err := newV1Input(cfg, connectorMock{}, input.Context{}) | ||
| if err != nil { | ||
| t.Fatalf("did not expect an error calling newV1Input: %s", err) | ||
| } | ||
| _, isLogInput := inp.(*log.Input) | ||
| if !isLogInput { | ||
| t.Fatalf("expecting an instance of *log.Input, got '%T' instead", inp) | ||
| } | ||
| } | ||
|
|
||
| type connectorMock struct{} | ||
|
|
||
| func (mock connectorMock) Connect(c *conf.C) (channel.Outleter, error) { | ||
| return outleterMock{}, nil | ||
| } | ||
|
|
||
| func (mock connectorMock) ConnectWith(c *conf.C, clientConfig beat.ClientConfig) (channel.Outleter, error) { | ||
| return outleterMock{}, nil | ||
| } | ||
|
|
||
| type outleterMock struct{} | ||
|
|
||
| func (o outleterMock) Close() error { return nil } | ||
| func (o outleterMock) Done() <-chan struct{} { return make(chan struct{}) } | ||
| func (o outleterMock) OnEvent(beat.Event) bool { return false } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||
| // the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //go:build integration && linux | ||
|
|
||
| package integration | ||
|
|
||
| import ( | ||
| _ "embed" | ||
| "fmt" | ||
| "os" | ||
| "path" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| cp "github.com/otiai10/copy" | ||
|
|
||
| "github.com/elastic/beats/v7/libbeat/tests/integration" | ||
| ) | ||
|
|
||
| //go:embed testdata/filebeat_system_module.yml | ||
| 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 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 | ||
| globWithoutFiles := filepath.Join(filebeat.TempDir(), "this-folder-does-not-exist") | ||
| 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.Fatalf("cannot get the current directory: %s", err) | ||
| } | ||
| localModules := filepath.Join(pwd, "../", "../", "module") | ||
| localModulesD := filepath.Join(pwd, "../", "../", "modules.d") | ||
|
|
||
| if err := cp.Copy(localModules, filepath.Join(dst, "module")); err != nil { | ||
| t.Fatalf("cannot copy 'module' folder to test folder: %s", err) | ||
| } | ||
| if err := cp.Copy(localModulesD, filepath.Join(dst, "modules.d")); err != nil { | ||
| t.Fatalf("cannot copy 'modules.d' folder to test folder: %s", err) | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
filebeat/tests/integration/testdata/filebeat_system_module.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| filebeat.modules: | ||
| - module: system | ||
| syslog: | ||
| enabled: true | ||
| var.paths: | ||
| - "%s" | ||
|
|
||
| path.home: %s | ||
|
|
||
| queue.mem: | ||
| flush.timeout: 0 | ||
|
|
||
| output: | ||
| file: | ||
| path: ${path.home} | ||
| filename: "output" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.