forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
405 lines (359 loc) · 10.3 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/receiver/receivertest"
md "github.com/open-telemetry/opentelemetry-collector-contrib/cmd/mdatagen/internal/metadata"
)
func TestRunContents(t *testing.T) {
tests := []struct {
yml string
wantMetricsGenerated bool
wantConfigGenerated bool
wantStatusGenerated bool
wantErr bool
}{
{
yml: "invalid.yaml",
wantErr: true,
},
{
yml: "metrics_and_type.yaml",
wantMetricsGenerated: true,
wantConfigGenerated: true,
wantStatusGenerated: true,
},
{
yml: "resource_attributes_only.yaml",
wantConfigGenerated: true,
wantStatusGenerated: true,
},
{
yml: "status_only.yaml",
wantStatusGenerated: true,
},
}
for _, tt := range tests {
t.Run(tt.yml, func(t *testing.T) {
tmpdir := t.TempDir()
ymlContent, err := os.ReadFile(filepath.Join("testdata", tt.yml))
require.NoError(t, err)
metadataFile := filepath.Join(tmpdir, "metadata.yaml")
require.NoError(t, os.WriteFile(metadataFile, ymlContent, 0600))
require.NoError(t, os.WriteFile(filepath.Join(tmpdir, "README.md"), []byte(`
<!-- status autogenerated section -->
foo
<!-- end autogenerated section -->`), 0600))
err = run(metadataFile)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
if tt.wantMetricsGenerated {
require.FileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_metrics.go"))
require.FileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_metrics_test.go"))
require.FileExists(t, filepath.Join(tmpdir, "documentation.md"))
} else {
require.NoFileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_metrics.go"))
require.NoFileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_metrics_test.go"))
require.NoFileExists(t, filepath.Join(tmpdir, "documentation.md"))
}
if tt.wantConfigGenerated {
require.FileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_config.go"))
require.FileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_config_test.go"))
} else {
require.NoFileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_config.go"))
require.NoFileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_config_test.go"))
}
if tt.wantStatusGenerated {
require.FileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_status.go"))
contents, err := os.ReadFile(filepath.Join(tmpdir, "README.md"))
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"))
require.NoError(t, err)
require.Contains(t, string(contents), "foo")
}
})
}
}
func TestRun(t *testing.T) {
type args struct {
ymlPath string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "no argument",
args: args{""},
wantErr: true,
},
{
name: "no such file",
args: args{"/no/such/file"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := run(tt.args.ymlPath); (err != nil) != tt.wantErr {
t.Errorf("run() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestInlineReplace(t *testing.T) {
tests := []struct {
name string
markdown string
outputFile string
componentClass string
warnings []string
stability map[string][]string
distros []string
codeowners *Codeowners
}{
{
name: "readme with empty status",
markdown: `# Some component
<!-- status autogenerated section -->
<!-- end autogenerated section -->
Some info about a component
`,
outputFile: "readme_with_status.md",
componentClass: "receiver",
distros: []string{"contrib"},
},
{
name: "readme with status for extension",
markdown: `# Some component
<!-- status autogenerated section -->
<!-- end autogenerated section -->
Some info about a component
`,
outputFile: "readme_with_status_extension.md",
componentClass: "extension",
distros: []string{"contrib"},
},
{
name: "readme with status with codeowners and emeritus",
markdown: `# Some component
<!-- status autogenerated section -->
<!-- end autogenerated section -->
Some info about a component
`,
outputFile: "readme_with_status_codeowners_and_emeritus.md",
componentClass: "receiver",
distros: []string{"contrib"},
codeowners: &Codeowners{
Active: []string{"foo"},
Emeritus: []string{"bar"},
},
},
{
name: "readme with status with codeowners",
markdown: `# Some component
<!-- status autogenerated section -->
<!-- end autogenerated section -->
Some info about a component
`,
outputFile: "readme_with_status_codeowners.md",
componentClass: "receiver",
distros: []string{"contrib"},
codeowners: &Codeowners{
Active: []string{"foo"},
},
},
{
name: "readme with status table",
markdown: `# Some component
<!-- status autogenerated section -->
| Status | |
| ------------------------ |-----------|
<!-- end autogenerated section -->
Some info about a component
`,
outputFile: "readme_with_status.md",
componentClass: "receiver",
distros: []string{"contrib"},
},
{
name: "readme with no status",
markdown: `# Some component
Some info about a component
`,
outputFile: "readme_without_status.md",
distros: []string{"contrib"},
},
{
name: "component with warnings",
markdown: `# Some component
<!-- status autogenerated section -->
<!-- end autogenerated section -->
Some info about a component
### warnings
Some warning there.
`,
outputFile: "readme_with_warnings.md",
warnings: []string{"warning1"},
distros: []string{"contrib"},
},
{
name: "readme with multiple signals",
markdown: `# Some component
<!-- status autogenerated section -->
<!-- end autogenerated section -->
Some info about a component
`,
outputFile: "readme_with_multiple_signals.md",
stability: map[string][]string{"beta": {"metrics"}, "alpha": {"logs"}},
distros: []string{"contrib"},
},
{
name: "readme with cmd class",
markdown: `# Some component
<!-- status autogenerated section -->
<!-- end autogenerated section -->
Some info about a component
`,
outputFile: "readme_with_cmd_class.md",
stability: map[string][]string{"beta": {"metrics"}, "alpha": {"logs"}},
componentClass: "cmd",
distros: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stability := map[string][]string{"beta": {"metrics"}}
if len(tt.stability) > 0 {
stability = tt.stability
}
md := metadata{
Type: "foo",
ShortFolderName: "foo",
Status: &Status{
Stability: stability,
Distributions: tt.distros,
Class: tt.componentClass,
Warnings: tt.warnings,
Codeowners: tt.codeowners,
},
}
tmpdir := t.TempDir()
readmeFile := filepath.Join(tmpdir, "README.md")
require.NoError(t, os.WriteFile(readmeFile, []byte(tt.markdown), 0600))
err := inlineReplace("templates/readme.md.tmpl", readmeFile, md, statusStart, statusEnd)
require.NoError(t, err)
require.FileExists(t, filepath.Join(tmpdir, "README.md"))
got, err := os.ReadFile(filepath.Join(tmpdir, "README.md"))
require.NoError(t, err)
got = bytes.ReplaceAll(got, []byte("\r\n"), []byte("\n"))
expected, err := os.ReadFile(filepath.Join("testdata", tt.outputFile))
require.NoError(t, err)
expected = bytes.ReplaceAll(expected, []byte("\r\n"), []byte("\n"))
fmt.Println(string(got))
fmt.Println(string(expected))
require.Equal(t, string(expected), string(got))
})
}
}
func TestGenerateStatusMetadata(t *testing.T) {
tests := []struct {
name string
output string
md metadata
expected string
}{
{
name: "foo component with beta status",
md: metadata{
Type: "foo",
Status: &Status{
Stability: map[string][]string{"beta": {"metrics"}},
Distributions: []string{"contrib"},
Class: "receiver",
},
},
expected: `// Code generated by mdatagen. DO NOT EDIT.
package metadata
import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)
var (
Type = component.MustNewType("foo")
)
const (
MetricsStability = component.StabilityLevelBeta
)
func Meter(settings component.TelemetrySettings) metric.Meter {
return settings.MeterProvider.Meter("")
}
func Tracer(settings component.TelemetrySettings) trace.Tracer {
return settings.TracerProvider.Tracer("")
}
`,
},
{
name: "foo component with alpha status",
md: metadata{
Type: "foo",
Status: &Status{
Stability: map[string][]string{"alpha": {"metrics"}},
Distributions: []string{"contrib"},
Class: "receiver",
},
},
expected: `// Code generated by mdatagen. DO NOT EDIT.
package metadata
import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)
var (
Type = component.MustNewType("foo")
)
const (
MetricsStability = component.StabilityLevelAlpha
)
func Meter(settings component.TelemetrySettings) metric.Meter {
return settings.MeterProvider.Meter("")
}
func Tracer(settings component.TelemetrySettings) trace.Tracer {
return settings.TracerProvider.Tracer("")
}
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpdir := t.TempDir()
err := generateFile("templates/status.go.tmpl",
filepath.Join(tmpdir, "generated_status.go"), tt.md, "metadata")
require.NoError(t, err)
actual, err := os.ReadFile(filepath.Join(tmpdir, "generated_status.go"))
require.NoError(t, err)
require.Equal(t, tt.expected, string(actual))
})
}
}
// TestGenerated verifies that the internal/metadata API is generated correctly.
func TestGenerated(t *testing.T) {
mb := md.NewMetricsBuilder(md.DefaultMetricsBuilderConfig(), receivertest.NewNopCreateSettings())
m := mb.Emit()
require.Equal(t, 0, m.ResourceMetrics().Len())
}