Skip to content

Commit

Permalink
[mdatagen] Remove use of ReportFatalError in generated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Jan 31, 2024
1 parent f37e376 commit bf672e4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
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: []
40 changes: 9 additions & 31 deletions cmd/mdatagen/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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 All @@ -33,27 +32,6 @@ import (
{{ end }}
)

// assertNoErrorHost implements a component.Host that asserts that there were no errors.
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 @@ -114,10 +92,10 @@ func Test_ComponentLifecycle(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 @@ -140,7 +118,7 @@ func Test_ComponentLifecycle(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 @@ -205,10 +183,10 @@ func Test_ComponentLifecycle(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 @@ -230,7 +208,7 @@ func Test_ComponentLifecycle(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 @@ -294,7 +272,7 @@ func Test_ComponentLifecycle(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 @@ -332,12 +310,12 @@ func Test_ComponentLifecycle(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

0 comments on commit bf672e4

Please sign in to comment.