Skip to content

Commit

Permalink
[chore] add mdatagen extension lifecycle tests (#29734)
Browse files Browse the repository at this point in the history
Continue work on #27849

---------

Co-authored-by: Evan Bradley <[email protected]>
  • Loading branch information
atoulme and evan-bradley authored Dec 14, 2023
1 parent 4b47a0f commit 0af7fce
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ func templatize(tmplFile string, md metadata) *template.Template {
"isReceiver": func() bool {
return md.Status.Class == "receiver"
},
"isExtension": func() bool {
return md.Status.Class == "extension"
},
"skipLifecycle": func() bool {
return md.Tests.SkipLifecycle
},
Expand Down
39 changes: 39 additions & 0 deletions cmd/mdatagen/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receivertest"
{{ end }}
{{ if isExtension }}
"go.opentelemetry.io/collector/extension/extensiontest"
{{ end }}
"go.opentelemetry.io/collector/confmap/confmaptest"
{{ if or (isExporter) (isProcessor) }}
Expand Down Expand Up @@ -299,3 +302,39 @@ func Test_ComponentLifecycle(t *testing.T) {
}
}
{{ end }}

{{ if isExtension }}
func Test_ComponentLifecycle(t *testing.T) {
factory := NewFactory()

cm, err := confmaptest.LoadConf("metadata.yaml")
require.NoError(t, err)
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub("tests::config")
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))

t.Run("shutdown", func(t *testing.T) {
e, err := factory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
err = e.Shutdown(context.Background())
require.NoError(t, err)
})

t.Run("lifecycle", func(t *testing.T) {
{{ if skipLifecycle }}
// TODO support lifecycle
t.SkipNow()
{{ 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.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.Shutdown(context.Background()))
})
}
{{ end }}
68 changes: 68 additions & 0 deletions extension/headerssetterextension/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions extension/headerssetterextension/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ status:
- sumo
codeowners:
active: [jpkrohling]
tests:
config:

0 comments on commit 0af7fce

Please sign in to comment.