From 76738824c37b2e19809b6093e8397b0127ff04da Mon Sep 17 00:00:00 2001 From: Paulin Todev Date: Wed, 11 Sep 2024 18:37:45 +0100 Subject: [PATCH] Fix broken unit tests (#1668) --- .../prometheus/prometheus.exporter.windows.md | 2 +- .../exporter/windows/config_windows.go | 7 ++-- .../internal/build/windows_exporter.go | 21 +++++++--- .../internal/build/windows_exporter_test.go | 38 +++++++++++++++++++ 4 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 internal/converter/internal/staticconvert/internal/build/windows_exporter_test.go diff --git a/docs/sources/reference/components/prometheus/prometheus.exporter.windows.md b/docs/sources/reference/components/prometheus/prometheus.exporter.windows.md index 9d4be3308e..03dce365c5 100644 --- a/docs/sources/reference/components/prometheus/prometheus.exporter.windows.md +++ b/docs/sources/reference/components/prometheus/prometheus.exporter.windows.md @@ -75,7 +75,7 @@ text_file | [text_file][] | Configures the text_file collector. | [scheduled_task]: #scheduledtask-block [service]: #service-block [smb]: #smb-block -[smb_client]: #smbclient-block +[smb_client]: #smb_client-block [smtp]: #smtp-block [text_file]: #textfile-block diff --git a/internal/component/prometheus/exporter/windows/config_windows.go b/internal/component/prometheus/exporter/windows/config_windows.go index 21ef692a7c..5a995855e2 100644 --- a/internal/component/prometheus/exporter/windows/config_windows.go +++ b/internal/component/prometheus/exporter/windows/config_windows.go @@ -1,6 +1,7 @@ package windows import ( + "slices" "strconv" "strings" @@ -13,10 +14,10 @@ func (a *Arguments) SetToDefault() { *a = Arguments{ EnabledCollectors: strings.Split(windows_integration.DefaultConfig.EnabledCollectors, ","), Dfsr: DfsrConfig{ - SourcesEnabled: col.ConfigDefaults.DFSR.CollectorsEnabled, + SourcesEnabled: slices.Clone(col.ConfigDefaults.DFSR.CollectorsEnabled), }, Exchange: ExchangeConfig{ - EnabledList: col.ConfigDefaults.Exchange.CollectorsEnabled, + EnabledList: slices.Clone(col.ConfigDefaults.Exchange.CollectorsEnabled), }, IIS: IISConfig{ AppBlackList: col.ConfigDefaults.IIS.AppExclude.String(), @@ -38,7 +39,7 @@ func (a *Arguments) SetToDefault() { Where: *col.ConfigDefaults.Msmq.QueryWhereClause, }, MSSQL: MSSQLConfig{ - EnabledClasses: col.ConfigDefaults.Mssql.CollectorsEnabled, + EnabledClasses: slices.Clone(col.ConfigDefaults.Mssql.CollectorsEnabled), }, Network: NetworkConfig{ BlackList: col.ConfigDefaults.Net.NicExclude.String(), diff --git a/internal/converter/internal/staticconvert/internal/build/windows_exporter.go b/internal/converter/internal/staticconvert/internal/build/windows_exporter.go index 8c877d06f9..7f701184c1 100644 --- a/internal/converter/internal/staticconvert/internal/build/windows_exporter.go +++ b/internal/converter/internal/staticconvert/internal/build/windows_exporter.go @@ -13,14 +13,23 @@ func (b *ConfigBuilder) appendWindowsExporter(config *windows_exporter.Config, i return b.appendExporterBlock(args, config.Name(), instanceKey, "windows") } +// Splits a string such as "example1,example2" +// into a list such as []string{"example1", "example2"}. +func split(collectorList string) []string { + if collectorList == "" { + return []string{} + } + return strings.Split(collectorList, ",") +} + func toWindowsExporter(config *windows_exporter.Config) *windows.Arguments { return &windows.Arguments{ - EnabledCollectors: strings.Split(config.EnabledCollectors, ","), + EnabledCollectors: split(config.EnabledCollectors), Dfsr: windows.DfsrConfig{ - SourcesEnabled: strings.Split(config.Dfsr.SourcesEnabled, ","), + SourcesEnabled: split(config.Dfsr.SourcesEnabled), }, Exchange: windows.ExchangeConfig{ - EnabledList: strings.Split(config.Exchange.EnabledList, ","), + EnabledList: split(config.Exchange.EnabledList), }, IIS: windows.IISConfig{ AppBlackList: config.IIS.AppBlackList, @@ -42,7 +51,7 @@ func toWindowsExporter(config *windows_exporter.Config) *windows.Arguments { Where: config.MSMQ.Where, }, MSSQL: windows.MSSQLConfig{ - EnabledClasses: strings.Split(config.MSSQL.EnabledClasses, ","), + EnabledClasses: split(config.MSSQL.EnabledClasses), }, Network: windows.NetworkConfig{ BlackList: config.Network.BlackList, @@ -73,10 +82,10 @@ func toWindowsExporter(config *windows_exporter.Config) *windows.Arguments { Where: config.Service.Where, }, SMB: windows.SMBConfig{ - EnabledList: strings.Split(config.SMB.EnabledList, ","), + EnabledList: split(config.SMB.EnabledList), }, SMBClient: windows.SMBClientConfig{ - EnabledList: strings.Split(config.SMBClient.EnabledList, ","), + EnabledList: split(config.SMBClient.EnabledList), }, SMTP: windows.SMTPConfig{ BlackList: config.SMTP.BlackList, diff --git a/internal/converter/internal/staticconvert/internal/build/windows_exporter_test.go b/internal/converter/internal/staticconvert/internal/build/windows_exporter_test.go new file mode 100644 index 0000000000..8afa454f50 --- /dev/null +++ b/internal/converter/internal/staticconvert/internal/build/windows_exporter_test.go @@ -0,0 +1,38 @@ +package build + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_split(t *testing.T) { + tests := []struct { + testName string + input string + expectedOutput []string + }{ + { + testName: "multiple values", + input: "example1,example2", + expectedOutput: []string{"example1", "example2"}, + }, + { + testName: "single value", + input: "example1", + expectedOutput: []string{"example1"}, + }, + { + testName: "empty string", + input: "", + expectedOutput: []string{}, + }, + } + + for _, tt := range tests { + t.Run(tt.testName, func(t *testing.T) { + got := split(tt.input) + require.Equal(t, tt.expectedOutput, got) + }) + } +}