|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Aspire.Hosting.ApplicationModel; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Microsoft.Extensions.Logging.Abstractions; |
| 7 | +using Microsoft.Extensions.Options; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Aspire.Hosting.Docker.Tests; |
| 11 | + |
| 12 | +public class DockerComposePublisherTests |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public async Task PublishAsync_GeneratesValidDockerComposeFile() |
| 16 | + { |
| 17 | + using var tempDir = new TempDirectory(); |
| 18 | + // Arrange |
| 19 | + var options = new OptionsMonitor(new DockerComposePublisherOptions { OutputPath = tempDir.Path }); |
| 20 | + var builder = DistributedApplication.CreateBuilder(); |
| 21 | + |
| 22 | + var param0 = builder.AddParameter("param0"); |
| 23 | + var param1 = builder.AddParameter("param1", secret: true); |
| 24 | + var param2 = builder.AddParameter("param2", "default", publishValueAsDefault: true); |
| 25 | + var cs = builder.AddConnectionString("cs", ReferenceExpression.Create($"Url={param0}, Secret={param1}")); |
| 26 | + |
| 27 | + // Add a container to the application |
| 28 | + var api = builder.AddContainer("myapp", "mcr.microsoft.com/dotnet/aspnet:8.0") |
| 29 | + .WithEnvironment("ASPNETCORE_ENVIRONMENT", "Development") |
| 30 | + .WithHttpEndpoint(env: "PORT") |
| 31 | + .WithEnvironment("param0", param0) |
| 32 | + .WithEnvironment("param1", param1) |
| 33 | + .WithEnvironment("param2", param2) |
| 34 | + .WithReference(cs) |
| 35 | + .WithArgs("--cs", cs.Resource); |
| 36 | + |
| 37 | + builder.AddProject<TestProject>("project1", launchProfileName: null) |
| 38 | + .WithReference(api.GetEndpoint("http")); |
| 39 | + |
| 40 | + var app = builder.Build(); |
| 41 | + |
| 42 | + var model = app.Services.GetRequiredService<DistributedApplicationModel>(); |
| 43 | + |
| 44 | + var publisher = new DockerComposePublisher("test", options, |
| 45 | + NullLogger<DockerComposePublisher>.Instance, |
| 46 | + new DistributedApplicationExecutionContext(DistributedApplicationOperation.Publish)); |
| 47 | + |
| 48 | + // Act |
| 49 | + await publisher.PublishAsync(model, default); |
| 50 | + |
| 51 | + // Assert |
| 52 | + var composePath = Path.Combine(tempDir.Path, "docker-compose.yaml"); |
| 53 | + var envPath = Path.Combine(tempDir.Path, ".env"); |
| 54 | + Assert.True(File.Exists(composePath)); |
| 55 | + Assert.True(File.Exists(envPath)); |
| 56 | + |
| 57 | + var content = await File.ReadAllTextAsync(composePath); |
| 58 | + var envContent = await File.ReadAllTextAsync(envPath); |
| 59 | + |
| 60 | + Assert.Equal( |
| 61 | + """ |
| 62 | + services: |
| 63 | + myapp: |
| 64 | + image: "mcr.microsoft.com/dotnet/aspnet:8.0" |
| 65 | + command: |
| 66 | + - "--cs" |
| 67 | + - "Url=${PARAM0}, Secret=${PARAM1}" |
| 68 | + environment: |
| 69 | + ASPNETCORE_ENVIRONMENT: "Development" |
| 70 | + PORT: "8001" |
| 71 | + param0: "${PARAM0}" |
| 72 | + param1: "${PARAM1}" |
| 73 | + param2: "${PARAM2}" |
| 74 | + ConnectionStrings__cs: "Url=${PARAM0}, Secret=${PARAM1}" |
| 75 | + ports: |
| 76 | + - "8001:8000" |
| 77 | + networks: |
| 78 | + - "aspire" |
| 79 | + project1: |
| 80 | + image: "${PROJECT1_IMAGE}" |
| 81 | + environment: |
| 82 | + OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES: "true" |
| 83 | + OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES: "true" |
| 84 | + OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY: "in_memory" |
| 85 | + services__myapp__http__0: "http://myapp:8000" |
| 86 | + networks: |
| 87 | + - "aspire" |
| 88 | + networks: |
| 89 | + aspire: |
| 90 | + driver: "bridge" |
| 91 | +
|
| 92 | + """, |
| 93 | + content, ignoreAllWhiteSpace: true, ignoreLineEndingDifferences: true); |
| 94 | + |
| 95 | + Assert.Equal( |
| 96 | + """ |
| 97 | + # Parameter param0 |
| 98 | + PARAM0= |
| 99 | +
|
| 100 | + # Parameter param1 |
| 101 | + PARAM1= |
| 102 | +
|
| 103 | + # Parameter param2 |
| 104 | + PARAM2=default |
| 105 | +
|
| 106 | + # Container image name for project1 |
| 107 | + PROJECT1_IMAGE=project1:latest |
| 108 | +
|
| 109 | +
|
| 110 | + """, |
| 111 | + envContent, ignoreAllWhiteSpace: true, ignoreLineEndingDifferences: true); |
| 112 | + } |
| 113 | + |
| 114 | + private sealed class OptionsMonitor(DockerComposePublisherOptions options) : IOptionsMonitor<DockerComposePublisherOptions> |
| 115 | + { |
| 116 | + public DockerComposePublisherOptions Get(string? name) => options; |
| 117 | + |
| 118 | + public IDisposable OnChange(Action<DockerComposePublisherOptions, string> listener) => null!; |
| 119 | + |
| 120 | + public DockerComposePublisherOptions CurrentValue => options; |
| 121 | + } |
| 122 | + |
| 123 | + private sealed class TempDirectory : IDisposable |
| 124 | + { |
| 125 | + public TempDirectory() |
| 126 | + { |
| 127 | + Path = Directory.CreateTempSubdirectory(".aspire-compose").FullName; |
| 128 | + } |
| 129 | + |
| 130 | + public string Path { get; } |
| 131 | + public void Dispose() |
| 132 | + { |
| 133 | + if (File.Exists(Path)) |
| 134 | + { |
| 135 | + File.Delete(Path); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private sealed class TestProject : IProjectMetadata |
| 141 | + { |
| 142 | + public string ProjectPath => "another-path"; |
| 143 | + |
| 144 | + public LaunchSettings? LaunchSettings { get; set; } |
| 145 | + } |
| 146 | +} |
0 commit comments