-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathconfig_test.go
209 lines (198 loc) · 5.52 KB
/
config_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package fileexporter
import (
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/confmap/xconfmap"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/fileexporter/internal/metadata"
)
func TestLoadConfig(t *testing.T) {
t.Parallel()
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
tests := []struct {
id component.ID
expected component.Config
errorMessage string
}{
{
id: component.NewIDWithName(metadata.Type, "2"),
expected: &Config{
Path: "./filename.json",
Rotation: &Rotation{
MaxMegabytes: 10,
MaxDays: 3,
MaxBackups: 3,
LocalTime: true,
},
FormatType: formatTypeJSON,
FlushInterval: time.Second,
GroupBy: &GroupBy{
MaxOpenFiles: defaultMaxOpenFiles,
ResourceAttribute: defaultResourceAttribute,
},
},
},
{
id: component.NewIDWithName(metadata.Type, "3"),
expected: &Config{
Path: "./filename",
Rotation: &Rotation{
MaxMegabytes: 10,
MaxDays: 3,
MaxBackups: 3,
LocalTime: true,
},
FormatType: formatTypeProto,
Compression: compressionZSTD,
FlushInterval: time.Second,
GroupBy: &GroupBy{
MaxOpenFiles: defaultMaxOpenFiles,
ResourceAttribute: defaultResourceAttribute,
},
},
},
{
id: component.NewIDWithName(metadata.Type, "rotation_with_default_settings"),
expected: &Config{
Path: "./foo",
FormatType: formatTypeJSON,
Rotation: &Rotation{
MaxBackups: defaultMaxBackups,
},
FlushInterval: time.Second,
GroupBy: &GroupBy{
MaxOpenFiles: defaultMaxOpenFiles,
ResourceAttribute: defaultResourceAttribute,
},
},
},
{
id: component.NewIDWithName(metadata.Type, "rotation_with_custom_settings"),
expected: &Config{
Path: "./foo",
Rotation: &Rotation{
MaxMegabytes: 1234,
MaxBackups: defaultMaxBackups,
},
FormatType: formatTypeJSON,
FlushInterval: time.Second,
GroupBy: &GroupBy{
MaxOpenFiles: defaultMaxOpenFiles,
ResourceAttribute: defaultResourceAttribute,
},
},
},
{
id: component.NewIDWithName(metadata.Type, "compression_error"),
errorMessage: "compression is not supported",
},
{
id: component.NewIDWithName(metadata.Type, "format_error"),
errorMessage: "format type is not supported",
},
{
id: component.NewIDWithName(metadata.Type, "flush_interval_5"),
expected: &Config{
Path: "./flushed",
FlushInterval: 5,
FormatType: formatTypeJSON,
GroupBy: &GroupBy{
MaxOpenFiles: defaultMaxOpenFiles,
ResourceAttribute: defaultResourceAttribute,
},
},
},
{
id: component.NewIDWithName(metadata.Type, "flush_interval_5s"),
expected: &Config{
Path: "./flushed",
FlushInterval: 5 * time.Second,
FormatType: formatTypeJSON,
GroupBy: &GroupBy{
MaxOpenFiles: defaultMaxOpenFiles,
ResourceAttribute: defaultResourceAttribute,
},
},
},
{
id: component.NewIDWithName(metadata.Type, "flush_interval_500ms"),
expected: &Config{
Path: "./flushed",
FlushInterval: 500 * time.Millisecond,
FormatType: formatTypeJSON,
GroupBy: &GroupBy{
MaxOpenFiles: defaultMaxOpenFiles,
ResourceAttribute: defaultResourceAttribute,
},
},
},
{
id: component.NewIDWithName(metadata.Type, "flush_interval_negative_value"),
errorMessage: "flush_interval must be larger than zero",
},
{
id: component.NewIDWithName(metadata.Type, ""),
errorMessage: "path must be non-empty",
},
{
id: component.NewIDWithName(metadata.Type, "group_by"),
expected: &Config{
Path: "./group_by/*.json",
FlushInterval: time.Second,
FormatType: formatTypeJSON,
GroupBy: &GroupBy{
Enabled: true,
MaxOpenFiles: 10,
ResourceAttribute: "dummy",
},
},
},
{
id: component.NewIDWithName(metadata.Type, "group_by_defaults"),
expected: &Config{
Path: "./group_by/*.json",
FlushInterval: time.Second,
FormatType: formatTypeJSON,
GroupBy: &GroupBy{
Enabled: true,
MaxOpenFiles: defaultMaxOpenFiles,
ResourceAttribute: defaultResourceAttribute,
},
},
},
{
id: component.NewIDWithName(metadata.Type, "group_by_invalid_path"),
errorMessage: "path must contain exactly one * when group_by is enabled",
},
{
id: component.NewIDWithName(metadata.Type, "group_by_invalid_path2"),
errorMessage: "path must not start with * when group_by is enabled",
},
{
id: component.NewIDWithName(metadata.Type, "group_by_empty_resource_attribute"),
errorMessage: "resource_attribute must not be empty when group_by is enabled",
},
}
for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, sub.Unmarshal(cfg))
if tt.expected == nil {
assert.EqualError(t, xconfmap.Validate(cfg), tt.errorMessage)
return
}
assert.NoError(t, xconfmap.Validate(cfg))
assert.Equal(t, tt.expected, cfg)
})
}
}