|
| 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 | +#pragma warning disable ASPIREAZURE001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. |
| 5 | +#pragma warning disable ASPIRECOMPUTE001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. |
| 6 | +#pragma warning disable ASPIREPUBLISHERS001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. |
| 7 | +#pragma warning disable ASPIREINTERACTION001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. |
| 8 | + |
| 9 | +using Aspire.Hosting.Utils; |
| 10 | +using Aspire.Hosting.Tests; |
| 11 | +using Microsoft.Extensions.DependencyInjection; |
| 12 | +using Aspire.Hosting.Azure.Provisioning.Internal; |
| 13 | +using Aspire.Hosting.Testing; |
| 14 | +using System.Text.Json.Nodes; |
| 15 | + |
| 16 | +namespace Aspire.Hosting.Azure.Tests; |
| 17 | + |
| 18 | +public class AzureDeployerTests(ITestOutputHelper output) |
| 19 | +{ |
| 20 | + [Fact] |
| 21 | + public void DeployAsync_EmitsPublishedResources() |
| 22 | + { |
| 23 | + // Arrange |
| 24 | + var tempDir = Directory.CreateTempSubdirectory(".azure-deployer-test"); |
| 25 | + output.WriteLine($"Temp directory: {tempDir.FullName}"); |
| 26 | + using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish, publisher: "default", outputPath: tempDir.FullName, isDeploy: true); |
| 27 | + // Configure Azure settings to avoid prompting during deployment for this test case |
| 28 | + builder.Configuration["Azure:SubscriptionId"] = "12345678-1234-1234-1234-123456789012"; |
| 29 | + builder.Configuration["Azure:ResourceGroup"] = "test-rg"; |
| 30 | + builder.Configuration["Azure:Location"] = "westus2"; |
| 31 | + |
| 32 | + var containerAppEnv = builder.AddAzureContainerAppEnvironment("env"); |
| 33 | + |
| 34 | + // Add a container that will use the container app environment |
| 35 | + builder.AddContainer("api", "my-api-image:latest") |
| 36 | + .WithHttpEndpoint(); |
| 37 | + |
| 38 | + // Act |
| 39 | + using var app = builder.Build(); |
| 40 | + app.Run(); |
| 41 | + |
| 42 | + // Assert files exist but don't verify contents |
| 43 | + var mainBicepPath = Path.Combine(tempDir.FullName, "main.bicep"); |
| 44 | + Assert.True(File.Exists(mainBicepPath)); |
| 45 | + var envBicepPath = Path.Combine(tempDir.FullName, "env", "env.bicep"); |
| 46 | + Assert.True(File.Exists(envBicepPath)); |
| 47 | + |
| 48 | + tempDir.Delete(recursive: true); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task DeployAsync_PromptsViaInteractionService() |
| 53 | + { |
| 54 | + // Arrange |
| 55 | + using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish, publisher: "default", isDeploy: true); |
| 56 | + var testInteractionService = new TestInteractionService(); |
| 57 | + ConfigureTestServices(builder, testInteractionService); |
| 58 | + |
| 59 | + // Add an Azure environment resource which will trigger the deployment prompting |
| 60 | + builder.AddAzureEnvironment(); |
| 61 | + |
| 62 | + // Act |
| 63 | + using var app = builder.Build(); |
| 64 | + |
| 65 | + var runTask = Task.Run(app.Run); |
| 66 | + |
| 67 | + // Assert - Wait for the first interaction (message bar) |
| 68 | + var messageBarInteraction = await testInteractionService.Interactions.Reader.ReadAsync(); |
| 69 | + Assert.Equal("Azure provisioning", messageBarInteraction.Title); |
| 70 | + Assert.Contains("Azure resources that require an Azure Subscription", messageBarInteraction.Message ?? ""); |
| 71 | + |
| 72 | + // Complete the message bar interaction to proceed to inputs dialog |
| 73 | + messageBarInteraction.CompletionTcs.SetResult(InteractionResult.Ok(true)); // Data = true (user clicked Enter Values) |
| 74 | + |
| 75 | + // Wait for the inputs interaction |
| 76 | + var inputsInteraction = await testInteractionService.Interactions.Reader.ReadAsync(); |
| 77 | + Assert.Equal("Azure provisioning", inputsInteraction.Title); |
| 78 | + Assert.True(inputsInteraction.Options!.EnableMessageMarkdown); |
| 79 | + |
| 80 | + // Verify the expected inputs for Azure provisioning |
| 81 | + Assert.Collection(inputsInteraction.Inputs, |
| 82 | + input => |
| 83 | + { |
| 84 | + Assert.Equal("Location", input.Label); |
| 85 | + Assert.Equal(InputType.Choice, input.InputType); |
| 86 | + Assert.True(input.Required); |
| 87 | + }, |
| 88 | + input => |
| 89 | + { |
| 90 | + Assert.Equal("Subscription ID", input.Label); |
| 91 | + Assert.Equal(InputType.SecretText, input.InputType); |
| 92 | + Assert.True(input.Required); |
| 93 | + }, |
| 94 | + input => |
| 95 | + { |
| 96 | + Assert.Equal("Resource group", input.Label); |
| 97 | + Assert.Equal(InputType.Text, input.InputType); |
| 98 | + Assert.False(input.Required); |
| 99 | + }); |
| 100 | + |
| 101 | + // Complete the inputs interaction with valid values |
| 102 | + inputsInteraction.Inputs[0].Value = inputsInteraction.Inputs[0].Options!.First(kvp => kvp.Key == "westus").Value; |
| 103 | + inputsInteraction.Inputs[1].Value = "12345678-1234-1234-1234-123456789012"; |
| 104 | + inputsInteraction.Inputs[2].Value = "test-rg"; |
| 105 | + |
| 106 | + inputsInteraction.CompletionTcs.SetResult(InteractionResult.Ok(inputsInteraction.Inputs)); |
| 107 | + |
| 108 | + // Wait for the run task to complete (or timeout) |
| 109 | + await runTask.WaitAsync(TimeSpan.FromSeconds(10)); |
| 110 | + } |
| 111 | + |
| 112 | + private static void ConfigureTestServices(IDistributedApplicationTestingBuilder builder, IInteractionService interactionService) |
| 113 | + { |
| 114 | + var options = ProvisioningTestHelpers.CreateOptions(null, null, null); |
| 115 | + var environment = ProvisioningTestHelpers.CreateEnvironment(); |
| 116 | + var logger = ProvisioningTestHelpers.CreateLogger(); |
| 117 | + var armClientProvider = ProvisioningTestHelpers.CreateArmClientProvider(); |
| 118 | + var userPrincipalProvider = ProvisioningTestHelpers.CreateUserPrincipalProvider(); |
| 119 | + var tokenCredentialProvider = ProvisioningTestHelpers.CreateTokenCredentialProvider(); |
| 120 | + builder.Services.AddSingleton(armClientProvider); |
| 121 | + builder.Services.AddSingleton(userPrincipalProvider); |
| 122 | + builder.Services.AddSingleton(tokenCredentialProvider); |
| 123 | + builder.Services.AddSingleton(environment); |
| 124 | + builder.Services.AddSingleton(logger); |
| 125 | + builder.Services.AddSingleton(options); |
| 126 | + builder.Services.AddSingleton(interactionService); |
| 127 | + builder.Services.AddSingleton<IProvisioningContextProvider, DefaultProvisioningContextProvider>(); |
| 128 | + builder.Services.AddSingleton<IUserSecretsManager, NoOpUserSecretsManager>(); |
| 129 | + } |
| 130 | + |
| 131 | + private sealed class NoOpUserSecretsManager : IUserSecretsManager |
| 132 | + { |
| 133 | + public Task<JsonObject> LoadUserSecretsAsync(CancellationToken cancellationToken = default) => Task.FromResult(new JsonObject()); |
| 134 | + |
| 135 | + public Task SaveUserSecretsAsync(JsonObject userSecrets, CancellationToken cancellationToken = default) => Task.CompletedTask; |
| 136 | + } |
| 137 | +} |
0 commit comments