|
| 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.Dapr; |
| 5 | +using Aspire.Hosting.Tests.Utils; |
| 6 | +using Aspire.Hosting.Utils; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Aspire.Hosting.Tests.Dapr; |
| 11 | + |
| 12 | +public class DaprTests |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public async Task WithDaprSideCarAddsAnnotationAndSidecarResource() |
| 16 | + { |
| 17 | + using var builder = TestDistributedApplicationBuilder.Create(); |
| 18 | + builder.AddDapr(o => |
| 19 | + { |
| 20 | + // Fake path to avoid throwing |
| 21 | + o.DaprPath = "dapr"; |
| 22 | + }); |
| 23 | + |
| 24 | + builder.AddContainer("name", "image") |
| 25 | + .WithEndpoint("http", e => |
| 26 | + { |
| 27 | + e.Port = 8000; |
| 28 | + e.AllocatedEndpoint = new(e, "localhost", 80); |
| 29 | + }) |
| 30 | + .WithDaprSidecar(); |
| 31 | + |
| 32 | + using var app = builder.Build(); |
| 33 | + await app.ExecuteBeforeStartHooksAsync(default); |
| 34 | + |
| 35 | + var model = app.Services.GetRequiredService<DistributedApplicationModel>(); |
| 36 | + |
| 37 | + Assert.Equal(3, model.Resources.Count); |
| 38 | + var container = Assert.Single(model.Resources.OfType<ContainerResource>()); |
| 39 | + var sidecarResource = Assert.Single(model.Resources.OfType<IDaprSidecarResource>()); |
| 40 | + var sideCarCli = Assert.Single(model.Resources.OfType<ExecutableResource>()); |
| 41 | + |
| 42 | + Assert.True(sideCarCli.TryGetEndpoints(out var endpoints)); |
| 43 | + |
| 44 | + var ports = new Dictionary<string, int> |
| 45 | + { |
| 46 | + ["http"] = 3500, |
| 47 | + ["grpc"] = 50001, |
| 48 | + ["metrics"] = 9090 |
| 49 | + }; |
| 50 | + |
| 51 | + foreach (var e in endpoints) |
| 52 | + { |
| 53 | + e.AllocatedEndpoint = new(e, "localhost", ports[e.Name]); |
| 54 | + } |
| 55 | + |
| 56 | + var config = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(container); |
| 57 | + var sidecarArgs = await ArgumentEvaluator.GetArgumentListAsync(sideCarCli); |
| 58 | + |
| 59 | + Assert.Equal("http://localhost:3500", config["DAPR_HTTP_ENDPOINT"]); |
| 60 | + Assert.Equal("http://localhost:50001", config["DAPR_GRPC_ENDPOINT"]); |
| 61 | + |
| 62 | + var expectedArgs = new[] |
| 63 | + { |
| 64 | + "run", |
| 65 | + "--app-id", |
| 66 | + "name", |
| 67 | + "--app-port", |
| 68 | + "80", |
| 69 | + "--dapr-grpc-port", |
| 70 | + "{{- portForServing \"name-dapr-cli_grpc\" -}}", |
| 71 | + "--dapr-http-port", |
| 72 | + "{{- portForServing \"name-dapr-cli_http\" -}}", |
| 73 | + "--metrics-port", |
| 74 | + "{{- portForServing \"name-dapr-cli_metrics\" -}}", |
| 75 | + "--app-channel-address", |
| 76 | + "localhost" |
| 77 | + }; |
| 78 | + |
| 79 | + Assert.Equal(expectedArgs, sidecarArgs); |
| 80 | + Assert.NotNull(container.Annotations.OfType<DaprSidecarAnnotation>()); |
| 81 | + } |
| 82 | +} |
0 commit comments