Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Aspire Dashboard module #1190 #1194

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Testcontainers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.ActiveMq", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.ArangoDb", "src\Testcontainers.ArangoDb\Testcontainers.ArangoDb.csproj", "{AB9C1563-07C7-4685-BACD-BB1FF64B3611}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.AspireDashboard", "src\Testcontainers.AspireDashboard\Testcontainers.AspireDashboard.csproj", "{9B8A4BDE-1D9C-48A0-A64A-C33FBC701E0A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Azurite", "src\Testcontainers.Azurite\Testcontainers.Azurite.csproj", "{3F2E254F-C203-43FD-A078-DC3E2CBC0F9F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.BigQuery", "src\Testcontainers.BigQuery\Testcontainers.BigQuery.csproj", "{A9FF9C7F-BBA0-4B44-90B7-48A60F9E00F3}"
Expand Down Expand Up @@ -105,6 +107,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.ActiveMq.Tes
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.ArangoDb.Tests", "tests\Testcontainers.ArangoDb.Tests\Testcontainers.ArangoDb.Tests.csproj", "{8E1E0A6D-EEBB-4455-B8E8-A55AF9B2062C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.AspireDashboard.Tests", "tests\Testcontainers.AspireDashboard.Tests\Testcontainers.AspireDashboard.Tests.csproj", "{82F79BC0-5E4A-4380-82C5-8B6E3CF05958}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Azurite.Tests", "tests\Testcontainers.Azurite.Tests\Testcontainers.Azurite.Tests.csproj", "{B272FDDE-5E01-425D-B9E1-10FF883DDAAA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.BigQuery.Tests", "tests\Testcontainers.BigQuery.Tests\Testcontainers.BigQuery.Tests.csproj", "{03E60673-078A-4508-99AD-8537CE6F78F1}"
Expand Down Expand Up @@ -216,6 +220,10 @@ Global
{AB9C1563-07C7-4685-BACD-BB1FF64B3611}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB9C1563-07C7-4685-BACD-BB1FF64B3611}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB9C1563-07C7-4685-BACD-BB1FF64B3611}.Release|Any CPU.Build.0 = Release|Any CPU
{9B8A4BDE-1D9C-48A0-A64A-C33FBC701E0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B8A4BDE-1D9C-48A0-A64A-C33FBC701E0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B8A4BDE-1D9C-48A0-A64A-C33FBC701E0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B8A4BDE-1D9C-48A0-A64A-C33FBC701E0A}.Release|Any CPU.Build.0 = Release|Any CPU
{3F2E254F-C203-43FD-A078-DC3E2CBC0F9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F2E254F-C203-43FD-A078-DC3E2CBC0F9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F2E254F-C203-43FD-A078-DC3E2CBC0F9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -392,6 +400,10 @@ Global
{8E1E0A6D-EEBB-4455-B8E8-A55AF9B2062C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E1E0A6D-EEBB-4455-B8E8-A55AF9B2062C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E1E0A6D-EEBB-4455-B8E8-A55AF9B2062C}.Release|Any CPU.Build.0 = Release|Any CPU
{82F79BC0-5E4A-4380-82C5-8B6E3CF05958}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{82F79BC0-5E4A-4380-82C5-8B6E3CF05958}.Debug|Any CPU.Build.0 = Debug|Any CPU
{82F79BC0-5E4A-4380-82C5-8B6E3CF05958}.Release|Any CPU.ActiveCfg = Release|Any CPU
{82F79BC0-5E4A-4380-82C5-8B6E3CF05958}.Release|Any CPU.Build.0 = Release|Any CPU
{B272FDDE-5E01-425D-B9E1-10FF883DDAAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B272FDDE-5E01-425D-B9E1-10FF883DDAAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B272FDDE-5E01-425D-B9E1-10FF883DDAAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -584,6 +596,7 @@ Global
GlobalSection(NestedProjects) = preSolution
{5365F780-0E6C-41F0-B1B9-7DC34368F80C} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{AB9C1563-07C7-4685-BACD-BB1FF64B3611} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{9B8A4BDE-1D9C-48A0-A64A-C33FBC701E0A} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{3F2E254F-C203-43FD-A078-DC3E2CBC0F9F} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{A9FF9C7F-BBA0-4B44-90B7-48A60F9E00F3} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{302EC1E0-AE75-4E99-A6BF-524F35338BC8} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
Expand Down Expand Up @@ -628,6 +641,7 @@ Global
{EC76857B-A3B8-4B7A-A1B0-8D867A4D1733} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{AB93C67F-0A53-4525-AE6C-29B065820ABE} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{8E1E0A6D-EEBB-4455-B8E8-A55AF9B2062C} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{82F79BC0-5E4A-4380-82C5-8B6E3CF05958} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{B272FDDE-5E01-425D-B9E1-10FF883DDAAA} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{03E60673-078A-4508-99AD-8537CE6F78F1} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{2E7B92E3-8526-4706-90F3-00F0F5C47C37} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
Expand Down
46 changes: 46 additions & 0 deletions docs/modules/aspire-dashboard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Aspire Dashboard

## Configuration

The Aspire Dashboard can be configured in the following ways:

- Use `AllowAnonymous(true)` to allow anonymous access to the dashboard.
- Use `AllowUnsecuredTransport` to allow unsecured transport, such as HTTP.
- Aspire Dashboard usually runs on port 18888. You can change the port by using the `WithPortBinding`

```csharp
public sealed class AspireDashboardContainerTest : IAsyncLifetime
{
private readonly AspireDashboardContainer _container = new AspireDashboardBuilder()
.AllowAnonymous(true)
.AllowUnsecuredTransport(false)
.WithPortBinding(
AspireDashboardBuilder.AspireDashboardFrontendPort,
AspireDashboardBuilder.AspireDashboardFrontendPort
)
.Build();

public Task InitializeAsync()
{
return _container.StartAsync();
}

public Task DisposeAsync()
{
return _container.DisposeAsync().AsTask();
}

[Fact]
public async Task GetDashboardReturnsHttpStatusCodeOk()
{
using var httpClient = new HttpClient();
var address = new Uri(_container.GetDashboardUrl());
httpClient.BaseAddress = address;

using var response = await httpClient.GetAsync("/");

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
```
[Aspire Dashboard on Microsoft Artifact Registry](https://mcr.microsoft.com/en-us/product/dotnet/aspire-dashboard/tags)
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ nav:
- modules/postgres.md
- modules/pulsar.md
- modules/rabbitmq.md
- modules/aspire-dashboard.md
1 change: 1 addition & 0 deletions src/Testcontainers.AspireDashboard/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
118 changes: 118 additions & 0 deletions src/Testcontainers.AspireDashboard/AspireDashboardBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
namespace Testcontainers.AspireDashboard;

/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class AspireDashboardBuilder
: ContainerBuilder<
AspireDashboardBuilder,
AspireDashboardContainer,
AspireDashboardConfiguration
>
{
// https://mcr.microsoft.com/en-us/product/dotnet/aspire-dashboard/tags
public const string AspireDashboardImage = "mcr.microsoft.com/dotnet/aspire-dashboard:8.1.0";

public const ushort AspireDashboardFrontendPort = 18888;

public const ushort AspireDashboardOtlpPort = 18889;

/// <summary>
/// Initializes a new instance of the <see cref="AspireDashboardBuilder" /> class.
/// </summary>
public AspireDashboardBuilder()
: this(new AspireDashboardConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}

/// <summary>
/// Initializes a new instance of the <see cref="AspireDashboardBuilder" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
private AspireDashboardBuilder(AspireDashboardConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}

/// <inheritdoc />
protected override AspireDashboardConfiguration DockerResourceConfiguration { get; }

/// <summary>
/// Configures the dashboard to accept anonymous access.
/// </summary>
/// <param name="allowed">A value indicating whether anonymous access is allowed.</param>
/// <returns>A configured instance of <see cref="AspireDashboardBuilder" />.</returns>
public AspireDashboardBuilder AllowAnonymous(bool allowed)
{
return Merge(DockerResourceConfiguration, new AspireDashboardConfiguration())
.WithEnvironment(
"DOTNET_DASHBOARD_UNSECURED_ALLOW_ANONYMOUS",
allowed.ToString().ToLowerInvariant()
);
}

/// <summary>
/// Configures the dashboard to allow unsecured transport.
/// </summary>
/// <param name="allowed">A value indicating whether unsecured transport is allowed.</param>
/// <returns>A configured instance of <see cref="AspireDashboardBuilder" />.</returns>
public AspireDashboardBuilder AllowUnsecuredTransport(bool allowed)
{
return Merge(DockerResourceConfiguration, new AspireDashboardConfiguration())
.WithEnvironment(
"ASPIRE_ALLOW_UNSECURED_TRANSPORT",
allowed.ToString().ToLowerInvariant()
);
}

/// <inheritdoc />
public override AspireDashboardContainer Build()
{
Validate();
return new AspireDashboardContainer(DockerResourceConfiguration);
}

/// <inheritdoc />
protected override AspireDashboardBuilder Init()
{
return base.Init()
.WithImage(AspireDashboardImage)
.WithPortBinding(AspireDashboardFrontendPort, true)
.WithPortBinding(AspireDashboardOtlpPort, true)
.AllowAnonymous(true)
.WithWaitStrategy(
Wait.ForUnixContainer()
.UntilHttpRequestIsSucceeded(r => r.ForPort(AspireDashboardFrontendPort))
);
}

/// <inheritdoc />
protected override AspireDashboardBuilder Clone(
IResourceConfiguration<CreateContainerParameters> resourceConfiguration
)
{
return Merge(
DockerResourceConfiguration,
new AspireDashboardConfiguration(resourceConfiguration)
);
}

/// <inheritdoc />
protected override AspireDashboardBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(
DockerResourceConfiguration,
new AspireDashboardConfiguration(resourceConfiguration)
);
}

/// <inheritdoc />
protected override AspireDashboardBuilder Merge(
AspireDashboardConfiguration oldValue,
AspireDashboardConfiguration newValue
)
{
return new AspireDashboardBuilder(new AspireDashboardConfiguration(oldValue, newValue));
}
}
54 changes: 54 additions & 0 deletions src/Testcontainers.AspireDashboard/AspireDashboardConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace Testcontainers.AspireDashboard;

/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class AspireDashboardConfiguration : ContainerConfiguration
{
/// <summary>
/// Initializes a new instance of the <see cref="AspireDashboardConfiguration" /> class.
/// </summary>
public AspireDashboardConfiguration() { }

/// <summary>
/// Initializes a new instance of the <see cref="AspireDashboardConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public AspireDashboardConfiguration(
IResourceConfiguration<CreateContainerParameters> resourceConfiguration
)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="AspireDashboardConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public AspireDashboardConfiguration(IContainerConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="AspireDashboardConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public AspireDashboardConfiguration(AspireDashboardConfiguration resourceConfiguration)
: this(new AspireDashboardConfiguration(), resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="AspireDashboardConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public AspireDashboardConfiguration(
AspireDashboardConfiguration oldValue,
AspireDashboardConfiguration newValue
)
: base(oldValue, newValue) { }
}
64 changes: 64 additions & 0 deletions src/Testcontainers.AspireDashboard/AspireDashboardContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Microsoft.Extensions.Logging;

namespace Testcontainers.AspireDashboard;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class AspireDashboardContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="AspireDashboardContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
public AspireDashboardContainer(AspireDashboardConfiguration configuration)
: base(configuration)
{
Started += (_, _) => Logger.LogInformation("AspireDashboard container is ready!");
Logger.LogInformation(
"Dashboard available at {Url}.",
new UriBuilder(
Uri.UriSchemeHttp,
Hostname,
GetMappedPublicPort(AspireDashboardBuilder.AspireDashboardFrontendPort)
)
);
Logger.LogInformation(
"OTLP endpoint available at {Url}.",
new UriBuilder(
Uri.UriSchemeHttp,
Hostname,
GetMappedPublicPort(AspireDashboardBuilder.AspireDashboardOtlpPort)
)
);
}

/// <summary>
/// Gets the AspireDashboard URL.
/// </summary>
/// <returns>The AspireDashboard URL.</returns>
public string GetDashboardUrl()
{
var endpoint = new UriBuilder(
Uri.UriSchemeHttp,
Hostname,
GetMappedPublicPort(AspireDashboardBuilder.AspireDashboardFrontendPort)
);

return endpoint.ToString();
}

/// <summary>
/// Gets the AspireDashboard OTLP endpoint URL.
/// </summary>
/// <returns>The AspireDashboard OTLP endpoint URL.</returns>
public string GetOtlpEndpointUrl()
{
var endpoint = new UriBuilder(
Uri.UriSchemeHttp,
Hostname,
GetMappedPublicPort(AspireDashboardBuilder.AspireDashboardOtlpPort)
);

return endpoint.ToString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0;netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" VersionOverride="2023.3.0" PrivateAssets="All"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Testcontainers/Testcontainers.csproj"/>
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions src/Testcontainers.AspireDashboard/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Net.Http;
global using System.Threading;
global using System.Threading.Tasks;
global using Docker.DotNet.Models;
global using DotNet.Testcontainers.Builders;
global using DotNet.Testcontainers.Configurations;
global using DotNet.Testcontainers.Containers;
global using JetBrains.Annotations;
1 change: 1 addition & 0 deletions tests/Testcontainers.AspireDashboard.Tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
Loading