Skip to content

Commit

Permalink
Merge pull request #46 from CommunityToolkit/improve-docker-testing-c…
Browse files Browse the repository at this point in the history
…hecks

Test improvements"
  • Loading branch information
aaronpowell authored Oct 3, 2024
2 parents a9b1561 + f5ea400 commit c69e49f
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 445 deletions.
7 changes: 7 additions & 0 deletions tests/.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<!-- Filter out failing (wrong framework, platform, runtime or activeissue) tests -->
<TestCaseFilter>category!=unsupported-platform</TestCaseFilter>
</RunConfiguration>
</RunSettings>
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Aspire.CommunityToolkit.Testing;
using FluentAssertions;
using Microsoft.TestUtilities;
using Aspire.Components.Common.Tests;

namespace Aspire.CommunityToolkit.Hosting.Java.Tests;

#pragma warning disable CTASPIRE001
[RequiresDocker]
public class JavaHostingComponentTests(AspireIntegrationTestFixture<Projects.Aspire_CommunityToolkit_Hosting_Java_AppHost> fixture) : IClassFixture<AspireIntegrationTestFixture<Projects.Aspire_CommunityToolkit_Hosting_Java_AppHost>>
{
[ConditionalTheory]
[OSSkipCondition(OperatingSystems.Windows)]
[Theory]
[InlineData("containerapp")]
[InlineData("executableapp")]
public async Task AppResourceWillRespondWithOk(string resourceName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Aspire.CommunityToolkit.Testing;
using Aspire.Components.Common.Tests;
using FluentAssertions;
using Microsoft.TestUtilities;

namespace Aspire.CommunityToolkit.Hosting.Ollama.Tests;

[RequiresDocker]
public class AppHostTests(AspireIntegrationTestFixture<Projects.Aspire_CommunityToolkit_Hosting_Ollama_AppHost> fixture) : IClassFixture<AspireIntegrationTestFixture<Projects.Aspire_CommunityToolkit_Hosting_Ollama_AppHost>>
{
[ConditionalTheory]
[OSSkipCondition(OperatingSystems.Windows)]
[Theory]
[InlineData("ollama")]
[InlineData("ollama-openwebui")]
public async Task ResourceStartsAndRespondsOk(string resourceName)
Expand Down
26 changes: 5 additions & 21 deletions tests/Aspire.CommunityToolkit.Testing/AspireIntegrationTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Microsoft.Extensions.DependencyInjection;
using Aspire.Components.Common.Tests;
using Aspire.Hosting;
using Microsoft.Extensions.Logging;
using System.Runtime.InteropServices;

namespace Aspire.CommunityToolkit.Testing;

public class AspireIntegrationTestFixture<TEntryPoint>() : DistributedApplicationFactory(typeof(TEntryPoint), []), IAsyncLifetime where TEntryPoint : class
Expand Down Expand Up @@ -32,22 +31,7 @@ protected override void OnBuilderCreated(DistributedApplicationBuilder applicati
base.OnBuilderCreated(applicationBuilder);
}

public async Task InitializeAsync() => await StartAsync();
public Task InitializeAsync() => StartAsync();

async Task IAsyncLifetime.DisposeAsync()
{
try
{
await DisposeAsync();
}
catch (Exception)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI")))
{
// GitHub Actions Windows runners don't support Linux Docker containers, which can result in a bunch of false errors, even if we try to skip the test run, so we only really want to throw
// if we're on a non-Windows runner or if we're on a Windows runner but not in a CI environment
throw;
}
}
}
async Task IAsyncLifetime.DisposeAsync() => await DisposeAsync();
}
6 changes: 6 additions & 0 deletions tests/Aspire.CommunityToolkit.Testing/PlatformDetection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Aspire.Components.Common.Tests;

public static class PlatformDetection
{
public static bool IsRunningOnCI => Environment.GetEnvironmentVariable("CI") is not null;
}
34 changes: 34 additions & 0 deletions tests/Aspire.CommunityToolkit.Testing/RequiresDockerAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Copied from: https://github.com/dotnet/aspire/blob/d31331d6132aeb22940dcd8834344956ba811373/tests/Aspire.Components.Common.Tests/RequiresDockerAttribute.cs

using Xunit.Sdk;

namespace Aspire.Components.Common.Tests;

[TraitDiscoverer("Aspire.Components.Common.Tests.RequiresDockerDiscoverer", "Aspire.CommunityToolkit.Testing")]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class RequiresDockerAttribute : Attribute, ITraitAttribute
{
// This property is `true` when docker is *expected* to be available.
//
// A hard-coded *expected* value is used here to ensure that docker
// dependent tests *fail* if docker is not available/usable in an environment
// where it is expected to be available. A run-time check would allow tests
// to fail silently, which is not desirable.
//
// scenarios:
// - Windows: assume installed only for *local* runs as docker isn't supported on CI yet
// - https://github.com/dotnet/aspire/issues/4291
// - Linux - Local, or CI: always assume that docker is installed
public static bool IsSupported =>
!OperatingSystem.IsWindows() ||
!PlatformDetection.IsRunningOnCI;

public string? Reason { get; init; }
public RequiresDockerAttribute(string? reason = null)
{
Reason = reason;
}
}
20 changes: 20 additions & 0 deletions tests/Aspire.CommunityToolkit.Testing/RequiresDockerDiscoverer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Copied from: https://github.com/dotnet/aspire/blob/d31331d6132aeb22940dcd8834344956ba811373/tests/Aspire.Components.Common.Tests/RequiresDockerDiscoverer.cs

using Xunit.Abstractions;
using Xunit.Sdk;

namespace Aspire.Components.Common.Tests;

public class RequiresDockerDiscoverer : ITraitDiscoverer
{
public IEnumerable<KeyValuePair<string, string>> GetTraits(IAttributeInfo traitAttribute)
{
if (!RequiresDockerAttribute.IsSupported)
{
yield return new KeyValuePair<string, string>("category", "unsupported-platform");
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

13 changes: 0 additions & 13 deletions tests/Aspire.CommunityToolkit.Testing/XUnit/ITestCondition.cs

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions tests/Aspire.CommunityToolkit.Testing/XUnit/OperatingSystems.cs

This file was deleted.

Loading

0 comments on commit c69e49f

Please sign in to comment.