Skip to content

Commit 1f08e35

Browse files
authored
Add tests of reader of filestream input (elastic#21814)
## What does this PR do? This PR adds tests for `logFile` in the `filestream` input. This element of the architecture is responsible for reading directly from the disk and closing the reader if the state or the position meets the configured criteria. Conditions tested in the PR: - file is removed - file is renamed - file is truncated - file is inactive for a time - file reader reaches EOF - timeout of the file reader is reached
1 parent bb79569 commit 1f08e35

3 files changed

Lines changed: 243 additions & 7 deletions

File tree

filebeat/input/filestream/filestream.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,16 @@ func (f *logFile) Read(buf []byte) (int, error) {
138138
}
139139

140140
func (f *logFile) startFileMonitoringIfNeeded() {
141-
if f.closeInactive == 0 && f.closeAfterInterval == 0 {
142-
return
143-
}
144-
145-
if f.closeInactive > 0 {
141+
if f.closeInactive > 0 || f.closeRemoved || f.closeRenamed {
146142
f.tg.Go(func(ctx unison.Canceler) error {
147-
f.closeIfTimeout(ctx)
143+
f.periodicStateCheck(ctx)
148144
return nil
149145
})
150146
}
151147

152148
if f.closeAfterInterval > 0 {
153149
f.tg.Go(func(ctx unison.Canceler) error {
154-
f.periodicStateCheck(ctx)
150+
f.closeIfTimeout(ctx)
155151
return nil
156152
})
157153
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package filestream
19+
20+
import (
21+
"context"
22+
"io"
23+
"io/ioutil"
24+
"os"
25+
"testing"
26+
"time"
27+
28+
"github.com/stretchr/testify/assert"
29+
30+
"github.com/elastic/beats/v7/libbeat/logp"
31+
)
32+
33+
func TestLogFileTimedClosing(t *testing.T) {
34+
testCases := map[string]struct {
35+
inactive time.Duration
36+
closeEOF bool
37+
afterInterval time.Duration
38+
expectedErr error
39+
}{
40+
"read from file and close inactive": {
41+
inactive: 2 * time.Second,
42+
expectedErr: ErrClosed,
43+
},
44+
"read from file and close after interval": {
45+
afterInterval: 3 * time.Second,
46+
expectedErr: ErrClosed,
47+
},
48+
"read from file and close on EOF": {
49+
closeEOF: true,
50+
expectedErr: io.EOF,
51+
},
52+
}
53+
54+
for name, test := range testCases {
55+
test := test
56+
57+
f := createTestLogFile()
58+
defer f.Close()
59+
defer os.Remove(f.Name())
60+
61+
t.Run(name, func(t *testing.T) {
62+
reader, err := newFileReader(
63+
logp.L(),
64+
context.TODO(),
65+
f,
66+
readerConfig{},
67+
closerConfig{
68+
OnStateChange: stateChangeCloserConfig{
69+
CheckInterval: 1 * time.Second,
70+
Inactive: test.inactive,
71+
},
72+
Reader: readerCloserConfig{
73+
OnEOF: test.closeEOF,
74+
AfterInterval: test.afterInterval,
75+
},
76+
},
77+
)
78+
if err != nil {
79+
t.Fatalf("error while creating logReader: %+v", err)
80+
}
81+
82+
err = readUntilError(reader)
83+
84+
assert.Equal(t, test.expectedErr, err)
85+
})
86+
}
87+
}
88+
89+
func TestLogFileTruncated(t *testing.T) {
90+
f := createTestLogFile()
91+
defer f.Close()
92+
defer os.Remove(f.Name())
93+
94+
reader, err := newFileReader(logp.L(), context.TODO(), f, readerConfig{}, closerConfig{})
95+
if err != nil {
96+
t.Fatalf("error while creating logReader: %+v", err)
97+
}
98+
99+
buf := make([]byte, 1024)
100+
_, err = reader.Read(buf)
101+
assert.Nil(t, err)
102+
103+
err = f.Truncate(0)
104+
if err != nil {
105+
t.Fatalf("error while truncating file: %+v", err)
106+
}
107+
108+
err = readUntilError(reader)
109+
110+
assert.Equal(t, ErrFileTruncate, err)
111+
}
112+
113+
func createTestLogFile() *os.File {
114+
f, err := ioutil.TempFile("", "filestream_reader_test")
115+
if err != nil {
116+
panic(err)
117+
}
118+
content := []byte("first log line\nanother interesting line\na third log message\n")
119+
if _, err := f.Write(content); err != nil {
120+
panic(err)
121+
}
122+
if _, err := f.Seek(0, io.SeekStart); err != nil {
123+
panic(err)
124+
}
125+
return f
126+
}
127+
128+
func readUntilError(reader *logFile) error {
129+
buf := make([]byte, 1024)
130+
_, err := reader.Read(buf)
131+
for err == nil {
132+
buf := make([]byte, 1024)
133+
_, err = reader.Read(buf)
134+
}
135+
return err
136+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
// +build !windows
19+
20+
package filestream
21+
22+
import (
23+
"context"
24+
"os"
25+
"testing"
26+
"time"
27+
28+
"github.com/stretchr/testify/assert"
29+
30+
"github.com/elastic/beats/v7/libbeat/logp"
31+
)
32+
33+
// these tests are separated as one cannot delete/rename files
34+
// while another process is working with it on Windows
35+
func TestLogFileRenamed(t *testing.T) {
36+
f := createTestLogFile()
37+
defer f.Close()
38+
39+
renamedFile := f.Name() + ".renamed"
40+
41+
reader, err := newFileReader(
42+
logp.L(),
43+
context.TODO(),
44+
f,
45+
readerConfig{},
46+
closerConfig{
47+
OnStateChange: stateChangeCloserConfig{
48+
CheckInterval: 1 * time.Second,
49+
Renamed: true,
50+
},
51+
},
52+
)
53+
if err != nil {
54+
t.Fatalf("error while creating logReader: %+v", err)
55+
}
56+
57+
buf := make([]byte, 1024)
58+
_, err = reader.Read(buf)
59+
assert.Nil(t, err)
60+
61+
err = os.Rename(f.Name(), renamedFile)
62+
if err != nil {
63+
t.Fatalf("error while renaming file: %+v", err)
64+
}
65+
66+
err = readUntilError(reader)
67+
os.Remove(renamedFile)
68+
69+
assert.Equal(t, ErrClosed, err)
70+
}
71+
72+
func TestLogFileRemoved(t *testing.T) {
73+
f := createTestLogFile()
74+
defer f.Close()
75+
76+
reader, err := newFileReader(
77+
logp.L(),
78+
context.TODO(),
79+
f,
80+
readerConfig{},
81+
closerConfig{
82+
OnStateChange: stateChangeCloserConfig{
83+
CheckInterval: 1 * time.Second,
84+
Removed: true,
85+
},
86+
},
87+
)
88+
if err != nil {
89+
t.Fatalf("error while creating logReader: %+v", err)
90+
}
91+
92+
buf := make([]byte, 1024)
93+
_, err = reader.Read(buf)
94+
assert.Nil(t, err)
95+
96+
err = os.Remove(f.Name())
97+
if err != nil {
98+
t.Fatalf("error while remove file: %+v", err)
99+
}
100+
101+
err = readUntilError(reader)
102+
103+
assert.Equal(t, ErrClosed, err)
104+
}

0 commit comments

Comments
 (0)