-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
examples_test.go
127 lines (117 loc) · 4.36 KB
/
examples_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package datadogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter"
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/otelcol"
"go.opentelemetry.io/collector/otelcol/otelcoltest"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/batchprocessor"
"go.opentelemetry.io/collector/processor/memorylimiterprocessor"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/otlpreceiver"
"gopkg.in/yaml.v2"
"github.com/open-telemetry/opentelemetry-collector-contrib/connector/datadogconnector"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sattributesprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/probabilisticsamplerprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/dockerstatsreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/filelogreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
)
// TestExamples ensures that the configuration in the YAML files can be loaded by the collector. It checks:
// - each *.yaml file in the folder ./examples/*
// - the ./examples/k8s-chart/configmap.yaml file
func TestExamples(t *testing.T) {
factories := newTestComponents(t)
const folder = "./examples"
files, err := os.ReadDir(folder)
require.NoError(t, err)
for _, f := range files {
if f.IsDir() {
continue
}
if filepath.Ext(f.Name()) != ".yaml" {
continue
}
t.Run(filepath.Base(f.Name()), func(t *testing.T) {
t.Setenv("DD_API_KEY", "testvalue")
name := filepath.Join(folder, f.Name())
_, err := otelcoltest.LoadConfigAndValidate(name, factories)
require.NoError(t, err, "All yaml config must validate. Please ensure that all necessary component factories are added in newTestComponents()")
})
}
const chartConfigFile = "./examples/k8s-chart/configmap.yaml"
t.Run(strings.TrimPrefix(chartConfigFile, "./examples/"), func(t *testing.T) {
var out struct {
Kind string `yaml:"kind"`
Data struct {
YAML string `yaml:"otel-agent-config"`
} `yaml:"data"`
}
slurp, err := os.ReadFile(chartConfigFile)
require.NoError(t, err)
err = yaml.Unmarshal(slurp, &out)
require.NoError(t, err)
require.Equal(t, out.Kind, "ConfigMap")
require.NotEmpty(t, out.Data.YAML)
data := []byte(out.Data.YAML)
f, err := os.CreateTemp("", "ddexporter-yaml-test-")
require.NoError(t, err)
n, err := f.Write(data)
require.NoError(t, err)
require.Equal(t, n, len(data))
require.NoError(t, f.Close())
defer os.RemoveAll(f.Name())
_, err = otelcoltest.LoadConfigAndValidate(f.Name(), factories)
require.NoError(t, err, "All yaml config must validate. Please ensure that all necessary component factories are added in newTestComponents()")
})
}
// newTestComponents returns the minimum amount of components necessary for
// running a collector with any of the examples/* yaml configuration files.
func newTestComponents(t *testing.T) otelcol.Factories {
var (
factories otelcol.Factories
err error
)
factories.Receivers, err = receiver.MakeFactoryMap(
[]receiver.Factory{
otlpreceiver.NewFactory(),
hostmetricsreceiver.NewFactory(),
dockerstatsreceiver.NewFactory(),
filelogreceiver.NewFactory(),
prometheusreceiver.NewFactory(),
}...,
)
require.NoError(t, err)
factories.Processors, err = processor.MakeFactoryMap(
[]processor.Factory{
batchprocessor.NewFactory(),
memorylimiterprocessor.NewFactory(),
k8sattributesprocessor.NewFactory(),
resourcedetectionprocessor.NewFactory(),
probabilisticsamplerprocessor.NewFactory(),
}...,
)
require.NoError(t, err)
factories.Connectors, err = connector.MakeFactoryMap(
[]connector.Factory{
datadogconnector.NewFactory(),
}...,
)
require.NoError(t, err)
factories.Exporters, err = exporter.MakeFactoryMap(
[]exporter.Factory{
NewFactory(),
}...,
)
require.NoError(t, err)
return factories
}