Skip to content

Commit

Permalink
[AzureMonitorDistro] Conditionally skip failing tests (#44423)
Browse files Browse the repository at this point in the history
* disable tests

* Enable only DistroLogForwarderIsAdded

* Enable only PublicLogForwarderIsAdded

* Enable only SelfDiagnosticsIsDisabled

* test conditionally skip

* preprocessor
  • Loading branch information
TimothyMothra authored Jun 6, 2024
1 parent 9b963ba commit 05dccb3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Compile Include="$(AzureCoreSharedSources)AzureEventSource.cs" LinkBase="SharedSource\Azure.Core" />
<Compile Include="..\..\..\Azure.Monitor.OpenTelemetry.Exporter\tests\Azure.Monitor.OpenTelemetry.Exporter.Tests\CommonTestFramework\EventSourceTestHelper.cs" LinkBase="CommonTestFramework" />
<Compile Include="..\..\..\Azure.Monitor.OpenTelemetry.Exporter\tests\Azure.Monitor.OpenTelemetry.Exporter.Tests\CommonTestFramework\TestEventListener.cs" LinkBase="CommonTestFramework" />
<Compile Include="..\..\..\Azure.Monitor.OpenTelemetry.Exporter\tests\Azure.Monitor.OpenTelemetry.Exporter.Tests\CommonTestFramework\CustomXunitAttributes.cs" LinkBase="CommonTestFramework" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Xunit;
using static Xunit.CustomXunitAttributes;

namespace Azure.Monitor.OpenTelemetry.AspNetCore.Tests
{
public class AzureSdkLoggingTests
{
#if NET6_0
[ConditionallySkipOSTheory(platformToSkip: "macos", reason: "This test consistently exceeds 1 hour runtime limit when running on MacOS & Net60")]
#else
[Theory]
#endif
[InlineData(LogLevel.Information, "TestInfoEvent: hello")]
[InlineData(LogLevel.Warning, "TestWarningEvent: hello")]
[InlineData(LogLevel.Debug, null)]
Expand Down Expand Up @@ -49,7 +54,11 @@ public async Task DistroLogForwarderIsAdded(LogLevel eventLevel, string expected
}
}

#if NET6_0
[ConditionallySkipOSTheory(platformToSkip: "macos", reason: "This test consistently exceeds 1 hour runtime limit when running on MacOS & Net60")]
#else
[Theory]
#endif
[InlineData(LogLevel.Information, "TestInfoEvent: hello")]
[InlineData(LogLevel.Warning, "TestWarningEvent: hello")]
[InlineData(LogLevel.Debug, null)]
Expand Down Expand Up @@ -86,7 +95,11 @@ public async Task PublicLogForwarderIsAdded(LogLevel eventLevel, string expected
}
}

#if NET6_0
[ConditionallySkipOSFact(platformToSkip: "macos", reason: "This test consistently exceeds 1 hour runtime limit when running on MacOS & Net60")]
#else
[Fact]
#endif
public async Task SelfDiagnosticsIsDisabled()
{
var enableLevel = LogLevel.Debug;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Runtime.InteropServices;

namespace Xunit
{
public class CustomXunitAttributes
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ConditionallySkipOSFactAttribute : FactAttribute
{
private readonly string _platformToSkip;
private readonly string _reason;

public ConditionallySkipOSFactAttribute(string platformToSkip, string reason)
{
_platformToSkip = platformToSkip;
_reason = reason;
}

public override string Skip
{
get => IsCurrentOS(_platformToSkip)
? $"Test skipped on {_platformToSkip}. {_reason}"
: base.Skip;
set => base.Skip = value;
}
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ConditionallySkipOSTheoryAttribute : TheoryAttribute
{
private readonly string _platformToSkip;
private readonly string _reason;

public ConditionallySkipOSTheoryAttribute(string platformToSkip, string reason)
{
_platformToSkip = platformToSkip;
_reason = reason;
}

public override string Skip
{
get
{
return IsCurrentOS(_platformToSkip)
? $"Test skipped on {_platformToSkip}. {_reason}"
: base.Skip;
}
set => base.Skip = value;
}
}

private static bool IsCurrentOS(string osName)
{
switch (osName.ToLowerInvariant())
{
case "windows":
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
case "linux":
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
case "osx":
case "macos":
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
default:
throw new ArgumentException($"Unsupported OS name: {osName}");
}
}
}
}

0 comments on commit 05dccb3

Please sign in to comment.