-
Notifications
You must be signed in to change notification settings - Fork 871
Fix WithReference to only use IResourceWithEndpoints #14254
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
Open
Falco20019
wants to merge
10
commits into
microsoft:main
Choose a base branch
from
Falco20019:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+214
−1
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
101c9e8
Fix WithReference to only use IResourceWithEndpoints
Falco20019 190a08b
Also ally to other variant
Falco20019 00e8642
Keep original ones
Falco20019 ef67ec8
Fix typo
Falco20019 7e90218
Rename to avoid issues and add tests
Falco20019 3f8e370
Only add ReferenceEnvironmentInjectionFlags.ServiceDiscovery on IReso…
Falco20019 ec595f0
Merge branch 'main' into patch-1
Falco20019 25934a6
Address review comments
Falco20019 f02b31a
Merge branch 'main' into patch-1
Falco20019 541b3b3
Try fix tests
Falco20019 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Hosting.Tests.Utils; | ||
| using Aspire.Hosting.Utils; | ||
| using Microsoft.AspNetCore.InternalTesting; | ||
|
|
||
| namespace Aspire.Hosting.Tests; | ||
|
|
||
| public class WithEndpointsTests | ||
| { | ||
| [Fact] | ||
| public async Task ResourceNamesWithDashesAreEncodedInEnvironmentVariables() | ||
| { | ||
| using var builder = TestDistributedApplicationBuilder.Create(); | ||
|
|
||
| var projectA = builder.AddProject<ProjectA>("project-a") | ||
| .WithHttpsEndpoint(1000, 2000, "mybinding") | ||
| .WithEndpoint("mybinding", e => e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 2000)); | ||
|
|
||
| var projectB = builder.AddProject<ProjectB>("consumer") | ||
| .WithEndpoints(projectA); | ||
|
|
||
| var config = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(projectB.Resource, DistributedApplicationOperation.Run, TestServiceProvider.Instance).DefaultTimeout(); | ||
|
|
||
| Assert.Equal("https://localhost:2000", config["services__project-a__https__0"]); | ||
| Assert.Equal("https://localhost:2000", config["PROJECT_A_MYBINDING"]); | ||
|
Falco20019 marked this conversation as resolved.
|
||
| Assert.DoesNotContain("services__project_a__mybinding__0", config.Keys); | ||
| Assert.DoesNotContain("PROJECT-A_MYBINDING", config.Keys); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task OverriddenServiceNamesAreEncodedInEnvironmentVariables() | ||
| { | ||
| using var builder = TestDistributedApplicationBuilder.Create(); | ||
|
|
||
| var projectA = builder.AddProject<ProjectA>("project-a") | ||
| .WithHttpsEndpoint(1000, 2000, "mybinding") | ||
| .WithEndpoint("mybinding", e => e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 2000)); | ||
|
|
||
| var projectB = builder.AddProject<ProjectB>("consumer") | ||
| .WithEndpoints(projectA, "custom-name"); | ||
|
|
||
| var config = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(projectB.Resource, DistributedApplicationOperation.Run, TestServiceProvider.Instance).DefaultTimeout(); | ||
|
|
||
| Assert.Equal("https://localhost:2000", config["services__custom-name__https__0"]); | ||
| Assert.Equal("https://localhost:2000", config["custom_name_MYBINDING"]); | ||
| Assert.DoesNotContain("services__custom_name__https__0", config.Keys); | ||
| Assert.DoesNotContain("custom-name_MYBINDING", config.Keys); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(ReferenceEnvironmentInjectionFlags.All)] | ||
| [InlineData(ReferenceEnvironmentInjectionFlags.ConnectionProperties)] | ||
| [InlineData(ReferenceEnvironmentInjectionFlags.ConnectionString)] | ||
| [InlineData(ReferenceEnvironmentInjectionFlags.ServiceDiscovery)] | ||
| [InlineData(ReferenceEnvironmentInjectionFlags.Endpoints)] | ||
| [InlineData(ReferenceEnvironmentInjectionFlags.None)] | ||
| public async Task ProjectWithEndpointRespectsCustomEnvironmentVariableNaming(ReferenceEnvironmentInjectionFlags flags) | ||
| { | ||
| using var builder = TestDistributedApplicationBuilder.Create(); | ||
|
|
||
| // Create a binding and its matching annotation (simulating DCP behavior) | ||
| var projectA = builder.AddProject<ProjectA>("projecta") | ||
| .WithHttpsEndpoint(1000, 2000, "mybinding") | ||
| .WithEndpoint("mybinding", e => e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 2000)); | ||
|
|
||
| // Get the service provider. | ||
| var projectB = builder.AddProject<ProjectB>("b") | ||
| .WithEndpoints(projectA, "custom") | ||
| .WithReferenceEnvironment(flags); | ||
|
|
||
| // Call environment variable callbacks. | ||
| var config = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(projectB.Resource, DistributedApplicationOperation.Run, TestServiceProvider.Instance).DefaultTimeout(); | ||
|
|
||
| switch (flags) | ||
| { | ||
| case ReferenceEnvironmentInjectionFlags.All: | ||
| Assert.Equal("https://localhost:2000", config["custom_MYBINDING"]); | ||
| Assert.Equal("https://localhost:2000", config["services__custom__https__0"]); | ||
| break; | ||
|
|
||
| case ReferenceEnvironmentInjectionFlags.ConnectionProperties: | ||
| case ReferenceEnvironmentInjectionFlags.ConnectionString: | ||
| case ReferenceEnvironmentInjectionFlags.None: | ||
| Assert.False(config.ContainsKey("custom_MYBINDING")); | ||
| Assert.False(config.ContainsKey("services__custom__https__0")); | ||
| break; | ||
|
|
||
| case ReferenceEnvironmentInjectionFlags.ServiceDiscovery: | ||
| Assert.False(config.ContainsKey("custom_MYBINDING")); | ||
| Assert.True(config.ContainsKey("services__custom__https__0")); | ||
| break; | ||
|
|
||
| case ReferenceEnvironmentInjectionFlags.Endpoints: | ||
| Assert.True(config.ContainsKey("custom_MYBINDING")); | ||
| Assert.False(config.ContainsKey("services__custom__https__0")); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(ReferenceEnvironmentInjectionFlags.All)] | ||
| [InlineData(ReferenceEnvironmentInjectionFlags.ConnectionProperties)] | ||
| [InlineData(ReferenceEnvironmentInjectionFlags.ConnectionString)] | ||
| [InlineData(ReferenceEnvironmentInjectionFlags.ServiceDiscovery)] | ||
| [InlineData(ReferenceEnvironmentInjectionFlags.Endpoints)] | ||
| [InlineData(ReferenceEnvironmentInjectionFlags.None)] | ||
| public async Task ContainerResourceWithEndpointRespectsCustomEnvironmentVariableNaming(ReferenceEnvironmentInjectionFlags flags) | ||
| { | ||
| using var builder = TestDistributedApplicationBuilder.Create(); | ||
|
|
||
| // Create a binding and its matching annotation (simulating DCP behavior) | ||
| var container = builder.AddContainer("mycontainer", "myimage") | ||
| .WithHttpsEndpoint(1000, 2000, "mybinding") | ||
| .WithEndpoint("mybinding", e => e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 2000)); | ||
|
|
||
| // Get the service provider. | ||
| var project = builder.AddProject<ProjectB>("b") | ||
| .WithEndpoints(container, "custom") | ||
| .WithReferenceEnvironment(flags); | ||
|
|
||
| // Call environment variable callbacks. | ||
| var config = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(project.Resource, DistributedApplicationOperation.Run, TestServiceProvider.Instance).DefaultTimeout(); | ||
|
|
||
| switch (flags) | ||
| { | ||
| case ReferenceEnvironmentInjectionFlags.All: | ||
| Assert.Equal("https://localhost:2000", config["custom_MYBINDING"]); | ||
| Assert.False(config.ContainsKey["services__custom__https__0"]); | ||
| break; | ||
|
|
||
| case ReferenceEnvironmentInjectionFlags.ConnectionProperties: | ||
| case ReferenceEnvironmentInjectionFlags.ConnectionString: | ||
| case ReferenceEnvironmentInjectionFlags.ServiceDiscovery: | ||
| case ReferenceEnvironmentInjectionFlags.None: | ||
| Assert.False(config.ContainsKey("custom_MYBINDING")); | ||
| Assert.False(config.ContainsKey["services__custom__https__0"]); | ||
| break; | ||
|
|
||
| case ReferenceEnvironmentInjectionFlags.Endpoints: | ||
| Assert.True(config.ContainsKey("custom_MYBINDING")); | ||
| Assert.False(config.ContainsKey["services__custom__https__0"]); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| private sealed class ProjectA : IProjectMetadata | ||
| { | ||
| public string ProjectPath => "projectA"; | ||
|
|
||
| public LaunchSettings LaunchSettings { get; } = new(); | ||
| } | ||
|
|
||
| private sealed class ProjectB : IProjectMetadata | ||
| { | ||
| public string ProjectPath => "projectB"; | ||
| public LaunchSettings LaunchSettings { get; } = new(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.