forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
482 lines (450 loc) · 13 KB
/
main_test.go
File metadata and controls
482 lines (450 loc) · 13 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package builder
import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"golang.org/x/mod/modfile"
)
const (
goModTestFile = `// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
module go.opentelemetry.io/collector/cmd/builder/internal/tester
go 1.20
require (
go.opentelemetry.io/collector/component v0.96.0
go.opentelemetry.io/collector/connector v0.94.1
go.opentelemetry.io/collector/exporter v0.94.1
go.opentelemetry.io/collector/extension v0.94.1
go.opentelemetry.io/collector/otelcol v0.94.1
go.opentelemetry.io/collector/processor v0.94.1
go.opentelemetry.io/collector/receiver v0.94.1
go.opentelemetry.io/collector v0.96.0
)`
modulePrefix = "go.opentelemetry.io/collector"
)
var replaceModules = []string{
"",
"/component",
"/component/componentstatus",
"/component/componenttest",
"/client",
"/config/configauth",
"/config/configcompression",
"/config/configgrpc",
"/config/confighttp",
"/config/configmiddleware",
"/config/confignet",
"/config/configopaque",
"/config/configoptional",
"/config/configretry",
"/config/configtelemetry",
"/config/configtls",
"/confmap",
"/confmap/xconfmap",
"/confmap/provider/envprovider",
"/confmap/provider/fileprovider",
"/confmap/provider/httpprovider",
"/confmap/provider/httpsprovider",
"/confmap/provider/yamlprovider",
"/consumer",
"/consumer/consumererror",
"/consumer/consumererror/xconsumererror",
"/consumer/xconsumer",
"/consumer/consumertest",
"/connector",
"/connector/connectortest",
"/connector/xconnector",
"/exporter",
"/exporter/debugexporter",
"/exporter/xexporter",
"/exporter/exportertest",
"/exporter/exporterhelper/xexporterhelper",
"/exporter/nopexporter",
"/exporter/otlpexporter",
"/exporter/otlphttpexporter",
"/extension",
"/extension/extensionauth",
"/extension/extensionauth/extensionauthtest",
"/extension/extensioncapabilities",
"/extension/extensionmiddleware",
"/extension/extensionmiddleware/extensionmiddlewaretest",
"/extension/extensiontest",
"/extension/zpagesextension",
"/extension/xextension",
"/featuregate",
"/internal/memorylimiter",
"/internal/fanoutconsumer",
"/internal/sharedcomponent",
"/internal/telemetry",
"/otelcol",
"/pdata",
"/pdata/testdata",
"/pdata/pprofile",
"/pdata/xpdata",
"/pipeline",
"/pipeline/xpipeline",
"/processor",
"/processor/processortest",
"/processor/batchprocessor",
"/processor/memorylimiterprocessor",
"/processor/processorhelper",
"/processor/processorhelper/xprocessorhelper",
"/processor/xprocessor",
"/receiver",
"/receiver/nopreceiver",
"/receiver/otlpreceiver",
"/receiver/receivertest",
"/receiver/receiverhelper",
"/receiver/xreceiver",
"/service",
"/service/hostcapabilities",
}
func newTestConfig(tb testing.TB) *Config {
cfg, err := NewDefaultConfig()
require.NoError(tb, err)
cfg.downloadModules.wait = 0
cfg.downloadModules.numRetries = 1
return cfg
}
func newInitializedConfig(t *testing.T) *Config {
cfg := newTestConfig(t)
// Validate and ParseModules will be called before the config is
// given to Generate.
assert.NoError(t, cfg.Validate())
assert.NoError(t, cfg.ParseModules())
return cfg
}
func TestGenerateDefault(t *testing.T) {
require.NoError(t, Generate(newInitializedConfig(t)))
}
func TestGenerateInvalidOutputPath(t *testing.T) {
cfg := newInitializedConfig(t)
cfg.Distribution.OutputPath = ":/invalid"
err := Generate(cfg)
require.ErrorContains(t, err, "failed to create output path")
}
func TestVersioning(t *testing.T) {
replaces := generateReplaces()
tests := []struct {
name string
cfgBuilder func() *Config
expectedErr error
}{
{
name: "defaults",
cfgBuilder: func() *Config {
cfg := newTestConfig(t)
cfg.Distribution.Go = "go"
cfg.Replaces = append(cfg.Replaces, replaces...)
return cfg
},
expectedErr: nil,
},
{
name: "only gomod file, skip generate",
cfgBuilder: func() *Config {
cfg := newTestConfig(t)
tempDir := t.TempDir()
err := makeModule(tempDir, []byte(goModTestFile))
require.NoError(t, err)
cfg.Distribution.OutputPath = tempDir
cfg.SkipGenerate = true
cfg.Distribution.Go = "go"
return cfg
},
expectedErr: ErrDepNotFound,
},
{
name: "old component version",
cfgBuilder: func() *Config {
cfg := newTestConfig(t)
cfg.Distribution.Go = "go"
cfg.Exporters = []Module{
{
GoMod: "go.opentelemetry.io/collector/exporter/otlpexporter v0.112.0",
},
}
cfg.ConfmapProviders = []Module{}
cfg.Replaces = append(cfg.Replaces, replaces...)
return cfg
},
expectedErr: nil,
},
{
name: "old component version without strict mode",
cfgBuilder: func() *Config {
cfg := newTestConfig(t)
cfg.Distribution.Go = "go"
cfg.SkipStrictVersioning = true
cfg.Exporters = []Module{
{
GoMod: "go.opentelemetry.io/collector/exporter/otlpexporter v0.112.0",
},
}
cfg.ConfmapProviders = []Module{}
cfg.Replaces = append(cfg.Replaces, replaces...)
return cfg
},
expectedErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := tt.cfgBuilder()
require.NoError(t, cfg.Validate())
require.NoError(t, cfg.ParseModules())
err := GenerateAndCompile(cfg)
require.ErrorIs(t, err, tt.expectedErr)
})
}
}
func TestSkipGenerate(t *testing.T) {
cfg := newInitializedConfig(t)
cfg.Distribution.OutputPath = t.TempDir()
cfg.SkipGenerate = true
err := Generate(cfg)
require.NoError(t, err)
outputFile, err := os.Open(cfg.Distribution.OutputPath)
defer func() {
require.NoError(t, outputFile.Close())
}()
require.NoError(t, err)
_, err = outputFile.Readdirnames(1)
require.ErrorIs(t, err, io.EOF, "skip generate should leave output directory empty")
}
func TestGenerateAndCompile(t *testing.T) {
replaces := generateReplaces()
testCases := []struct {
name string
cfgBuilder func(t *testing.T) *Config
}{
{
name: "Default Configuration Compilation",
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig(t)
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
return cfg
},
},
{
name: "LDFlags Compilation",
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig(t)
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
cfg.LDSet = true
cfg.LDFlags = `-X "test.gitVersion=0743dc6c6411272b98494a9b32a63378e84c34da" -X "test.gitTag=local-testing" -X "test.goVersion=go version go1.20.7 darwin/amd64"`
return cfg
},
},
{
name: "GCFlags Compilation",
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig(t)
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
cfg.GCSet = true
cfg.GCFlags = `all=-N -l`
return cfg
},
},
{
name: "Build Tags Compilation",
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig(t)
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
cfg.Distribution.BuildTags = "customTag"
return cfg
},
},
{
name: "Debug Compilation",
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig(t)
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
cfg.Logger = zap.NewNop()
cfg.Distribution.DebugCompilation = true
return cfg
},
},
{
name: "No providers",
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig(t)
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
cfg.ConfmapProviders = []Module{}
return cfg
},
},
{
name: "With confmap factories",
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig(t)
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
cfg.SkipStrictVersioning = true
return cfg
},
},
{
name: "ConfResolverDefaultURIScheme set",
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig(t)
cfg.ConfResolver = ConfResolver{
DefaultURIScheme: "env",
}
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
return cfg
},
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
cfg := tt.cfgBuilder(t)
assert.NoError(t, cfg.Validate())
assert.NoError(t, cfg.SetGoPath())
assert.NoError(t, cfg.ParseModules())
require.NoError(t, GenerateAndCompile(cfg))
})
}
}
// Test that the go.mod files that other tests in this file
// may generate have all their modules covered by our
// "replace" statements created in `generateReplaces`.
//
// An incomplete set of replace statements in these tests
// may cause them to fail during the release process, when
// the local version of modules in the release branch is
// not yet available on the Go package repository.
// Unless the replace statements route all modules to the
// local copy, `go get` will try to fetch the unreleased
// version remotely and some tests will fail.
func TestReplaceStatementsAreComplete(t *testing.T) {
workspaceDir := getWorkspaceDir()
replaceMods := map[string]bool{}
for _, suffix := range replaceModules {
replaceMods[modulePrefix+suffix] = false
}
for _, mod := range replaceModules {
verifyGoMod(t, workspaceDir+mod, replaceMods)
}
var err error
dir := t.TempDir()
cfg, err := NewDefaultConfig()
require.NoError(t, err)
cfg.Distribution.Go = "go"
cfg.Distribution.OutputPath = dir
cfg.Replaces = append(cfg.Replaces, generateReplaces()...)
// Configure all components that we want to use elsewhere in these tests.
// This ensures the resulting go.mod file has maximum coverage of modules
// that exist in the Core repository.
usedNames := make(map[string]int)
cfg.Exporters, err = parseModules([]Module{
{
GoMod: "go.opentelemetry.io/collector/exporter/debugexporter v1.9999.9999",
},
{
GoMod: "go.opentelemetry.io/collector/exporter/nopexporter v1.9999.9999",
},
{
GoMod: "go.opentelemetry.io/collector/exporter/otlpexporter v1.9999.9999",
},
{
GoMod: "go.opentelemetry.io/collector/exporter/otlphttpexporter v1.9999.9999",
},
}, usedNames)
require.NoError(t, err)
cfg.Receivers, err = parseModules([]Module{
{
GoMod: "go.opentelemetry.io/collector/receiver/nopreceiver v1.9999.9999",
},
{
GoMod: "go.opentelemetry.io/collector/receiver/otlpreceiver v1.9999.9999",
},
}, usedNames)
require.NoError(t, err)
cfg.Extensions, err = parseModules([]Module{
{
GoMod: "go.opentelemetry.io/collector/extension/zpagesextension v1.9999.9999",
},
}, usedNames)
require.NoError(t, err)
cfg.Processors, err = parseModules([]Module{
{
GoMod: "go.opentelemetry.io/collector/processor/batchprocessor v1.9999.9999",
},
{
GoMod: "go.opentelemetry.io/collector/processor/memorylimiterprocessor v1.9999.9999",
},
}, usedNames)
require.NoError(t, err)
require.NoError(t, cfg.Validate())
require.NoError(t, cfg.ParseModules())
err = GenerateAndCompile(cfg)
require.NoError(t, err)
verifyGoMod(t, dir, replaceMods)
for k, v := range replaceMods {
assert.Truef(t, v, "Module not used: %s", k)
}
}
func verifyGoMod(t *testing.T, dir string, replaceMods map[string]bool) {
gomodpath := path.Join(dir, "go.mod")
//nolint:gosec // #nosec G304 We control this path and generate the file inside, so we can assume it is safe.
gomod, err := os.ReadFile(gomodpath)
require.NoError(t, err)
mod, err := modfile.Parse(gomodpath, gomod, nil)
require.NoError(t, err)
for _, req := range mod.Require {
if !strings.HasPrefix(req.Mod.Path, modulePrefix) {
continue
}
_, ok := replaceMods[req.Mod.Path]
assert.Truef(t, ok, "Module missing from replace statements list: %s", req.Mod.Path)
replaceMods[req.Mod.Path] = true
}
}
func makeModule(dir string, fileContents []byte) error {
// if the file does not exist, try to create it
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err = os.Mkdir(dir, 0o750); err != nil {
return fmt.Errorf("failed to create output path: %w", err)
}
} else if err != nil {
return fmt.Errorf("failed to create output path: %w", err)
}
err := os.WriteFile(filepath.Clean(filepath.Join(dir, "go.mod")), fileContents, 0o600)
if err != nil {
return fmt.Errorf("failed to write go.mod file: %w", err)
}
return nil
}
func generateReplaces() []string {
workspaceDir := getWorkspaceDir()
modules := replaceModules
replaces := make([]string, len(modules))
for i, mod := range modules {
replaces[i] = fmt.Sprintf("%s%s => %s%s", modulePrefix, mod, workspaceDir, mod)
}
return replaces
}
func getWorkspaceDir() string {
// This is dependent on the current file structure.
// The goal is find the root of the repo so we can replace the root module.
_, thisFile, _, _ := runtime.Caller(0)
return filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(thisFile)))))
}