summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/agent/discovery/file/watch_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/go/collectors/go.d.plugin/agent/discovery/file/watch_test.go')
-rw-r--r--src/go/collectors/go.d.plugin/agent/discovery/file/watch_test.go588
1 files changed, 307 insertions, 281 deletions
diff --git a/src/go/collectors/go.d.plugin/agent/discovery/file/watch_test.go b/src/go/collectors/go.d.plugin/agent/discovery/file/watch_test.go
index cc8f8a6568..20e21e65eb 100644
--- a/src/go/collectors/go.d.plugin/agent/discovery/file/watch_test.go
+++ b/src/go/collectors/go.d.plugin/agent/discovery/file/watch_test.go
@@ -3,6 +3,7 @@
package file
import (
+ "fmt"
"testing"
"time"
@@ -28,325 +29,350 @@ func TestNewWatcher(t *testing.T) {
}
for name, test := range tests {
- t.Run(name, func(t *testing.T) { assert.NotNil(t, NewWatcher(test.reg, test.paths)) })
+ t.Run(name, func(t *testing.T) {
+ assert.NotNil(t, NewWatcher(test.reg, test.paths))
+ })
}
}
func TestWatcher_Run(t *testing.T) {
- tests := map[string]func(tmp *tmpDir) discoverySim{
- "file exists before start": func(tmp *tmpDir) discoverySim {
- reg := confgroup.Registry{
- "module": {},
- }
- cfg := sdConfig{
- {
- "name": "name",
- "module": "module",
- },
- }
- filename := tmp.join("module.conf")
- discovery := prepareDiscovery(t, Config{
- Registry: reg,
- Watch: []string{tmp.join("*.conf")},
- })
- expected := []*confgroup.Group{
- {
- Source: filename,
- Configs: []confgroup.Config{
- {
- "name": "name",
- "module": "module",
- "update_every": module.UpdateEvery,
- "autodetection_retry": module.AutoDetectionRetry,
- "priority": module.Priority,
- "__source__": filename,
- "__provider__": "file watcher",
+ tests := map[string]struct {
+ createSim func(tmp *tmpDir) discoverySim
+ }{
+ "file exists before start": {
+ createSim: func(tmp *tmpDir) discoverySim {
+ reg := confgroup.Registry{
+ "module": {},
+ }
+ cfg := sdConfig{
+ {
+ "name": "name",
+ "module": "module",
+ },
+ }
+ filename := tmp.join("module.conf")
+ discovery := prepareDiscovery(t, Config{
+ Registry: reg,
+ Watch: []string{tmp.join("*.conf")},
+ })
+ expected := []*confgroup.Group{
+ {
+ Source: filename,
+ Configs: []confgroup.Config{
+ {
+ "name": "name",
+ "module": "module",
+ "update_every": module.UpdateEvery,
+ "autodetection_retry": module.AutoDetectionRetry,
+ "priority": module.Priority,
+ "__provider__": "file watcher",
+ "__source_type__": confgroup.TypeStock,
+ "__source__": fmt.Sprintf("discoverer=file_watcher,file=%s", filename),
+ },
},
},
- },
- }
+ }
- sim := discoverySim{
- discovery: discovery,
- beforeRun: func() {
- tmp.writeYAML(filename, cfg)
- },
- expectedGroups: expected,
- }
- return sim
+ sim := discoverySim{
+ discovery: discovery,
+ beforeRun: func() {
+ tmp.writeYAML(filename, cfg)
+ },
+ expectedGroups: expected,
+ }
+ return sim
+ },
},
- "empty file": func(tmp *tmpDir) discoverySim {
- reg := confgroup.Registry{
- "module": {},
- }
- filename := tmp.join("module.conf")
- discovery := prepareDiscovery(t, Config{
- Registry: reg,
- Watch: []string{tmp.join("*.conf")},
- })
- expected := []*confgroup.Group{
- {
- Source: filename,
- },
- }
+ "empty file": {
+ createSim: func(tmp *tmpDir) discoverySim {
+ reg := confgroup.Registry{
+ "module": {},
+ }
+ filename := tmp.join("module.conf")
+ discovery := prepareDiscovery(t, Config{
+ Registry: reg,
+ Watch: []string{tmp.join("*.conf")},
+ })
+ expected := []*confgroup.Group{
+ {
+ Source: filename,
+ },
+ }
- sim := discoverySim{
- discovery: discovery,
- beforeRun: func() {
- tmp.writeString(filename, "")
- },
- expectedGroups: expected,
- }
- return sim
+ sim := discoverySim{
+ discovery: discovery,
+ beforeRun: func() {
+ tmp.writeString(filename, "")
+ },
+ expectedGroups: expected,
+ }
+ return sim
+ },
},
- "only comments, no data": func(tmp *tmpDir) discoverySim {
- reg := confgroup.Registry{
- "module": {},
- }
- filename := tmp.join("module.conf")
- discovery := prepareDiscovery(t, Config{
- Registry: reg,
- Watch: []string{tmp.join("*.conf")},
- })
- expected := []*confgroup.Group{
- {
- Source: filename,
- },
- }
+ "only comments, no data": {
+ createSim: func(tmp *tmpDir) discoverySim {
+ reg := confgroup.Registry{
+ "module": {},
+ }
+ filename := tmp.join("module.conf")
+ discovery := prepareDiscovery(t, Config{
+ Registry: reg,
+ Watch: []string{tmp.join("*.conf")},
+ })
+ expected := []*confgroup.Group{
+ {
+ Source: filename,
+ },
+ }
- sim := discoverySim{
- discovery: discovery,
- beforeRun: func() {
- tmp.writeString(filename, "# a comment")
- },
- expectedGroups: expected,
- }
- return sim
+ sim := discoverySim{
+ discovery: discovery,
+ beforeRun: func() {
+ tmp.writeString(filename, "# a comment")
+ },
+ expectedGroups: expected,
+ }
+ return sim
+ },
},
- "add file": func(tmp *tmpDir) discoverySim {
- reg := confgroup.Registry{
- "module": {},
- }
- cfg := sdConfig{
- {
- "name": "name",
- "module": "module",
- },
- }
- filename := tmp.join("module.conf")
- discovery := prepareDiscovery(t, Config{
- Registry: reg,
- Watch: []string{tmp.join("*.conf")},
- })
- expected := []*confgroup.Group{
- {
- Source: filename,
- Configs: []confgroup.Config{
- {
- "name": "name",
- "module": "module",
- "update_every": module.UpdateEvery,
- "autodetection_retry": module.AutoDetectionRetry,
- "priority": module.Priority,
- "__source__": filename,
- "__provider__": "file watcher",
+ "add file": {
+ createSim: func(tmp *tmpDir) discoverySim {
+ reg := confgroup.Registry{
+ "module": {},
+ }
+ cfg := sdConfig{
+ {
+ "name": "name",
+ "module": "module",
+ },
+ }
+ filename := tmp.join("module.conf")
+ discovery := prepareDiscovery(t, Config{
+ Registry: reg,
+ Watch: []string{tmp.join("*.conf")},
+ })
+ expected := []*confgroup.Group{
+ {
+ Source: filename,
+ Configs: []confgroup.Config{
+ {
+ "name": "name",
+ "module": "module",
+ "update_every": module.UpdateEvery,
+ "autodetection_retry": module.AutoDetectionRetry,
+ "priority": module.Priority,
+ "__provider__": "file watcher",
+ "__source_type__": confgroup.TypeStock,
+ "__source__": fmt.Sprintf("discoverer=file_watcher,file=%s", filename),
+ },
},
},
- },
- }
+ }
- sim := discoverySim{
- discovery: discovery,
- afterRun: func() {
- tmp.writeYAML(filename, cfg)
- },
- expectedGroups: expected,
- }
- return sim
+ sim := discoverySim{
+ discovery: discovery,
+ afterRun: func() {
+ tmp.writeYAML(filename, cfg)
+ },
+ expectedGroups: expected,
+ }
+ return sim
+ },
},
- "remove file": func(tmp *tmpDir) discoverySim {
- reg := confgroup.Registry{
- "module": {},
- }
- cfg := sdConfig{
- {
- "name": "name",
- "module": "module",
- },
- }
- filename := tmp.join("module.conf")
- discovery := prepareDiscovery(t, Config{
- Registry: reg,
- Watch: []string{tmp.join("*.conf")},
- })
- expected := []*confgroup.Group{
- {
- Source: filename,
- Configs: []confgroup.Config{
- {
- "name": "name",
- "module": "module",
- "update_every": module.UpdateEvery,
- "autodetection_retry": module.AutoDetectionRetry,
- "priority": module.Priority,
- "__source__": filename,
- "__provider__": "file watcher",
+ "remove file": {
+ createSim: func(tmp *tmpDir) discoverySim {
+ reg := confgroup.Registry{
+ "module": {},
+ }
+ cfg := sdConfig{
+ {
+ "name": "name",
+ "module": "module",
+ },
+ }
+ filename := tmp.join("module.conf")
+ discovery := prepareDiscovery(t, Config{
+ Registry: reg,
+ Watch: []string{tmp.join("*.conf")},
+ })
+ expected := []*confgroup.Group{
+ {
+ Source: filename,
+ Configs: []confgroup.Config{
+ {
+ "name": "name",
+ "module": "module",
+ "update_every": module.UpdateEvery,
+ "autodetection_retry": module.AutoDetectionRetry,
+ "priority": module.Priority,
+ "__provider__": "file watcher",
+ "__source_type__": confgroup.TypeStock,
+ "__source__": fmt.Sprintf("discoverer=file_watcher,file=%s", filename),
+ },
},
},
- },
- {
- Source: filename,
- Configs: nil,
- },
- }
+ {
+ Source: filename,
+ Configs: nil,
+ },
+ }
- sim := discoverySim{
- discovery: discovery,
- beforeRun: func() {
- tmp.writeYAML(filename, cfg)
- },
- afterRun: func() {
- tmp.removeFile(filename)
- },
- expectedGroups: expected,
- }
- return sim
+ sim := discoverySim{
+ discovery: discovery,
+ beforeRun: func() {
+ tmp.writeYAML(filename, cfg)
+ },
+ afterRun: func() {
+ tmp.removeFile(filename)
+ },
+ expectedGroups: expected,
+ }
+ return sim
+ },
},
- "change file": func(tmp *tmpDir) discoverySim {
- reg := confgroup.Registry{
- "module": {},
- }
- cfgOrig := sdConfig{
- {
- "name": "name",
- "module": "module",
- },
- }
- cfgChanged := sdConfig{
- {
- "name": "name_changed",
- "module": "module",
- },
- }
- filename := tmp.join("module.conf")
- discovery := prepareDiscovery(t, Config{
- Registry: reg,
- Watch: []string{tmp.join("*.conf")},
- })
- expected := []*confgroup.Group{
- {
- Source: filename,
- Configs: []confgroup.Config{
- {
- "name": "name",
- "module": "module",
- "update_every": module.UpdateEvery,
- "autodetection_retry": module.AutoDetectionRetry,
- "priority": module.Priority,
- "__source__": filename,
- "__provider__": "file watcher",
+ "change file": {
+ createSim: func(tmp *tmpDir) discoverySim {
+ reg := confgroup.Registry{
+ "module": {},
+ }
+ cfgOrig := sdConfig{
+ {
+ "name": "name",
+ "module": "module",
+ },
+ }
+ cfgChanged := sdConfig{
+ {
+ "name": "name_changed",
+ "module": "module",
+ },
+ }
+ filename := tmp.join("module.conf")
+ discovery := prepareDiscovery(t, Config{
+ Registry: reg,
+ Watch: []string{tmp.join("*.conf")},
+ })
+ expected := []*confgroup.Group{
+ {
+ Source: filename,
+ Configs: []confgroup.Config{
+ {
+ "name": "name",
+ "module": "module",
+ "update_every": module.UpdateEvery,
+ "autodetection_retry": module.AutoDetectionRetry,
+ "priority": module.Priority,
+ "__provider__": "file watcher",
+ "__source_type__": confgroup.TypeStock,
+ "__source__": fmt.Sprintf("discoverer=file_watcher,file=%s", filename),
+ },
},
},
- },
- {
- Source: filename,
- Configs: []confgroup.Config{
- {
- "name": "name_changed",
- "module": "module",
- "update_every": module.UpdateEvery,
- "autodetection_retry": module.AutoDetectionRetry,
- "priority": module.Priority,
- "__source__": filename,
- "__provider__": "file watcher",
+ {
+ Source: filename,
+ Configs: []confgroup.Config{
+ {
+ "name": "name_changed",
+ "module": "module",
+ "update_every": module.UpdateEvery,
+ "autodetection_retry": module.AutoDetectionRetry,
+ "priority": module.Priority,
+ "__provider__": "file watcher",
+ "__source_type__": confgroup.TypeStock,
+ "__source__": fmt.Sprintf("discoverer=file_watcher,file=%s", filename),
+ },
},
},
- },
- }
+ }
- sim := discoverySim{
- discovery: discovery,
- beforeRun: func() {
- tmp.writeYAML(filename, cfgOrig)
- },
- afterRun: func() {
- tmp.writeYAML(filename, cfgChanged)
- time.Sleep(time.Millisecond * 500)
- },
- expectedGroups: expected,
- }
- return sim
+ sim := discoverySim{
+ discovery: discovery,
+ beforeRun: func() {
+ tmp.writeYAML(filename, cfgOrig)
+ },
+ afterRun: func() {
+ tmp.writeYAML(filename, cfgChanged)
+ time.Sleep(time.Millisecond * 500)
+ },
+ expectedGroups: expected,
+ }
+ return sim
+ },
},
- "vim 'backupcopy=no' (writing to a file and backup)": func(tmp *tmpDir) discoverySim {
- reg := confgroup.Registry{
- "module": {},
- }
- cfg := sdConfig{
- {
- "name": "name",
- "module": "module",
- },
- }
- filename := tmp.join("module.conf")
- discovery := prepareDiscovery(t, Config{
- Registry: reg,
- Watch: []string{tmp.join("*.conf")},
- })
- expected := []*confgroup.Group{
- {
- Source: filename,
- Configs: []confgroup.Config{
- {
- "name": "name",
- "module": "module",
- "update_every": module.UpdateEvery,
- "autodetection_retry": module.AutoDetectionRetry,
- "priority": module.Priority,
- "__source__": filename,
- "__provider__": "file watcher",
+ "vim 'backupcopy=no' (writing to a file and backup)": {
+ createSim: func(tmp *tmpDir) discoverySim {
+ reg := confgroup.Registry{
+ "module": {},
+ }
+ cfg := sdConfig{
+ {
+ "name": "name",
+ "module": "module",
+ },
+ }
+ filename := tmp.join("module.conf")
+ discovery := prepareDiscovery(t, Config{
+ Registry: reg,
+ Watch: []string{tmp.join("*.conf")},
+ })
+ expected := []*confgroup.Group{
+ {
+ Source: filename,
+ Configs: []confgroup.Config{
+ {
+ "name": "name",
+ "module": "module",
+ "update_every": module.UpdateEvery,
+ "autodetection_retry": module.AutoDetectionRetry,
+ "priority": module.Priority,
+ "__provider__": "file watcher",
+ "__source_type__": confgroup.TypeStock,
+ "__source__": fmt.Sprintf("discoverer=file_watcher,file=%s", filename),
+ },
},
},
- },
- {
- Source: filename,
- Configs: []confgroup.Config{
- {
- "name": "name",
- "module": "module",
- "update_every": module.UpdateEvery,
- "autodetection_retry": module.AutoDetectionRetry,
- "priority": module.Priority,
- "__source__": filename,
- "__provider__": "file watcher",
+ {
+ Source: filename,
+ Configs: []confgroup.Config{
+ {
+ "name": "name",
+ "module": "module",
+ "update_every": module.UpdateEvery,
+ "autodetection_retry": module.AutoDetectionRetry,
+ "priority": module.Priority,
+ "__provider__": "file watcher",
+ "__source_type__": "stock",
+ "__source__": fmt.Sprintf("discoverer=file_watcher,file=%s", filename),
+ },
},
},
- },
- }
+ }
- sim := discoverySim{
- discovery: discovery,
- beforeRun: func() {
- tmp.writeYAML(filename, cfg)
- },
- afterRun: func() {
- newFilename := filename + ".swp"
- tmp.renameFile(filename, newFilename)
- tmp.writeYAML(filename, cfg)
- tmp.removeFile(newFilename)
- time.Sleep(time.Millisecond * 500)
- },
- expectedGroups: expected,
- }
- return sim
+ sim := discoverySim{
+ discovery: discovery,
+ beforeRun: func() {
+ tmp.writeYAML(filename, cfg)
+ },
+ afterRun: func() {
+ newFilename := filename + ".swp"
+ tmp.renameFile(filename, newFilename)
+ tmp.writeYAML(filename, cfg)
+ tmp.removeFile(newFilename)
+ time.Sleep(time.Millisecond * 500)
+ },
+ expectedGroups: expected,
+ }
+ return sim
+ },
},
}
- for name, createSim := range tests {
+ for name, test := range tests {
t.Run(name, func(t *testing.T) {
tmp := newTmpDir(t, "watch-run-*")
defer tmp.cleanup()
- createSim(tmp).run(t)
+ test.createSim(tmp).run(t)
})
}
}