From 46957950368797d38506b5ef5c04c6a37db197fa Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Fri, 23 Sep 2022 17:55:19 -0400 Subject: [PATCH] fix(#590): Do not pull non existing images in monitor event unit tests --- .../ISystemOperations.Tests.cs | 56 +++++-------------- 1 file changed, 14 insertions(+), 42 deletions(-) diff --git a/test/Docker.DotNet.Tests/ISystemOperations.Tests.cs b/test/Docker.DotNet.Tests/ISystemOperations.Tests.cs index a7c22d24..61f805a4 100644 --- a/test/Docker.DotNet.Tests/ISystemOperations.Tests.cs +++ b/test/Docker.DotNet.Tests/ISystemOperations.Tests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -88,16 +88,6 @@ public async Task MonitorEventsAsync_Succeeds() { var newTag = $"MonitorTests-{Guid.NewGuid().ToString().Substring(1, 10)}"; - var progressJSONMessage = new Progress((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((m) => @@ -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)); @@ -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, @@ -276,7 +250,7 @@ await _dockerClient.Images.TagImageAsync( var progress = new Progress((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}"); }); @@ -284,12 +258,10 @@ await _dockerClient.Images.TagImageAsync( 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()); - - 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));