Skip to content

Commit

Permalink
[tests] DockerUtils.AttemptDeleteDockerVolume - allow throwing on fai…
Browse files Browse the repository at this point in the history
…lures (#5315)

.. failures. This is useful when expecting to create a fresh new volume,
and this would cause an exception to be thrown if a volume with the same
name could not be deleted.
  • Loading branch information
radical authored Aug 16, 2024
1 parent 8a865c3 commit b74f2c6
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
volumeName = VolumeNameGenerator.CreateVolumeName(elasticsearch1, nameof(WithDataShouldPersistStateBetweenUsages));

// if the volume already exists (because of a crashing previous run), try to delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName);
// if the volume already exists (because of a crashing previous run), delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName, throwOnFailure: true);
elasticsearch1.WithDataVolume(volumeName);
}
else
Expand Down
4 changes: 2 additions & 2 deletions tests/Aspire.Hosting.Kafka.Tests/KafkaFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
volumeName = VolumeNameGenerator.CreateVolumeName(kafka1, nameof(WithDataShouldPersistStateBetweenUsages));

// if the volume already exists (because of a crashing previous run), try to delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName);
// if the volume already exists (because of a crashing previous run), delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName, throwOnFailure: true);
kafka1.WithDataVolume(volumeName);
}
else
Expand Down
4 changes: 2 additions & 2 deletions tests/Aspire.Hosting.Milvus.Tests/MilvusFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
volumeName = VolumeNameGenerator.CreateVolumeName(milvus1, nameof(WithDataShouldPersistStateBetweenUsages));

// if the volume already exists (because of a crashing previous run), try to delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName);
// if the volume already exists (because of a crashing previous run), delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName, throwOnFailure: true);
milvus1.WithDataVolume(volumeName);
}
else
Expand Down
4 changes: 2 additions & 2 deletions tests/Aspire.Hosting.MongoDB.Tests/MongoDbFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
volumeName = VolumeNameGenerator.CreateVolumeName(mongodb1, nameof(WithDataShouldPersistStateBetweenUsages));

// if the volume already exists (because of a crashing previous run), try to delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName);
// if the volume already exists (because of a crashing previous run), delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName, throwOnFailure: true);
mongodb1.WithDataVolume(volumeName);
}
else
Expand Down
4 changes: 2 additions & 2 deletions tests/Aspire.Hosting.MySql.Tests/MySqlFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
volumeName = VolumeNameGenerator.CreateVolumeName(mysql1, nameof(WithDataShouldPersistStateBetweenUsages));

// If the volume already exists (because of a crashing previous run), try to delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName);
// if the volume already exists (because of a crashing previous run), delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName, throwOnFailure: true);
mysql1.WithDataVolume(volumeName);
}
else
Expand Down
4 changes: 2 additions & 2 deletions tests/Aspire.Hosting.Nats.Tests/NatsFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
volumeName = VolumeNameGenerator.CreateVolumeName(nats1, nameof(WithDataShouldPersistStateBetweenUsages));

// if the volume already exists (because of a crashing previous run), try to delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName);
// if the volume already exists (because of a crashing previous run), delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName, throwOnFailure: true);
nats1.WithDataVolume(volumeName);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
volumeName = VolumeNameGenerator.CreateVolumeName(postgres1, nameof(WithDataShouldPersistStateBetweenUsages));

// If the volume already exists (because of a crashing previous run), try to delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName);
// if the volume already exists (because of a crashing previous run), delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName, throwOnFailure: true);
postgres1.WithDataVolume(volumeName);
}
else
Expand Down
4 changes: 2 additions & 2 deletions tests/Aspire.Hosting.Qdrant.Tests/QdrantFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
volumeName = VolumeNameGenerator.CreateVolumeName(qdrant1, nameof(WithDataShouldPersistStateBetweenUsages));

// if the volume already exists (because of a crashing previous run), try to delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName);
// if the volume already exists (because of a crashing previous run), delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName, throwOnFailure: true);
qdrant1.WithDataVolume(volumeName);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
volumeName = VolumeNameGenerator.CreateVolumeName(rabbitMQ1, nameof(WithDataShouldPersistStateBetweenUsages));

// if the volume already exists (because of a crashing previous run), try to delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName);
// if the volume already exists (because of a crashing previous run), delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName, throwOnFailure: true);
rabbitMQ1.WithDataVolume(volumeName);
}
else
Expand Down
2 changes: 2 additions & 0 deletions tests/Aspire.Hosting.Redis.Tests/RedisFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public async Task WithDataVolumeShouldPersistStateBetweenUsages()
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
var volumeName = VolumeNameGenerator.CreateVolumeName(redis1, nameof(WithDataVolumeShouldPersistStateBetweenUsages));
redis1.WithDataVolume(volumeName);
// if the volume already exists (because of a crashing previous run), delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName, throwOnFailure: true);

using (var app = builder1.Build())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
volumeName = VolumeNameGenerator.CreateVolumeName(sqlserver1, nameof(WithDataShouldPersistStateBetweenUsages));

// If the volume already exists (because of a crashing previous run), try to delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName);
// if the volume already exists (because of a crashing previous run), delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName, throwOnFailure: true);
sqlserver1.WithDataVolume(volumeName);
}
else
Expand Down
25 changes: 24 additions & 1 deletion tests/Aspire.Hosting.Tests/Utils/DockerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Aspire.Hosting.Utils;

public sealed class DockerUtils
{
public static void AttemptDeleteDockerVolume(string volumeName)
public static void AttemptDeleteDockerVolume(string volumeName, bool throwOnFailure = false)
{
for (var i = 0; i < 3; i++)
{
Expand All @@ -29,5 +29,28 @@ public static void AttemptDeleteDockerVolume(string volumeName)
}
}
}

if (throwOnFailure)
{
if (Process.Start("docker", $"volume inspect {volumeName}") is { } process)
{
var exited = process.WaitForExit(TimeSpan.FromSeconds(3));
var exitCode = process.ExitCode;
process.Kill(entireProcessTree: true);
process.Dispose();
if (!exited)
{
throw new InvalidOperationException($"Failed to inspect the deleted volume named '{volumeName}', the inspect process did not exit.");
}
if (exitCode == 0)
{
throw new InvalidOperationException($"Failed to delete docker volume named '{volumeName}'. Attempted to inspect the volume and it still exists.");
}
}
else
{
throw new InvalidOperationException($"Failed to inspect the deleted volume named '{volumeName}', the inspect process did not start.");
}
}
}
}
4 changes: 2 additions & 2 deletions tests/Aspire.Hosting.Valkey.Tests/ValkeyFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
// Use a deterministic volume name to prevent them from exhausting the machines if deletion fails
volumeName = VolumeNameGenerator.CreateVolumeName(valkey1, nameof(WithDataShouldPersistStateBetweenUsages));

// if the volume already exists (because of a crashing previous run), try to delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName);
// if the volume already exists (because of a crashing previous run), delete it
DockerUtils.AttemptDeleteDockerVolume(volumeName, throwOnFailure: true);
valkey1.WithDataVolume(volumeName);
}
else
Expand Down

0 comments on commit b74f2c6

Please sign in to comment.