Testing broken network connections
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
I am using Aspire with an application that has many different resources. I want to test how the distributed application behaves when certain resources go offline.
Say I have a web app, and I want to test how it behaves if a redis cache resource goes offline. I can conditionally add the resource to the `DistributedApplicationBuilder`, and this works, but this isn't really testing what happens with the resource offline, it is testing what happens if the resource is not there. Rather than looking for the ability to conditionally include a resource, I am looking for the ability to change a specific resources networking config.
### Describe the solution you'd like
I am looking for an API that would let me cut internet or intranet access for a specific resource, while that resource is running. The API should work with `IDistributedApplicationBuilder` and `IDistributedApplicationTestingBuilder`
I imagine the API usage would be something like this:
```
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache").WithNetworkCommands();
builder.AddProject()
.WithReference(cache);
```
In this example `WithNetworkCommands` would be an extension method that can apply to any resource, something like (Pseudo code):
```
public static IResourceBuilder WithDatabaseCommands(this IResourceBuilder builder) where T : IResource
{
builder.WithCommand(
name: "cut-network",
displayName: "Cut Network",
executeCommand: context => OnCutNetworkCommandAsync(builder, context),
updateState: OnUpdateResourceState
);
return builder;
}
private static async Task OnCutNetworkCommandAsync(IResourceBuilder builder, ExecuteCommandContext context)
{
var networkInterfaces= await builder.Resource.GetNetworkInterfaces();
networkInterfaces.Clear();
return CommandResults.Success();
}
```
For testing:
```
var appHost = await DistributedApplicationTestingBuilder.CreateAsync();
var app = await appHost.BuildAsync();
// to test what happens if a resource is started without network:
appHost .Resources.OfType().ConfigureNetworks(config => { config.NetworkInterfaces.Clear();})
await app.StartAsync();
// to test what happens if network is lost while running:
appHost .Resources.OfType().ConfigureNetworks(config => { config.NetworkInterfaces.Clear();})
```
### Additional context
- Should work with normal builder & testing builder (`IDistributedApplicationBuilder` and `IDistributedApplicationTestingBuilder`)
- Should work with removing specific network access, like removing single interface or all interfaces, to simulate intranet vs internet access
- Should work for removing network access at different points in resource lifetime, like before startup, or during resource execution
- Should be designed so that it exposes all network interfaces, so that in the future, things like network throttling and lossy networks can be simulated
Contributor guide
Research direction
Start by reading the IDistributedApplicationBuilder and IDistributedApplicationTestingBuilder APIs, then inspect the resource networking configuration and command examples described in the issue. Determine how network interfaces can be exposed and changed before startup and during execution. Done means both builders support targeted interface removal for simulating lost network access.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- networking, testing-qa
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100