| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package internal |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"fmt" |
| 9 |
"go/parser" |
| 10 |
"go/token" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/spf13/cobra" |
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
|
| 19 |
"go.opentelemetry.io/collector/component" |
| 20 |
) |
| 21 |
|
| 22 |
func TestNewCommand(t *testing.T) { |
| 23 |
cmd, err := NewCommand() |
| 24 |
require.NoError(t, err) |
| 25 |
|
| 26 |
assert.NotNil(t, cmd) |
| 27 |
assert.IsType(t, &cobra.Command{}, cmd) |
| 28 |
assert.Equal(t, "mdatagen", cmd.Use) |
| 29 |
assert.True(t, cmd.SilenceUsage) |
| 30 |
} |
| 31 |
|
| 32 |
func TestRunContents(t *testing.T) { |
| 33 |
tests := []struct { |
| 34 |
yml string |
| 35 |
wantMetricsGenerated bool |
| 36 |
// TODO: we should add one more flag for logs builder |
| 37 |
wantEventsGenerated bool |
| 38 |
wantMetricsContext bool |
| 39 |
wantLogsGenerated bool |
| 40 |
wantConfigGenerated bool |
| 41 |
wantTelemetryGenerated bool |
| 42 |
wantResourceAttributesGenerated bool |
| 43 |
wantReadmeGenerated bool |
| 44 |
wantStatusGenerated bool |
| 45 |
wantComponentTestGenerated bool |
| 46 |
wantGoleakIgnore bool |
| 47 |
wantGoleakSkip bool |
| 48 |
wantGoleakSetup bool |
| 49 |
wantGoleakTeardown bool |
| 50 |
wantErr bool |
| 51 |
wantOrderErr bool |
| 52 |
wantAttributes []string |
| 53 |
}{ |
| 54 |
{ |
| 55 |
yml: "invalid.yaml", |
| 56 |
wantErr: true, |
| 57 |
}, |
| 58 |
{ |
| 59 |
yml: "unsorted_rattr.yaml", |
| 60 |
wantOrderErr: true, |
| 61 |
}, |
| 62 |
{ |
| 63 |
yml: "basic_connector.yaml", |
| 64 |
wantErr: false, |
| 65 |
wantStatusGenerated: true, |
| 66 |
wantReadmeGenerated: true, |
| 67 |
wantComponentTestGenerated: true, |
| 68 |
}, |
| 69 |
{ |
| 70 |
yml: "basic_receiver.yaml", |
| 71 |
wantErr: false, |
| 72 |
wantStatusGenerated: true, |
| 73 |
wantReadmeGenerated: true, |
| 74 |
wantComponentTestGenerated: true, |
| 75 |
wantLogsGenerated: true, |
| 76 |
}, |
| 77 |
{ |
| 78 |
yml: "basic_pkg.yaml", |
| 79 |
wantErr: false, |
| 80 |
wantStatusGenerated: false, |
| 81 |
wantReadmeGenerated: true, |
| 82 |
}, |
| 83 |
{ |
| 84 |
yml: "metrics_and_type.yaml", |
| 85 |
wantMetricsGenerated: true, |
| 86 |
wantConfigGenerated: true, |
| 87 |
wantStatusGenerated: true, |
| 88 |
wantReadmeGenerated: true, |
| 89 |
wantComponentTestGenerated: true, |
| 90 |
}, |
| 91 |
{ |
| 92 |
yml: "resource_attributes_only.yaml", |
| 93 |
wantConfigGenerated: true, |
| 94 |
wantStatusGenerated: true, |
| 95 |
wantResourceAttributesGenerated: true, |
| 96 |
wantReadmeGenerated: true, |
| 97 |
wantComponentTestGenerated: true, |
| 98 |
wantLogsGenerated: true, |
| 99 |
}, |
| 100 |
{ |
| 101 |
yml: "status_only.yaml", |
| 102 |
wantStatusGenerated: true, |
| 103 |
wantReadmeGenerated: true, |
| 104 |
wantComponentTestGenerated: true, |
| 105 |
}, |
| 106 |
{ |
| 107 |
yml: "with_tests_receiver.yaml", |
| 108 |
wantStatusGenerated: true, |
| 109 |
wantReadmeGenerated: true, |
| 110 |
wantComponentTestGenerated: true, |
| 111 |
wantLogsGenerated: true, |
| 112 |
}, |
| 113 |
{ |
| 114 |
yml: "with_tests_exporter.yaml", |
| 115 |
wantStatusGenerated: true, |
| 116 |
wantReadmeGenerated: true, |
| 117 |
wantComponentTestGenerated: true, |
| 118 |
}, |
| 119 |
{ |
| 120 |
yml: "with_tests_processor.yaml", |
| 121 |
wantStatusGenerated: true, |
| 122 |
wantReadmeGenerated: true, |
| 123 |
wantComponentTestGenerated: true, |
| 124 |
}, |
| 125 |
{ |
| 126 |
yml: "with_tests_extension.yaml", |
| 127 |
wantStatusGenerated: true, |
| 128 |
wantReadmeGenerated: true, |
| 129 |
wantComponentTestGenerated: true, |
| 130 |
}, |
| 131 |
{ |
| 132 |
yml: "with_tests_connector.yaml", |
| 133 |
wantStatusGenerated: true, |
| 134 |
wantReadmeGenerated: true, |
| 135 |
wantComponentTestGenerated: true, |
| 136 |
}, |
| 137 |
{ |
| 138 |
yml: "with_goleak_ignores.yaml", |
| 139 |
wantStatusGenerated: true, |
| 140 |
wantGoleakIgnore: true, |
| 141 |
wantReadmeGenerated: true, |
| 142 |
wantComponentTestGenerated: true, |
| 143 |
}, |
| 144 |
{ |
| 145 |
yml: "with_goleak_skip.yaml", |
| 146 |
wantStatusGenerated: true, |
| 147 |
wantGoleakSkip: true, |
| 148 |
wantReadmeGenerated: true, |
| 149 |
wantComponentTestGenerated: true, |
| 150 |
}, |
| 151 |
{ |
| 152 |
yml: "with_goleak_setup.yaml", |
| 153 |
wantStatusGenerated: true, |
| 154 |
wantGoleakSetup: true, |
| 155 |
wantReadmeGenerated: true, |
| 156 |
wantComponentTestGenerated: true, |
| 157 |
}, |
| 158 |
{ |
| 159 |
yml: "with_goleak_teardown.yaml", |
| 160 |
wantStatusGenerated: true, |
| 161 |
wantGoleakTeardown: true, |
| 162 |
wantReadmeGenerated: true, |
| 163 |
wantComponentTestGenerated: true, |
| 164 |
}, |
| 165 |
{ |
| 166 |
yml: "with_telemetry.yaml", |
| 167 |
wantStatusGenerated: true, |
| 168 |
wantTelemetryGenerated: true, |
| 169 |
wantReadmeGenerated: true, |
| 170 |
wantComponentTestGenerated: true, |
| 171 |
wantAttributes: []string{"name"}, |
| 172 |
wantLogsGenerated: true, |
| 173 |
}, |
| 174 |
{ |
| 175 |
yml: "invalid_telemetry_missing_value_type_for_histogram.yaml", |
| 176 |
wantErr: true, |
| 177 |
wantReadmeGenerated: true, |
| 178 |
wantComponentTestGenerated: true, |
| 179 |
}, |
| 180 |
{ |
| 181 |
yml: "async_metric.yaml", |
| 182 |
wantMetricsGenerated: true, |
| 183 |
wantConfigGenerated: true, |
| 184 |
wantStatusGenerated: true, |
| 185 |
wantReadmeGenerated: true, |
| 186 |
wantComponentTestGenerated: true, |
| 187 |
}, |
| 188 |
{ |
| 189 |
yml: "custom_generated_package_name.yaml", |
| 190 |
wantStatusGenerated: true, |
| 191 |
wantReadmeGenerated: true, |
| 192 |
wantComponentTestGenerated: true, |
| 193 |
wantLogsGenerated: true, |
| 194 |
}, |
| 195 |
{ |
| 196 |
yml: "with_conditional_attribute.yaml", |
| 197 |
wantStatusGenerated: true, |
| 198 |
wantReadmeGenerated: true, |
| 199 |
wantMetricsGenerated: true, |
| 200 |
wantLogsGenerated: true, |
| 201 |
wantConfigGenerated: true, |
| 202 |
wantComponentTestGenerated: true, |
| 203 |
}, |
| 204 |
{ |
| 205 |
yml: "events/basic_event.yaml", |
| 206 |
wantStatusGenerated: true, |
| 207 |
wantReadmeGenerated: true, |
| 208 |
wantComponentTestGenerated: true, |
| 209 |
wantConfigGenerated: true, |
| 210 |
wantEventsGenerated: true, |
| 211 |
wantLogsGenerated: true, |
| 212 |
}, |
| 213 |
} |
| 214 |
for _, tt := range tests { |
| 215 |
t.Run(tt.yml, func(t *testing.T) { |
| 216 |
tmpdir := filepath.Join(t.TempDir(), "shortname") |
| 217 |
err := os.MkdirAll(tmpdir, 0o750) |
| 218 |
require.NoError(t, err) |
| 219 |
ymlContent, err := os.ReadFile(filepath.Join("testdata", tt.yml)) |
| 220 |
require.NoError(t, err) |
| 221 |
metadataFile := filepath.Join(tmpdir, "metadata.yaml") |
| 222 |
require.NoError(t, os.WriteFile(metadataFile, ymlContent, 0o600)) |
| 223 |
require.NoError(t, os.WriteFile(filepath.Join(tmpdir, "empty.go"), []byte("package shortname"), 0o600)) |
| 224 |
require.NoError(t, os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte("module shortname"), 0o600)) |
| 225 |
require.NoError(t, os.WriteFile(filepath.Join(tmpdir, "README.md"), []byte(` |
| 226 |
<!-- status autogenerated section --> |
| 227 |
foo |
| 228 |
<!-- end autogenerated section -->`), 0o600)) |
| 229 |
md, err := LoadMetadata(metadataFile) |
| 230 |
if tt.wantErr { |
| 231 |
require.Error(t, err) |
| 232 |
return |
| 233 |
} |
| 234 |
require.NoError(t, err) |
| 235 |
generatedPackageDir := filepath.Join("internal", md.GeneratedPackageName) |
| 236 |
require.NoError(t, os.MkdirAll(filepath.Join(tmpdir, generatedPackageDir), 0o700)) |
| 237 |
require.NoError(t, os.WriteFile(filepath.Join(tmpdir, generatedPackageDir, "generated_status.go"), []byte("status"), 0o600)) |
| 238 |
require.NoError(t, os.WriteFile(filepath.Join(tmpdir, generatedPackageDir, "generated_telemetry_test.go"), []byte("test"), 0o600)) |
| 239 |
require.NoError(t, os.WriteFile(filepath.Join(tmpdir, generatedPackageDir, "generated_component_test.go"), []byte("test"), 0o600)) |
| 240 |
|
| 241 |
err = run(metadataFile) |
| 242 |
if tt.wantOrderErr { |
| 243 |
require.Error(t, err) |
| 244 |
require.Contains(t, err.Error(), "metadata.yaml ordering check failed") |
| 245 |
return |
| 246 |
} |
| 247 |
require.NoError(t, err) |
| 248 |
|
| 249 |
var contents []byte |
| 250 |
if tt.wantMetricsGenerated { |
| 251 |
require.FileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_metrics.go")) |
| 252 |
require.FileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_metrics_test.go")) |
| 253 |
require.FileExists(t, filepath.Join(tmpdir, "documentation.md")) |
| 254 |
if len(tt.wantAttributes) > 0 { |
| 255 |
contents, err = os.ReadFile(filepath.Join(tmpdir, "documentation.md")) //nolint:gosec |
| 256 |
require.NoError(t, err) |
| 257 |
for _, attr := range tt.wantAttributes { |
| 258 |
require.Contains(t, string(contents), attr) |
| 259 |
} |
| 260 |
} |
| 261 |
contents, err = os.ReadFile(filepath.Join(tmpdir, generatedPackageDir, "generated_metrics.go")) //nolint:gosec |
| 262 |
require.NoError(t, err) |
| 263 |
if tt.wantMetricsContext { |
| 264 |
require.Contains(t, string(contents), "\"context\"") |
| 265 |
} else { |
| 266 |
require.NotContains(t, string(contents), "\"context\"") |
| 267 |
} |
| 268 |
} else { |
| 269 |
require.NoFileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_metrics.go")) |
| 270 |
require.NoFileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_metrics_test.go")) |
| 271 |
} |
| 272 |
|
| 273 |
if tt.wantLogsGenerated { |
| 274 |
require.FileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_logs.go")) |
| 275 |
require.FileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_logs_test.go")) |
| 276 |
} else { |
| 277 |
require.NoFileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_logs.go")) |
| 278 |
require.NoFileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_logs_test.go")) |
| 279 |
} |
| 280 |
|
| 281 |
if tt.wantConfigGenerated { |
| 282 |
require.FileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_config.go")) |
| 283 |
require.FileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_config_test.go")) |
| 284 |
} else { |
| 285 |
require.NoFileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_config.go")) |
| 286 |
require.NoFileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_config_test.go")) |
| 287 |
} |
| 288 |
|
| 289 |
if tt.wantTelemetryGenerated { |
| 290 |
require.FileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_telemetry.go")) |
| 291 |
require.FileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_telemetry_test.go")) |
| 292 |
require.FileExists(t, filepath.Join(tmpdir, "documentation.md")) |
| 293 |
contents, err = os.ReadFile(filepath.Join(tmpdir, generatedPackageDir, "generated_telemetry.go")) //nolint:gosec |
| 294 |
require.NoError(t, err) |
| 295 |
if tt.wantMetricsContext { |
| 296 |
require.Contains(t, string(contents), "\"context\"") |
| 297 |
} else { |
| 298 |
require.NotContains(t, string(contents), "\"context\"") |
| 299 |
} |
| 300 |
} else { |
| 301 |
require.NoFileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_telemetry.go")) |
| 302 |
} |
| 303 |
|
| 304 |
if !tt.wantMetricsGenerated && !tt.wantTelemetryGenerated && !tt.wantResourceAttributesGenerated && !tt.wantEventsGenerated { |
| 305 |
require.NoFileExists(t, filepath.Join(tmpdir, "documentation.md")) |
| 306 |
} |
| 307 |
|
| 308 |
if tt.wantStatusGenerated { |
| 309 |
require.FileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_status.go")) |
| 310 |
} else { |
| 311 |
require.NoFileExists(t, filepath.Join(tmpdir, generatedPackageDir, "generated_status.go")) |
| 312 |
} |
| 313 |
|
| 314 |
contents, err = os.ReadFile(filepath.Join(tmpdir, "README.md")) //nolint:gosec |
| 315 |
require.NoError(t, err) |
| 316 |
if tt.wantReadmeGenerated { |
| 317 |
require.NotContains(t, string(contents), "foo") |
| 318 |
} else { |
| 319 |
require.Contains(t, string(contents), "foo") |
| 320 |
} |
| 321 |
|
| 322 |
if tt.wantComponentTestGenerated { |
| 323 |
require.FileExists(t, filepath.Join(tmpdir, "generated_component_test.go")) |
| 324 |
contents, err = os.ReadFile(filepath.Join(tmpdir, "generated_component_test.go")) //nolint:gosec |
| 325 |
require.NoError(t, err) |
| 326 |
require.Contains(t, string(contents), "func Test") |
| 327 |
_, err = parser.ParseFile(token.NewFileSet(), "", contents, parser.DeclarationErrors) |
| 328 |
require.NoError(t, err) |
| 329 |
} else { |
| 330 |
require.NoFileExists(t, filepath.Join(tmpdir, "generated_component_test.go")) |
| 331 |
} |
| 332 |
|
| 333 |
require.FileExists(t, filepath.Join(tmpdir, "generated_package_test.go")) |
| 334 |
contents, err = os.ReadFile(filepath.Join(tmpdir, "generated_package_test.go")) //nolint:gosec |
| 335 |
require.NoError(t, err) |
| 336 |
require.Contains(t, string(contents), "func TestMain") |
| 337 |
_, err = parser.ParseFile(token.NewFileSet(), "", contents, parser.DeclarationErrors) |
| 338 |
require.NoError(t, err) |
| 339 |
|
| 340 |
if tt.wantGoleakSkip { |
| 341 |
require.Contains(t, string(contents), "skipping goleak test") |
| 342 |
} else { |
| 343 |
require.NotContains(t, string(contents), "skipping goleak test") |
| 344 |
} |
| 345 |
|
| 346 |
if tt.wantGoleakIgnore { |
| 347 |
require.Contains(t, string(contents), "IgnoreTopFunction") |
| 348 |
require.Contains(t, string(contents), "IgnoreAnyFunction") |
| 349 |
} else { |
| 350 |
require.NotContains(t, string(contents), "IgnoreTopFunction") |
| 351 |
require.NotContains(t, string(contents), "IgnoreAnyFunction") |
| 352 |
} |
| 353 |
|
| 354 |
if tt.wantGoleakSetup { |
| 355 |
require.Contains(t, string(contents), "setupFunc") |
| 356 |
} else { |
| 357 |
require.NotContains(t, string(contents), "setupFunc") |
| 358 |
} |
| 359 |
|
| 360 |
if tt.wantGoleakTeardown { |
| 361 |
require.Contains(t, string(contents), "teardownFunc") |
| 362 |
} else { |
| 363 |
require.NotContains(t, string(contents), "teardownFunc") |
| 364 |
} |
| 365 |
}) |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
func TestRun(t *testing.T) { |
| 370 |
type args struct { |
| 371 |
ymlPath string |
| 372 |
} |
| 373 |
tests := []struct { |
| 374 |
name string |
| 375 |
args args |
| 376 |
wantErr bool |
| 377 |
}{ |
| 378 |
{ |
| 379 |
name: "no argument", |
| 380 |
args: args{""}, |
| 381 |
wantErr: true, |
| 382 |
}, |
| 383 |
{ |
| 384 |
name: "no such file", |
| 385 |
args: args{"/no/such/file"}, |
| 386 |
wantErr: true, |
| 387 |
}, |
| 388 |
} |
| 389 |
for _, tt := range tests { |
| 390 |
t.Run(tt.name, func(t *testing.T) { |
| 391 |
err := run(tt.args.ymlPath) |
| 392 |
if !tt.wantErr { |
| 393 |
require.NoError(t, err, "run()") |
| 394 |
} else { |
| 395 |
require.Error(t, err) |
| 396 |
} |
| 397 |
}) |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
func TestInlineReplace(t *testing.T) { |
| 402 |
tests := []struct { |
| 403 |
name string |
| 404 |
markdown string |
| 405 |
outputFile string |
| 406 |
componentClass string |
| 407 |
warnings []string |
| 408 |
stability map[component.StabilityLevel][]string |
| 409 |
deprecation map[string]DeprecationInfo |
| 410 |
distros []string |
| 411 |
codeowners *Codeowners |
| 412 |
githubProject string |
| 413 |
}{ |
| 414 |
{ |
| 415 |
name: "readme with empty status", |
| 416 |
markdown: `# Some component |
| 417 |
|
| 418 |
<!-- status autogenerated section --> |
| 419 |
<!-- end autogenerated section --> |
| 420 |
|
| 421 |
Some info about a component |
| 422 |
`, |
| 423 |
outputFile: "readme_with_status.md", |
| 424 |
componentClass: "receiver", |
| 425 |
distros: []string{"contrib"}, |
| 426 |
githubProject: "open-telemetry/opentelemetry-collector", |
| 427 |
}, |
| 428 |
{ |
| 429 |
name: "readme with status for extension", |
| 430 |
markdown: `# Some component |
| 431 |
|
| 432 |
<!-- status autogenerated section --> |
| 433 |
<!-- end autogenerated section --> |
| 434 |
|
| 435 |
Some info about a component |
| 436 |
`, |
| 437 |
outputFile: "readme_with_status_extension.md", |
| 438 |
componentClass: "extension", |
| 439 |
distros: []string{"contrib"}, |
| 440 |
}, |
| 441 |
{ |
| 442 |
name: "readme with status for converter", |
| 443 |
markdown: `# Some component |
| 444 |
|
| 445 |
<!-- status autogenerated section --> |
| 446 |
<!-- end autogenerated section --> |
| 447 |
|
| 448 |
Some info about a component |
| 449 |
`, |
| 450 |
outputFile: "readme_with_status_converter.md", |
| 451 |
componentClass: "converter", |
| 452 |
distros: []string{"contrib"}, |
| 453 |
}, |
| 454 |
{ |
| 455 |
name: "readme with status for provider", |
| 456 |
markdown: `# Some component |
| 457 |
|
| 458 |
<!-- status autogenerated section --> |
| 459 |
<!-- end autogenerated section --> |
| 460 |
|
| 461 |
Some info about a component |
| 462 |
`, |
| 463 |
outputFile: "readme_with_status_provider.md", |
| 464 |
componentClass: "provider", |
| 465 |
distros: []string{"contrib"}, |
| 466 |
}, |
| 467 |
{ |
| 468 |
name: "readme with status with codeowners and seeking new", |
| 469 |
markdown: `# Some component |
| 470 |
|
| 471 |
<!-- status autogenerated section --> |
| 472 |
<!-- end autogenerated section --> |
| 473 |
|
| 474 |
Some info about a component |
| 475 |
`, |
| 476 |
outputFile: "readme_with_status_codeowners_and_seeking_new.md", |
| 477 |
componentClass: "receiver", |
| 478 |
distros: []string{"contrib"}, |
| 479 |
codeowners: &Codeowners{ |
| 480 |
Active: []string{"foo"}, |
| 481 |
SeekingNew: true, |
| 482 |
}, |
| 483 |
}, |
| 484 |
{ |
| 485 |
name: "readme with status with codeowners and emeritus", |
| 486 |
markdown: `# Some component |
| 487 |
|
| 488 |
<!-- status autogenerated section --> |
| 489 |
<!-- end autogenerated section --> |
| 490 |
|
| 491 |
Some info about a component |
| 492 |
`, |
| 493 |
outputFile: "readme_with_status_codeowners_and_emeritus.md", |
| 494 |
componentClass: "receiver", |
| 495 |
distros: []string{"contrib"}, |
| 496 |
codeowners: &Codeowners{ |
| 497 |
Active: []string{"foo"}, |
| 498 |
Emeritus: []string{"bar"}, |
| 499 |
}, |
| 500 |
}, |
| 501 |
{ |
| 502 |
name: "readme with status with codeowners", |
| 503 |
markdown: `# Some component |
| 504 |
|
| 505 |
<!-- status autogenerated section --> |
| 506 |
<!-- end autogenerated section --> |
| 507 |
|
| 508 |
Some info about a component |
| 509 |
`, |
| 510 |
outputFile: "readme_with_status_codeowners.md", |
| 511 |
componentClass: "receiver", |
| 512 |
distros: []string{"contrib"}, |
| 513 |
codeowners: &Codeowners{ |
| 514 |
Active: []string{"open-telemetry/collector-approvers"}, |
| 515 |
}, |
| 516 |
}, |
| 517 |
{ |
| 518 |
name: "readme with status table", |
| 519 |
markdown: `# Some component |
| 520 |
|
| 521 |
<!-- status autogenerated section --> |
| 522 |
| Status | | |
| 523 |
| ------------------------ |-----------| |
| 524 |
<!-- end autogenerated section --> |
| 525 |
|
| 526 |
Some info about a component |
| 527 |
`, |
| 528 |
outputFile: "readme_with_status.md", |
| 529 |
componentClass: "receiver", |
| 530 |
distros: []string{"contrib"}, |
| 531 |
githubProject: "open-telemetry/opentelemetry-collector", |
| 532 |
}, |
| 533 |
{ |
| 534 |
name: "readme with no status", |
| 535 |
markdown: `# Some component |
| 536 |
|
| 537 |
Some info about a component |
| 538 |
`, |
| 539 |
outputFile: "readme_without_status.md", |
| 540 |
distros: []string{"contrib"}, |
| 541 |
}, |
| 542 |
{ |
| 543 |
name: "component with warnings", |
| 544 |
markdown: `# Some component |
| 545 |
|
| 546 |
<!-- status autogenerated section --> |
| 547 |
<!-- end autogenerated section --> |
| 548 |
|
| 549 |
Some info about a component |
| 550 |
### warnings |
| 551 |
Some warning there. |
| 552 |
`, |
| 553 |
outputFile: "readme_with_warnings.md", |
| 554 |
warnings: []string{"warning1"}, |
| 555 |
distros: []string{"contrib"}, |
| 556 |
}, |
| 557 |
{ |
| 558 |
name: "readme with multiple signals", |
| 559 |
markdown: `# Some component |
| 560 |
|
| 561 |
<!-- status autogenerated section --> |
| 562 |
<!-- end autogenerated section --> |
| 563 |
|
| 564 |
Some info about a component |
| 565 |
`, |
| 566 |
outputFile: "readme_with_multiple_signals.md", |
| 567 |
stability: map[component.StabilityLevel][]string{ |
| 568 |
component.StabilityLevelBeta: {"metrics"}, |
| 569 |
component.StabilityLevelAlpha: {"logs"}, |
| 570 |
}, |
| 571 |
distros: []string{"contrib"}, |
| 572 |
}, |
| 573 |
{ |
| 574 |
name: "readme with multiple signals and deprecation", |
| 575 |
markdown: `# Some component |
| 576 |
|
| 577 |
<!-- status autogenerated section --> |
| 578 |
<!-- end autogenerated section --> |
| 579 |
|
| 580 |
Some info about a component |
| 581 |
`, |
| 582 |
outputFile: "readme_with_multiple_signals_and_deprecation.md", |
| 583 |
stability: map[component.StabilityLevel][]string{ |
| 584 |
component.StabilityLevelBeta: {"metrics"}, |
| 585 |
component.StabilityLevelAlpha: {"logs"}, |
| 586 |
component.StabilityLevelDeprecated: {"traces"}, |
| 587 |
}, |
| 588 |
deprecation: DeprecationMap{ |
| 589 |
"traces": DeprecationInfo{ |
| 590 |
Date: "2025-02-05", |
| 591 |
Migration: "no migration needed", |
| 592 |
}, |
| 593 |
}, |
| 594 |
distros: []string{"contrib"}, |
| 595 |
}, |
| 596 |
{ |
| 597 |
name: "readme with cmd class", |
| 598 |
markdown: `# Some component |
| 599 |
|
| 600 |
<!-- status autogenerated section --> |
| 601 |
<!-- end autogenerated section --> |
| 602 |
|
| 603 |
Some info about a component |
| 604 |
`, |
| 605 |
outputFile: "readme_with_cmd_class.md", |
| 606 |
stability: map[component.StabilityLevel][]string{ |
| 607 |
component.StabilityLevelBeta: {"metrics"}, |
| 608 |
component.StabilityLevelAlpha: {"logs"}, |
| 609 |
}, |
| 610 |
componentClass: "cmd", |
| 611 |
distros: []string{}, |
| 612 |
}, |
| 613 |
} |
| 614 |
for _, tt := range tests { |
| 615 |
t.Run(tt.name, func(t *testing.T) { |
| 616 |
stability := map[component.StabilityLevel][]string{component.StabilityLevelBeta: {"metrics"}} |
| 617 |
if len(tt.stability) > 0 { |
| 618 |
stability = tt.stability |
| 619 |
} |
| 620 |
md := Metadata{ |
| 621 |
GithubProject: tt.githubProject, |
| 622 |
Type: "foo", |
| 623 |
ShortFolderName: "foo", |
| 624 |
Status: &Status{ |
| 625 |
DisableCodeCov: true, |
| 626 |
Stability: stability, |
| 627 |
Distributions: tt.distros, |
| 628 |
Class: tt.componentClass, |
| 629 |
Warnings: tt.warnings, |
| 630 |
Codeowners: tt.codeowners, |
| 631 |
Deprecation: tt.deprecation, |
| 632 |
}, |
| 633 |
} |
| 634 |
tmpdir := t.TempDir() |
| 635 |
|
| 636 |
readmeFile := filepath.Join(tmpdir, "README.md") |
| 637 |
require.NoError(t, os.WriteFile(readmeFile, []byte(tt.markdown), 0o600)) |
| 638 |
|
| 639 |
err := inlineReplace("templates/readme.md.tmpl", readmeFile, md, statusStart, statusEnd, "metadata") |
| 640 |
require.NoError(t, err) |
| 641 |
|
| 642 |
require.FileExists(t, filepath.Join(tmpdir, "README.md")) |
| 643 |
got, err := os.ReadFile(filepath.Join(tmpdir, "README.md")) //nolint:gosec |
| 644 |
require.NoError(t, err) |
| 645 |
got = bytes.ReplaceAll(got, []byte("\r\n"), []byte("\n")) |
| 646 |
expected, err := os.ReadFile(filepath.Join("testdata", tt.outputFile)) |
| 647 |
require.NoError(t, err) |
| 648 |
expected = bytes.ReplaceAll(expected, []byte("\r\n"), []byte("\n")) |
| 649 |
fmt.Println(string(got)) |
| 650 |
fmt.Println(string(expected)) |
| 651 |
require.Equal(t, string(expected), string(got)) |
| 652 |
}) |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
func TestGenerateStatusMetadata(t *testing.T) { |
| 657 |
tests := []struct { |
| 658 |
name string |
| 659 |
output string |
| 660 |
md Metadata |
| 661 |
expected string |
| 662 |
}{ |
| 663 |
{ |
| 664 |
name: "foo component with beta status", |
| 665 |
md: Metadata{ |
| 666 |
Type: "foo", |
| 667 |
Status: &Status{ |
| 668 |
Stability: map[component.StabilityLevel][]string{ |
| 669 |
component.StabilityLevelBeta: {"metrics"}, |
| 670 |
}, |
| 671 |
Distributions: []string{"contrib"}, |
| 672 |
Class: "receiver", |
| 673 |
}, |
| 674 |
}, |
| 675 |
expected: `// Code generated by mdatagen. DO NOT EDIT. |
| 676 |
|
| 677 |
package metadata |
| 678 |
|
| 679 |
import ( |
| 680 |
"go.opentelemetry.io/collector/component" |
| 681 |
) |
| 682 |
|
| 683 |
var ( |
| 684 |
Type = component.MustNewType("foo") |
| 685 |
ScopeName = "" |
| 686 |
) |
| 687 |
|
| 688 |
const ( |
| 689 |
MetricsStability = component.StabilityLevelBeta |
| 690 |
) |
| 691 |
`, |
| 692 |
}, |
| 693 |
{ |
| 694 |
name: "foo component with alpha status", |
| 695 |
md: Metadata{ |
| 696 |
Type: "foo", |
| 697 |
Status: &Status{ |
| 698 |
Stability: map[component.StabilityLevel][]string{ |
| 699 |
component.StabilityLevelAlpha: {"metrics"}, |
| 700 |
}, |
| 701 |
Distributions: []string{"contrib"}, |
| 702 |
Class: "receiver", |
| 703 |
}, |
| 704 |
}, |
| 705 |
expected: `// Code generated by mdatagen. DO NOT EDIT. |
| 706 |
|
| 707 |
package metadata |
| 708 |
|
| 709 |
import ( |
| 710 |
"go.opentelemetry.io/collector/component" |
| 711 |
) |
| 712 |
|
| 713 |
var ( |
| 714 |
Type = component.MustNewType("foo") |
| 715 |
ScopeName = "" |
| 716 |
) |
| 717 |
|
| 718 |
const ( |
| 719 |
MetricsStability = component.StabilityLevelAlpha |
| 720 |
) |
| 721 |
`, |
| 722 |
}, |
| 723 |
} |
| 724 |
|
| 725 |
for _, tt := range tests { |
| 726 |
t.Run(tt.name, func(t *testing.T) { |
| 727 |
tmpdir := t.TempDir() |
| 728 |
err := generateFile("templates/status.go.tmpl", |
| 729 |
filepath.Join(tmpdir, "generated_status.go"), tt.md, "metadata") |
| 730 |
require.NoError(t, err) |
| 731 |
actual, err := os.ReadFile(filepath.Join(tmpdir, "generated_status.go")) //nolint:gosec |
| 732 |
require.NoError(t, err) |
| 733 |
require.Equal(t, tt.expected, string(actual)) |
| 734 |
}) |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
func TestGenerateTelemetryMetadata(t *testing.T) { |
| 739 |
tests := []struct { |
| 740 |
name string |
| 741 |
output string |
| 742 |
md Metadata |
| 743 |
expected string |
| 744 |
}{ |
| 745 |
{ |
| 746 |
name: "foo component with beta status", |
| 747 |
md: Metadata{ |
| 748 |
Type: "foo", |
| 749 |
Status: &Status{ |
| 750 |
Stability: map[component.StabilityLevel][]string{ |
| 751 |
component.StabilityLevelBeta: {"metrics"}, |
| 752 |
}, |
| 753 |
Distributions: []string{"contrib"}, |
| 754 |
Class: "receiver", |
| 755 |
}, |
| 756 |
}, |
| 757 |
expected: `// Code generated by mdatagen. DO NOT EDIT. |
| 758 |
|
| 759 |
package metadata |
| 760 |
|
| 761 |
import ( |
| 762 |
"go.opentelemetry.io/otel/metric" |
| 763 |
"go.opentelemetry.io/otel/trace" |
| 764 |
|
| 765 |
"go.opentelemetry.io/collector/component" |
| 766 |
) |
| 767 |
|
| 768 |
func Meter(settings component.TelemetrySettings) metric.Meter { |
| 769 |
return settings.MeterProvider.Meter("") |
| 770 |
} |
| 771 |
|
| 772 |
func Tracer(settings component.TelemetrySettings) trace.Tracer { |
| 773 |
return settings.TracerProvider.Tracer("") |
| 774 |
} |
| 775 |
`, |
| 776 |
}, |
| 777 |
{ |
| 778 |
name: "foo component with alpha status", |
| 779 |
md: Metadata{ |
| 780 |
Type: "foo", |
| 781 |
Status: &Status{ |
| 782 |
Stability: map[component.StabilityLevel][]string{ |
| 783 |
component.StabilityLevelAlpha: {"metrics"}, |
| 784 |
}, |
| 785 |
Distributions: []string{"contrib"}, |
| 786 |
Class: "receiver", |
| 787 |
}, |
| 788 |
}, |
| 789 |
expected: `// Code generated by mdatagen. DO NOT EDIT. |
| 790 |
|
| 791 |
package metadata |
| 792 |
|
| 793 |
import ( |
| 794 |
"go.opentelemetry.io/otel/metric" |
| 795 |
"go.opentelemetry.io/otel/trace" |
| 796 |
|
| 797 |
"go.opentelemetry.io/collector/component" |
| 798 |
) |
| 799 |
|
| 800 |
func Meter(settings component.TelemetrySettings) metric.Meter { |
| 801 |
return settings.MeterProvider.Meter("") |
| 802 |
} |
| 803 |
|
| 804 |
func Tracer(settings component.TelemetrySettings) trace.Tracer { |
| 805 |
return settings.TracerProvider.Tracer("") |
| 806 |
} |
| 807 |
`, |
| 808 |
}, |
| 809 |
} |
| 810 |
|
| 811 |
for _, tt := range tests { |
| 812 |
t.Run(tt.name, func(t *testing.T) { |
| 813 |
tmpdir := t.TempDir() |
| 814 |
err := generateFile("templates/telemetry.go.tmpl", |
| 815 |
filepath.Join(tmpdir, "generated_telemetry.go"), tt.md, "metadata") |
| 816 |
require.NoError(t, err) |
| 817 |
actual, err := os.ReadFile(filepath.Join(tmpdir, "generated_telemetry.go")) //nolint:gosec |
| 818 |
require.NoError(t, err) |
| 819 |
require.Equal(t, tt.expected, string(actual)) |
| 820 |
}) |
| 821 |
} |
| 822 |
} |