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 11, 2024
1 parent fd0d0da commit fc2385f
Show file tree
Hide file tree
Showing 22 changed files with 169 additions and 109 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
27 changes: 11 additions & 16 deletions cmd/mdatagen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package main
import (
"bytes"
"fmt"
"go/parser"
"go/token"
"os"
"path/filepath"
"testing"
Expand All @@ -21,7 +23,6 @@ func TestRunContents(t *testing.T) {
wantMetricsGenerated bool
wantConfigGenerated bool
wantStatusGenerated bool
wantTestsGenerated bool
wantErr bool
}{
{
Expand All @@ -45,27 +46,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 @@ -108,26 +104,25 @@ foo
require.NoFileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_config_test.go"))
}

var contents []byte
if tt.wantStatusGenerated {
require.FileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_status.go"))
contents, err := os.ReadFile(filepath.Join(tmpdir, "README.md")) // nolint: gosec
contents, err = os.ReadFile(filepath.Join(tmpdir, "README.md")) // nolint: gosec
require.NoError(t, err)
require.NotContains(t, string(contents), "foo")
} else {
require.NoFileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_status.go"))
contents, err := os.ReadFile(filepath.Join(tmpdir, "README.md")) // nolint: gosec
contents, err = os.ReadFile(filepath.Join(tmpdir, "README.md")) // nolint: gosec
require.NoError(t, err)
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")
_, err = parser.ParseFile(token.NewFileSet(), "", contents, parser.DeclarationErrors)
require.NoError(t, err)
})
}
}
Expand Down
20 changes: 17 additions & 3 deletions cmd/mdatagen/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
package {{ .Package }}

import (
{{- if not (and .Tests.SkipLifecycle .Tests.SkipShutdown) }}
"context"
{{- end }}
"testing"
{{- if or isExporter isProcessor }}
{{- if and (not (and .Tests.SkipLifecycle .Tests.SkipShutdown)) (or isExporter isProcessor) }}
"time"
{{- end }}

"github.com/stretchr/testify/require"
{{- if not (and .Tests.SkipLifecycle .Tests.SkipShutdown) }}
"go.opentelemetry.io/collector/component"
{{- if not .Tests.SkipLifecycle }}
"go.opentelemetry.io/collector/component/componenttest"
{{- end }}
"go.opentelemetry.io/collector/component/componenttest"
{{- if not (and .Tests.SkipLifecycle .Tests.SkipShutdown) }}
{{- if isExporter }}
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exportertest"
Expand Down Expand Up @@ -47,8 +50,18 @@ import (
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
{{- end }}
{{- end }}
)

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

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

{{ if not (and .Tests.SkipLifecycle .Tests.SkipShutdown) -}}
{{ if isExporter -}}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()
Expand Down Expand Up @@ -499,3 +512,4 @@ func generateLifecycleTestTraces() ptrace.Traces {
return traces
}
{{- end }}
{{- end }}
41 changes: 0 additions & 41 deletions cmd/otelcorecol/components_test.go

This file was deleted.

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.

Loading

0 comments on commit fc2385f

Please sign in to comment.