examples_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package datadogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter"
  4. import (
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/exporter"
  11. "go.opentelemetry.io/collector/otelcol"
  12. "go.opentelemetry.io/collector/otelcol/otelcoltest"
  13. "go.opentelemetry.io/collector/processor"
  14. "go.opentelemetry.io/collector/processor/batchprocessor"
  15. "go.opentelemetry.io/collector/receiver"
  16. "go.opentelemetry.io/collector/receiver/otlpreceiver"
  17. "gopkg.in/yaml.v2"
  18. "github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sattributesprocessor"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor"
  20. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/dockerstatsreceiver"
  21. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/filelogreceiver"
  22. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver"
  23. )
  24. // TestExamples ensures that the configuration in the YAML files can be loaded by the collector. It checks:
  25. // - each *.yaml file in the folder ./examples/*
  26. // - the ./examples/k8s-chart/configmap.yaml file
  27. func TestExamples(t *testing.T) {
  28. factories := newTestComponents(t)
  29. const folder = "./examples"
  30. files, err := os.ReadDir(folder)
  31. require.NoError(t, err)
  32. for _, f := range files {
  33. if f.IsDir() {
  34. continue
  35. }
  36. if filepath.Ext(f.Name()) != ".yaml" {
  37. continue
  38. }
  39. t.Run(filepath.Base(f.Name()), func(t *testing.T) {
  40. t.Setenv("DD_API_KEY", "testvalue")
  41. name := filepath.Join(folder, f.Name())
  42. _, err := otelcoltest.LoadConfigAndValidate(name, factories)
  43. require.NoError(t, err, "All yaml config must validate. Please ensure that all necessary component factories are added in newTestComponents()")
  44. })
  45. }
  46. const chartConfigFile = "./examples/k8s-chart/configmap.yaml"
  47. t.Run(strings.TrimPrefix(chartConfigFile, "./examples/"), func(t *testing.T) {
  48. var out struct {
  49. Kind string `yaml:"kind"`
  50. Data struct {
  51. YAML string `yaml:"otel-agent-config"`
  52. } `yaml:"data"`
  53. }
  54. slurp, err := os.ReadFile(chartConfigFile)
  55. require.NoError(t, err)
  56. err = yaml.Unmarshal(slurp, &out)
  57. require.NoError(t, err)
  58. require.Equal(t, out.Kind, "ConfigMap")
  59. require.NotEmpty(t, out.Data.YAML)
  60. data := []byte(out.Data.YAML)
  61. f, err := os.CreateTemp("", "ddexporter-yaml-test-")
  62. require.NoError(t, err)
  63. n, err := f.Write(data)
  64. require.NoError(t, err)
  65. require.Equal(t, n, len(data))
  66. require.NoError(t, f.Close())
  67. defer os.RemoveAll(f.Name())
  68. _, err = otelcoltest.LoadConfigAndValidate(f.Name(), factories)
  69. require.NoError(t, err, "All yaml config must validate. Please ensure that all necessary component factories are added in newTestComponents()")
  70. })
  71. }
  72. // newTestComponents returns the minimum amount of components necessary for
  73. // running a collector with any of the examples/* yaml configuration files.
  74. func newTestComponents(t *testing.T) otelcol.Factories {
  75. var (
  76. factories otelcol.Factories
  77. err error
  78. )
  79. factories.Receivers, err = receiver.MakeFactoryMap(
  80. []receiver.Factory{
  81. otlpreceiver.NewFactory(),
  82. hostmetricsreceiver.NewFactory(),
  83. dockerstatsreceiver.NewFactory(),
  84. filelogreceiver.NewFactory(),
  85. }...,
  86. )
  87. require.NoError(t, err)
  88. factories.Processors, err = processor.MakeFactoryMap(
  89. []processor.Factory{
  90. batchprocessor.NewFactory(),
  91. k8sattributesprocessor.NewFactory(),
  92. resourcedetectionprocessor.NewFactory(),
  93. }...,
  94. )
  95. require.NoError(t, err)
  96. factories.Exporters, err = exporter.MakeFactoryMap(
  97. []exporter.Factory{
  98. NewFactory(),
  99. }...,
  100. )
  101. require.NoError(t, err)
  102. return factories
  103. }