@@ -17,19 +17,36 @@ import (
1717 "go.opentelemetry.io/collector/config/confighttp"
1818 "go.opentelemetry.io/collector/config/confignet"
1919 "go.opentelemetry.io/collector/config/configopaque"
20+ "go.opentelemetry.io/collector/config/configoptional"
2021 "go.opentelemetry.io/collector/config/configtls"
2122 "go.opentelemetry.io/collector/confmap"
2223 "go.opentelemetry.io/collector/confmap/confmaptest"
2324 "go.opentelemetry.io/collector/confmap/xconfmap"
2425)
2526
27+ // GetOrInsertDefault is a helper function to get or insert a default value for a configoptional.Optional type.
28+ func GetOrInsertDefault [T any ](t * testing.T , opt * configoptional.Optional [T ]) * T {
29+ if opt .HasValue () {
30+ return opt .Get ()
31+ }
32+
33+ empty := confmap .NewFromStringMap (map [string ]any {})
34+ require .NoError (t , empty .Unmarshal (opt ))
35+ val := opt .Get ()
36+ require .NotNil (t , "Expected a default value to be set for %T" , val )
37+ return val
38+ }
39+
2640func TestUnmarshalDefaultConfig (t * testing.T ) {
2741 cm , err := confmaptest .LoadConf (filepath .Join ("testdata" , "default.yaml" ))
2842 require .NoError (t , err )
2943 factory := NewFactory ()
3044 cfg := factory .CreateDefaultConfig ()
3145 require .NoError (t , cm .Unmarshal (& cfg ))
32- assert .Equal (t , factory .CreateDefaultConfig (), cfg )
46+ expectedCfg := factory .CreateDefaultConfig ().(* Config )
47+ GetOrInsertDefault (t , & expectedCfg .GRPC )
48+ GetOrInsertDefault (t , & expectedCfg .HTTP )
49+ assert .Equal (t , expectedCfg , cfg )
3350}
3451
3552func TestUnmarshalConfigOnlyGRPC (t * testing.T ) {
@@ -40,7 +57,7 @@ func TestUnmarshalConfigOnlyGRPC(t *testing.T) {
4057 require .NoError (t , cm .Unmarshal (& cfg ))
4158
4259 defaultOnlyGRPC := factory .CreateDefaultConfig ().(* Config )
43- defaultOnlyGRPC .HTTP = nil
60+ GetOrInsertDefault ( t , & defaultOnlyGRPC .GRPC )
4461 assert .Equal (t , defaultOnlyGRPC , cfg )
4562}
4663
@@ -52,7 +69,7 @@ func TestUnmarshalConfigOnlyHTTP(t *testing.T) {
5269 require .NoError (t , cm .Unmarshal (& cfg ))
5370
5471 defaultOnlyHTTP := factory .CreateDefaultConfig ().(* Config )
55- defaultOnlyHTTP .GRPC = nil
72+ GetOrInsertDefault ( t , & defaultOnlyHTTP .HTTP )
5673 assert .Equal (t , defaultOnlyHTTP , cfg )
5774}
5875
@@ -64,7 +81,7 @@ func TestUnmarshalConfigOnlyHTTPNull(t *testing.T) {
6481 require .NoError (t , cm .Unmarshal (& cfg ))
6582
6683 defaultOnlyHTTP := factory .CreateDefaultConfig ().(* Config )
67- defaultOnlyHTTP .GRPC = nil
84+ GetOrInsertDefault ( t , & defaultOnlyHTTP .HTTP )
6885 assert .Equal (t , defaultOnlyHTTP , cfg )
6986}
7087
@@ -76,7 +93,7 @@ func TestUnmarshalConfigOnlyHTTPEmptyMap(t *testing.T) {
7693 require .NoError (t , cm .Unmarshal (& cfg ))
7794
7895 defaultOnlyHTTP := factory .CreateDefaultConfig ().(* Config )
79- defaultOnlyHTTP .GRPC = nil
96+ GetOrInsertDefault ( t , & defaultOnlyHTTP .HTTP )
8097 assert .Equal (t , defaultOnlyHTTP , cfg )
8198}
8299
@@ -89,7 +106,7 @@ func TestUnmarshalConfig(t *testing.T) {
89106 assert .Equal (t ,
90107 & Config {
91108 Protocols : Protocols {
92- GRPC : & configgrpc.ServerConfig {
109+ GRPC : configoptional . Some ( configgrpc.ServerConfig {
93110 NetAddr : confignet.AddrConfig {
94111 Endpoint : "localhost:4317" ,
95112 Transport : confignet .TransportTypeTCP ,
@@ -117,8 +134,8 @@ func TestUnmarshalConfig(t *testing.T) {
117134 PermitWithoutStream : true ,
118135 },
119136 },
120- },
121- HTTP : & HTTPConfig {
137+ }) ,
138+ HTTP : configoptional . Some ( HTTPConfig {
122139 ServerConfig : confighttp.ServerConfig {
123140 Auth : & confighttp.AuthConfig {
124141 Config : configauth.Config {
@@ -141,7 +158,7 @@ func TestUnmarshalConfig(t *testing.T) {
141158 TracesURLPath : "/traces" ,
142159 MetricsURLPath : "/v2/metrics" ,
143160 LogsURLPath : "/log/ingest" ,
144- },
161+ }) ,
145162 },
146163 }, cfg )
147164}
@@ -155,15 +172,15 @@ func TestUnmarshalConfigUnix(t *testing.T) {
155172 assert .Equal (t ,
156173 & Config {
157174 Protocols : Protocols {
158- GRPC : & configgrpc.ServerConfig {
175+ GRPC : configoptional . Some ( configgrpc.ServerConfig {
159176 NetAddr : confignet.AddrConfig {
160177 Endpoint : "/tmp/grpc_otlp.sock" ,
161178 Transport : confignet .TransportTypeUnix ,
162179 },
163180 ReadBufferSize : 512 * 1024 ,
164181 Keepalive : configgrpc .NewDefaultKeepaliveServerConfig (),
165- },
166- HTTP : & HTTPConfig {
182+ }) ,
183+ HTTP : configoptional . Some ( HTTPConfig {
167184 ServerConfig : confighttp.ServerConfig {
168185 Endpoint : "/tmp/http_otlp.sock" ,
169186 CORS : confighttp .NewDefaultCORSConfig (),
@@ -172,7 +189,7 @@ func TestUnmarshalConfigUnix(t *testing.T) {
172189 TracesURLPath : defaultTracesURLPath ,
173190 MetricsURLPath : defaultMetricsURLPath ,
174191 LogsURLPath : defaultLogsURLPath ,
175- },
192+ }) ,
176193 },
177194 }, cfg )
178195}
0 commit comments