Skip to content

[componenttest] Move CheckConfigStruct to confmaptest #12580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/deprecate-componenttest-checkconfigstruct.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: componenttest

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate `componenttest.CheckConfigStruct`

# One or more tracking issues or pull requests related to the change
issues: [12580]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: Please use `confmaptest.CheckConfigStruct` instead.

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
25 changes: 25 additions & 0 deletions .chloggen/move-componenttest-checkconfigstruct.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confmaptest

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `CheckConfigStruct` from `componenttest`

# One or more tracking issues or pull requests related to the change
issues: [12580]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: This function validates struct field tags on config structs to ensure confmap can effectively unmarshal them.

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/mdatagen/internal/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestComponentFactoryType(t *testing.T) {
}

func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
require.NoError(t, confmaptest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}

{{ if not (and .Tests.SkipLifecycle .Tests.SkipShutdown) -}}
Expand Down
2 changes: 1 addition & 1 deletion component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ type Factory interface {
// configuration and should not cause side effects that prevent the creation
// of multiple instances of the Component.
// The object returned by this method needs to pass the checks implemented by
// 'componenttest.CheckConfigStruct'. It is recommended to have these checks in the
// 'confmaptest.CheckConfigStruct'. It is recommended to have these checks in the
// tests of any implementation of the Factory interface.
CreateDefaultConfig() Config
}
Expand Down
1 change: 1 addition & 0 deletions component/componenttest/configtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var configFieldTagRegExp = regexp.MustCompile("^[a-z0-9][a-z0-9_]*$")
// of components and extensions. It is recommended for implementers of components
// to call this function on their tests passing the default configuration of the
// component factory.
// Deprecated: [v0.122.0] Use confmaptest.CheckConfigStruct instead.
func CheckConfigStruct(config any) error {
Comment on lines +23 to 24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Deprecated: [v0.122.0] Use confmaptest.CheckConfigStruct instead.
func CheckConfigStruct(config any) error {
// Deprecated: [v0.122.0] Use confmaptest.CheckConfigStruct instead.
var CheckConfigStruct = confmaptest.CheckConfigStruct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea, I'll do that.

t := reflect.TypeOf(config)
if t.Kind() == reflect.Ptr {
Expand Down
2 changes: 1 addition & 1 deletion component/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ package component // import "go.opentelemetry.io/collector/component"
// MUST implement xconfmap.Validator if any validation is required for that part of the configuration
// (e.g. check if a required field is present).
//
// A valid implementation MUST pass the check componenttest.CheckConfigStruct (return nil error).
// A valid implementation MUST pass the check confmaptest.CheckConfigStruct (return nil error).
type Config any
127 changes: 127 additions & 0 deletions confmap/confmaptest/configtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
"fmt"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"

"go.uber.org/multierr"
"gopkg.in/yaml.v3"

"go.opentelemetry.io/collector/confmap"
)

// The regular expression for valid config field tag.
var configFieldTagRegExp = regexp.MustCompile("^[a-z0-9][a-z0-9_]*$")

// LoadConf loads a confmap.Conf from file, and does NOT validate the configuration.
func LoadConf(fileName string) (*confmap.Conf, error) {
// Clean the path before using it.
Expand Down Expand Up @@ -47,3 +53,124 @@

return nil
}

// CheckConfigStruct enforces that given configuration object is following the patterns
// used by the collector. This ensures consistency between different implementations
// of components and extensions. It is recommended for implementers of components
// to call this function on their tests passing the default configuration of the
// component factory.
func CheckConfigStruct(config any) error {
t := reflect.TypeOf(config)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}

if t.Kind() != reflect.Struct {
return fmt.Errorf("config must be a struct or a pointer to one, the passed object is a %s", t.Kind())
}

return validateConfigDataType(t)
}

// validateConfigDataType performs a descending validation of the given type.
// If the type is a struct it goes to each of its fields to check for the proper
// tags.
func validateConfigDataType(t reflect.Type) error {
var errs error

switch t.Kind() {
case reflect.Ptr:
errs = multierr.Append(errs, validateConfigDataType(t.Elem()))
case reflect.Struct:
// Reflect on the pointed data and check each of its fields.
nf := t.NumField()
for i := 0; i < nf; i++ {
f := t.Field(i)
errs = multierr.Append(errs, checkStructFieldTags(f))
}
default:
// The config object can carry other types but they are not used when
// reading the configuration via koanf so ignore them. Basically ignore:
// reflect.Uintptr, reflect.Chan, reflect.Func, reflect.Interface, and
// reflect.UnsafePointer.
}

if errs != nil {
return fmt.Errorf("type %q from package %q has invalid config settings: %w", t.Name(), t.PkgPath(), errs)
}

return nil
}

// checkStructFieldTags inspects the tags of a struct field.
func checkStructFieldTags(f reflect.StructField) error {
tagValue := f.Tag.Get(confmap.MapstructureTag)
if tagValue == "" {
// Ignore special types.
switch f.Type.Kind() {
case reflect.Interface, reflect.Chan, reflect.Func, reflect.Uintptr, reflect.UnsafePointer:
// Allow the config to carry the types above, but since they are not read
// when loading configuration, just ignore them.
return nil
}

// Public fields of other types should be tagged.
chars := []byte(f.Name)
if len(chars) > 0 && chars[0] >= 'A' && chars[0] <= 'Z' {
return fmt.Errorf("mapstructure tag not present on field %q", f.Name)
}

// Not public field, no need to have a tag.
return nil
}

tagParts := strings.Split(tagValue, ",")
if tagParts[0] != "" {
if tagParts[0] == "-" {
// Nothing to do, as mapstructure decode skips this field.
return nil
}

Check warning on line 132 in confmap/confmaptest/configtest.go

View check run for this annotation

Codecov / codecov/patch

confmap/confmaptest/configtest.go#L130-L132

Added lines #L130 - L132 were not covered by tests
}

// Check if squash is specified.
squash := false
for _, tag := range tagParts[1:] {
if tag == "squash" {
squash = true
break
}
}

if squash {
// Field was squashed.
if (f.Type.Kind() != reflect.Struct) && (f.Type.Kind() != reflect.Ptr || f.Type.Elem().Kind() != reflect.Struct) {
return fmt.Errorf(
"attempt to squash non-struct type on field %q", f.Name)
}
}

switch f.Type.Kind() {
case reflect.Struct:
// It is another struct, continue down-level.
return validateConfigDataType(f.Type)

Check warning on line 155 in confmap/confmaptest/configtest.go

View check run for this annotation

Codecov / codecov/patch

confmap/confmaptest/configtest.go#L153-L155

Added lines #L153 - L155 were not covered by tests

case reflect.Map, reflect.Slice, reflect.Array:
// The element of map, array, or slice can be itself a configuration object.
return validateConfigDataType(f.Type.Elem())

default:
fieldTag := tagParts[0]
if !configFieldTagRegExp.MatchString(fieldTag) {
if f.Name == "AdditionalProperties" {
return nil
}

Check warning on line 166 in confmap/confmaptest/configtest.go

View check run for this annotation

Codecov / codecov/patch

confmap/confmaptest/configtest.go#L165-L166

Added lines #L165 - L166 were not covered by tests
return fmt.Errorf(
"field %q has config tag %q which doesn't satisfy %q",
f.Name,
fieldTag,
configFieldTagRegExp.String())
}
}

return nil
}
Loading
Loading