master
go 139 lines 3.32 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package file
4
5 import (
6 "fmt"
7 "testing"
8
9 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
10 "github.com/netdata/netdata/go/plugins/plugin/framework/confgroup"
11
12 "github.com/stretchr/testify/assert"
13 )
14
15 func TestReader_New(t *testing.T) {
16 tests := map[string]struct {
17 run func(t *testing.T)
18 }{
19 "string is not empty": {
20 run: func(t *testing.T) {
21 assert.NotEmpty(t, NewReader(confgroup.Registry{}, nil))
22 },
23 },
24 "empty inputs create reader": {
25 run: func(t *testing.T) {
26 assert.NotNil(t, NewReader(confgroup.Registry{}, []string{}))
27 },
28 },
29 }
30
31 for name, tc := range tests {
32 t.Run(name, func(t *testing.T) {
33 tc.run(t)
34 })
35 }
36 }
37
38 func TestReader_Run(t *testing.T) {
39 tests := map[string]struct {
40 createSim func(tmp *tmpDir) discoverySim
41 }{
42 "read multiple files": {
43 createSim: func(tmp *tmpDir) discoverySim {
44 module1 := tmp.join("module1.conf")
45 module2 := tmp.join("module2.conf")
46 module3 := tmp.join("module3.conf")
47
48 tmp.writeYAML(module1, staticConfig{
49 Jobs: []confgroup.Config{{"name": "name"}},
50 })
51 tmp.writeYAML(module2, staticConfig{
52 Jobs: []confgroup.Config{{"name": "name"}},
53 })
54 tmp.writeString(module3, "# a comment")
55
56 reg := confgroup.Registry{
57 "module1": {},
58 "module2": {},
59 "module3": {},
60 }
61 discovery := prepareDiscovery(t, Config{
62 Registry: reg,
63 Read: []string{module1, module2, module3},
64 })
65 expected := []*confgroup.Group{
66 {
67 Source: module1,
68 Configs: []confgroup.Config{
69 {
70 "name": "name",
71 "module": "module1",
72 "update_every": collectorapi.UpdateEvery,
73 "autodetection_retry": collectorapi.AutoDetectionRetry,
74 "priority": collectorapi.Priority,
75 "__provider__": "file reader",
76 "__source_type__": confgroup.TypeStock,
77 "__source__": fmt.Sprintf("discoverer=file_reader,file=%s", module1),
78 },
79 },
80 },
81 {
82 Source: module2,
83 Configs: []confgroup.Config{
84 {
85 "name": "name",
86 "module": "module2",
87 "update_every": collectorapi.UpdateEvery,
88 "autodetection_retry": collectorapi.AutoDetectionRetry,
89 "priority": collectorapi.Priority,
90 "__provider__": "file reader",
91 "__source_type__": confgroup.TypeStock,
92 "__source__": fmt.Sprintf("discoverer=file_reader,file=%s", module2),
93 },
94 },
95 },
96 {
97 Source: module3,
98 },
99 }
100
101 return discoverySim{
102 discovery: discovery,
103 expectedGroups: expected,
104 }
105 },
106 },
107 }
108
109 for name, test := range tests {
110 t.Run(name, func(t *testing.T) {
111 tmp := newTmpDir(t, "reader-run-*")
112 defer tmp.cleanup()
113
114 test.createSim(tmp).run(t)
115 })
116 }
117 }
118
119 func TestConfigSourceType(t *testing.T) {
120 tests := map[string]struct {
121 path string
122 want string
123 }{
124 "user path under /etc": {
125 path: "/etc/netdata/go.d/module.conf",
126 want: confgroup.TypeUser,
127 },
128 "stock path outside /etc": {
129 path: "/usr/lib/netdata/conf.d/go.d/module.conf",
130 want: confgroup.TypeStock,
131 },
132 }
133
134 for name, tc := range tests {
135 t.Run(name, func(t *testing.T) {
136 assert.Equal(t, tc.want, configSourceType(tc.path))
137 })
138 }
139 }