Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/node_exporter/config_test.go
5318 views
1
package node_exporter //nolint:golint
2
3
import (
4
"fmt"
5
"testing"
6
7
"github.com/stretchr/testify/require"
8
"gopkg.in/yaml.v2"
9
)
10
11
func TestNodeExporter_Config(t *testing.T) {
12
var c Config
13
14
err := yaml.Unmarshal([]byte("{}"), &c)
15
require.NoError(t, err)
16
require.Equal(t, DefaultConfig, c)
17
}
18
19
func TestNodeExporter_ConfigMigrate(t *testing.T) {
20
tt := []struct {
21
name string
22
in string
23
expectError string
24
expectWarnings []string
25
check func(t *testing.T, c *Config)
26
}{
27
{
28
name: "old fields migrate",
29
in: `
30
netdev_device_whitelist: netdev_wl
31
netdev_device_blacklist: netdev_bl
32
systemd_unit_whitelist: systemd_wl
33
systemd_unit_blacklist: systemd_bl
34
filesystem_ignored_mount_points: fs_mp
35
filesystem_ignored_fs_types: fs_types
36
diskstats_ignored_devices: diskstats_exclude
37
`,
38
expectWarnings: []string{
39
`"netdev_device_whitelist" is deprecated by "netdev_device_include" and will be removed in a future version`,
40
`"netdev_device_blacklist" is deprecated by "netdev_device_exclude" and will be removed in a future version`,
41
`"systemd_unit_whitelist" is deprecated by "systemd_unit_include" and will be removed in a future version`,
42
`"systemd_unit_blacklist" is deprecated by "systemd_unit_exclude" and will be removed in a future version`,
43
`"filesystem_ignored_mount_points" is deprecated by "filesystem_mount_points_exclude" and will be removed in a future version`,
44
`"filesystem_ignored_fs_types" is deprecated by "filesystem_fs_types_exclude" and will be removed in a future version`,
45
`"diskstats_ignored_devices" is deprecated by "diskstats_device_exclude" and will be removed in a future version`,
46
},
47
check: func(t *testing.T, c *Config) {
48
t.Helper()
49
50
require.Equal(t, c.NetdevDeviceInclude, "netdev_wl")
51
require.Equal(t, c.NetdevDeviceExclude, "netdev_bl")
52
require.Equal(t, c.SystemdUnitInclude, "systemd_wl")
53
require.Equal(t, c.SystemdUnitExclude, "systemd_bl")
54
require.Equal(t, c.FilesystemMountPointsExclude, "fs_mp")
55
require.Equal(t, c.FilesystemFSTypesExclude, "fs_types")
56
require.Equal(t, c.DiskStatsDeviceExclude, "diskstats_exclude")
57
},
58
},
59
{
60
name: "new fields valid",
61
in: `
62
netdev_device_include: netdev_wl
63
netdev_device_exclude: netdev_bl
64
systemd_unit_include: systemd_wl
65
systemd_unit_exclude: systemd_bl
66
filesystem_mount_points_exclude: fs_mp
67
filesystem_fs_types_exclude: fs_types
68
diskstats_device_exclude: diskstats_exclude
69
`,
70
check: func(t *testing.T, c *Config) {
71
t.Helper()
72
73
require.Equal(t, c.NetdevDeviceInclude, "netdev_wl")
74
require.Equal(t, c.NetdevDeviceExclude, "netdev_bl")
75
require.Equal(t, c.SystemdUnitInclude, "systemd_wl")
76
require.Equal(t, c.SystemdUnitExclude, "systemd_bl")
77
require.Equal(t, c.FilesystemMountPointsExclude, "fs_mp")
78
require.Equal(t, c.FilesystemFSTypesExclude, "fs_types")
79
require.Equal(t, c.DiskStatsDeviceExclude, "diskstats_exclude")
80
},
81
},
82
{
83
name: `netdev_device_whitelist and netdev_device_include`,
84
in: illegalConfig("netdev_device_whitelist", "netdev_device_include"),
85
expectError: `only one of "netdev_device_whitelist" and "netdev_device_include" may be specified`,
86
},
87
{
88
name: `netdev_device_blacklist and netdev_device_exclude`,
89
in: illegalConfig("netdev_device_blacklist", "netdev_device_exclude"),
90
expectError: `only one of "netdev_device_blacklist" and "netdev_device_exclude" may be specified`,
91
},
92
{
93
name: `systemd_unit_whitelist and systemd_unit_include`,
94
in: illegalConfig("systemd_unit_whitelist", "systemd_unit_include"),
95
expectError: `only one of "systemd_unit_whitelist" and "systemd_unit_include" may be specified`,
96
},
97
{
98
name: `systemd_unit_blacklist and systemd_unit_exclude`,
99
in: illegalConfig("systemd_unit_blacklist", "systemd_unit_exclude"),
100
expectError: `only one of "systemd_unit_blacklist" and "systemd_unit_exclude" may be specified`,
101
},
102
{
103
name: `filesystem_ignored_mount_points and filesystem_mount_points_exclude`,
104
in: illegalConfig("filesystem_ignored_mount_points", "filesystem_mount_points_exclude"),
105
expectError: `only one of "filesystem_ignored_mount_points" and "filesystem_mount_points_exclude" may be specified`,
106
},
107
{
108
name: `filesystem_ignored_fs_types and filesystem_fs_types_exclude`,
109
in: illegalConfig("filesystem_ignored_fs_types", "filesystem_fs_types_exclude"),
110
expectError: `only one of "filesystem_ignored_fs_types" and "filesystem_fs_types_exclude" may be specified`,
111
},
112
{
113
name: `diskstats_ignored_devices and diskstats_device_exclude`,
114
in: illegalConfig("diskstats_ignored_devices", "diskstats_device_exclude"),
115
expectError: `only one of "diskstats_ignored_devices" and "diskstats_device_exclude" may be specified`,
116
},
117
}
118
119
for _, tc := range tt {
120
t.Run(tc.name, func(t *testing.T) {
121
var c Config
122
123
err := yaml.Unmarshal([]byte(tc.in), &c)
124
if tc.expectError != "" {
125
require.EqualError(t, err, tc.expectError)
126
return
127
}
128
require.NoError(t, err)
129
130
require.ElementsMatch(t, tc.expectWarnings, c.UnmarshalWarnings)
131
if tc.check != nil {
132
tc.check(t, &c)
133
}
134
})
135
}
136
}
137
138
func illegalConfig(oldName, newName string) string {
139
return fmt.Sprintf(`
140
%s: illegal
141
%s: illegal
142
`, oldName, newName)
143
}
144
145