[cmd/mdatagen] Move component config test from cmd/builder (#9940)

The tests generated by cmd/builder 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, the 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
not used in any collector bundle.
This commit is contained in:
Dmitrii Anoshin 2024-04-11 10:43:05 -07:00 committed by GitHub
parent f1a7475cf6
commit 1f643f4ce1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
23 changed files with 169 additions and 112 deletions

View File

@ -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: []

View File

@ -80,7 +80,6 @@ func Generate(cfg Config) error {
mainOthersTemplate,
mainWindowsTemplate,
componentsTemplate,
componentsTestTemplate,
goModTemplate,
} {
if err := processAndWrite(cfg, tmpl, tmpl.Name(), cfg); err != nil {

View File

@ -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)

View File

@ -1,39 +0,0 @@
// Code generated by "go.opentelemetry.io/collector/cmd/builder". DO NOT EDIT.
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component/componenttest"
)
func TestValidateConfigs(t *testing.T) {
factories, err := components()
assert.NoError(t, err)
for k, factory := range factories.Receivers {
assert.Equal(t, k, factory.Type())
assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
for k, factory := range factories.Processors {
assert.Equal(t, k, factory.Type())
assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
for k, factory := range factories.Exporters {
assert.Equal(t, k, factory.Type())
assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
for k, factory := range factories.Connectors {
assert.Equal(t, k, factory.Type())
assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
for k, factory := range factories.Extensions {
assert.Equal(t, k, factory.Type())
assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
}

View File

@ -17,6 +17,14 @@ import (
"go.opentelemetry.io/collector/receiver/receivertest"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "sample", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -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
}
}

View File

@ -6,6 +6,8 @@ package main
import (
"bytes"
"fmt"
"go/parser"
"go/token"
"os"
"path/filepath"
"testing"
@ -21,7 +23,6 @@ func TestRunContents(t *testing.T) {
wantMetricsGenerated bool
wantConfigGenerated bool
wantStatusGenerated bool
wantTestsGenerated bool
wantErr bool
}{
{
@ -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,
},
}
@ -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)
})
}
}

View File

@ -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"
@ -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()
@ -499,3 +512,4 @@ func generateLifecycleTestTraces() ptrace.Traces {
return traces
}
{{- end }}
{{- end }}

View File

@ -1,41 +0,0 @@
// Code generated by "go.opentelemetry.io/collector/cmd/builder". DO NOT EDIT.
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component/componenttest"
)
func TestValidateConfigs(t *testing.T) {
factories, err := components()
assert.NoError(t, err)
for k, factory := range factories.Receivers {
assert.Equal(t, k, factory.Type())
assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
for k, factory := range factories.Processors {
assert.Equal(t, k, factory.Type())
assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
for k, factory := range factories.Exporters {
assert.Equal(t, k, factory.Type())
assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
for k, factory := range factories.Connectors {
assert.Equal(t, k, factory.Type())
assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
for k, factory := range factories.Extensions {
assert.Equal(t, k, factory.Type())
assert.NoError(t, componenttest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
}

View File

@ -5,7 +5,6 @@ module go.opentelemetry.io/collector/cmd/otelcorecol
go 1.21
require (
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/component v0.98.0
go.opentelemetry.io/collector/connector v0.98.0
go.opentelemetry.io/collector/connector/forwardconnector v0.98.0
@ -34,7 +33,6 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
@ -59,7 +57,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mostynb/go-grpc-compression v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect

View File

@ -16,6 +16,14 @@ import (
"go.opentelemetry.io/collector/consumer/consumertest"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "forward", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -20,6 +20,14 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "debug", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -20,6 +20,14 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "logging", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -20,6 +20,14 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "nop", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -20,6 +20,14 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "otlp", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -20,6 +20,14 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "otlphttp", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -14,6 +14,14 @@ import (
"go.opentelemetry.io/collector/extension/extensiontest"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "memory_ballast", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -14,6 +14,14 @@ import (
"go.opentelemetry.io/collector/extension/extensiontest"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "memory_limiter", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -14,6 +14,14 @@ import (
"go.opentelemetry.io/collector/extension/extensiontest"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "zpages", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -21,6 +21,14 @@ import (
"go.opentelemetry.io/collector/processor/processortest"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "batch", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -21,6 +21,14 @@ import (
"go.opentelemetry.io/collector/processor/processortest"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "memory_limiter", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -16,6 +16,14 @@ import (
"go.opentelemetry.io/collector/receiver/receivertest"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "nop", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

View File

@ -16,6 +16,14 @@ import (
"go.opentelemetry.io/collector/receiver/receivertest"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "otlp", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()