Skip to content

Commit fb138b5

Browse files
authored
[chore] Make additional config validation tests more resilient (#37594)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Follow up to #37579. This should address issues with the remaining tests.
1 parent 44eb293 commit fb138b5

File tree

7 files changed

+60
-53
lines changed

7 files changed

+60
-53
lines changed

receiver/dockerstatsreceiver/config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func TestValidateErrors(t *testing.T) {
101101
cfg := &Config{ControllerConfig: scraperhelper.NewDefaultControllerConfig(), Config: docker.Config{
102102
DockerAPIVersion: "1.25",
103103
}}
104-
assert.Equal(t, "endpoint must be specified", component.ValidateConfig(cfg).Error())
104+
assert.ErrorContains(t, component.ValidateConfig(cfg), "endpoint must be specified")
105105

106106
cfg = &Config{
107107
Config: docker.Config{
@@ -110,7 +110,7 @@ func TestValidateErrors(t *testing.T) {
110110
},
111111
ControllerConfig: scraperhelper.ControllerConfig{CollectionInterval: 1 * time.Second},
112112
}
113-
assert.Equal(t, `"api_version" 1.21 must be at least 1.25`, component.ValidateConfig(cfg).Error())
113+
assert.ErrorContains(t, component.ValidateConfig(cfg), `"api_version" 1.21 must be at least 1.25`)
114114

115115
cfg = &Config{
116116
Config: docker.Config{
@@ -119,7 +119,7 @@ func TestValidateErrors(t *testing.T) {
119119
},
120120
ControllerConfig: scraperhelper.ControllerConfig{},
121121
}
122-
assert.Equal(t, `"collection_interval": requires positive value`, component.ValidateConfig(cfg).Error())
122+
assert.ErrorContains(t, component.ValidateConfig(cfg), `"collection_interval": requires positive value`)
123123
}
124124

125125
func TestApiVersionCustomError(t *testing.T) {

receiver/k8sclusterreceiver/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestInvalidConfig(t *testing.T) {
8585
}
8686
err := component.ValidateConfig(cfg)
8787
assert.Error(t, err)
88-
assert.Equal(t, "invalid authType for kubernetes: ", err.Error())
88+
assert.ErrorContains(t, err, "invalid authType for kubernetes: ")
8989

9090
// Wrong distro
9191
cfg = &Config{

receiver/podmanreceiver/config_test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ func TestLoadConfig(t *testing.T) {
2424
require.NoError(t, err)
2525

2626
tests := []struct {
27-
id component.ID
28-
expected component.Config
29-
expectedErrMsg string
27+
id component.ID
28+
expected component.Config
29+
expectedErrMsgs []string
3030
}{
3131
{
3232
id: component.NewIDWithName(metadata.Type, ""),
@@ -55,12 +55,12 @@ func TestLoadConfig(t *testing.T) {
5555
},
5656
},
5757
{
58-
id: component.NewIDWithName(metadata.Type, "empty_endpoint"),
59-
expectedErrMsg: "config.Endpoint must be specified",
58+
id: component.NewIDWithName(metadata.Type, "empty_endpoint"),
59+
expectedErrMsgs: []string{"config.Endpoint must be specified"},
6060
},
6161
{
62-
id: component.NewIDWithName(metadata.Type, "invalid_collection_interval"),
63-
expectedErrMsg: `config.CollectionInterval must be specified; "collection_interval": requires positive value`,
62+
id: component.NewIDWithName(metadata.Type, "invalid_collection_interval"),
63+
expectedErrMsgs: []string{`config.CollectionInterval must be specified`, `"collection_interval": requires positive value`},
6464
},
6565
}
6666

@@ -73,8 +73,10 @@ func TestLoadConfig(t *testing.T) {
7373
require.NoError(t, err)
7474
require.NoError(t, sub.Unmarshal(cfg))
7575

76-
if tt.expectedErrMsg != "" {
77-
assert.ErrorContains(t, component.ValidateConfig(cfg), tt.expectedErrMsg)
76+
if len(tt.expectedErrMsgs) > 0 {
77+
for _, msg := range tt.expectedErrMsgs {
78+
assert.ErrorContains(t, component.ValidateConfig(cfg), msg)
79+
}
7880
return
7981
}
8082

receiver/postgresqlreceiver/config_test.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"go.opentelemetry.io/collector/config/confignet"
1616
"go.opentelemetry.io/collector/config/configtls"
1717
"go.opentelemetry.io/collector/confmap/confmaptest"
18-
"go.uber.org/multierr"
1918

2019
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver/internal/metadata"
2120
)
@@ -24,33 +23,33 @@ func TestValidate(t *testing.T) {
2423
testCases := []struct {
2524
desc string
2625
defaultConfigModifier func(cfg *Config)
27-
expected error
26+
expected []error
2827
}{
2928
{
3029
desc: "missing username and password",
3130
defaultConfigModifier: func(*Config) {},
32-
expected: multierr.Combine(
31+
expected: []error{
3332
errors.New(ErrNoUsername),
3433
errors.New(ErrNoPassword),
35-
),
34+
},
3635
},
3736
{
3837
desc: "missing password",
3938
defaultConfigModifier: func(cfg *Config) {
4039
cfg.Username = "otel"
4140
},
42-
expected: multierr.Combine(
41+
expected: []error{
4342
errors.New(ErrNoPassword),
44-
),
43+
},
4544
},
4645
{
4746
desc: "missing username",
4847
defaultConfigModifier: func(cfg *Config) {
4948
cfg.Password = "otel"
5049
},
51-
expected: multierr.Combine(
50+
expected: []error{
5251
errors.New(ErrNoUsername),
53-
),
52+
},
5453
},
5554
{
5655
desc: "bad endpoint",
@@ -59,9 +58,9 @@ func TestValidate(t *testing.T) {
5958
cfg.Password = "otel"
6059
cfg.Endpoint = "open-telemetry"
6160
},
62-
expected: multierr.Combine(
61+
expected: []error{
6362
errors.New(ErrHostPort),
64-
),
63+
},
6564
},
6665
{
6766
desc: "bad transport",
@@ -70,9 +69,9 @@ func TestValidate(t *testing.T) {
7069
cfg.Password = "otel"
7170
cfg.Transport = "udp"
7271
},
73-
expected: multierr.Combine(
72+
expected: []error{
7473
errors.New(ErrTransportsSupported),
75-
),
74+
},
7675
},
7776
{
7877
desc: "unsupported SSL params",
@@ -83,11 +82,11 @@ func TestValidate(t *testing.T) {
8382
cfg.MinVersion = "1.0"
8483
cfg.MaxVersion = "1.0"
8584
},
86-
expected: multierr.Combine(
85+
expected: []error{
8786
fmt.Errorf(ErrNotSupported, "ServerName"),
8887
fmt.Errorf(ErrNotSupported, "MaxVersion"),
8988
fmt.Errorf(ErrNotSupported, "MinVersion"),
90-
),
89+
},
9190
},
9291
{
9392
desc: "no error",
@@ -104,7 +103,11 @@ func TestValidate(t *testing.T) {
104103
cfg := factory.CreateDefaultConfig().(*Config)
105104
tC.defaultConfigModifier(cfg)
106105
actual := component.ValidateConfig(cfg)
107-
require.Equal(t, tC.expected, actual)
106+
if len(tC.expected) > 0 {
107+
for _, err := range tC.expected {
108+
require.ErrorContains(t, actual, err.Error())
109+
}
110+
}
108111
})
109112
}
110113
}

receiver/prometheusreceiver/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func TestRejectUnsupportedPrometheusFeatures(t *testing.T) {
216216
rule_files`
217217

218218
gotErrMsg := strings.ReplaceAll(err.Error(), "\t", strings.Repeat(" ", 8))
219-
require.Equal(t, wantErrMsg, gotErrMsg)
219+
require.Contains(t, gotErrMsg, wantErrMsg)
220220
}
221221

222222
func TestNonExistentAuthCredentialsFile(t *testing.T) {

receiver/solacereceiver/config_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestLoadConfig(t *testing.T) {
7171
require.NoError(t, sub.Unmarshal(cfg))
7272

7373
if tt.expectedErr != nil {
74-
assert.ErrorIs(t, component.ValidateConfig(cfg), tt.expectedErr)
74+
assert.ErrorContains(t, component.ValidateConfig(cfg), tt.expectedErr.Error())
7575
return
7676
}
7777
assert.NoError(t, component.ValidateConfig(cfg))
@@ -84,7 +84,7 @@ func TestConfigValidateMissingAuth(t *testing.T) {
8484
cfg := createDefaultConfig().(*Config)
8585
cfg.Queue = "someQueue"
8686
err := component.ValidateConfig(cfg)
87-
assert.Equal(t, errMissingAuthDetails, err)
87+
assert.ErrorContains(t, err, errMissingAuthDetails.Error())
8888
}
8989

9090
func TestConfigValidateMultipleAuth(t *testing.T) {
@@ -93,14 +93,14 @@ func TestConfigValidateMultipleAuth(t *testing.T) {
9393
cfg.Auth.PlainText = &SaslPlainTextConfig{"Username", "Password"}
9494
cfg.Auth.XAuth2 = &SaslXAuth2Config{"Username", "Bearer"}
9595
err := component.ValidateConfig(cfg)
96-
assert.Equal(t, errTooManyAuthDetails, err)
96+
assert.ErrorContains(t, err, errTooManyAuthDetails.Error())
9797
}
9898

9999
func TestConfigValidateMissingQueue(t *testing.T) {
100100
cfg := createDefaultConfig().(*Config)
101101
cfg.Auth.PlainText = &SaslPlainTextConfig{"Username", "Password"}
102102
err := component.ValidateConfig(cfg)
103-
assert.Equal(t, errMissingQueueName, err)
103+
assert.ErrorContains(t, err, errMissingQueueName.Error())
104104
}
105105

106106
func TestConfigValidateMissingFlowControl(t *testing.T) {
@@ -110,7 +110,7 @@ func TestConfigValidateMissingFlowControl(t *testing.T) {
110110
// this should never happen in reality, test validation anyway
111111
cfg.Flow.DelayedRetry = nil
112112
err := cfg.Validate()
113-
assert.Equal(t, errMissingFlowControl, err)
113+
assert.ErrorContains(t, err, errMissingFlowControl.Error())
114114
}
115115

116116
func TestConfigValidateInvalidFlowControlDelayedRetryDelay(t *testing.T) {
@@ -121,7 +121,7 @@ func TestConfigValidateInvalidFlowControlDelayedRetryDelay(t *testing.T) {
121121
Delay: -30 * time.Second,
122122
}
123123
err := cfg.Validate()
124-
assert.Equal(t, errInvalidDelayedRetryDelay, err)
124+
assert.ErrorContains(t, err, errInvalidDelayedRetryDelay.Error())
125125
}
126126

127127
func TestConfigValidateSuccess(t *testing.T) {

receiver/windowsperfcountersreceiver/config_test.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ func TestLoadConfig(t *testing.T) {
4848
}
4949

5050
tests := []struct {
51-
id component.ID
52-
expected component.Config
53-
expectedErr string
51+
id component.ID
52+
expected component.Config
53+
expectedErrs []string
5454
}{
5555
{
5656
id: component.NewIDWithName(metadata.Type, ""),
@@ -180,34 +180,34 @@ func TestLoadConfig(t *testing.T) {
180180
},
181181
},
182182
{
183-
id: component.NewIDWithName(metadata.Type, "negative-collection-interval"),
184-
expectedErr: "collection_interval must be a positive duration; " + negativeCollectionIntervalErr,
183+
id: component.NewIDWithName(metadata.Type, "negative-collection-interval"),
184+
expectedErrs: []string{"collection_interval must be a positive duration", negativeCollectionIntervalErr},
185185
},
186186
{
187-
id: component.NewIDWithName(metadata.Type, "noperfcounters"),
188-
expectedErr: noPerfCountersErr,
187+
id: component.NewIDWithName(metadata.Type, "noperfcounters"),
188+
expectedErrs: []string{noPerfCountersErr},
189189
},
190190
{
191-
id: component.NewIDWithName(metadata.Type, "noobjectname"),
192-
expectedErr: noObjectNameErr,
191+
id: component.NewIDWithName(metadata.Type, "noobjectname"),
192+
expectedErrs: []string{noObjectNameErr},
193193
},
194194
{
195-
id: component.NewIDWithName(metadata.Type, "nocounters"),
196-
expectedErr: fmt.Sprintf(noCountersErr, "object"),
195+
id: component.NewIDWithName(metadata.Type, "nocounters"),
196+
expectedErrs: []string{fmt.Sprintf(noCountersErr, "object")},
197197
},
198198
{
199199
id: component.NewIDWithName(metadata.Type, "allerrors"),
200-
expectedErr: fmt.Sprintf(
201-
"collection_interval must be a positive duration; %s; %s; %s; %s",
200+
expectedErrs: []string{
201+
"collection_interval must be a positive duration",
202202
fmt.Sprintf(noCountersErr, "object"),
203203
fmt.Sprintf(emptyInstanceErr, "object"),
204204
noObjectNameErr,
205205
negativeCollectionIntervalErr,
206-
),
206+
},
207207
},
208208
{
209-
id: component.NewIDWithName(metadata.Type, "emptyinstance"),
210-
expectedErr: fmt.Sprintf(emptyInstanceErr, "object"),
209+
id: component.NewIDWithName(metadata.Type, "emptyinstance"),
210+
expectedErrs: []string{fmt.Sprintf(emptyInstanceErr, "object")},
211211
},
212212
}
213213

@@ -220,8 +220,10 @@ func TestLoadConfig(t *testing.T) {
220220
require.NoError(t, err)
221221
require.NoError(t, sub.Unmarshal(cfg))
222222

223-
if tt.expectedErr != "" {
224-
assert.Equal(t, tt.expectedErr, component.ValidateConfig(cfg).Error())
223+
if len(tt.expectedErrs) > 0 {
224+
for _, err := range tt.expectedErrs {
225+
assert.ErrorContains(t, component.ValidateConfig(cfg), err)
226+
}
225227
return
226228
}
227229
assert.NoError(t, component.ValidateConfig(cfg))

0 commit comments

Comments
 (0)