Skip to content

Commit 3210bf4

Browse files
authored
[cmd/mdatagen] Make lifecycle tests generated by default (#31532)
The tests can be disabled with `skip_lifecycle` and `skip_shutdown` metadata.yaml options. But it's strongly encouraged to avoid skipping them. This PR doesn't have any functional changes. Only tests changes
1 parent 0cbfd39 commit 3210bf4

File tree

296 files changed

+1010
-1851
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

296 files changed

+1010
-1851
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: 'enhancement'
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: 'cmd/mdatagen'
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Make lifecycle tests generated by default
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [31532]
14+
15+
# If your change doesn't affect end users or the exported elements of any package,
16+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
17+
# Optional: The change log or logs in which this entry should be included.
18+
# e.g. '[user]' or '[user, api]'
19+
# Include 'user' if the change is relevant to end users.
20+
# Include 'api' if there is a change to a library API.
21+
# Default: '[user]'
22+
change_logs: [user]

cmd/mdatagen/internal/samplereceiver/generated_component_test.go

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/loader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ func (a attribute) TestValue() string {
202202
type tests struct {
203203
Config any `mapstructure:"config"`
204204
SkipLifecycle bool `mapstructure:"skip_lifecycle"`
205+
SkipShutdown bool `mapstructure:"skip_shutdown"`
205206
ExpectConsumerError bool `mapstructure:"expect_consumer_error"`
206207
}
207208

@@ -225,7 +226,7 @@ type metadata struct {
225226
// ShortFolderName is the shortened folder name of the component, removing class if present
226227
ShortFolderName string `mapstructure:"-"`
227228

228-
Tests *tests `mapstructure:"tests"`
229+
Tests tests `mapstructure:"tests"`
229230
}
230231

231232
func setAttributesFullName(attrs map[attributeName]attribute) {

cmd/mdatagen/loader_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ func TestLoadMetadata(t *testing.T) {
219219
},
220220
ScopeName: "otelcol/samplereceiver",
221221
ShortFolderName: "sample",
222-
Tests: &tests{},
223222
},
224223
},
225224
{

cmd/mdatagen/main.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ func run(ymlPath string) error {
6262
filepath.Join(codeDir, "generated_status.go"), md, "metadata"); err != nil {
6363
return err
6464
}
65+
if !md.Tests.SkipLifecycle || !md.Tests.SkipShutdown {
66+
if err = generateFile(filepath.Join(tmplDir, "component_test.go.tmpl"),
67+
filepath.Join(ymlDir, "generated_component_test.go"), md, packageName); err != nil {
68+
return err
69+
}
70+
}
6571
}
6672

6773
if _, err = os.Stat(filepath.Join(ymlDir, "README.md")); err == nil {
@@ -74,13 +80,6 @@ func run(ymlPath string) error {
7480
}
7581
}
7682

77-
if md.Tests != nil {
78-
if err = generateFile(filepath.Join(tmplDir, "component_test.go.tmpl"),
79-
filepath.Join(ymlDir, "generated_component_test.go"), md, packageName); err != nil {
80-
return err
81-
}
82-
}
83-
8483
if len(md.Metrics) == 0 && len(md.ResourceAttributes) == 0 {
8584
return nil
8685
}
@@ -195,9 +194,6 @@ func templatize(tmplFile string, md metadata) *template.Template {
195194
"isConnector": func() bool {
196195
return md.Status.Class == "connector"
197196
},
198-
"skipLifecycle": func() bool {
199-
return md.Tests.SkipLifecycle
200-
},
201197
"supportsLogs": func() bool {
202198
for _, signals := range md.Status.Stability {
203199
for _, s := range signals {

cmd/mdatagen/metadata-schema.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,9 @@ metrics:
109109
# Lifecycle tests generated for this component.
110110
tests:
111111
config: # {} by default, specific testing configuration for lifecycle tests.
112+
# Skip lifecycle tests for this component. Not recommended for components that are not in development.
112113
skip_lifecycle: false # false by default
114+
# Skip shutdown tests for this component. Not recommended for components that are not in development.
115+
skip_shutdown: false # false by default
116+
# Whether it's expected that the Consume[Logs|Metrics|Traces] method will return an error with the given configuration.
113117
expect_consumer_error: true # false by default

cmd/mdatagen/templates/component_test.go.tmpl

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Code generated by mdatagen. DO NOT EDIT.
22

3-
{{ if len .Status.UnsupportedPlatforms -}}
3+
{{- if len .Status.UnsupportedPlatforms }}
44
//go:build {{ range $i, $v := .Status.UnsupportedPlatforms }}{{ if $i }} && {{ end }}!{{ . }}{{ end }}
55
{{- end }}
66

@@ -9,45 +9,47 @@ package {{ .Package }}
99
import (
1010
"context"
1111
"testing"
12-
{{- if or isExporter isProcessor }}
13-
"time"
14-
{{- end }}
12+
{{- if or isExporter isProcessor }}
13+
"time"
14+
{{- end }}
1515

1616
"github.com/stretchr/testify/require"
1717
"go.opentelemetry.io/collector/component"
18+
{{- if not .Tests.SkipLifecycle }}
1819
"go.opentelemetry.io/collector/component/componenttest"
19-
{{ if isExporter }}
20+
{{- end }}
21+
{{- if isExporter }}
2022
"go.opentelemetry.io/collector/exporter"
2123
"go.opentelemetry.io/collector/exporter/exportertest"
22-
{{ end }}
23-
{{ if isProcessor }}
24+
{{- end }}
25+
{{- if isProcessor }}
2426
"go.opentelemetry.io/collector/consumer/consumertest"
2527
"go.opentelemetry.io/collector/processor"
2628
"go.opentelemetry.io/collector/processor/processortest"
27-
{{ end }}
28-
{{ if isReceiver }}
29+
{{- end }}
30+
{{- if isReceiver }}
2931
"go.opentelemetry.io/collector/consumer/consumertest"
3032
"go.opentelemetry.io/collector/receiver"
3133
"go.opentelemetry.io/collector/receiver/receivertest"
32-
{{ end }}
33-
{{ if isExtension }}
34+
{{- end }}
35+
{{- if isExtension }}
3436
"go.opentelemetry.io/collector/extension/extensiontest"
35-
{{ end }}
36-
{{ if isConnector }}
37+
{{- end }}
38+
{{- if isConnector }}
3739
"go.opentelemetry.io/collector/consumer/consumertest"
3840
"go.opentelemetry.io/collector/connector"
3941
"go.opentelemetry.io/collector/connector/connectortest"
40-
{{ end }}
42+
{{- end }}
4143
"go.opentelemetry.io/collector/confmap/confmaptest"
42-
{{- if or isExporter isProcessor }}
44+
{{- if or isExporter isProcessor }}
4345
"go.opentelemetry.io/collector/pdata/pcommon"
4446
"go.opentelemetry.io/collector/pdata/plog"
4547
"go.opentelemetry.io/collector/pdata/pmetric"
4648
"go.opentelemetry.io/collector/pdata/ptrace"
47-
{{- end }}
49+
{{- end }}
4850
)
4951

50-
{{ if isExporter }}
52+
{{ if isExporter -}}
5153
func TestComponentLifecycle(t *testing.T) {
5254
factory := NewFactory()
5355

@@ -89,18 +91,17 @@ func TestComponentLifecycle(t *testing.T) {
8991
require.NoError(t, component.UnmarshalConfig(sub, cfg))
9092

9193
for _, test := range tests {
94+
{{- if not .Tests.SkipShutdown }}
9295
t.Run(test.name + "-shutdown", func(t *testing.T) {
9396
c, err := test.createFn(context.Background(), exportertest.NewNopCreateSettings(), cfg)
9497
require.NoError(t, err)
9598
err = c.Shutdown(context.Background())
9699
require.NoError(t, err)
97100
})
101+
{{- end }}
98102

103+
{{- if not .Tests.SkipLifecycle }}
99104
t.Run(test.name + "-lifecycle", func(t *testing.T) {
100-
{{ if skipLifecycle }}
101-
// TODO support lifecycle
102-
t.SkipNow()
103-
{{ end }}
104105
c, err := test.createFn(context.Background(), exportertest.NewNopCreateSettings(), cfg)
105106
require.NoError(t, err)
106107
host := componenttest.NewNopHost()
@@ -134,6 +135,7 @@ func TestComponentLifecycle(t *testing.T) {
134135
err = c.Shutdown(context.Background())
135136
require.NoError(t, err)
136137
})
138+
{{- end }}
137139
}
138140
}
139141
{{ end }}
@@ -180,18 +182,17 @@ func TestComponentLifecycle(t *testing.T) {
180182
require.NoError(t, component.UnmarshalConfig(sub, cfg))
181183

182184
for _, test := range tests {
185+
{{- if not .Tests.SkipShutdown }}
183186
t.Run(test.name + "-shutdown", func(t *testing.T) {
184187
c, err := test.createFn(context.Background(), processortest.NewNopCreateSettings(), cfg)
185188
require.NoError(t, err)
186189
err = c.Shutdown(context.Background())
187190
require.NoError(t, err)
188191
})
192+
{{- end }}
189193

194+
{{- if not .Tests.SkipLifecycle }}
190195
t.Run(test.name + "-lifecycle", func(t *testing.T) {
191-
{{ if skipLifecycle }}
192-
// TODO support lifecycle
193-
t.SkipNow()
194-
{{ end }}
195196
c, err := test.createFn(context.Background(), processortest.NewNopCreateSettings(), cfg)
196197
require.NoError(t, err)
197198
host := componenttest.NewNopHost()
@@ -223,6 +224,7 @@ func TestComponentLifecycle(t *testing.T) {
223224
err = c.Shutdown(context.Background())
224225
require.NoError(t, err)
225226
})
227+
{{- end }}
226228
}
227229
}
228230
{{ end }}
@@ -269,18 +271,17 @@ func TestComponentLifecycle(t *testing.T) {
269271
require.NoError(t, component.UnmarshalConfig(sub, cfg))
270272

271273
for _, test := range tests {
274+
{{- if not .Tests.SkipShutdown }}
272275
t.Run(test.name + "-shutdown", func(t *testing.T) {
273276
c, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg)
274277
require.NoError(t, err)
275278
err = c.Shutdown(context.Background())
276279
require.NoError(t, err)
277280
})
281+
{{- end }}
278282

283+
{{- if not .Tests.SkipLifecycle }}
279284
t.Run(test.name + "-lifecycle", func(t *testing.T) {
280-
{{ if skipLifecycle }}
281-
// TODO support lifecycle
282-
t.SkipNow()
283-
{{ end }}
284285
firstRcvr, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg)
285286
require.NoError(t, err)
286287
host := componenttest.NewNopHost()
@@ -292,6 +293,7 @@ func TestComponentLifecycle(t *testing.T) {
292293
require.NoError(t, secondRcvr.Start(context.Background(), host))
293294
require.NoError(t, secondRcvr.Shutdown(context.Background()))
294295
})
296+
{{- end }}
295297
}
296298
}
297299
{{ end }}
@@ -307,18 +309,17 @@ func TestComponentLifecycle(t *testing.T) {
307309
require.NoError(t, err)
308310
require.NoError(t, component.UnmarshalConfig(sub, cfg))
309311

312+
{{- if not .Tests.SkipShutdown }}
310313
t.Run("shutdown", func(t *testing.T) {
311314
e, err := factory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
312315
require.NoError(t, err)
313316
err = e.Shutdown(context.Background())
314317
require.NoError(t, err)
315318
})
319+
{{- end }}
316320

321+
{{- if not .Tests.SkipLifecycle }}
317322
t.Run("lifecycle", func(t *testing.T) {
318-
{{ if skipLifecycle }}
319-
// TODO support lifecycle
320-
t.SkipNow()
321-
{{ end }}
322323
firstExt, err := factory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
323324
require.NoError(t, err)
324325
require.NoError(t, firstExt.Start(context.Background(), componenttest.NewNopHost()))
@@ -329,6 +330,7 @@ func TestComponentLifecycle(t *testing.T) {
329330
require.NoError(t, secondExt.Start(context.Background(), componenttest.NewNopHost()))
330331
require.NoError(t, secondExt.Shutdown(context.Background()))
331332
})
333+
{{- end }}
332334
}
333335
{{ end }}
334336

@@ -422,18 +424,17 @@ func TestComponentLifecycle(t *testing.T) {
422424
require.NoError(t, component.UnmarshalConfig(sub, cfg))
423425

424426
for _, test := range tests {
427+
{{- if not .Tests.SkipShutdown }}
425428
t.Run(test.name + "-shutdown", func(t *testing.T) {
426429
c, err := test.createFn(context.Background(), connectortest.NewNopCreateSettings(), cfg)
427430
require.NoError(t, err)
428431
err = c.Shutdown(context.Background())
429432
require.NoError(t, err)
430433
})
434+
{{- end }}
431435

436+
{{- if not .Tests.SkipLifecycle }}
432437
t.Run(test.name + "-lifecycle", func(t *testing.T) {
433-
{{ if skipLifecycle }}
434-
// TODO support lifecycle
435-
t.SkipNow()
436-
{{ end }}
437438
firstConnector, err := test.createFn(context.Background(), connectortest.NewNopCreateSettings(), cfg)
438439
require.NoError(t, err)
439440
host := componenttest.NewNopHost()
@@ -445,6 +446,7 @@ func TestComponentLifecycle(t *testing.T) {
445446
require.NoError(t, secondConnector.Start(context.Background(), host))
446447
require.NoError(t, secondConnector.Shutdown(context.Background()))
447448
})
449+
{{- end }}
448450
}
449451
}
450452
{{ end }}

cmd/mdatagen/templates/status.go.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
package {{ .Package }}
44

55
import (
6-
"go.opentelemetry.io/collector/component"
6+
"go.opentelemetry.io/collector/component"
77
"go.opentelemetry.io/otel/metric"
88
"go.opentelemetry.io/otel/trace"
99
)
1010

11-
{{ if eq .Parent "" }}
11+
{{ if eq .Parent "" -}}
1212
var (
1313
Type = component.MustNewType("{{ .Type }}")
1414
)

cmd/mdatagen/testdata/metrics_and_type.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ metrics:
1717
unit: s
1818
gauge:
1919
value_type: double
20+
21+
tests:
22+
skip_lifecycle: true
23+
skip_shutdown: true

cmd/mdatagen/testdata/resource_attributes_only.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ resource_attributes:
1515
description: Resource attribute 1.
1616
type: string
1717
enabled: true
18+
19+
tests:
20+
skip_lifecycle: true
21+
skip_shutdown: true

cmd/mdatagen/testdata/status_only.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ status:
44
stability:
55
beta: [traces, metrics, logs]
66
distributions: [contrib]
7+
8+
tests:
9+
skip_lifecycle: true
10+
skip_shutdown: true

0 commit comments

Comments
 (0)