forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into feature/packaging…
…-store-in-another-location-too * upstream/master: [Ingest Manager] Prevent reporting ecs version twice (elastic#21616) [CI] Use google storage to keep artifacts (elastic#21910) Update docs.asciidoc (elastic#21849) Kubernetes leaderelection improvements (elastic#21896) Apply name changes to elastic agent docs (elastic#21549) Add 7.7.1 relnotes to 7.8 docs (elastic#21937) (elastic#21941) [libbeat] Fix potential deadlock in the disk queue + add more unit tests (elastic#21930) Refactor docker watcher to fix flaky test and other small issues (elastic#21851) [CI] Add stage name in the step (elastic#21887) [docs] Remove extra word in autodiscover docs (elastic#21871) [CI] lint stage doesn't produce test reports (elastic#21888) Add tests of reader of filestream input (elastic#21814) [Ingest Manager] Use local temp instead of system one (elastic#21883)
- Loading branch information
Showing
27 changed files
with
1,000 additions
and
231 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,136 @@ | ||
// 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 filestream | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/v7/libbeat/logp" | ||
) | ||
|
||
func TestLogFileTimedClosing(t *testing.T) { | ||
testCases := map[string]struct { | ||
inactive time.Duration | ||
closeEOF bool | ||
afterInterval time.Duration | ||
expectedErr error | ||
}{ | ||
"read from file and close inactive": { | ||
inactive: 2 * time.Second, | ||
expectedErr: ErrClosed, | ||
}, | ||
"read from file and close after interval": { | ||
afterInterval: 3 * time.Second, | ||
expectedErr: ErrClosed, | ||
}, | ||
"read from file and close on EOF": { | ||
closeEOF: true, | ||
expectedErr: io.EOF, | ||
}, | ||
} | ||
|
||
for name, test := range testCases { | ||
test := test | ||
|
||
f := createTestLogFile() | ||
defer f.Close() | ||
defer os.Remove(f.Name()) | ||
|
||
t.Run(name, func(t *testing.T) { | ||
reader, err := newFileReader( | ||
logp.L(), | ||
context.TODO(), | ||
f, | ||
readerConfig{}, | ||
closerConfig{ | ||
OnStateChange: stateChangeCloserConfig{ | ||
CheckInterval: 1 * time.Second, | ||
Inactive: test.inactive, | ||
}, | ||
Reader: readerCloserConfig{ | ||
OnEOF: test.closeEOF, | ||
AfterInterval: test.afterInterval, | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
t.Fatalf("error while creating logReader: %+v", err) | ||
} | ||
|
||
err = readUntilError(reader) | ||
|
||
assert.Equal(t, test.expectedErr, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestLogFileTruncated(t *testing.T) { | ||
f := createTestLogFile() | ||
defer f.Close() | ||
defer os.Remove(f.Name()) | ||
|
||
reader, err := newFileReader(logp.L(), context.TODO(), f, readerConfig{}, closerConfig{}) | ||
if err != nil { | ||
t.Fatalf("error while creating logReader: %+v", err) | ||
} | ||
|
||
buf := make([]byte, 1024) | ||
_, err = reader.Read(buf) | ||
assert.Nil(t, err) | ||
|
||
err = f.Truncate(0) | ||
if err != nil { | ||
t.Fatalf("error while truncating file: %+v", err) | ||
} | ||
|
||
err = readUntilError(reader) | ||
|
||
assert.Equal(t, ErrFileTruncate, err) | ||
} | ||
|
||
func createTestLogFile() *os.File { | ||
f, err := ioutil.TempFile("", "filestream_reader_test") | ||
if err != nil { | ||
panic(err) | ||
} | ||
content := []byte("first log line\nanother interesting line\na third log message\n") | ||
if _, err := f.Write(content); err != nil { | ||
panic(err) | ||
} | ||
if _, err := f.Seek(0, io.SeekStart); err != nil { | ||
panic(err) | ||
} | ||
return f | ||
} | ||
|
||
func readUntilError(reader *logFile) error { | ||
buf := make([]byte, 1024) | ||
_, err := reader.Read(buf) | ||
for err == nil { | ||
buf := make([]byte, 1024) | ||
_, err = reader.Read(buf) | ||
} | ||
return err | ||
} |
104 changes: 104 additions & 0 deletions
104
filebeat/input/filestream/filestream_test_non_windows.go
This file contains 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,104 @@ | ||
// 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. | ||
|
||
// +build !windows | ||
|
||
package filestream | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/v7/libbeat/logp" | ||
) | ||
|
||
// these tests are separated as one cannot delete/rename files | ||
// while another process is working with it on Windows | ||
func TestLogFileRenamed(t *testing.T) { | ||
f := createTestLogFile() | ||
defer f.Close() | ||
|
||
renamedFile := f.Name() + ".renamed" | ||
|
||
reader, err := newFileReader( | ||
logp.L(), | ||
context.TODO(), | ||
f, | ||
readerConfig{}, | ||
closerConfig{ | ||
OnStateChange: stateChangeCloserConfig{ | ||
CheckInterval: 1 * time.Second, | ||
Renamed: true, | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
t.Fatalf("error while creating logReader: %+v", err) | ||
} | ||
|
||
buf := make([]byte, 1024) | ||
_, err = reader.Read(buf) | ||
assert.Nil(t, err) | ||
|
||
err = os.Rename(f.Name(), renamedFile) | ||
if err != nil { | ||
t.Fatalf("error while renaming file: %+v", err) | ||
} | ||
|
||
err = readUntilError(reader) | ||
os.Remove(renamedFile) | ||
|
||
assert.Equal(t, ErrClosed, err) | ||
} | ||
|
||
func TestLogFileRemoved(t *testing.T) { | ||
f := createTestLogFile() | ||
defer f.Close() | ||
|
||
reader, err := newFileReader( | ||
logp.L(), | ||
context.TODO(), | ||
f, | ||
readerConfig{}, | ||
closerConfig{ | ||
OnStateChange: stateChangeCloserConfig{ | ||
CheckInterval: 1 * time.Second, | ||
Removed: true, | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
t.Fatalf("error while creating logReader: %+v", err) | ||
} | ||
|
||
buf := make([]byte, 1024) | ||
_, err = reader.Read(buf) | ||
assert.Nil(t, err) | ||
|
||
err = os.Remove(f.Name()) | ||
if err != nil { | ||
t.Fatalf("error while remove file: %+v", err) | ||
} | ||
|
||
err = readUntilError(reader) | ||
|
||
assert.Equal(t, ErrClosed, err) | ||
} |
Oops, something went wrong.