Skip to content
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

Improve Elasticsearch functional tests reliability #7014

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class ElasticsearchFunctionalTests(ITestOutputHelper testOutputHelper)

[Fact]
[RequiresDocker]
[ActiveIssue("https://github.com/dotnet/aspire/issues/5821")]
eerhardt marked this conversation as resolved.
Show resolved Hide resolved
public async Task VerifyElasticsearchResource()
{
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(10));
Expand All @@ -42,6 +41,10 @@ public async Task VerifyElasticsearchResource()

await app.StartAsync();

var rns = app.Services.GetRequiredService<ResourceNotificationService>();

await rns.WaitForResourceHealthyAsync(elasticsearch.Resource.Name, cts.Token);

var hb = Host.CreateApplicationBuilder();

hb.Configuration[$"ConnectionStrings:{elasticsearch.Resource.Name}"] = await elasticsearch.Resource.ConnectionStringExpression.GetValueAsync(default);
Expand Down Expand Up @@ -101,7 +104,11 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)

using (var app = builder1.Build())
{
await app.StartAsync();
await app.StartAsync(cts.Token);

var rns = app.Services.GetRequiredService<ResourceNotificationService>();

await rns.WaitForResourceHealthyAsync(elasticsearch1.Resource.Name, cts.Token);

try
{
Expand All @@ -121,6 +128,18 @@ await pipeline.ExecuteAsync(
var elasticsearchClient = host.Services.GetRequiredService<ElasticsearchClient>();
await CreateTestData(elasticsearchClient, testOutputHelper, token);
}, cts.Token);

await app.StopAsync();

// Wait for the container to be stopped and to release the volume files before continuing

await pipeline.ExecuteAsync(
async token =>
{
var elasticsearchClient = host.Services.GetRequiredService<ElasticsearchClient>();
var getResponse = await elasticsearchClient.GetAsync<Person>(IndexName, s_person.Id, token);
Assert.False(getResponse.IsSuccess());
}, cts.Token);
}
}
finally
Expand Down Expand Up @@ -148,6 +167,10 @@ await pipeline.ExecuteAsync(
{
await app.StartAsync();

var rns = app.Services.GetRequiredService<ResourceNotificationService>();

await rns.WaitForResourceHealthyAsync(elasticsearch2.Resource.Name, cts.Token);

try
{
var hb = Host.CreateApplicationBuilder();
Expand All @@ -171,6 +194,17 @@ await pipeline.ExecuteAsync(
Assert.Equal(s_person.Id, getResponse.Source?.Id);
}, cts.Token);

await app.StopAsync();

// Wait for the container to be stopped and to release the volume files before continuing

await pipeline.ExecuteAsync(
async token =>
{
var elasticsearchClient = host.Services.GetRequiredService<ElasticsearchClient>();
var getResponse = await elasticsearchClient.GetAsync<Person>(IndexName, s_person.Id, token);
Assert.False(getResponse.IsSuccess());
}, cts.Token);
}
}
finally
Expand Down Expand Up @@ -204,7 +238,6 @@ await pipeline.ExecuteAsync(

[Fact]
[RequiresDocker]
[ActiveIssue("https://github.com/dotnet/aspire/issues/5844")]
public async Task VerifyWaitForOnElasticsearchBlocksDependentResources()
{
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(10));
Expand All @@ -219,7 +252,7 @@ public async Task VerifyWaitForOnElasticsearchBlocksDependentResources()
var resource = builder.AddElasticsearch("resource")
.WithHealthCheck("blocking_check");

var dependentResource = builder.AddElasticsearch("dependentresource")
var dependentResource = builder.AddContainer("nginx", "mcr.microsoft.com/cbl-mariner/base/nginx", "1.22")
.WaitFor(resource);

using var app = builder.Build();
Expand All @@ -234,7 +267,7 @@ public async Task VerifyWaitForOnElasticsearchBlocksDependentResources()

healthCheckTcs.SetResult(HealthCheckResult.Healthy());

await rns.WaitForResourceAsync(resource.Resource.Name, (re => re.Snapshot.HealthStatus == Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Healthy), cts.Token);
await rns.WaitForResourceHealthyAsync(resource.Resource.Name, cts.Token);

await rns.WaitForResourceAsync(dependentResource.Resource.Name, KnownResourceStates.Running, cts.Token);

Expand Down
Loading