-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
config_test.go
273 lines (258 loc) · 7.56 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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package filestorage
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/google/uuid"
"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/extension"
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/storage/filestorage/internal/metadata"
)
func TestLoadConfig(t *testing.T) {
t.Parallel()
tests := []struct {
id component.ID
expected component.Config
}{
{
id: component.NewID(metadata.Type),
expected: func() component.Config {
ret := NewFactory().CreateDefaultConfig()
ret.(*Config).Directory = "."
return ret
}(),
},
{
id: component.NewIDWithName(metadata.Type, "all_settings"),
expected: &Config{
Directory: ".",
Compaction: &CompactionConfig{
Directory: ".",
OnStart: true,
OnRebound: true,
MaxTransactionSize: 2048,
ReboundTriggerThresholdMiB: 16,
ReboundNeededThresholdMiB: 128,
CheckInterval: time.Second * 5,
CleanupOnStart: true,
},
Timeout: 2 * time.Second,
FSync: true,
CreateDirectory: false,
DirectoryPermissions: "0750",
},
},
}
for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, sub.Unmarshal(cfg))
assert.NoError(t, component.ValidateConfig(cfg))
assert.Equal(t, tt.expected, cfg)
})
}
}
func TestHandleNonExistingDirectoryWithAnError(t *testing.T) {
f := NewFactory()
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = "/not/a/dir"
err := component.ValidateConfig(cfg)
require.Error(t, err)
require.True(t, strings.HasPrefix(err.Error(), "directory must exist: "))
}
func TestHandleProvidingFilePathAsDirWithAnError(t *testing.T) {
f := NewFactory()
cfg := f.CreateDefaultConfig().(*Config)
file, err := os.CreateTemp("", "")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, file.Close())
require.NoError(t, os.Remove(file.Name()))
})
cfg.Directory = file.Name()
err = component.ValidateConfig(cfg)
require.Error(t, err)
require.EqualError(t, err, file.Name()+" is not a directory")
}
func TestDirectoryCreateConfig(t *testing.T) {
tests := []struct {
name string
config func(*testing.T, extension.Factory) *Config
err error
}{
{
name: "create directory true - no error",
config: func(t *testing.T, f extension.Factory) *Config {
storageDir := filepath.Join(t.TempDir(), uuid.NewString())
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = storageDir
cfg.CreateDirectory = true
return cfg
},
err: nil,
},
{
name: "create directory true - no error - 0700 permissions",
config: func(t *testing.T, f extension.Factory) *Config {
storageDir := filepath.Join(t.TempDir(), uuid.NewString())
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = storageDir
cfg.CreateDirectory = true
cfg.DirectoryPermissions = "0700"
return cfg
},
err: nil,
},
{
name: "create directory false - error",
config: func(t *testing.T, f extension.Factory) *Config {
storageDir := filepath.Join(t.TempDir(), uuid.NewString())
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = storageDir
cfg.CreateDirectory = false
return cfg
},
err: os.ErrNotExist,
},
{
name: "create directory true - invalid permissions",
config: func(t *testing.T, f extension.Factory) *Config {
storageDir := filepath.Join(t.TempDir(), uuid.NewString())
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = storageDir
cfg.CreateDirectory = true
cfg.DirectoryPermissions = "invalid string"
return cfg
},
err: errInvalidOctal,
},
{
name: "create directory true - rwxr--r-- (should be octal string)",
config: func(t *testing.T, f extension.Factory) *Config {
storageDir := filepath.Join(t.TempDir(), uuid.NewString())
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = storageDir
cfg.CreateDirectory = true
cfg.DirectoryPermissions = "rwxr--r--"
return cfg
},
err: errInvalidOctal,
},
{
name: "create directory true - 0778 (invalid octal)",
config: func(t *testing.T, f extension.Factory) *Config {
storageDir := filepath.Join(t.TempDir(), uuid.NewString())
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = storageDir
cfg.CreateDirectory = true
cfg.DirectoryPermissions = "0778"
return cfg
},
err: errInvalidOctal,
},
{
name: "create directory true - 07771 (invalid permission bits)",
config: func(t *testing.T, f extension.Factory) *Config {
storageDir := filepath.Join(t.TempDir(), uuid.NewString())
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = storageDir
cfg.CreateDirectory = true
cfg.DirectoryPermissions = "07771"
return cfg
},
err: errInvalidPermissionBits,
},
{
name: "create directory false - 07771 (invalid string) - no error",
config: func(t *testing.T, f extension.Factory) *Config {
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = t.TempDir()
cfg.CreateDirectory = false
cfg.DirectoryPermissions = "07771"
return cfg
},
err: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := NewFactory()
config := tt.config(t, f)
require.ErrorIs(t, config.Validate(), tt.err)
})
}
}
func TestCompactionDirectory(t *testing.T) {
f := NewFactory()
tests := []struct {
name string
config func(*testing.T) *Config
err error
}{
{
name: "directory-must-exists-error",
config: func(t *testing.T) *Config {
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = t.TempDir() // actual directory
cfg.Compaction.Directory = "/not/a/dir" // not a directory
cfg.Compaction.OnRebound = true
cfg.Compaction.OnStart = true
return cfg
},
err: os.ErrNotExist,
},
{
name: "directory-must-exists-error-on-start",
config: func(t *testing.T) *Config {
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = t.TempDir() // actual directory
cfg.Compaction.Directory = "/not/a/dir" // not a directory
cfg.Compaction.OnRebound = false
cfg.Compaction.OnStart = true
return cfg
},
err: os.ErrNotExist,
},
{
name: "directory-must-exists-error-on-rebound",
config: func(t *testing.T) *Config {
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = t.TempDir() // actual directory
cfg.Compaction.Directory = "/not/a/dir" // not a directory
cfg.Compaction.OnRebound = true
cfg.Compaction.OnStart = false
return cfg
},
err: os.ErrNotExist,
},
{
name: "compaction-disabled-no-error",
config: func(t *testing.T) *Config {
cfg := f.CreateDefaultConfig().(*Config)
cfg.Directory = t.TempDir() // actual directory
cfg.Compaction.Directory = "/not/a/dir" // not a directory
cfg.Compaction.OnRebound = false
cfg.Compaction.OnStart = false
return cfg
},
err: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.ErrorIs(t, component.ValidateConfig(test.config(t)), test.err)
})
}
}