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

[mdatagen] Remove use of ReportFatalError in generated tests #9439

Merged
merged 1 commit into from
Feb 3, 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
25 changes: 25 additions & 0 deletions .chloggen/remove_assert_host.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: mdatagen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Remove use of ReportFatalError in generated tests

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

# (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:

# 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: []
39 changes: 9 additions & 30 deletions cmd/mdatagen/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
Expand Down Expand Up @@ -42,26 +41,6 @@ import (
{{ end }}
)

// assertNoErrorHost implements a component.Host that asserts that there were no errors.
Copy link
Member

Choose a reason for hiding this comment

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

Should we instead generate an assert no error StatusReporter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd do it in phases. I am fairly certain that if we generate an assert no error StatusReporter, tests will fail because it's ok for them to report status errors on start if say we have no database to connect to.

I would also create this reporter in a common lib rather than in every generated test.

Copy link
Member

Choose a reason for hiding this comment

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

I think we can assert if the status is fatal not on error.

type assertNoErrorHost struct {
component.Host
*testing.T
}

var _ component.Host = (*assertNoErrorHost)(nil)

// newAssertNoErrorHost returns a new instance of assertNoErrorHost.
func newAssertNoErrorHost(t *testing.T) component.Host {
return &assertNoErrorHost{
componenttest.NewNopHost(),
t,
}
}

func (aneh *assertNoErrorHost) ReportFatalError(err error) {
assert.NoError(aneh, err)
}

func TestCheckConfigStruct(t *testing.T) {
componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig())
}
Expand Down Expand Up @@ -122,10 +101,10 @@ func TestComponentLifecycle(t *testing.T) {
{{ end }}
c, err := test.createFn(context.Background(), exportertest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
host := newAssertNoErrorHost(t)
host := componenttest.NewNopHost()
err = c.Start(context.Background(), host)
require.NoError(t, err)
assert.NotPanics(t, func() {
require.NotPanics(t, func() {
switch e := c.(type) {
case exporter.Logs:
logs := testdata.GenerateLogsManyLogRecordsSameResource(2)
Expand All @@ -148,7 +127,7 @@ func TestComponentLifecycle(t *testing.T) {
}
})
{{ if not expectConsumerError }}
assert.NoError(t, err)
require.NoError(t, err)
{{ end }}
err = c.Shutdown(context.Background())
require.NoError(t, err)
Expand Down Expand Up @@ -213,10 +192,10 @@ func TestComponentLifecycle(t *testing.T) {
{{ end }}
c, err := test.createFn(context.Background(), processortest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
host := newAssertNoErrorHost(t)
host := componenttest.NewNopHost()
err = c.Start(context.Background(), host)
require.NoError(t, err)
assert.NotPanics(t, func() {
require.NotPanics(t, func() {
switch e := c.(type) {
case processor.Logs:
logs := testdata.GenerateLogsManyLogRecordsSameResource(2)
Expand All @@ -238,7 +217,7 @@ func TestComponentLifecycle(t *testing.T) {
err = e.ConsumeTraces(context.Background(), traces)
}
})
assert.NoError(t, err)
require.NoError(t, err)
err = c.Shutdown(context.Background())
require.NoError(t, err)
})
Expand Down Expand Up @@ -302,7 +281,7 @@ func TestComponentLifecycle(t *testing.T) {
{{ end }}
firstRcvr, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
host := newAssertNoErrorHost(t)
host := componenttest.NewNopHost()
require.NoError(t, err)
require.NoError(t, firstRcvr.Start(context.Background(), host))
require.NoError(t, firstRcvr.Shutdown(context.Background()))
Expand Down Expand Up @@ -340,12 +319,12 @@ func TestComponentLifecycle(t *testing.T) {
{{ end }}
firstExt, err := factory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
require.NoError(t, firstExt.Start(context.Background(), newAssertNoErrorHost(t)))
require.NoError(t, firstExt.Start(context.Background(), componenttest.NewNopHost()))
require.NoError(t, firstExt.Shutdown(context.Background()))

secondExt, err := factory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
require.NoError(t, secondExt.Start(context.Background(), newAssertNoErrorHost(t)))
require.NoError(t, secondExt.Start(context.Background(), componenttest.NewNopHost()))
require.NoError(t, secondExt.Shutdown(context.Background()))
})
}
Expand Down