Skip to content

Commit

Permalink
Fix broken unit tests (#1668) (#1669)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptodev committed Sep 11, 2024
1 parent 9cb05b9 commit 1346625
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package windows

import (
"slices"
"strconv"
"strings"

Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit 1346625

Please sign in to comment.