summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/agent/discovery/file/read_test.go
blob: d2404d54e489e636e4e8c735d6aa92d5993fa6e9 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package file

import (
	"fmt"
	"testing"

	"github.com/netdata/netdata/go/go.d.plugin/agent/confgroup"
	"github.com/netdata/netdata/go/go.d.plugin/agent/module"

	"github.com/stretchr/testify/assert"
)

func TestReader_String(t *testing.T) {
	assert.NotEmpty(t, NewReader(confgroup.Registry{}, nil))
}

func TestNewReader(t *testing.T) {
	tests := map[string]struct {
		reg   confgroup.Registry
		paths []string
	}{
		"empty inputs": {
			reg:   confgroup.Registry{},
			paths: []string{},
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			assert.NotNil(t, NewReader(test.reg, test.paths))
		})
	}
}

func TestReader_Run(t *testing.T) {
	tests := map[string]struct {
		createSim func(tmp *tmpDir) discoverySim
	}{
		"read multiple files": {
			createSim: func(tmp *tmpDir) discoverySim {
				module1 := tmp.join("module1.conf")
				module2 := tmp.join("module2.conf")
				module3 := tmp.join("module3.conf")

				tmp.writeYAML(module1, staticConfig{
					Jobs: []confgroup.Config{{"name": "name"}},
				})
				tmp.writeYAML(module2, staticConfig{
					Jobs: []confgroup.Config{{"name": "name"}},
				})
				tmp.writeString(module3, "# a comment")

				reg := confgroup.Registry{
					"module1": {},
					"module2": {},
					"module3": {},
				}
				discovery := prepareDiscovery(t, Config{
					Registry: reg,
					Read:     []string{module1, module2, module3},
				})
				expected := []*confgroup.Group{
					{
						Source: module1,
						Configs: []confgroup.Config{
							{
								"name":                "name",
								"module":              "module1",
								"update_every":        module.UpdateEvery,
								"autodetection_retry": module.AutoDetectionRetry,
								"priority":            module.Priority,
								"__provider__":        "file reader",
								"__source_type__":     confgroup.TypeStock,
								"__source__":          fmt.Sprintf("discoverer=file_reader,file=%s", module1),
							},
						},
					},
					{
						Source: module2,
						Configs: []confgroup.Config{
							{
								"name":                "name",
								"module":              "module2",
								"update_every":        module.UpdateEvery,
								"autodetection_retry": module.AutoDetectionRetry,
								"priority":            module.Priority,
								"__provider__":        "file reader",
								"__source_type__":     confgroup.TypeStock,
								"__source__":          fmt.Sprintf("discoverer=file_reader,file=%s", module2),
							},
						},
					},
					{
						Source: module3,
					},
				}

				return discoverySim{
					discovery:      discovery,
					expectedGroups: expected,
				}
			},
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			tmp := newTmpDir(t, "reader-run-*")
			defer tmp.cleanup()

			test.createSim(tmp).run(t)
		})
	}
}