Skip to content

Commit

Permalink
[cmd/mdatagen] Move component config test from cmd/builder
Browse files Browse the repository at this point in the history
The generated tests are skipped in contrib because they cause the CI timeouts. We moved the lifecycle tests to the tests generated by mdatagen, but the config/factory smoke tests are still part of the files generated by cmd/builder.

Recently, loadbalancing exporter got an invalid camelCase config field because of this coverage gap.

This change moves the config/factory tests from cmd/builder to cmd/mdatagen. So they are always generated for every component even if they are not used in any collector bundle.
  • Loading branch information
dmitryax committed Apr 10, 2024
1 parent fd0d0da commit 2797e5f
Show file tree
Hide file tree
Showing 21 changed files with 153 additions and 63 deletions.
26 changes: 26 additions & 0 deletions .chloggen/mdatagen-introduce-config-factory-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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: cmd/mdatagen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Generate config and factory tests covering their requirements.

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

# (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: |
The tests are moved from cmd/builder.
# 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: []
1 change: 0 additions & 1 deletion cmd/builder/internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func Generate(cfg Config) error {
mainOthersTemplate,
mainWindowsTemplate,
componentsTemplate,
componentsTestTemplate,
goModTemplate,
} {
if err := processAndWrite(cfg, tmpl, tmpl.Name(), cfg); err != nil {
Expand Down
4 changes: 0 additions & 4 deletions cmd/builder/internal/builder/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ var (
componentsBytes []byte
componentsTemplate = parseTemplate("components.go", componentsBytes)

//go:embed templates/components_test.go.tmpl
componentsTestBytes []byte
componentsTestTemplate = parseTemplate("components_test.go", componentsTestBytes)

//go:embed templates/main.go.tmpl
mainBytes []byte
mainTemplate = parseTemplate("main.go", mainBytes)
Expand Down
39 changes: 0 additions & 39 deletions cmd/builder/internal/builder/templates/components_test.go.tmpl

This file was deleted.

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

8 changes: 3 additions & 5 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ func run(ymlPath string) error {
filepath.Join(codeDir, "generated_status.go"), md, "metadata"); err != nil {
return err
}
if !md.Tests.SkipLifecycle || !md.Tests.SkipShutdown {
if err = generateFile(filepath.Join(tmplDir, "component_test.go.tmpl"),
filepath.Join(ymlDir, "generated_component_test.go"), md, packageName); err != nil {
return err
}
if err = generateFile(filepath.Join(tmplDir, "component_test.go.tmpl"),
filepath.Join(ymlDir, "generated_component_test.go"), md, packageName); err != nil {
return err

Check warning on line 67 in cmd/mdatagen/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/mdatagen/main.go#L67

Added line #L67 was not covered by tests
}
}

Expand Down
18 changes: 4 additions & 14 deletions cmd/mdatagen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func TestRunContents(t *testing.T) {
wantMetricsGenerated bool
wantConfigGenerated bool
wantStatusGenerated bool
wantTestsGenerated bool
wantErr bool
}{
{
Expand All @@ -45,27 +44,22 @@ func TestRunContents(t *testing.T) {
},
{
yml: "with_tests_receiver.yaml",
wantTestsGenerated: true,
wantStatusGenerated: true,
},
{
yml: "with_tests_exporter.yaml",
wantTestsGenerated: true,
wantStatusGenerated: true,
},
{
yml: "with_tests_processor.yaml",
wantTestsGenerated: true,
wantStatusGenerated: true,
},
{
yml: "with_tests_extension.yaml",
wantTestsGenerated: true,
wantStatusGenerated: true,
},
{
yml: "with_tests_connector.yaml",
wantTestsGenerated: true,
wantStatusGenerated: true,
},
}
Expand Down Expand Up @@ -120,14 +114,10 @@ foo
require.Contains(t, string(contents), "foo")
}

if tt.wantTestsGenerated {
require.FileExists(t, filepath.Join(tmpdir, "generated_component_test.go"))
contents, err := os.ReadFile(filepath.Join(tmpdir, "generated_component_test.go")) // nolint: gosec
require.NoError(t, err)
require.Contains(t, string(contents), "func Test")
} else {
require.NoFileExists(t, filepath.Join(tmpdir, "generated_component_test.go"))
}
require.FileExists(t, filepath.Join(tmpdir, "generated_component_test.go"))
contents, err := os.ReadFile(filepath.Join(tmpdir, "generated_component_test.go")) // nolint: gosec
require.NoError(t, err)
require.Contains(t, string(contents), "func Test")
})
}
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/mdatagen/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ import (
{{- end }}
)

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

func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "{{ .Type }}", NewFactory().Type().String())
}

{{ if isExporter -}}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()
Expand Down
8 changes: 8 additions & 0 deletions connector/forwardconnector/generated_component_test.go

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

8 changes: 8 additions & 0 deletions exporter/debugexporter/generated_component_test.go

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

8 changes: 8 additions & 0 deletions exporter/loggingexporter/generated_component_test.go

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

8 changes: 8 additions & 0 deletions exporter/nopexporter/generated_component_test.go

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

8 changes: 8 additions & 0 deletions exporter/otlpexporter/generated_component_test.go

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

8 changes: 8 additions & 0 deletions exporter/otlphttpexporter/generated_component_test.go

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

8 changes: 8 additions & 0 deletions extension/ballastextension/generated_component_test.go

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

8 changes: 8 additions & 0 deletions extension/memorylimiterextension/generated_component_test.go

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

8 changes: 8 additions & 0 deletions extension/zpagesextension/generated_component_test.go

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

8 changes: 8 additions & 0 deletions processor/batchprocessor/generated_component_test.go

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

8 changes: 8 additions & 0 deletions processor/memorylimiterprocessor/generated_component_test.go

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

8 changes: 8 additions & 0 deletions receiver/nopreceiver/generated_component_test.go

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

8 changes: 8 additions & 0 deletions receiver/otlpreceiver/generated_component_test.go

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

0 comments on commit 2797e5f

Please sign in to comment.