-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathfilestream_nonwindows_test.go
More file actions
121 lines (102 loc) · 3.02 KB
/
Copy pathfilestream_nonwindows_test.go
File metadata and controls
121 lines (102 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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 !windows
package filestream
import (
"context"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/elastic/elastic-agent-libs/logp/logptest"
)
// 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(t)
defer f.Close()
renamedFile := f.Name() + ".renamed"
reader, _, err := newFileReader(
logptest.NewFileLogger(t, filepath.Join("..", "..", "build", "integration-tests")).Logger,
context.TODO(),
f,
readerConfig{},
closerConfig{
OnStateChange: stateChangeCloserConfig{
CheckInterval: 1 * time.Second,
Renamed: true,
},
},
false,
)
if err != nil {
t.Fatalf("error while creating logReader: %+v", err)
}
buf := make([]byte, 1024)
_, err = reader.Read(buf)
assert.NoError(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(t)
defer f.Close()
reader, _, err := newFileReader(
logptest.NewFileLogger(t, filepath.Join("..", "..", "build", "integration-tests")).Logger,
context.TODO(),
f,
readerConfig{},
closerConfig{
OnStateChange: stateChangeCloserConfig{
CheckInterval: 1 * time.Second,
Removed: true,
},
},
false,
)
if err != nil {
t.Fatalf("error while creating logReader: %+v", err)
}
buf := make([]byte, 1024)
_, err = reader.Read(buf)
assert.NoError(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)
}
// createTestLogFile creates a temporary plain-text log file with a few lines of
// content, wrapped as a filestream File ready to be passed to newFileReader.
func createTestLogFile(t *testing.T) File {
t.Helper()
fs := filestream{
readerConfig: readerConfig{BufferSize: 512},
compression: CompressionNone,
}
f, err := fs.newFile(createTestPlainLogFile(t))
require.NoError(t, err, "could not create test log file")
return f
}