| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otlpreceiver |
| 5 |
|
| 6 |
import ( |
| 7 |
"path/filepath" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/component" |
| 15 |
"go.opentelemetry.io/collector/config/configauth" |
| 16 |
"go.opentelemetry.io/collector/config/configgrpc" |
| 17 |
"go.opentelemetry.io/collector/config/confighttp" |
| 18 |
"go.opentelemetry.io/collector/config/confignet" |
| 19 |
"go.opentelemetry.io/collector/config/configoptional" |
| 20 |
"go.opentelemetry.io/collector/config/configtls" |
| 21 |
"go.opentelemetry.io/collector/confmap" |
| 22 |
"go.opentelemetry.io/collector/confmap/confmaptest" |
| 23 |
"go.opentelemetry.io/collector/confmap/xconfmap" |
| 24 |
) |
| 25 |
|
| 26 |
func TestUnmarshalDefaultConfig(t *testing.T) { |
| 27 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "default.yaml")) |
| 28 |
require.NoError(t, err) |
| 29 |
factory := NewFactory() |
| 30 |
cfg := factory.CreateDefaultConfig() |
| 31 |
require.NoError(t, cm.Unmarshal(&cfg)) |
| 32 |
expectedCfg := factory.CreateDefaultConfig().(*Config) |
| 33 |
expectedCfg.GRPC.GetOrInsertDefault() |
| 34 |
expectedCfg.HTTP.GetOrInsertDefault() |
| 35 |
assert.Equal(t, expectedCfg, cfg) |
| 36 |
} |
| 37 |
|
| 38 |
func TestUnmarshalConfigOnlyGRPC(t *testing.T) { |
| 39 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "only_grpc.yaml")) |
| 40 |
require.NoError(t, err) |
| 41 |
factory := NewFactory() |
| 42 |
cfg := factory.CreateDefaultConfig() |
| 43 |
require.NoError(t, cm.Unmarshal(&cfg)) |
| 44 |
|
| 45 |
defaultOnlyGRPC := factory.CreateDefaultConfig().(*Config) |
| 46 |
defaultOnlyGRPC.GRPC.GetOrInsertDefault() |
| 47 |
assert.Equal(t, defaultOnlyGRPC, cfg) |
| 48 |
} |
| 49 |
|
| 50 |
func TestUnmarshalConfigOnlyHTTP(t *testing.T) { |
| 51 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "only_http.yaml")) |
| 52 |
require.NoError(t, err) |
| 53 |
factory := NewFactory() |
| 54 |
cfg := factory.CreateDefaultConfig() |
| 55 |
require.NoError(t, cm.Unmarshal(&cfg)) |
| 56 |
|
| 57 |
defaultOnlyHTTP := factory.CreateDefaultConfig().(*Config) |
| 58 |
defaultOnlyHTTP.HTTP.GetOrInsertDefault() |
| 59 |
assert.Equal(t, defaultOnlyHTTP, cfg) |
| 60 |
} |
| 61 |
|
| 62 |
func TestUnmarshalConfigOnlyHTTPNull(t *testing.T) { |
| 63 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "only_http_null.yaml")) |
| 64 |
require.NoError(t, err) |
| 65 |
factory := NewFactory() |
| 66 |
cfg := factory.CreateDefaultConfig() |
| 67 |
require.NoError(t, cm.Unmarshal(&cfg)) |
| 68 |
|
| 69 |
defaultOnlyHTTP := factory.CreateDefaultConfig().(*Config) |
| 70 |
defaultOnlyHTTP.HTTP.GetOrInsertDefault() |
| 71 |
assert.Equal(t, defaultOnlyHTTP, cfg) |
| 72 |
} |
| 73 |
|
| 74 |
func TestUnmarshalConfigOnlyHTTPEmptyMap(t *testing.T) { |
| 75 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "only_http_empty_map.yaml")) |
| 76 |
require.NoError(t, err) |
| 77 |
factory := NewFactory() |
| 78 |
cfg := factory.CreateDefaultConfig() |
| 79 |
require.NoError(t, cm.Unmarshal(&cfg)) |
| 80 |
|
| 81 |
defaultOnlyHTTP := factory.CreateDefaultConfig().(*Config) |
| 82 |
defaultOnlyHTTP.HTTP.GetOrInsertDefault() |
| 83 |
assert.Equal(t, defaultOnlyHTTP, cfg) |
| 84 |
} |
| 85 |
|
| 86 |
func TestUnmarshalConfig(t *testing.T) { |
| 87 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) |
| 88 |
require.NoError(t, err) |
| 89 |
factory := NewFactory() |
| 90 |
cfg := factory.CreateDefaultConfig() |
| 91 |
require.NoError(t, cm.Unmarshal(&cfg)) |
| 92 |
assert.Equal(t, |
| 93 |
&Config{ |
| 94 |
Protocols: Protocols{ |
| 95 |
GRPC: configoptional.Some(configgrpc.ServerConfig{ |
| 96 |
NetAddr: confignet.AddrConfig{ |
| 97 |
Endpoint: "localhost:4317", |
| 98 |
Transport: confignet.TransportTypeTCP, |
| 99 |
}, |
| 100 |
TLS: configoptional.Some(configtls.ServerConfig{ |
| 101 |
Config: configtls.Config{ |
| 102 |
CertFile: "test.crt", |
| 103 |
KeyFile: "test.key", |
| 104 |
}, |
| 105 |
}), |
| 106 |
MaxRecvMsgSizeMiB: 32, |
| 107 |
MaxConcurrentStreams: 16, |
| 108 |
ReadBufferSize: 1024, |
| 109 |
WriteBufferSize: 1024, |
| 110 |
Keepalive: configoptional.Some(configgrpc.KeepaliveServerConfig{ |
| 111 |
ServerParameters: configoptional.Some(configgrpc.KeepaliveServerParameters{ |
| 112 |
MaxConnectionIdle: 11 * time.Second, |
| 113 |
MaxConnectionAge: 12 * time.Second, |
| 114 |
MaxConnectionAgeGrace: 13 * time.Second, |
| 115 |
Time: 30 * time.Second, |
| 116 |
Timeout: 5 * time.Second, |
| 117 |
}), |
| 118 |
EnforcementPolicy: configoptional.Some(configgrpc.KeepaliveEnforcementPolicy{ |
| 119 |
MinTime: 10 * time.Second, |
| 120 |
PermitWithoutStream: true, |
| 121 |
}), |
| 122 |
}), |
| 123 |
}), |
| 124 |
HTTP: configoptional.Some(HTTPConfig{ |
| 125 |
ServerConfig: confighttp.ServerConfig{ |
| 126 |
Auth: configoptional.Some(confighttp.AuthConfig{ |
| 127 |
Config: configauth.Config{ |
| 128 |
AuthenticatorID: component.MustNewID("test"), |
| 129 |
}, |
| 130 |
}), |
| 131 |
Endpoint: "localhost:4318", |
| 132 |
TLS: configoptional.Some(configtls.ServerConfig{ |
| 133 |
Config: configtls.Config{ |
| 134 |
CertFile: "test.crt", |
| 135 |
KeyFile: "test.key", |
| 136 |
}, |
| 137 |
}), |
| 138 |
CORS: configoptional.Some(confighttp.CORSConfig{ |
| 139 |
AllowedOrigins: []string{"https://*.test.com", "https://test.com"}, |
| 140 |
MaxAge: 7200, |
| 141 |
}), |
| 142 |
KeepAlivesEnabled: true, |
| 143 |
}, |
| 144 |
TracesURLPath: "/traces", |
| 145 |
MetricsURLPath: "/v2/metrics", |
| 146 |
LogsURLPath: "/log/ingest", |
| 147 |
}), |
| 148 |
}, |
| 149 |
}, cfg) |
| 150 |
} |
| 151 |
|
| 152 |
func TestUnmarshalConfigUnix(t *testing.T) { |
| 153 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "uds.yaml")) |
| 154 |
require.NoError(t, err) |
| 155 |
factory := NewFactory() |
| 156 |
cfg := factory.CreateDefaultConfig() |
| 157 |
require.NoError(t, cm.Unmarshal(&cfg)) |
| 158 |
assert.Equal(t, |
| 159 |
&Config{ |
| 160 |
Protocols: Protocols{ |
| 161 |
GRPC: configoptional.Some(configgrpc.ServerConfig{ |
| 162 |
NetAddr: confignet.AddrConfig{ |
| 163 |
Endpoint: "/tmp/grpc_otlp.sock", |
| 164 |
Transport: confignet.TransportTypeUnix, |
| 165 |
}, |
| 166 |
ReadBufferSize: 512 * 1024, |
| 167 |
Keepalive: configoptional.Some(configgrpc.NewDefaultKeepaliveServerConfig()), |
| 168 |
}), |
| 169 |
HTTP: configoptional.Some(HTTPConfig{ |
| 170 |
ServerConfig: confighttp.ServerConfig{ |
| 171 |
Endpoint: "/tmp/http_otlp.sock", |
| 172 |
KeepAlivesEnabled: true, |
| 173 |
}, |
| 174 |
TracesURLPath: defaultTracesURLPath, |
| 175 |
MetricsURLPath: defaultMetricsURLPath, |
| 176 |
LogsURLPath: defaultLogsURLPath, |
| 177 |
}), |
| 178 |
}, |
| 179 |
}, cfg) |
| 180 |
} |
| 181 |
|
| 182 |
// cspell:ignore htttp |
| 183 |
func TestUnmarshalConfigTypoDefaultProtocol(t *testing.T) { |
| 184 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "typo_default_proto_config.yaml")) |
| 185 |
require.NoError(t, err) |
| 186 |
factory := NewFactory() |
| 187 |
cfg := factory.CreateDefaultConfig() |
| 188 |
assert.ErrorContains(t, cm.Unmarshal(&cfg), "'protocols' has invalid keys: htttp") |
| 189 |
} |
| 190 |
|
| 191 |
func TestUnmarshalConfigInvalidProtocol(t *testing.T) { |
| 192 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "bad_proto_config.yaml")) |
| 193 |
require.NoError(t, err) |
| 194 |
factory := NewFactory() |
| 195 |
cfg := factory.CreateDefaultConfig() |
| 196 |
assert.ErrorContains(t, cm.Unmarshal(&cfg), "'protocols' has invalid keys: thrift") |
| 197 |
} |
| 198 |
|
| 199 |
func TestUnmarshalConfigEmptyProtocols(t *testing.T) { |
| 200 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "bad_no_proto_config.yaml")) |
| 201 |
require.NoError(t, err) |
| 202 |
factory := NewFactory() |
| 203 |
cfg := factory.CreateDefaultConfig() |
| 204 |
require.NoError(t, cm.Unmarshal(&cfg)) |
| 205 |
assert.EqualError(t, xconfmap.Validate(cfg), "must specify at least one protocol when using the OTLP receiver") |
| 206 |
} |
| 207 |
|
| 208 |
func TestUnmarshalConfigInvalidSignalPath(t *testing.T) { |
| 209 |
tests := []struct { |
| 210 |
name string |
| 211 |
testDataFn string |
| 212 |
}{ |
| 213 |
{ |
| 214 |
name: "Invalid traces URL path", |
| 215 |
testDataFn: "invalid_traces_path.yaml", |
| 216 |
}, |
| 217 |
{ |
| 218 |
name: "Invalid metrics URL path", |
| 219 |
testDataFn: "invalid_metrics_path.yaml", |
| 220 |
}, |
| 221 |
{ |
| 222 |
name: "Invalid logs URL path", |
| 223 |
testDataFn: "invalid_logs_path.yaml", |
| 224 |
}, |
| 225 |
} |
| 226 |
|
| 227 |
for _, tt := range tests { |
| 228 |
t.Run(tt.name, func(t *testing.T) { |
| 229 |
cm, err := confmaptest.LoadConf(filepath.Join("testdata", tt.testDataFn)) |
| 230 |
require.NoError(t, err) |
| 231 |
factory := NewFactory() |
| 232 |
cfg := factory.CreateDefaultConfig() |
| 233 |
assert.ErrorContains(t, cm.Unmarshal(&cfg), "invalid HTTP URL path set for signal: parse \":invalid\": missing protocol scheme") |
| 234 |
}) |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
func TestUnmarshalConfigEmpty(t *testing.T) { |
| 239 |
factory := NewFactory() |
| 240 |
cfg := factory.CreateDefaultConfig() |
| 241 |
require.NoError(t, confmap.New().Unmarshal(&cfg)) |
| 242 |
assert.EqualError(t, xconfmap.Validate(cfg), "must specify at least one protocol when using the OTLP receiver") |
| 243 |
} |