Skip to content

Commit

Permalink
refactor: Use async API (IAsyncLifetime) to create test fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Aug 18, 2022
1 parent 415228e commit 6a4ab2b
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 215 deletions.
23 changes: 13 additions & 10 deletions test/Docker.DotNet.Tests/IContainerOperationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,29 @@

namespace Docker.DotNet.Tests
{
[Collection("Test collection")]
[Collection(nameof(TestCollection))]
public class IContainerOperationsTests
{
private readonly CancellationTokenSource _cts;
private readonly DockerClient _dockerClient;
private readonly DockerClientConfiguration _dockerClientConfiguration;

private readonly TestOutput _output;
private readonly string _imageId;
private readonly Tests.TestOutput _output;
private readonly DockerClientConfiguration _dockerClientConfiguration;
private readonly DockerClient _dockerClient;

public IContainerOperationsTests(TestFixture testFixture, ITestOutputHelper outputHelper)
{
_output = new TestOutput(outputHelper);

_dockerClientConfiguration = testFixture.DockerClientConfiguration;
_dockerClient = _dockerClientConfiguration.CreateClient();

// Do not wait forever in case it gets stuck
_cts = CancellationTokenSource.CreateLinkedTokenSource(testFixture.cts.Token);
_cts = CancellationTokenSource.CreateLinkedTokenSource(testFixture.Cts.Token);
_cts.CancelAfter(TimeSpan.FromMinutes(5));
_cts.Token.Register(() => throw new TimeoutException("IContainerOperationsTest timeout"));
_cts.Token.Register(() => throw new TimeoutException("ContainerOperationsTests timeout"));

_dockerClient = testFixture.dockerClient;
_dockerClientConfiguration = testFixture.dockerClientConfiguration;
_output = new TestOutput(outputHelper);
_imageId = testFixture.imageId;
_imageId = testFixture.Image.ID;
}

[Fact]
Expand Down
20 changes: 9 additions & 11 deletions test/Docker.DotNet.Tests/IImageOperationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,29 @@

namespace Docker.DotNet.Tests
{
[Collection("Test collection")]
[Collection(nameof(TestCollection))]
public class IImageOperationsTests
{

private readonly CancellationTokenSource _cts;

private readonly TestOutput _output;
private readonly string _repositoryName;
private readonly string _tag = Guid.NewGuid().ToString();
private readonly DockerClientConfiguration _dockerConfiguration;
private readonly string _tag;
private readonly DockerClient _dockerClient;

public IImageOperationsTests(TestFixture testFixture, ITestOutputHelper _outputHelper)
public IImageOperationsTests(TestFixture testFixture, ITestOutputHelper outputHelper)
{
_output = new TestOutput(_outputHelper);
_output = new TestOutput(outputHelper);

_dockerConfiguration = new DockerClientConfiguration();
_dockerClient = _dockerConfiguration.CreateClient();
_dockerClient = testFixture.DockerClient;

// Do not wait forever in case it gets stuck
_cts = new CancellationTokenSource(TimeSpan.FromMinutes(5));
_cts = CancellationTokenSource.CreateLinkedTokenSource(testFixture.Cts.Token);
_cts.CancelAfter(TimeSpan.FromMinutes(5));
_cts.Token.Register(() => throw new TimeoutException("ImageOperationTests timeout"));

_repositoryName = testFixture.repositoryName;
_tag = testFixture.tag;
_repositoryName = testFixture.Repository;
_tag = testFixture.Tag;
}

[Fact]
Expand Down
52 changes: 26 additions & 26 deletions test/Docker.DotNet.Tests/ISwarmOperationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@

namespace Docker.DotNet.Tests
{
[Collection("Test collection")]
[Collection(nameof(TestCollection))]
public class ISwarmOperationsTests
{
private readonly DockerClient _client;
private readonly DockerClient _dockerClient;
private readonly string _imageId;

public ISwarmOperationsTests(TestFixture testFixture)
{
_client = testFixture.dockerClient;
_imageId = testFixture.imageId;
_dockerClient = testFixture.DockerClient;
_imageId = testFixture.Image.ID;
}

[Fact]
public async Task GetFilteredServicesByName_Succeeds()
{
var firstServiceName = $"service1-{Guid.NewGuid().ToString().Substring(1, 10)}";
var firstServiceId = _client.Swarm.CreateServiceAsync(new ServiceCreateParameters
var firstServiceId = _dockerClient.Swarm.CreateServiceAsync(new ServiceCreateParameters
{
Service = new ServiceSpec
{
Expand All @@ -32,7 +32,7 @@ public async Task GetFilteredServicesByName_Succeeds()
}
}).Result.ID;

var secondServiceId = _client.Swarm.CreateServiceAsync(new ServiceCreateParameters
var secondServiceId = _dockerClient.Swarm.CreateServiceAsync(new ServiceCreateParameters
{
Service = new ServiceSpec
{
Expand All @@ -41,7 +41,7 @@ public async Task GetFilteredServicesByName_Succeeds()
}
}).Result.ID;

var thirdServiceid = _client.Swarm.CreateServiceAsync(new ServiceCreateParameters
var thirdServiceid = _dockerClient.Swarm.CreateServiceAsync(new ServiceCreateParameters
{
Service = new ServiceSpec
{
Expand All @@ -50,7 +50,7 @@ public async Task GetFilteredServicesByName_Succeeds()
}
}).Result.ID;

var services = await _client.Swarm.ListServicesAsync(
var services = await _dockerClient.Swarm.ListServicesAsync(
new ServicesListParameters
{
Filters = new ServiceFilter
Expand All @@ -65,15 +65,15 @@ public async Task GetFilteredServicesByName_Succeeds()

Assert.Single(services);

await _client.Swarm.RemoveServiceAsync(firstServiceId, default);
await _client.Swarm.RemoveServiceAsync(secondServiceId, default);
await _client.Swarm.RemoveServiceAsync(thirdServiceid, default);
await _dockerClient.Swarm.RemoveServiceAsync(firstServiceId, default);
await _dockerClient.Swarm.RemoveServiceAsync(secondServiceId, default);
await _dockerClient.Swarm.RemoveServiceAsync(thirdServiceid, default);
}

[Fact]
public async Task GetFilteredServicesById_Succeeds()
{
var firstServiceId = _client.Swarm.CreateServiceAsync(new ServiceCreateParameters
var firstServiceId = _dockerClient.Swarm.CreateServiceAsync(new ServiceCreateParameters
{
Service = new ServiceSpec
{
Expand All @@ -82,7 +82,7 @@ public async Task GetFilteredServicesById_Succeeds()
}
}).Result.ID;

var secondServiceId = _client.Swarm.CreateServiceAsync(new ServiceCreateParameters
var secondServiceId = _dockerClient.Swarm.CreateServiceAsync(new ServiceCreateParameters
{
Service = new ServiceSpec
{
Expand All @@ -91,7 +91,7 @@ public async Task GetFilteredServicesById_Succeeds()
}
}).Result.ID;

var thirdServiceid = _client.Swarm.CreateServiceAsync(new ServiceCreateParameters
var thirdServiceid = _dockerClient.Swarm.CreateServiceAsync(new ServiceCreateParameters
{
Service = new ServiceSpec
{
Expand All @@ -100,20 +100,20 @@ public async Task GetFilteredServicesById_Succeeds()
}
}).Result.ID;

var services = await _client.Swarm.ListServicesAsync(new ServicesListParameters { Filters = new ServiceFilter { Id = new string[] { firstServiceId } } }, CancellationToken.None);
var services = await _dockerClient.Swarm.ListServicesAsync(new ServicesListParameters { Filters = new ServiceFilter { Id = new string[] { firstServiceId } } }, CancellationToken.None);
Assert.Single(services);

await _client.Swarm.RemoveServiceAsync(firstServiceId, default);
await _client.Swarm.RemoveServiceAsync(secondServiceId, default);
await _client.Swarm.RemoveServiceAsync(thirdServiceid, default);
await _dockerClient.Swarm.RemoveServiceAsync(firstServiceId, default);
await _dockerClient.Swarm.RemoveServiceAsync(secondServiceId, default);
await _dockerClient.Swarm.RemoveServiceAsync(thirdServiceid, default);
}

[Fact]
public async Task GetServices_Succeeds()
{
var initialServiceCount = _client.Swarm.ListServicesAsync(cancellationToken: CancellationToken.None).Result.Count();
var initialServiceCount = _dockerClient.Swarm.ListServicesAsync(cancellationToken: CancellationToken.None).Result.Count();

var firstServiceId = _client.Swarm.CreateServiceAsync(new ServiceCreateParameters
var firstServiceId = _dockerClient.Swarm.CreateServiceAsync(new ServiceCreateParameters
{
Service = new ServiceSpec
{
Expand All @@ -122,7 +122,7 @@ public async Task GetServices_Succeeds()
}
}).Result.ID;

var secondServiceId = _client.Swarm.CreateServiceAsync(new ServiceCreateParameters
var secondServiceId = _dockerClient.Swarm.CreateServiceAsync(new ServiceCreateParameters
{
Service = new ServiceSpec
{
Expand All @@ -131,7 +131,7 @@ public async Task GetServices_Succeeds()
}
}).Result.ID;

var thirdServiceid = _client.Swarm.CreateServiceAsync(new ServiceCreateParameters
var thirdServiceid = _dockerClient.Swarm.CreateServiceAsync(new ServiceCreateParameters
{
Service = new ServiceSpec
{
Expand All @@ -140,13 +140,13 @@ public async Task GetServices_Succeeds()
}
}).Result.ID;

var services = await _client.Swarm.ListServicesAsync(cancellationToken: CancellationToken.None);
var services = await _dockerClient.Swarm.ListServicesAsync(cancellationToken: CancellationToken.None);

Assert.True(services.Count() > initialServiceCount);

await _client.Swarm.RemoveServiceAsync(firstServiceId, default);
await _client.Swarm.RemoveServiceAsync(secondServiceId, default);
await _client.Swarm.RemoveServiceAsync(thirdServiceid, default);
await _dockerClient.Swarm.RemoveServiceAsync(firstServiceId, default);
await _dockerClient.Swarm.RemoveServiceAsync(secondServiceId, default);
await _dockerClient.Swarm.RemoveServiceAsync(thirdServiceid, default);
}
}
}
Loading

0 comments on commit 6a4ab2b

Please sign in to comment.