dotnet / aspire

Tools, templates, and packages to accelerate building observable, production-ready apps
https://learn.microsoft.com/dotnet/aspire
MIT License
3.94k stars 481 forks source link

Aspire.Hosting.Testing should populate DOTNET_LAUNCH_PROFILE #5093

Open eerhardt opened 4 months ago

eerhardt commented 4 months ago

When Aspire.Hosting.Testing's DistributedApplicationTestingBuilder creates and builds a DistributedApplicationBuider, it should pass in the DOTNET_LAUNCH_PROFILE configuration setting so AppHost code can switch off the launch profile, like documented in https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables#dotnet_launch_profile.

We use this in our samples here:

https://github.com/dotnet/aspire-samples/blob/08c14cdc112c74811c44976064b8d85e39e8bd93/samples/AspireWithNode/AspireWithNode.AppHost/Program.cs#L16-L20

But that check doesn't work when running in an Aspire.Hosting.Testing test because that configuration setting isn't being populated by the testing infrastructure.

cc @ReubenBond @mitchdenny @radical

mackie1001 commented 2 months ago

Interested in the outcome of this.

I'm looking to feed in some configuration from test host to the underlying Aspire host as we have various options that are controlled via launchProfile environment vars when running the host directly.

Being able to set the launch profile (it appears no launch profile is used when using DistributedApplicationTestingBuilder) or directly setting environment vars or even injecting into the inner builder's configuration collection directly would be very handy.

As a workaround we've used the command line args directly and ensured that they've loaded into the ambient configuration in the Aspire host. As long as we use the .Net configuration stuff and not environment variables directly this works in all scenarios.

Test project:

        //xunit IAsyncLifetime
        public async Task InitializeAsync()
        {
            string[] args = 
            [
                "USE_EMULATORS=False",
                "USE_USER_SECRETS=True",
                "PROVISION_DATABASES_BEFORE_START=False"
            ];

            var builder = await DistributedApplicationTestingBuilder
                .CreateAsync<Projects.Membership_Aspire_Host>(args, (options, builderSettings) => { });

            AspireHost = await builder.BuildAsync();
            await AspireHost.StartAsync();
        }

Aspire host:

        //Aspire.AppHost/Program.cs
        private static async Task Main(string[] args)
        {
            var options = new DistributedApplicationOptions() { Args = args }; //Passing args here does not seem to init builder.Configuration
            var builder = DistributedApplication.CreateBuilder(options);
            builder.Configuration.AddCommandLine(args); //This does work and make the args passed from the text fixture available
            ...
            var useEmulators = builder.Configuration["USE_EMULATORS"] == "True";