forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory_test.go
170 lines (159 loc) · 3.8 KB
/
factory_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package fileexporter
import (
"context"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/exporter/exportertest"
"gopkg.in/natefinch/lumberjack.v2"
)
func TestCreateDefaultConfig(t *testing.T) {
cfg := createDefaultConfig()
assert.NotNil(t, cfg, "failed to create default config")
assert.NoError(t, componenttest.CheckConfigStruct(cfg))
}
func TestCreateMetricsExporterError(t *testing.T) {
cfg := &Config{
FormatType: formatTypeJSON,
}
_, err := createMetricsExporter(
context.Background(),
exportertest.NewNopCreateSettings(),
cfg)
assert.Error(t, err)
}
func TestCreateMetricsExporter(t *testing.T) {
cfg := &Config{
FormatType: formatTypeJSON,
Path: tempFileName(t),
}
exp, err := createMetricsExporter(
context.Background(),
exportertest.NewNopCreateSettings(),
cfg)
assert.NoError(t, err)
require.NotNil(t, exp)
assert.NoError(t, exp.Shutdown(context.Background()))
}
func TestCreateTracesExporter(t *testing.T) {
cfg := &Config{
FormatType: formatTypeJSON,
Path: tempFileName(t),
}
exp, err := createTracesExporter(
context.Background(),
exportertest.NewNopCreateSettings(),
cfg)
assert.NoError(t, err)
require.NotNil(t, exp)
assert.NoError(t, exp.Shutdown(context.Background()))
}
func TestCreateTracesExporterError(t *testing.T) {
cfg := &Config{
FormatType: formatTypeJSON,
}
_, err := createTracesExporter(
context.Background(),
exportertest.NewNopCreateSettings(),
cfg)
assert.Error(t, err)
}
func TestCreateLogsExporter(t *testing.T) {
cfg := &Config{
FormatType: formatTypeJSON,
Path: tempFileName(t),
}
exp, err := createLogsExporter(
context.Background(),
exportertest.NewNopCreateSettings(),
cfg)
assert.NoError(t, err)
require.NotNil(t, exp)
assert.NoError(t, exp.Shutdown(context.Background()))
}
func TestCreateLogsExporterError(t *testing.T) {
cfg := &Config{
FormatType: formatTypeJSON,
}
_, err := createLogsExporter(
context.Background(),
exportertest.NewNopCreateSettings(),
cfg)
assert.Error(t, err)
}
func TestBuildFileWriter(t *testing.T) {
type args struct {
cfg *Config
}
tests := []struct {
name string
args args
want io.WriteCloser
validate func(*testing.T, io.WriteCloser)
}{
{
name: "single file",
args: args{
cfg: &Config{
Path: tempFileName(t),
},
},
validate: func(t *testing.T, closer io.WriteCloser) {
_, ok := closer.(*bufferedWriteCloser)
assert.Equal(t, true, ok)
},
},
{
name: "rotation file",
args: args{
cfg: &Config{
Path: tempFileName(t),
Rotation: &Rotation{
MaxBackups: defaultMaxBackups,
},
},
},
validate: func(t *testing.T, closer io.WriteCloser) {
writer, ok := closer.(*lumberjack.Logger)
assert.Equal(t, true, ok)
assert.Equal(t, defaultMaxBackups, writer.MaxBackups)
},
},
{
name: "rotation file with user's configuration",
args: args{
cfg: &Config{
Path: tempFileName(t),
Rotation: &Rotation{
MaxMegabytes: 30,
MaxDays: 100,
MaxBackups: 3,
LocalTime: true,
},
},
},
validate: func(t *testing.T, closer io.WriteCloser) {
writer, ok := closer.(*lumberjack.Logger)
assert.Equal(t, true, ok)
assert.Equal(t, 3, writer.MaxBackups)
assert.Equal(t, 30, writer.MaxSize)
assert.Equal(t, 100, writer.MaxAge)
assert.Equal(t, true, writer.LocalTime)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := buildFileWriter(tt.args.cfg)
defer func() {
assert.NoError(t, got.Close())
}()
assert.NoError(t, err)
tt.validate(t, got)
})
}
}