Skip to content

Commit

Permalink
Add UnitTests
Browse files Browse the repository at this point in the history
  • Loading branch information
rfavreau committed Apr 1, 2024
1 parent bde44c9 commit ed23209
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
4 changes: 3 additions & 1 deletion test/Elastic.Apm.Tests.Utilities/MockConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public MockConfiguration(IApmLogger logger = null,
string spanCompressionEnabled = null,
string spanCompressionExactMatchMaxDuration = null,
string spanCompressionSameKindMaxDuration = null,
string traceContinuationStrategy = null
string traceContinuationStrategy = null,
string overwritediscoverdefaultservicename = null
) : base(
logger,
new ConfigurationDefaults { DebugName = nameof(MockConfiguration) },
Expand Down Expand Up @@ -97,6 +98,7 @@ public MockConfiguration(IApmLogger logger = null,
ConfigurationOption.MaxBatchEventCount => maxBatchEventCount,
ConfigurationOption.MaxQueueEventCount => maxQueueEventCount,
ConfigurationOption.MetricsInterval => metricsInterval,
ConfigurationOption.OverwriteDiscoverDefaultServiceName => overwritediscoverdefaultservicename,
ConfigurationOption.Recording => recording,
ConfigurationOption.SanitizeFieldNames => sanitizeFieldNames,
ConfigurationOption.SecretToken => secretToken,
Expand Down
37 changes: 37 additions & 0 deletions test/Elastic.Apm.Tests/Config/ConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,43 @@ public void DefaultApplicationNamespaceConfig()
excludedNamespaces.Should().BeEquivalentTo(DefaultValues.DefaultExcludedNamespaces);
}

/// <summary>
/// Makes sure that in case OverwriteDiscoverDefaultServiceName is not set, the agent uses true as default value
/// </summary>
[Fact]
public void OverwriteDiscoverDefaultServiceNameTestWithNoValue()
{
using var agent =
new ApmAgent(new TestAgentComponents(
configuration: new MockConfiguration()));
agent.Configuration.OverwriteDiscoverDefaultServiceName.Should().BeTrue();
}

/// <summary>
/// Makes sure that in case OverwriteDiscoverDefaultServiceName is set to invalid value, the agent uses true as default value
/// </summary>
[Fact]
public void OverwriteDiscoverDefaultServiceNameTestWithInvalidValue()
{
using var agent =
new ApmAgent(new TestAgentComponents(
configuration: new MockConfiguration(overwritediscoverdefaultservicename: "foobar")));
agent.Configuration.OverwriteDiscoverDefaultServiceName.Should().BeTrue();
}

[Theory]
[InlineData("true", true)]
[InlineData("false", false)]
[InlineData("True", true)]
[InlineData("False", false)]
[InlineData(" True ", true)]
[InlineData(" False ", false)]
public void OverwriteDiscoverDefaultServiceNameTestWithValidValue(string value, bool expected)
{
using var agent = new ApmAgent(new TestAgentComponents(configuration: new MockConfiguration(overwritediscoverdefaultservicename: value)));
agent.Configuration.OverwriteDiscoverDefaultServiceName.Should().Be(expected);
}

private static double MetricsIntervalTestCommon(string configValue)
{
Environment.SetEnvironmentVariable(MetricsInterval.ToEnvironmentVariable(), configValue);
Expand Down
1 change: 1 addition & 0 deletions test/Elastic.Apm.Tests/ConstructorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private class LogConfiguration : IConfiguration, IConfigurationDescription
public int MaxBatchEventCount => ConfigConsts.DefaultValues.MaxBatchEventCount;
public int MaxQueueEventCount => ConfigConsts.DefaultValues.MaxQueueEventCount;
public double MetricsIntervalInMilliseconds => ConfigConsts.DefaultValues.MetricsIntervalInMilliseconds;
public bool OverwriteDiscoverDefaultServiceName => ConfigConsts.DefaultValues.OverwriteDiscoverDefaultServiceName;
public string SecretToken { get; }
public string ServerCert { get; }
public string ApiKey { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System.Threading.Tasks;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
using static Elastic.Apm.Api.Outcome;
using static Elastic.Apm.AzureFunctionApp.Core.FunctionName;

namespace Elastic.Apm.Azure.Functions.Tests;

[Collection("AzureFunctions")]
public class AzureFunctionsIsolatedNotOverwriteTests : AzureFunctionsTestBase, IClassFixture<IsolatedContextNotOverwite>
{
public AzureFunctionsIsolatedNotOverwriteTests(ITestOutputHelper output, IsolatedContextNotOverwite context)
: base(output, context) { }

[Fact]
public async Task OverwriteDiscoverDefaultServiceName_False()
{
var transaction = await InvokeAndAssertFunction(SampleHttpTrigger);

transaction.Outcome.Should().Be(Success);
transaction.Result.Should().Be("HTTP 2xx");
transaction.Context.Response.StatusCode.Should().Be(200);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,22 @@ private void AssertMetaData(MetadataDto metaData)
metaData.Service.Agent.ActivationMethod.Should().Be(Consts.ActivationMethodNuGet);
metaData.Cloud.Provider.Should().Be("azure");
metaData.Cloud.Service.Name.Should().Be("functions");
metaData.Service.Name.Should().Be(Context.WebsiteName);
AssertServiceName(metaData.Service.Name);
metaData.Service.Runtime.Name.Should().Be(Context.RuntimeName);
metaData.Service.Framework.Name.Should().Be("Azure Functions");
metaData.Service.Framework.Version.Should().Be("4");
// TODO - temporarily removing this assertion as we can no longer seem to set this value without causing a host error
//metaData.Service.Node.ConfiguredName.Should().Be("20367ea8-70b9-41b4-a552-b2a826b3aa0b");
}

private void AssertServiceName(string name)
{
if (Context.OverwriteDiscoverDefaultServiceName == null || (bool)Context.OverwriteDiscoverDefaultServiceName)
name.Should().Be(Context.WebsiteName);
else
name.Should().NotBe(Context.WebsiteName);
}

private static void AssertTracing(TransactionDto transaction) =>
transaction.TraceId.Should().Be("0af7651916cd43dd8448eb211c80319c");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,27 @@ public class IsolatedContext : AzureFunctionTestContextBase
protected override Uri BaseUri { get; } = new("http://localhost:7071");
public override string WebsiteName { get; } = "testfaas";
public override string RuntimeName { get; } = "dotnet-isolated";
public override bool? OverwriteDiscoverDefaultServiceName { get; } = null;

public IsolatedContext() : base(FunctionType.Isolated) { }
}

public class IsolatedContextNotOverwite : AzureFunctionTestContextBase
{
protected override Uri BaseUri { get; } = new("http://localhost:7071");
public override string WebsiteName { get; } = "testfaas";
public override string RuntimeName { get; } = "dotnet-isolated";
public override bool? OverwriteDiscoverDefaultServiceName { get; } = false;

public IsolatedContextNotOverwite() : base(FunctionType.Isolated) { }
}

public class InProcessContext : AzureFunctionTestContextBase
{
protected override Uri BaseUri { get; } = new("http://localhost:17073");
public override string WebsiteName { get; } = "testfaas";
public override string RuntimeName { get; } = "dotnet";
public override bool? OverwriteDiscoverDefaultServiceName { get; } = null;

public InProcessContext() : base(FunctionType.InProcess) { }
}
Expand All @@ -46,6 +58,7 @@ public abstract class AzureFunctionTestContextBase : IDisposable
protected abstract Uri BaseUri { get; }
public abstract string WebsiteName { get; }
public abstract string RuntimeName { get; }
public abstract bool? OverwriteDiscoverDefaultServiceName { get; }

public bool IsFirst { get; internal set; }

Expand Down Expand Up @@ -85,7 +98,8 @@ internal AzureFunctionTestContextBase(FunctionType functionType)
EnvironmentVariables =
{
["ELASTIC_APM_SERVER_URL"] = $"http://localhost:{port}",
["ELASTIC_APM_FLUSH_INTERVAL"] = "0"
["ELASTIC_APM_FLUSH_INTERVAL"] = "0",
["ELASTIC_APM_OVERWRITE_DISCOVER_DEFAULT_SERVICE_NAME"] = $"{OverwriteDiscoverDefaultServiceName}"
},
UseShellExecute = false
}
Expand Down

0 comments on commit ed23209

Please sign in to comment.