-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
152 lines (133 loc) · 3.72 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
// Copyright (c) 2025 Z5Labs and Contributors
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package humus
import (
"context"
"errors"
"testing"
"github.com/z5labs/humus/config"
"github.com/stretchr/testify/assert"
bedrockcfg "github.com/z5labs/bedrock/config"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"google.golang.org/grpc"
)
func TestConfig_InitializeOTel(t *testing.T) {
t.Run("will not return an error", func(t *testing.T) {
t.Run("with the default parameters", func(t *testing.T) {
m, err := bedrockcfg.Read(DefaultConfig())
if !assert.Nil(t, err) {
return
}
var cfg Config
err = m.Unmarshal(&cfg)
if !assert.Nil(t, err) {
return
}
err = cfg.InitializeOTel(context.Background())
if !assert.Nil(t, err) {
return
}
})
})
}
func TestTraceProviderInitializer(t *testing.T) {
t.Run("will return an error", func(t *testing.T) {
t.Run("if it is enabled but no grpc.ClientConn has been initialized", func(t *testing.T) {
tpi := traceProviderInitializer{
cfg: config.Trace{
Enabled: true,
},
cc: nil,
}
err := tpi.Init(context.Background())
if !assert.ErrorIs(t, err, ErrOTLPMustBeEnabled) {
return
}
})
t.Run("if it fails to create the exporter", func(t *testing.T) {
newExporterErr := errors.New("failed to create exporter")
tpi := traceProviderInitializer{
cfg: config.Trace{
Enabled: true,
},
cc: &grpc.ClientConn{},
newExporter: func(ctx context.Context, o ...otlptracegrpc.Option) (*otlptrace.Exporter, error) {
return nil, newExporterErr
},
}
err := tpi.Init(context.Background())
if !assert.ErrorIs(t, err, newExporterErr) {
return
}
})
})
}
func TestMeterProviderInitializer(t *testing.T) {
t.Run("will return an error", func(t *testing.T) {
t.Run("if it is enabled but no grpc.ClientConn has been initialized", func(t *testing.T) {
mpi := meterProviderInitializer{
cfg: config.Metric{
Enabled: true,
},
cc: nil,
}
err := mpi.Init(context.Background())
if !assert.ErrorIs(t, err, ErrOTLPMustBeEnabled) {
return
}
})
t.Run("if it fails to create the exporter", func(t *testing.T) {
newExporterErr := errors.New("failed to create exporter")
mpi := meterProviderInitializer{
cfg: config.Metric{
Enabled: true,
},
cc: &grpc.ClientConn{},
newExporter: func(ctx context.Context, o ...otlpmetricgrpc.Option) (*otlpmetricgrpc.Exporter, error) {
return nil, newExporterErr
},
}
err := mpi.Init(context.Background())
if !assert.ErrorIs(t, err, newExporterErr) {
return
}
})
})
}
func TestLogProviderInitializer(t *testing.T) {
t.Run("will return an error", func(t *testing.T) {
t.Run("if it is enabled but no grpc.ClientConn has been initialized", func(t *testing.T) {
lpi := logProviderInitializer{
cfg: config.Log{
Enabled: true,
},
cc: nil,
}
err := lpi.Init(context.Background())
if !assert.ErrorIs(t, err, ErrOTLPMustBeEnabled) {
return
}
})
t.Run("if it fails to create the exporter", func(t *testing.T) {
newExporterErr := errors.New("failed to create exporter")
lpi := logProviderInitializer{
cfg: config.Log{
Enabled: true,
},
cc: &grpc.ClientConn{},
newExporter: func(ctx context.Context, o ...otlploggrpc.Option) (*otlploggrpc.Exporter, error) {
return nil, newExporterErr
},
}
err := lpi.Init(context.Background())
if !assert.ErrorIs(t, err, newExporterErr) {
return
}
})
})
}