Skip to content
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

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

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
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.

3 changes: 0 additions & 3 deletions cmd/otelcorecol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
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.