dotnet / aspire

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

Support stopping a running resource from an integration test #2899

Open DamianEdwards opened 8 months ago

DamianEdwards commented 8 months ago

It would be useful to be able to stop a running resource in a testing scenario for the purposes of verifying behavior when dependencies aren't running. I imagine this would be a method on DistributedApplication or perhaps a new type that can be resolved from the DI container to allow stopping a resource. DCP already supports stopping resources it started.

e.g.:

[Fact]
public async Task HomePageReturnsOkIfRedisIsUnavailable()
{
    var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Program>();
    await using var app = await appHost.BuildAsync();
    await app.StartAsync();
    var httpClient = app.CreateHttpClient("webfrontend");

    await httpClient.GetStringAsync("/"); // This will retry until the webfrontend has started successfully
    await app.StopResourceAsync("cache"); // Now stop the cache container
    var response = await httpClient.GetAsync("/");

    Assert.Equal(HttpStatus.OK, response.StatusCode);
}
mitchdenny commented 7 months ago

As soon as you ask for the ability to stop, you'll ask for the ability to start it again ;) I think being able to stop, start, and scale resources is a good idea.