Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/discovery/file/file_test.go
4096 views
1
//go:build !windows
2
3
// This should run on windows but windows does not like the tight timing of file creation and deletion.
4
package file
5
6
import (
7
"os"
8
"path"
9
"strings"
10
"testing"
11
"time"
12
13
"github.com/grafana/agent/component/discovery"
14
15
"context"
16
17
"github.com/grafana/agent/component"
18
"github.com/grafana/agent/pkg/util"
19
"github.com/prometheus/client_golang/prometheus"
20
"github.com/stretchr/testify/require"
21
)
22
23
func TestFile(t *testing.T) {
24
dir := path.Join(os.TempDir(), "agent_testing", "t1")
25
err := os.MkdirAll(dir, 0755)
26
require.NoError(t, err)
27
writeFile(t, dir, "t1.txt")
28
t.Cleanup(func() {
29
os.RemoveAll(dir)
30
})
31
c := createComponent(t, dir, []string{path.Join(dir, "*.txt")}, nil)
32
ct := context.Background()
33
ct, ccl := context.WithTimeout(ct, 5*time.Second)
34
defer ccl()
35
c.args.SyncPeriod = 10 * time.Millisecond
36
go c.Run(ct)
37
time.Sleep(20 * time.Millisecond)
38
ct.Done()
39
foundFiles := c.getWatchedFiles()
40
require.Len(t, foundFiles, 1)
41
require.True(t, contains(foundFiles, "t1.txt"))
42
}
43
44
func TestDirectoryFile(t *testing.T) {
45
dir := path.Join(os.TempDir(), "agent_testing", "t1")
46
subdir := path.Join(dir, "subdir")
47
err := os.MkdirAll(subdir, 0755)
48
require.NoError(t, err)
49
writeFile(t, subdir, "t1.txt")
50
t.Cleanup(func() {
51
os.RemoveAll(dir)
52
})
53
c := createComponent(t, dir, []string{path.Join(dir, "**/")}, nil)
54
ct := context.Background()
55
ct, ccl := context.WithTimeout(ct, 5*time.Second)
56
defer ccl()
57
c.args.SyncPeriod = 10 * time.Millisecond
58
go c.Run(ct)
59
time.Sleep(20 * time.Millisecond)
60
ct.Done()
61
foundFiles := c.getWatchedFiles()
62
require.Len(t, foundFiles, 1)
63
require.True(t, contains(foundFiles, "t1.txt"))
64
}
65
66
func TestAddingFile(t *testing.T) {
67
dir := path.Join(os.TempDir(), "agent_testing", "t2")
68
err := os.MkdirAll(dir, 0755)
69
require.NoError(t, err)
70
writeFile(t, dir, "t1.txt")
71
t.Cleanup(func() {
72
os.RemoveAll(dir)
73
})
74
c := createComponent(t, dir, []string{path.Join(dir, "*.txt")}, nil)
75
76
ct := context.Background()
77
ct, ccl := context.WithTimeout(ct, 40*time.Second)
78
defer ccl()
79
c.args.SyncPeriod = 10 * time.Millisecond
80
go c.Run(ct)
81
time.Sleep(20 * time.Millisecond)
82
writeFile(t, dir, "t2.txt")
83
ct.Done()
84
foundFiles := c.getWatchedFiles()
85
require.Len(t, foundFiles, 2)
86
require.True(t, contains(foundFiles, "t1.txt"))
87
require.True(t, contains(foundFiles, "t2.txt"))
88
}
89
90
func TestAddingFileInSubDir(t *testing.T) {
91
dir := path.Join(os.TempDir(), "agent_testing", "t3")
92
os.MkdirAll(dir, 0755)
93
writeFile(t, dir, "t1.txt")
94
t.Cleanup(func() {
95
os.RemoveAll(dir)
96
})
97
c := createComponent(t, dir, []string{path.Join(dir, "**", "*.txt")}, nil)
98
ct := context.Background()
99
ct, ccl := context.WithTimeout(ct, 40*time.Second)
100
defer ccl()
101
c.args.SyncPeriod = 10 * time.Millisecond
102
go c.Run(ct)
103
time.Sleep(20 * time.Millisecond)
104
writeFile(t, dir, "t2.txt")
105
subdir := path.Join(dir, "subdir")
106
os.Mkdir(subdir, 0755)
107
time.Sleep(20 * time.Millisecond)
108
err := os.WriteFile(path.Join(subdir, "t3.txt"), []byte("asdf"), 0664)
109
require.NoError(t, err)
110
time.Sleep(20 * time.Millisecond)
111
ct.Done()
112
foundFiles := c.getWatchedFiles()
113
require.Len(t, foundFiles, 3)
114
require.True(t, contains(foundFiles, "t1.txt"))
115
require.True(t, contains(foundFiles, "t2.txt"))
116
require.True(t, contains(foundFiles, "t3.txt"))
117
}
118
119
func TestAddingRemovingFileInSubDir(t *testing.T) {
120
dir := path.Join(os.TempDir(), "agent_testing", "t3")
121
os.MkdirAll(dir, 0755)
122
writeFile(t, dir, "t1.txt")
123
t.Cleanup(func() {
124
os.RemoveAll(dir)
125
})
126
c := createComponent(t, dir, []string{path.Join(dir, "**", "*.txt")}, nil)
127
128
ct := context.Background()
129
ct, ccl := context.WithTimeout(ct, 40*time.Second)
130
defer ccl()
131
c.args.SyncPeriod = 10 * time.Millisecond
132
go c.Run(ct)
133
time.Sleep(20 * time.Millisecond)
134
writeFile(t, dir, "t2.txt")
135
subdir := path.Join(dir, "subdir")
136
os.Mkdir(subdir, 0755)
137
time.Sleep(100 * time.Millisecond)
138
err := os.WriteFile(path.Join(subdir, "t3.txt"), []byte("asdf"), 0664)
139
require.NoError(t, err)
140
time.Sleep(100 * time.Millisecond)
141
foundFiles := c.getWatchedFiles()
142
require.Len(t, foundFiles, 3)
143
require.True(t, contains(foundFiles, "t1.txt"))
144
require.True(t, contains(foundFiles, "t2.txt"))
145
require.True(t, contains(foundFiles, "t3.txt"))
146
147
err = os.RemoveAll(subdir)
148
require.NoError(t, err)
149
time.Sleep(1000 * time.Millisecond)
150
foundFiles = c.getWatchedFiles()
151
require.Len(t, foundFiles, 2)
152
require.True(t, contains(foundFiles, "t1.txt"))
153
require.True(t, contains(foundFiles, "t2.txt"))
154
}
155
156
func TestExclude(t *testing.T) {
157
dir := path.Join(os.TempDir(), "agent_testing", "t3")
158
os.MkdirAll(dir, 0755)
159
writeFile(t, dir, "t1.txt")
160
t.Cleanup(func() {
161
os.RemoveAll(dir)
162
})
163
c := createComponent(t, dir, []string{path.Join(dir, "**", "*.txt")}, []string{path.Join(dir, "**", "*.bad")})
164
ct := context.Background()
165
ct, ccl := context.WithTimeout(ct, 40*time.Second)
166
defer ccl()
167
c.args.SyncPeriod = 10 * time.Millisecond
168
go c.Run(ct)
169
time.Sleep(100 * time.Millisecond)
170
subdir := path.Join(dir, "subdir")
171
os.Mkdir(subdir, 0755)
172
writeFile(t, subdir, "t3.txt")
173
time.Sleep(100 * time.Millisecond)
174
foundFiles := c.getWatchedFiles()
175
require.Len(t, foundFiles, 2)
176
require.True(t, contains(foundFiles, "t1.txt"))
177
require.True(t, contains(foundFiles, "t3.txt"))
178
}
179
180
func TestMultiLabels(t *testing.T) {
181
dir := path.Join(os.TempDir(), "agent_testing", "t3")
182
os.MkdirAll(dir, 0755)
183
writeFile(t, dir, "t1.txt")
184
t.Cleanup(func() {
185
os.RemoveAll(dir)
186
})
187
c := createComponentWithLabels(t, dir, []string{path.Join(dir, "**", "*.txt"), path.Join(dir, "**", "*.txt")}, nil, map[string]string{
188
"foo": "bar",
189
"fruit": "apple",
190
})
191
c.args.PathTargets[0]["newlabel"] = "test"
192
ct := context.Background()
193
ct, ccl := context.WithTimeout(ct, 40*time.Second)
194
defer ccl()
195
c.args.SyncPeriod = 10 * time.Millisecond
196
go c.Run(ct)
197
time.Sleep(100 * time.Millisecond)
198
foundFiles := c.getWatchedFiles()
199
require.Len(t, foundFiles, 2)
200
require.True(t, contains([]discovery.Target{foundFiles[0]}, "t1.txt"))
201
require.True(t, contains([]discovery.Target{foundFiles[1]}, "t1.txt"))
202
}
203
204
func createComponent(t *testing.T, dir string, paths []string, excluded []string) *Component {
205
return createComponentWithLabels(t, dir, paths, excluded, nil)
206
}
207
208
func createComponentWithLabels(t *testing.T, dir string, paths []string, excluded []string, labels map[string]string) *Component {
209
tPaths := make([]discovery.Target, 0)
210
for _, p := range paths {
211
tar := discovery.Target{"__path__": p}
212
for k, v := range labels {
213
tar[k] = v
214
}
215
tPaths = append(tPaths, tar)
216
}
217
for _, p := range excluded {
218
tar := discovery.Target{"__path_exclude__": p}
219
for k, v := range labels {
220
tar[k] = v
221
}
222
tPaths = append(tPaths, tar)
223
}
224
c, err := New(component.Options{
225
ID: "test",
226
Logger: util.TestFlowLogger(t),
227
DataPath: dir,
228
OnStateChange: func(e component.Exports) {
229
230
},
231
Registerer: prometheus.DefaultRegisterer,
232
Tracer: nil,
233
HTTPListenAddr: "",
234
HTTPPath: "",
235
}, Arguments{
236
PathTargets: tPaths,
237
SyncPeriod: 1 * time.Second,
238
})
239
240
require.NoError(t, err)
241
require.NotNil(t, c)
242
return c
243
}
244
245
func contains(sources []discovery.Target, match string) bool {
246
for _, s := range sources {
247
p := s["__path__"]
248
if strings.Contains(p, match) {
249
return true
250
}
251
}
252
return false
253
}
254
255
func writeFile(t *testing.T, dir string, name string) {
256
err := os.WriteFile(path.Join(dir, name), []byte("asdf"), 0664)
257
require.NoError(t, err)
258
time.Sleep(20 * time.Millisecond)
259
}
260
261