Skip to content

Commit

Permalink
[chore] relax checks on confmap validation (#34068)
Browse files Browse the repository at this point in the history
**Description:** <Describe what has changed.>
Relax checks on confmap validation messages to avoid running into
conflicts with mapstructure upgrades

See open-telemetry/opentelemetry-collector#10610
- tests don't pass with this upgrade of the mapstructure lib.
  • Loading branch information
atoulme committed Jul 15, 2024
1 parent a16cc64 commit e2293ba
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 24 deletions.
18 changes: 13 additions & 5 deletions exporter/datadogexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func TestUnmarshal(t *testing.T) {
configMap *confmap.Conf
cfg *Config
err string
field string
}{
{
name: "invalid cumulative monotonic mode",
Expand All @@ -206,7 +207,8 @@ func TestUnmarshal(t *testing.T) {
},
},
}),
err: "1 error(s) decoding:\n\n* error decoding 'metrics.sums.cumulative_monotonic_mode': invalid cumulative monotonic sum mode \"invalid_mode\"",
err: "invalid cumulative monotonic sum mode \"invalid_mode\"",
field: "metrics.sums.cumulative_monotonic_mode",
},
{
name: "invalid host metadata hostname source",
Expand All @@ -215,7 +217,8 @@ func TestUnmarshal(t *testing.T) {
"hostname_source": "invalid_source",
},
}),
err: "1 error(s) decoding:\n\n* error decoding 'host_metadata.hostname_source': invalid host metadata hostname source \"invalid_source\"",
err: "invalid host metadata hostname source \"invalid_source\"",
field: "host_metadata.hostname_source",
},
{
name: "invalid summary mode",
Expand All @@ -226,7 +229,8 @@ func TestUnmarshal(t *testing.T) {
},
},
}),
err: "1 error(s) decoding:\n\n* error decoding 'metrics.summaries.mode': invalid summary mode \"invalid_mode\"",
err: "invalid summary mode \"invalid_mode\"",
field: "metrics.summaries.mode",
},
{
name: "metrics::send_monotonic_counter custom error",
Expand Down Expand Up @@ -312,7 +316,8 @@ func TestUnmarshal(t *testing.T) {
},
},
}),
err: "1 error(s) decoding:\n\n* error decoding 'metrics.sums.initial_cumulative_monotonic_value': invalid initial value mode \"invalid_mode\"",
err: "invalid initial value mode \"invalid_mode\"",
field: "metrics.sums.initial_cumulative_monotonic_value",
},
{
name: "initial cumulative monotonic value mode set with raw_value",
Expand Down Expand Up @@ -349,7 +354,10 @@ func TestUnmarshal(t *testing.T) {
cfg := f.CreateDefaultConfig().(*Config)
err := cfg.Unmarshal(testInstance.configMap)
if err != nil || testInstance.err != "" {
assert.EqualError(t, err, testInstance.err)
assert.ErrorContains(t, err, testInstance.err)
if testInstance.field != "" {
assert.ErrorContains(t, err, testInstance.field)
}
} else {
assert.Equal(t, testInstance.cfg, cfg)
}
Expand Down
5 changes: 1 addition & 4 deletions exporter/datasetexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ func TestConfigUnmarshalUnknownAttributes(t *testing.T) {
})
err := configMap.Unmarshal(config)

unmarshalErr := fmt.Errorf("1 error(s) decoding:\n\n* '' has invalid keys: unknown_attribute")
expectedError := fmt.Errorf("cannot unmarshal config: %w", unmarshalErr)

assert.Equal(t, expectedError.Error(), err.Error())
assert.ErrorContains(t, err, "has invalid keys: unknown_attribute")
}

func TestConfigUseDefaults(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions exporter/logicmonitorexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestUnmarshal(t *testing.T) {
"resource_mapping_op": "invalid_op",
},
}),
err: "1 error(s) decoding:\n\n* error decoding 'logs.resource_mapping_op': unsupported mapping operation \"invalid_op\"",
err: "'logs.resource_mapping_op': unsupported mapping operation \"invalid_op\"",
},
}

