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

Remove InternalsVisibleTo from Aspire.Hosting to Aspire.Hosting.Testing #3193

Merged
Merged
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
69 changes: 8 additions & 61 deletions src/Aspire.Hosting.Testing/DistributedApplicationFactoryOfT.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Reflection;
using Aspire.Hosting.Dcp;
using Aspire.Hosting.Publishing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace Aspire.Hosting.Testing;

Expand Down Expand Up @@ -135,6 +130,14 @@ private void OnBuilderCreatingCore(DistributedApplicationOptions applicationOpti
hostBuilderOptions.ApplicationName = typeof(TEntryPoint).Assembly.GetName().Name ?? string.Empty;
applicationOptions.AssemblyName = typeof(TEntryPoint).Assembly.GetName().Name ?? string.Empty;
applicationOptions.DisableDashboard = true;
var cfg = hostBuilderOptions.Configuration ??= new();
cfg.AddInMemoryCollection(new Dictionary<string, string?>
{
["DcpPublisher:RandomizePorts"] = "true",
["DcpPublisher:DeleteResourcesOnShutdown"] = "true",
["DcpPublisher:ResourceNameSuffix"] = $"{Random.Shared.Next():x}",
});

OnBuilderCreating(applicationOptions, hostBuilderOptions);
}

Expand All @@ -147,14 +150,6 @@ private void OnBuildingCore(DistributedApplicationBuilder applicationBuilder)
{
// Patch DcpOptions configuration
var services = applicationBuilder.Services;
services.RemoveAll<IConfigureOptions<DcpOptions>>();
services.AddSingleton<IConfigureOptions<DcpOptions>, ConfigureDcpOptions>();
services.Configure<DcpOptions>(o =>
{
o.ResourceNameSuffix = $"{Random.Shared.Next():x}";
o.DeleteResourcesOnShutdown = true;
o.RandomizePorts = true;
});

services.AddHttpClient();
services.ConfigureHttpClientDefaults(http => http.AddStandardResilienceHandler());
Expand Down Expand Up @@ -385,52 +380,4 @@ public async Task StartAsync(CancellationToken cancellationToken = default)

public Task StopAsync(CancellationToken cancellationToken = default) => innerHost.StopAsync(cancellationToken);
}

private sealed class ConfigureDcpOptions(IConfiguration configuration) : IConfigureOptions<DcpOptions>
{
private const string DcpCliPathMetadataKey = "DcpCliPath";
private const string DcpExtensionsPathMetadataKey = "DcpExtensionsPath";
private const string DcpBinPathMetadataKey = "DcpBinPath";

public void Configure(DcpOptions options)
{
var dcpPublisherConfiguration = configuration.GetSection("DcpPublisher");
var publishingConfiguration = configuration.GetSection("Publishing");

string? publisher = publishingConfiguration[nameof(PublishingOptions.Publisher)];
string? cliPath;

if (publisher is not null && publisher != "dcp")
{
// If DCP is not set as the publisher, don't calculate the DCP config
return;
}

if (!string.IsNullOrEmpty(dcpPublisherConfiguration["CliPath"]))
{
// If an explicit path to DCP was provided from configuration, don't try to resolve via assembly attributes
cliPath = dcpPublisherConfiguration["CliPath"];
options.CliPath = cliPath;
}
else
{
var entryPointAssembly = typeof(TEntryPoint).Assembly;
var assemblyMetadata = entryPointAssembly?.GetCustomAttributes<AssemblyMetadataAttribute>();
cliPath = GetMetadataValue(assemblyMetadata, DcpCliPathMetadataKey);
options.CliPath = cliPath;
options.ExtensionsPath = GetMetadataValue(assemblyMetadata, DcpExtensionsPathMetadataKey);
options.BinPath = GetMetadataValue(assemblyMetadata, DcpBinPathMetadataKey);
}

if (string.IsNullOrEmpty(cliPath))
{
throw new InvalidOperationException($"Could not resolve the path to the Aspire application host. The application cannot be run without it.");
}
}

private static string? GetMetadataValue(IEnumerable<AssemblyMetadataAttribute>? assemblyMetadata, string key)
{
return assemblyMetadata?.FirstOrDefault(m => string.Equals(m.Key, key, StringComparison.OrdinalIgnoreCase))?.Value;
}
}
}
1 change: 0 additions & 1 deletion src/Aspire.Hosting/Aspire.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Aspire.Hosting.Testing" />
<InternalsVisibleTo Include="Aspire.Hosting.Tests" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Hosting/Dcp/ApplicationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ private void PrepareServices()
var uniqueServiceName = GenerateUniqueServiceName(serviceNames, candidateServiceName);
var svc = Service.Create(uniqueServiceName);

var port = _options.Value.RandomizePorts is true && endpoint.IsProxied ? null : endpoint.Port;
var port = _options.Value.RandomizePorts && endpoint.IsProxied ? null : endpoint.Port;
svc.Spec.Port = port;
svc.Spec.Protocol = PortProtocol.FromProtocolType(endpoint.Protocol);
svc.Spec.AddressAllocationMode = endpoint.IsProxied ? AddressAllocationModes.Localhost : AddressAllocationModes.Proxyless;
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Hosting/Dcp/DcpHostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public async Task StartAsync(CancellationToken cancellationToken = default)

public async Task StopAsync(CancellationToken cancellationToken = default)
{
if (_dcpOptions.DeleteResourcesOnShutdown is true)
if (_dcpOptions.DeleteResourcesOnShutdown)
{
try
{
Expand Down
7 changes: 5 additions & 2 deletions src/Aspire.Hosting/Dcp/DcpOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ internal sealed class DcpOptions
/// <summary>
/// Whether to delete resources created by this application when the application is shut down.
/// </summary>
public bool? DeleteResourcesOnShutdown { get; set; }
public bool DeleteResourcesOnShutdown { get; set; }

/// <summary>
/// Whether to randomize ports used by resources during orchestration.
/// </summary>
public bool? RandomizePorts { get; set; }
public bool RandomizePorts { get; set; }

public int KubernetesConfigReadRetryCount { get; set; } = 300;

Expand Down Expand Up @@ -137,6 +137,9 @@ public void ApplyApplicationConfiguration(DistributedApplicationOptions appOptio
ResourceNameSuffix = dcpPublisherConfiguration[nameof(ResourceNameSuffix)];
}

DeleteResourcesOnShutdown = dcpPublisherConfiguration.GetValue<bool>(nameof(DeleteResourcesOnShutdown), DeleteResourcesOnShutdown);
RandomizePorts = dcpPublisherConfiguration.GetValue<bool>(nameof(RandomizePorts), RandomizePorts);

if (string.IsNullOrEmpty(CliPath))
{
throw new InvalidOperationException($"Could not resolve the path to the Aspire application host. The application cannot be run without it.");
Expand Down