| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package internal |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/component/componenttest" |
| 15 |
"go.opentelemetry.io/collector/consumer" |
| 16 |
"go.opentelemetry.io/collector/consumer/consumererror" |
| 17 |
"go.opentelemetry.io/collector/exporter" |
| 18 |
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/request" |
| 19 |
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/requesttest" |
| 20 |
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/sendertest" |
| 21 |
"go.opentelemetry.io/collector/exporter/exportertest" |
| 22 |
"go.opentelemetry.io/collector/pdata/plog" |
| 23 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 24 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 25 |
) |
| 26 |
|
| 27 |
func TestLogsRequest_NilLogger(t *testing.T) { |
| 28 |
le, err := NewLogsRequest(context.Background(), exporter.Settings{}, requesttest.RequestFromLogsFunc(nil), sendertest.NewNopSenderFunc[request.Request]()) |
| 29 |
require.Nil(t, le) |
| 30 |
require.Equal(t, errNilLogger, err) |
| 31 |
} |
| 32 |
|
| 33 |
func TestLogsRequest_NilLogsConverter(t *testing.T) { |
| 34 |
le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), nil, sendertest.NewNopSenderFunc[request.Request]()) |
| 35 |
require.Nil(t, le) |
| 36 |
require.Equal(t, errNilLogsConverter, err) |
| 37 |
} |
| 38 |
|
| 39 |
func TestLogsRequest_NilPushLogsData(t *testing.T) { |
| 40 |
le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), requesttest.RequestFromLogsFunc(nil), nil) |
| 41 |
require.Nil(t, le) |
| 42 |
require.Equal(t, errNilConsumeRequest, err) |
| 43 |
} |
| 44 |
|
| 45 |
func TestLogsRequest_Default(t *testing.T) { |
| 46 |
ld := plog.NewLogs() |
| 47 |
le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 48 |
requesttest.RequestFromLogsFunc(nil), sendertest.NewNopSenderFunc[request.Request]()) |
| 49 |
assert.NotNil(t, le) |
| 50 |
require.NoError(t, err) |
| 51 |
|
| 52 |
assert.Equal(t, consumer.Capabilities{MutatesData: false}, le.Capabilities()) |
| 53 |
assert.NoError(t, le.Start(context.Background(), componenttest.NewNopHost())) |
| 54 |
assert.NoError(t, le.ConsumeLogs(context.Background(), ld)) |
| 55 |
assert.NoError(t, le.Shutdown(context.Background())) |
| 56 |
} |
| 57 |
|
| 58 |
func TestLogsRequest_WithCapabilities(t *testing.T) { |
| 59 |
capabilities := consumer.Capabilities{MutatesData: true} |
| 60 |
le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 61 |
requesttest.RequestFromLogsFunc(nil), sendertest.NewNopSenderFunc[request.Request](), WithCapabilities(capabilities)) |
| 62 |
require.NoError(t, err) |
| 63 |
require.NotNil(t, le) |
| 64 |
|
| 65 |
assert.Equal(t, capabilities, le.Capabilities()) |
| 66 |
} |
| 67 |
|
| 68 |
func TestLogsRequest_WithShutdown(t *testing.T) { |
| 69 |
shutdownCalled := false |
| 70 |
shutdown := func(context.Context) error { shutdownCalled = true; return nil } |
| 71 |
|
| 72 |
le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 73 |
requesttest.RequestFromLogsFunc(nil), sendertest.NewNopSenderFunc[request.Request](), WithShutdown(shutdown)) |
| 74 |
assert.NotNil(t, le) |
| 75 |
assert.NoError(t, err) |
| 76 |
|
| 77 |
assert.NoError(t, le.Shutdown(context.Background())) |
| 78 |
assert.True(t, shutdownCalled) |
| 79 |
} |
| 80 |
|
| 81 |
func TestLogsRequest_Default_ConvertError(t *testing.T) { |
| 82 |
ld := plog.NewLogs() |
| 83 |
want := errors.New("convert_error") |
| 84 |
le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 85 |
requesttest.RequestFromLogsFunc(want), sendertest.NewNopSenderFunc[request.Request]()) |
| 86 |
require.NoError(t, err) |
| 87 |
require.NotNil(t, le) |
| 88 |
require.Equal(t, consumererror.NewPermanent(want), le.ConsumeLogs(context.Background(), ld)) |
| 89 |
} |
| 90 |
|
| 91 |
func TestLogsRequest_Default_ExportError(t *testing.T) { |
| 92 |
ld := plog.NewLogs() |
| 93 |
want := errors.New("export_error") |
| 94 |
le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 95 |
requesttest.RequestFromLogsFunc(nil), sendertest.NewErrSenderFunc[request.Request](want)) |
| 96 |
require.NoError(t, err) |
| 97 |
require.NotNil(t, le) |
| 98 |
require.Equal(t, want, le.ConsumeLogs(context.Background(), ld)) |
| 99 |
} |
| 100 |
|
| 101 |
func TestLogsRequest_WithShutdown_ReturnError(t *testing.T) { |
| 102 |
want := errors.New("my_error") |
| 103 |
shutdownErr := func(context.Context) error { return want } |
| 104 |
|
| 105 |
le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 106 |
requesttest.RequestFromLogsFunc(nil), sendertest.NewNopSenderFunc[request.Request](), WithShutdown(shutdownErr)) |
| 107 |
assert.NotNil(t, le) |
| 108 |
require.NoError(t, err) |
| 109 |
|
| 110 |
assert.Equal(t, want, le.Shutdown(context.Background())) |
| 111 |
} |
| 112 |
|
| 113 |
func TestTracesRequest_NilLogger(t *testing.T) { |
| 114 |
te, err := NewTracesRequest(context.Background(), exporter.Settings{}, requesttest.RequestFromTracesFunc(nil), sendertest.NewNopSenderFunc[request.Request]()) |
| 115 |
require.Nil(t, te) |
| 116 |
require.Equal(t, errNilLogger, err) |
| 117 |
} |
| 118 |
|
| 119 |
func TestTracesRequest_NilTracesConverter(t *testing.T) { |
| 120 |
te, err := NewTracesRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), nil, sendertest.NewNopSenderFunc[request.Request]()) |
| 121 |
require.Nil(t, te) |
| 122 |
require.Equal(t, errNilTracesConverter, err) |
| 123 |
} |
| 124 |
|
| 125 |
func TestTracesRequest_NilPushTraceData(t *testing.T) { |
| 126 |
te, err := NewTracesRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), requesttest.RequestFromTracesFunc(nil), nil) |
| 127 |
require.Nil(t, te) |
| 128 |
require.Equal(t, errNilConsumeRequest, err) |
| 129 |
} |
| 130 |
|
| 131 |
func TestTracesRequest_Default(t *testing.T) { |
| 132 |
td := ptrace.NewTraces() |
| 133 |
te, err := NewTracesRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 134 |
requesttest.RequestFromTracesFunc(nil), sendertest.NewNopSenderFunc[request.Request]()) |
| 135 |
assert.NotNil(t, te) |
| 136 |
require.NoError(t, err) |
| 137 |
|
| 138 |
assert.Equal(t, consumer.Capabilities{MutatesData: false}, te.Capabilities()) |
| 139 |
assert.NoError(t, te.Start(context.Background(), componenttest.NewNopHost())) |
| 140 |
assert.NoError(t, te.ConsumeTraces(context.Background(), td)) |
| 141 |
assert.NoError(t, te.Shutdown(context.Background())) |
| 142 |
} |
| 143 |
|
| 144 |
func TestTracesRequest_WithCapabilities(t *testing.T) { |
| 145 |
capabilities := consumer.Capabilities{MutatesData: true} |
| 146 |
te, err := NewTracesRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 147 |
requesttest.RequestFromTracesFunc(nil), sendertest.NewNopSenderFunc[request.Request](), WithCapabilities(capabilities)) |
| 148 |
assert.NotNil(t, te) |
| 149 |
require.NoError(t, err) |
| 150 |
|
| 151 |
assert.Equal(t, capabilities, te.Capabilities()) |
| 152 |
} |
| 153 |
|
| 154 |
func TestTracesRequest_Default_ConvertError(t *testing.T) { |
| 155 |
td := ptrace.NewTraces() |
| 156 |
want := errors.New("convert_error") |
| 157 |
te, err := NewTracesRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 158 |
requesttest.RequestFromTracesFunc(want), sendertest.NewNopSenderFunc[request.Request]()) |
| 159 |
require.NoError(t, err) |
| 160 |
require.NotNil(t, te) |
| 161 |
require.Equal(t, consumererror.NewPermanent(want), te.ConsumeTraces(context.Background(), td)) |
| 162 |
} |
| 163 |
|
| 164 |
func TestTracesRequest_Default_ExportError(t *testing.T) { |
| 165 |
td := ptrace.NewTraces() |
| 166 |
want := errors.New("export_error") |
| 167 |
te, err := NewTracesRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 168 |
requesttest.RequestFromTracesFunc(nil), sendertest.NewErrSenderFunc[request.Request](want)) |
| 169 |
require.NoError(t, err) |
| 170 |
require.NotNil(t, te) |
| 171 |
require.Equal(t, want, te.ConsumeTraces(context.Background(), td)) |
| 172 |
} |
| 173 |
|
| 174 |
func TestTracesRequest_WithShutdown(t *testing.T) { |
| 175 |
shutdownCalled := false |
| 176 |
shutdown := func(context.Context) error { shutdownCalled = true; return nil } |
| 177 |
|
| 178 |
te, err := NewTracesRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 179 |
requesttest.RequestFromTracesFunc(nil), sendertest.NewNopSenderFunc[request.Request](), WithShutdown(shutdown)) |
| 180 |
assert.NotNil(t, te) |
| 181 |
assert.NoError(t, err) |
| 182 |
|
| 183 |
assert.NoError(t, te.Shutdown(context.Background())) |
| 184 |
assert.True(t, shutdownCalled) |
| 185 |
} |
| 186 |
|
| 187 |
func TestTracesRequest_WithShutdown_ReturnError(t *testing.T) { |
| 188 |
want := errors.New("my_error") |
| 189 |
shutdownErr := func(context.Context) error { return want } |
| 190 |
|
| 191 |
te, err := NewTracesRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 192 |
requesttest.RequestFromTracesFunc(nil), sendertest.NewNopSenderFunc[request.Request](), WithShutdown(shutdownErr)) |
| 193 |
assert.NotNil(t, te) |
| 194 |
require.NoError(t, err) |
| 195 |
|
| 196 |
assert.Equal(t, want, te.Shutdown(context.Background())) |
| 197 |
} |
| 198 |
|
| 199 |
func TestMetricsRequest_NilLogger(t *testing.T) { |
| 200 |
me, err := NewMetricsRequest(context.Background(), exporter.Settings{}, requesttest.RequestFromMetricsFunc(nil), sendertest.NewNopSenderFunc[request.Request]()) |
| 201 |
require.Nil(t, me) |
| 202 |
require.Equal(t, errNilLogger, err) |
| 203 |
} |
| 204 |
|
| 205 |
func TestMetricsRequest_NilMetricsConverter(t *testing.T) { |
| 206 |
me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), nil, sendertest.NewNopSenderFunc[request.Request]()) |
| 207 |
require.Nil(t, me) |
| 208 |
require.Equal(t, errNilMetricsConverter, err) |
| 209 |
} |
| 210 |
|
| 211 |
func TestMetricsRequest_NilPushMetricsData(t *testing.T) { |
| 212 |
me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), requesttest.RequestFromMetricsFunc(nil), nil) |
| 213 |
require.Nil(t, me) |
| 214 |
require.Equal(t, errNilConsumeRequest, err) |
| 215 |
} |
| 216 |
|
| 217 |
func TestMetricsRequest_Default(t *testing.T) { |
| 218 |
md := pmetric.NewMetrics() |
| 219 |
me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 220 |
requesttest.RequestFromMetricsFunc(nil), sendertest.NewNopSenderFunc[request.Request]()) |
| 221 |
require.NoError(t, err) |
| 222 |
assert.NotNil(t, me) |
| 223 |
|
| 224 |
assert.Equal(t, consumer.Capabilities{MutatesData: false}, me.Capabilities()) |
| 225 |
assert.NoError(t, me.Start(context.Background(), componenttest.NewNopHost())) |
| 226 |
assert.NoError(t, me.ConsumeMetrics(context.Background(), md)) |
| 227 |
assert.NoError(t, me.Shutdown(context.Background())) |
| 228 |
} |
| 229 |
|
| 230 |
func TestMetricsRequest_WithCapabilities(t *testing.T) { |
| 231 |
capabilities := consumer.Capabilities{MutatesData: true} |
| 232 |
me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 233 |
requesttest.RequestFromMetricsFunc(nil), sendertest.NewNopSenderFunc[request.Request](), WithCapabilities(capabilities)) |
| 234 |
require.NoError(t, err) |
| 235 |
assert.NotNil(t, me) |
| 236 |
|
| 237 |
assert.Equal(t, capabilities, me.Capabilities()) |
| 238 |
} |
| 239 |
|
| 240 |
func TestMetricsRequest_Default_ConvertError(t *testing.T) { |
| 241 |
md := pmetric.NewMetrics() |
| 242 |
want := errors.New("convert_error") |
| 243 |
me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 244 |
requesttest.RequestFromMetricsFunc(want), sendertest.NewNopSenderFunc[request.Request]()) |
| 245 |
require.NoError(t, err) |
| 246 |
require.NotNil(t, me) |
| 247 |
require.Equal(t, consumererror.NewPermanent(want), me.ConsumeMetrics(context.Background(), md)) |
| 248 |
} |
| 249 |
|
| 250 |
func TestMetricsRequest_Default_ExportError(t *testing.T) { |
| 251 |
md := pmetric.NewMetrics() |
| 252 |
want := errors.New("export_error") |
| 253 |
me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 254 |
requesttest.RequestFromMetricsFunc(nil), sendertest.NewErrSenderFunc[request.Request](want)) |
| 255 |
require.NoError(t, err) |
| 256 |
require.NotNil(t, me) |
| 257 |
require.Equal(t, want, me.ConsumeMetrics(context.Background(), md)) |
| 258 |
} |
| 259 |
|
| 260 |
func TestMetricsRequest_WithShutdown(t *testing.T) { |
| 261 |
shutdownCalled := false |
| 262 |
shutdown := func(context.Context) error { shutdownCalled = true; return nil } |
| 263 |
|
| 264 |
me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 265 |
requesttest.RequestFromMetricsFunc(nil), sendertest.NewNopSenderFunc[request.Request](), WithShutdown(shutdown)) |
| 266 |
assert.NotNil(t, me) |
| 267 |
assert.NoError(t, err) |
| 268 |
|
| 269 |
assert.NoError(t, me.Start(context.Background(), componenttest.NewNopHost())) |
| 270 |
assert.NoError(t, me.Shutdown(context.Background())) |
| 271 |
assert.True(t, shutdownCalled) |
| 272 |
} |
| 273 |
|
| 274 |
func TestMetricsRequest_WithShutdown_ReturnError(t *testing.T) { |
| 275 |
want := errors.New("my_error") |
| 276 |
shutdownErr := func(context.Context) error { return want } |
| 277 |
|
| 278 |
me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(exportertest.NopType), |
| 279 |
requesttest.RequestFromMetricsFunc(nil), sendertest.NewNopSenderFunc[request.Request](), WithShutdown(shutdownErr)) |
| 280 |
assert.NotNil(t, me) |
| 281 |
assert.NoError(t, err) |
| 282 |
|
| 283 |
assert.NoError(t, me.Start(context.Background(), componenttest.NewNopHost())) |
| 284 |
assert.Equal(t, want, me.Shutdown(context.Background())) |
| 285 |
} |