Expand All @@ -183,7 +183,7 @@ func TestUnmarshal(t *testing.T) {
cfg := f.CreateDefaultConfig().(*Config)
err := tt.configMap.Unmarshal(cfg)
if err != nil || tt.err != "" {
assert.EqualError(t, err, tt.err)
assert.ErrorContains(t, err, tt.err)
} else {
assert.Equal(t, tt.cfg, cfg)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ property_value: '!/property.value/'`,
{
name: "invalid regex",
yaml: "dimension_name: '/(?=not.in.re2)/'",
expectedError: "1 error(s) decoding:\n\n* error decoding 'dimension_name': error parsing regexp: invalid or unsupported Perl syntax: `(?=`",
expectedError: "'dimension_name': error parsing regexp: invalid or unsupported Perl syntax: `(?=`",
},
{
name: "invalid glob",
yaml: "dimension_value: '*[c-a]'",
expectedError: "1 error(s) decoding:\n\n* error decoding 'dimension_value': hi character 'a' should be greater than lo 'c'",
expectedError: "'dimension_value': hi character 'a' should be greater than lo 'c'",
},
} {
t.Run(test.name, func(t *testing.T) {
Expand All @@ -74,7 +74,7 @@ property_value: '!/property.value/'`,
pf := &PropertyFilter{}
err = cm.Unmarshal(pf)
if test.expectedError != "" {
require.EqualError(t, err, test.expectedError)
require.ErrorContains(t, err, test.expectedError)
} else {
require.NoError(t, err)
require.Equal(t, test.expectedPropertyFilter, *pf)
Expand Down
4 changes: 2 additions & 2 deletions receiver/jaegerreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ func TestFailedLoadConfig(t *testing.T) {
sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "typo_default_proto_config").String())
require.NoError(t, err)
err = sub.Unmarshal(cfg)
assert.EqualError(t, err, "1 error(s) decoding:\n\n* 'protocols' has invalid keys: thrift_htttp")
assert.ErrorContains(t, err, "'protocols' has invalid keys: thrift_htttp")

sub, err = cm.Sub(component.NewIDWithName(metadata.Type, "bad_proto_config").String())
require.NoError(t, err)
err = sub.Unmarshal(cfg)
assert.EqualError(t, err, "1 error(s) decoding:\n\n* 'protocols' has invalid keys: thrift_htttp")
assert.ErrorContains(t, err, "'protocols' has invalid keys: thrift_htttp")

sub, err = cm.Sub(component.NewIDWithName(metadata.Type, "empty").String())
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions receiver/otelarrowreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ func TestUnmarshalConfigTypoDefaultProtocol(t *testing.T) {
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.EqualError(t, cm.Unmarshal(cfg), "1 error(s) decoding:\n\n* 'protocols' has invalid keys: htttp")
assert.ErrorContains(t, cm.Unmarshal(cfg), "'protocols' has invalid keys: htttp")
}

func TestUnmarshalConfigInvalidProtocol(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "bad_proto_config.yaml"))
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.EqualError(t, cm.Unmarshal(cfg), "1 error(s) decoding:\n\n* 'protocols' has invalid keys: thrift")
assert.ErrorContains(t, cm.Unmarshal(cfg), "'protocols' has invalid keys: thrift")
}

func TestUnmarshalConfigNoProtocols(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions receiver/receivercreator/observerhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestOnAddForMetrics(t *testing.T) {
name: "inherits unsupported endpoint",
receiverTemplateID: component.MustNewIDWithName("without_endpoint", "some.name"),
receiverTemplateConfig: userConfigMap{"endpoint": "unsupported.endpoint"},
expectedError: "failed to load \"without_endpoint/some.name\" template config: 1 error(s) decoding:\n\n* '' has invalid keys: endpoint",
expectedError: "'' has invalid keys: endpoint",
},
} {
t.Run(test.name, func(t *testing.T) {
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestOnAddForMetrics(t *testing.T) {
if test.expectedError != "" {
assert.Equal(t, 0, handler.receiversByEndpointID.Size())
require.Error(t, mr.lastError)
require.EqualError(t, mr.lastError, test.expectedError)
require.ErrorContains(t, mr.lastError, test.expectedError)
require.Nil(t, mr.startedComponent)
return
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestOnAddForLogs(t *testing.T) {
name: "inherits unsupported endpoint",
receiverTemplateID: component.MustNewIDWithName("without_endpoint", "some.name"),
receiverTemplateConfig: userConfigMap{"endpoint": "unsupported.endpoint"},
expectedError: "failed to load \"without_endpoint/some.name\" template config: 1 error(s) decoding:\n\n* '' has invalid keys: endpoint",
expectedError: "'' has invalid keys: endpoint",
},
} {
t.Run(test.name, func(t *testing.T) {
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestOnAddForLogs(t *testing.T) {
if test.expectedError != "" {
assert.Equal(t, 0, handler.receiversByEndpointID.Size())
require.Error(t, mr.lastError)
require.EqualError(t, mr.lastError, test.expectedError)
require.ErrorContains(t, mr.lastError, test.expectedError)
require.Nil(t, mr.startedComponent)
return
}
Expand Down Expand Up @@ -266,7 +266,7 @@ func TestOnAddForTraces(t *testing.T) {
name: "inherits unsupported endpoint",
receiverTemplateID: component.MustNewIDWithName("without_endpoint", "some.name"),
receiverTemplateConfig: userConfigMap{"endpoint": "unsupported.endpoint"},
expectedError: "failed to load \"without_endpoint/some.name\" template config: 1 error(s) decoding:\n\n* '' has invalid keys: endpoint",
expectedError: "'' has invalid keys: endpoint",
},
} {
t.Run(test.name, func(t *testing.T) {
Expand Down Expand Up @@ -294,7 +294,7 @@ func TestOnAddForTraces(t *testing.T) {
if test.expectedError != "" {
assert.Equal(t, 0, handler.receiversByEndpointID.Size())
require.Error(t, mr.lastError)
require.EqualError(t, mr.lastError, test.expectedError)
require.ErrorContains(t, mr.lastError, test.expectedError)
require.Nil(t, mr.startedComponent)
return
}
Expand Down

0 comments on commit e2293ba

Please sign in to comment.