Path: blob/main/pkg/integrations/node_exporter/config_test.go
5318 views
package node_exporter //nolint:golint12import (3"fmt"4"testing"56"github.com/stretchr/testify/require"7"gopkg.in/yaml.v2"8)910func TestNodeExporter_Config(t *testing.T) {11var c Config1213err := yaml.Unmarshal([]byte("{}"), &c)14require.NoError(t, err)15require.Equal(t, DefaultConfig, c)16}1718func TestNodeExporter_ConfigMigrate(t *testing.T) {19tt := []struct {20name string21in string22expectError string23expectWarnings []string24check func(t *testing.T, c *Config)25}{26{27name: "old fields migrate",28in: `29netdev_device_whitelist: netdev_wl30netdev_device_blacklist: netdev_bl31systemd_unit_whitelist: systemd_wl32systemd_unit_blacklist: systemd_bl33filesystem_ignored_mount_points: fs_mp34filesystem_ignored_fs_types: fs_types35diskstats_ignored_devices: diskstats_exclude36`,37expectWarnings: []string{38`"netdev_device_whitelist" is deprecated by "netdev_device_include" and will be removed in a future version`,39`"netdev_device_blacklist" is deprecated by "netdev_device_exclude" and will be removed in a future version`,40`"systemd_unit_whitelist" is deprecated by "systemd_unit_include" and will be removed in a future version`,41`"systemd_unit_blacklist" is deprecated by "systemd_unit_exclude" and will be removed in a future version`,42`"filesystem_ignored_mount_points" is deprecated by "filesystem_mount_points_exclude" and will be removed in a future version`,43`"filesystem_ignored_fs_types" is deprecated by "filesystem_fs_types_exclude" and will be removed in a future version`,44`"diskstats_ignored_devices" is deprecated by "diskstats_device_exclude" and will be removed in a future version`,45},46check: func(t *testing.T, c *Config) {47t.Helper()4849require.Equal(t, c.NetdevDeviceInclude, "netdev_wl")50require.Equal(t, c.NetdevDeviceExclude, "netdev_bl")51require.Equal(t, c.SystemdUnitInclude, "systemd_wl")52require.Equal(t, c.SystemdUnitExclude, "systemd_bl")53require.Equal(t, c.FilesystemMountPointsExclude, "fs_mp")54require.Equal(t, c.FilesystemFSTypesExclude, "fs_types")55require.Equal(t, c.DiskStatsDeviceExclude, "diskstats_exclude")56},57},58{59name: "new fields valid",60in: `61netdev_device_include: netdev_wl62netdev_device_exclude: netdev_bl63systemd_unit_include: systemd_wl64systemd_unit_exclude: systemd_bl65filesystem_mount_points_exclude: fs_mp66filesystem_fs_types_exclude: fs_types67diskstats_device_exclude: diskstats_exclude68`,69check: func(t *testing.T, c *Config) {70t.Helper()7172require.Equal(t, c.NetdevDeviceInclude, "netdev_wl")73require.Equal(t, c.NetdevDeviceExclude, "netdev_bl")74require.Equal(t, c.SystemdUnitInclude, "systemd_wl")75require.Equal(t, c.SystemdUnitExclude, "systemd_bl")76require.Equal(t, c.FilesystemMountPointsExclude, "fs_mp")77require.Equal(t, c.FilesystemFSTypesExclude, "fs_types")78require.Equal(t, c.DiskStatsDeviceExclude, "diskstats_exclude")79},80},81{82name: `netdev_device_whitelist and netdev_device_include`,83in: illegalConfig("netdev_device_whitelist", "netdev_device_include"),84expectError: `only one of "netdev_device_whitelist" and "netdev_device_include" may be specified`,85},86{87name: `netdev_device_blacklist and netdev_device_exclude`,88in: illegalConfig("netdev_device_blacklist", "netdev_device_exclude"),89expectError: `only one of "netdev_device_blacklist" and "netdev_device_exclude" may be specified`,90},91{92name: `systemd_unit_whitelist and systemd_unit_include`,93in: illegalConfig("systemd_unit_whitelist", "systemd_unit_include"),94expectError: `only one of "systemd_unit_whitelist" and "systemd_unit_include" may be specified`,95},96{97name: `systemd_unit_blacklist and systemd_unit_exclude`,98in: illegalConfig("systemd_unit_blacklist", "systemd_unit_exclude"),99expectError: `only one of "systemd_unit_blacklist" and "systemd_unit_exclude" may be specified`,100},101{102name: `filesystem_ignored_mount_points and filesystem_mount_points_exclude`,103in: illegalConfig("filesystem_ignored_mount_points", "filesystem_mount_points_exclude"),104expectError: `only one of "filesystem_ignored_mount_points" and "filesystem_mount_points_exclude" may be specified`,105},106{107name: `filesystem_ignored_fs_types and filesystem_fs_types_exclude`,108in: illegalConfig("filesystem_ignored_fs_types", "filesystem_fs_types_exclude"),109expectError: `only one of "filesystem_ignored_fs_types" and "filesystem_fs_types_exclude" may be specified`,110},111{112name: `diskstats_ignored_devices and diskstats_device_exclude`,113in: illegalConfig("diskstats_ignored_devices", "diskstats_device_exclude"),114expectError: `only one of "diskstats_ignored_devices" and "diskstats_device_exclude" may be specified`,115},116}117118for _, tc := range tt {119t.Run(tc.name, func(t *testing.T) {120var c Config121122err := yaml.Unmarshal([]byte(tc.in), &c)123if tc.expectError != "" {124require.EqualError(t, err, tc.expectError)125return126}127require.NoError(t, err)128129require.ElementsMatch(t, tc.expectWarnings, c.UnmarshalWarnings)130if tc.check != nil {131tc.check(t, &c)132}133})134}135}136137func illegalConfig(oldName, newName string) string {138return fmt.Sprintf(`139%s: illegal140%s: illegal141`, oldName, newName)142}143144145