Skip to content

Commit

Permalink
fix(#590): Do not pull non existing images in monitor event unit tests (
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn authored Sep 23, 2022
1 parent 4119439 commit 30181cc
Showing 1 changed file with 14 additions and 42 deletions.
56 changes: 14 additions & 42 deletions test/Docker.DotNet.Tests/ISystemOperations.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -88,16 +88,6 @@ public async Task MonitorEventsAsync_Succeeds()
{
var newTag = $"MonitorTests-{Guid.NewGuid().ToString().Substring(1, 10)}";

var progressJSONMessage = new Progress<JSONMessage>((m) =>
{
// Status could be 'Pulling from...'
Assert.NotNull(m);
_output.WriteLine($"MonitorEventsAsync_Succeeds: JSONMessage - {m.ID} - {m.Status} {m.From} - {m.Stream}");
});

const string repositoryHello = "hello-world"; // Well known image name.
const string tagLatest = "latest";

var wasProgressCalled = false;

var progressMessage = new Progress<Message>((m) =>
Expand All @@ -114,29 +104,15 @@ public async Task MonitorEventsAsync_Succeeds()
progressMessage,
cts.Token);

try
{
await _dockerClient.Images.CreateImageAsync(
new ImagesCreateParameters { FromImage = $"{repositoryHello}:{tagLatest}" }, null, progressJSONMessage,
_cts.Token);

await _dockerClient.Images.TagImageAsync($"{repositoryHello}:{tagLatest}",
new ImageTagParameters { RepositoryName = repositoryHello, Tag = newTag }, _cts.Token);

await _dockerClient.Images.DeleteImageAsync(
name: $"{repositoryHello}:{newTag}",
new ImageDeleteParameters
{
Force = true
},
_cts.Token);
}
catch
{
// Ignore
}
await _dockerClient.Images.TagImageAsync($"{_repositoryName}:{_tag}", new ImageTagParameters { RepositoryName = _repositoryName, Tag = newTag }, _cts.Token);

;
await _dockerClient.Images.DeleteImageAsync(
name: $"{_repositoryName}:{newTag}",
new ImageDeleteParameters
{
Force = true
},
_cts.Token);

// Give it some time for output operation to complete before cancelling task
await Task.Delay(TimeSpan.FromSeconds(1));
Expand Down Expand Up @@ -221,10 +197,8 @@ public async Task MonitorEventsFiltered_Succeeds()
string newTag = $"MonitorTests-{Guid.NewGuid().ToString().Substring(1, 10)}";
string newImageRespositoryName = Guid.NewGuid().ToString();

const string repositoryHello = "hello-world"; // Well known image name.
const string tagLatest = "latest";
await _dockerClient.Images.TagImageAsync(
$"{repositoryHello}:{tagLatest}",
$"{_repositoryName}:{_tag}",
new ImageTagParameters
{
RepositoryName = newImageRespositoryName,
Expand Down Expand Up @@ -276,20 +250,18 @@ await _dockerClient.Images.TagImageAsync(

var progress = new Progress<Message>((m) =>
{
progressCalledCounter++;
Interlocked.Increment(ref progressCalledCounter);
Assert.True(m.Status == "tag" || m.Status == "untag");
_output.WriteLine($"MonitorEventsFiltered_Succeeds: Message received: {m.Action} - {m.Status} {m.From} - {m.Type}");
});

using var cts = CancellationTokenSource.CreateLinkedTokenSource(_cts.Token);
var task = Task.Run(() => _dockerClient.System.MonitorEventsAsync(eventsParams, progress, cts.Token));

await _dockerClient.Images.CreateImageAsync(new ImagesCreateParameters { FromImage = $"{repositoryHello}:{tagLatest}" }, null, new Progress<JSONMessage>());

await _dockerClient.Images.TagImageAsync($"{repositoryHello}:{tagLatest}", new ImageTagParameters { RepositoryName = repositoryHello, Tag = newTag });
await _dockerClient.Images.DeleteImageAsync($"{repositoryHello}:{newTag}", new ImageDeleteParameters());
await _dockerClient.Images.TagImageAsync($"{_repositoryName}:{_tag}", new ImageTagParameters { RepositoryName = _repositoryName, Tag = newTag });
await _dockerClient.Images.DeleteImageAsync($"{_repositoryName}:{newTag}", new ImageDeleteParameters());

var createContainerResponse = await _dockerClient.Containers.CreateContainerAsync(new CreateContainerParameters { Image = $"{repositoryHello}:{tagLatest}" });
var createContainerResponse = await _dockerClient.Containers.CreateContainerAsync(new CreateContainerParameters { Image = $"{_repositoryName}:{_tag}" });
await _dockerClient.Containers.RemoveContainerAsync(createContainerResponse.ID, new ContainerRemoveParameters(), cts.Token);

await Task.Delay(TimeSpan.FromSeconds(1));
Expand Down

0 comments on commit 30181cc

Please sign in to comment.