Skip to content

Commit

Permalink
Add Export method tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Fogliatto authored and mfogliatto committed Feb 25, 2023
1 parent f7f5358 commit 283f7a4
Showing 1 changed file with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Moq;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
using OpenTelemetry.Internal;
using OpenTelemetry.Logs;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
using Xunit;
using OtlpCollector = OpenTelemetry.Proto.Collector.Logs.V1;
using OtlpCommon = OpenTelemetry.Proto.Common.V1;
using OtlpLogs = OpenTelemetry.Proto.Logs.V1;

Expand Down Expand Up @@ -500,6 +503,69 @@ public void CheckToOtlpLogRecordRespectsAttributeLimits()
Assert.Null(exceptionStackTraceAtt);
}

[Fact]
public void Export_WhenExportClientIsProvidedInCtor_UsesProvidedExportClient()
{
// Arrange.
var fakeExportClient = new Mock<IExportClient<OtlpCollector.ExportLogsServiceRequest>>();
var emptyLogRecords = Array.Empty<LogRecord>();
var emptyBatch = new Batch<LogRecord>(emptyLogRecords, emptyLogRecords.Length);
var sut = new OtlpLogExporter(
new OtlpExporterOptions(),
new SdkLimitOptions(),
fakeExportClient.Object);

// Act.
var result = sut.Export(emptyBatch);

// Assert.
fakeExportClient.Verify(x => x.SendExportRequest(It.IsAny<OtlpCollector.ExportLogsServiceRequest>(), default), Times.Once());
}

[Fact]
public void Export_WhenExportClientThrowsException_ReturnsExportResultFailure()
{
// Arrange.
var fakeExportClient = new Mock<IExportClient<OtlpCollector.ExportLogsServiceRequest>>();
var emptyLogRecords = Array.Empty<LogRecord>();
var emptyBatch = new Batch<LogRecord>(emptyLogRecords, emptyLogRecords.Length);
fakeExportClient
.Setup(_ => _.SendExportRequest(It.IsAny<OtlpCollector.ExportLogsServiceRequest>(), default))
.Throws(new Exception("Test Exception"));
var sut = new OtlpLogExporter(
new OtlpExporterOptions(),
new SdkLimitOptions(),
fakeExportClient.Object);

// Act.
var result = sut.Export(emptyBatch);

// Assert.
Assert.Equal(ExportResult.Failure, result);
}

[Fact]
public void Export_WhenExportIsSuccessful_ReturnsExportResultSuccess()
{
// Arrange.
var fakeExportClient = new Mock<IExportClient<OtlpCollector.ExportLogsServiceRequest>>();
var emptyLogRecords = Array.Empty<LogRecord>();
var emptyBatch = new Batch<LogRecord>(emptyLogRecords, emptyLogRecords.Length);
fakeExportClient
.Setup(_ => _.SendExportRequest(It.IsAny<OtlpCollector.ExportLogsServiceRequest>(), default))
.Returns(true);
var sut = new OtlpLogExporter(
new OtlpExporterOptions(),
new SdkLimitOptions(),
fakeExportClient.Object);

// Act.
var result = sut.Export(emptyBatch);

// Assert.
Assert.Equal(ExportResult.Success, result);
}

private static OtlpCommon.KeyValue TryGetAttribute(OtlpLogs.LogRecord record, string key)
{
return record.Attributes.FirstOrDefault(att => att.Key == key);
Expand Down

0 comments on commit 283f7a4

Please sign in to comment.