Skip to content

Commit

Permalink
Fix #518, #522 (#586)
Browse files Browse the repository at this point in the history
* fix(#518): Dispose HttpClient

* fix(#522): Remove extra properties from ImageBuildParameters

* fix: Disable CreateContainerAsync_TimeoutExpires_Fails (not deterministic), await containerLogsTask at the end of the GetContainerLogs_* tests
  • Loading branch information
HofmeisterAn authored Sep 18, 2022
1 parent 13ad3a1 commit 27cef85
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 133 deletions.
1 change: 1 addition & 0 deletions src/Docker.DotNet/DockerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ await sock.ConnectAsync(new Microsoft.Net.Http.Client.UnixDomainSocketEndPoint(p
public void Dispose()
{
Configuration.Dispose();
_client.Dispose();
}

internal Task<DockerApiResponse> MakeRequestAsync(
Expand Down
21 changes: 0 additions & 21 deletions src/Docker.DotNet/Models/ImageBuildParameters.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,12 @@ public class ImageBuildParameters // (main.ImageBuildParameters)
[QueryStringParameter("forcerm", false, typeof(BoolQueryStringConverter))]
public bool? ForceRemove { get; set; }

[QueryStringParameter("pullparent", false, typeof(BoolQueryStringConverter))]
public bool? PullParent { get; set; }

[QueryStringParameter("pull", false)]
public string Pull { get; set; }

[QueryStringParameter("isolation", false)]
public string Isolation { get; set; }

[QueryStringParameter("cpusetcpus", false)]
public string CPUSetCPUs { get; set; }

[QueryStringParameter("cpusetmems", false)]
public string CPUSetMems { get; set; }

[QueryStringParameter("cpushares", false)]
public long? CPUShares { get; set; }

Expand All @@ -54,9 +45,6 @@ public class ImageBuildParameters // (main.ImageBuildParameters)
[QueryStringParameter("memswap", false)]
public long? MemorySwap { get; set; }

[QueryStringParameter("cgroupparent", false)]
public string CgroupParent { get; set; }

[QueryStringParameter("networkmode", false)]
public string NetworkMode { get; set; }

Expand All @@ -66,9 +54,6 @@ public class ImageBuildParameters // (main.ImageBuildParameters)
[QueryStringParameter("dockerfile", false)]
public string Dockerfile { get; set; }

[QueryStringParameter("ulimits", false, typeof(EnumerableQueryStringConverter))]
public IList<Ulimit> Ulimits { get; set; }

[QueryStringParameter("buildargs", false, typeof(MapQueryStringConverter))]
public IDictionary<string, string> BuildArgs { get; set; }

Expand All @@ -81,18 +66,12 @@ public class ImageBuildParameters // (main.ImageBuildParameters)
[QueryStringParameter("cachefrom", false, typeof(EnumerableQueryStringConverter))]
public IList<string> CacheFrom { get; set; }

[QueryStringParameter("securityopt", false, typeof(EnumerableQueryStringConverter))]
public IList<string> SecurityOpt { get; set; }

[QueryStringParameter("extrahosts", false, typeof(EnumerableQueryStringConverter))]
public IList<string> ExtraHosts { get; set; }

[QueryStringParameter("target", false)]
public string Target { get; set; }

[QueryStringParameter("session", false)]
public string SessionID { get; set; }

[QueryStringParameter("platform", false)]
public string Platform { get; set; }

Expand Down
153 changes: 49 additions & 104 deletions test/Docker.DotNet.Tests/IContainerOperationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task CreateContainerAsync_CreatesContainer()
}

// Timeout causing task to be cancelled
[Theory]
[Theory(Skip = "There is nothing we can do to delay CreateContainerAsync (aka HttpClient.SendAsync) deterministic. We cannot control if it responses successful before the timeout.")]
[InlineData(1)]
[InlineData(5)]
[InlineData(10)]
Expand Down Expand Up @@ -87,10 +87,9 @@ public async Task CreateContainerAsync_TimeoutExpires_Fails(int millisecondsTime
}

[Fact]
public async Task GetContainerLogs_Follow_False_TaskIsCompleted()
public async Task GetContainerLogs_Tty_False_Follow_True_TaskIsCompleted()
{
using var containerLogsCts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
var logList = new List<string>();

var createContainerResponse = await _dockerClient.Containers.CreateContainerAsync(
new CreateContainerParameters()
Expand All @@ -108,7 +107,7 @@ await _dockerClient.Containers.StartContainerAsync(
_cts.Token
);

containerLogsCts.CancelAfter(TimeSpan.FromSeconds(20));
containerLogsCts.CancelAfter(TimeSpan.FromSeconds(5));

var containerLogsTask = _dockerClient.Containers.GetContainerLogsAsync(
createContainerResponse.ID,
Expand All @@ -120,7 +119,7 @@ await _dockerClient.Containers.StartContainerAsync(
Follow = true
},
containerLogsCts.Token,
new Progress<string>((m) => { _output.WriteLine(m); logList.Add(m); })
new Progress<string>(m => _output.WriteLine(m))
);

await _dockerClient.Containers.StopContainerAsync(
Expand All @@ -136,7 +135,7 @@ await _dockerClient.Containers.StopContainerAsync(
[Fact]
public async Task GetContainerLogs_Tty_False_Follow_False_ReadsLogs()
{
using var containerLogsCts = new CancellationTokenSource(TimeSpan.FromSeconds(50));
using var containerLogsCts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
var logList = new List<string>();

var createContainerResponse = await _dockerClient.Containers.CreateContainerAsync(
Expand All @@ -155,7 +154,9 @@ await _dockerClient.Containers.StartContainerAsync(
_cts.Token
);

await _dockerClient.Containers.GetContainerLogsAsync(
containerLogsCts.CancelAfter(TimeSpan.FromSeconds(5));

var containerLogsTask = _dockerClient.Containers.GetContainerLogsAsync(
createContainerResponse.ID,
new ContainerLogsParameters
{
Expand All @@ -165,33 +166,33 @@ await _dockerClient.Containers.GetContainerLogsAsync(
Follow = false
},
containerLogsCts.Token,
new Progress<string>((m) => { logList.Add(m); _output.WriteLine(m); })
new Progress<string>(m => { logList.Add(m); _output.WriteLine(m); })
);

await _dockerClient.Containers.StopContainerAsync(
createContainerResponse.ID,
new ContainerStopParameters(),
_cts.Token
);
);

await containerLogsTask;
_output.WriteLine($"Line count: {logList.Count}");

Assert.NotEmpty(logList);
}

[Fact]
public async Task GetContainerLogs_Tty_False_Follow_True_Requires_Task_To_Be_Cancelled()
public async Task GetContainerLogs_Tty_True_Follow_False_ReadsLogs()
{
using var containerLogsCts = CancellationTokenSource.CreateLinkedTokenSource(_cts.Token);

using var containerLogsCts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
var logList = new List<string>();

var createContainerResponse = await _dockerClient.Containers.CreateContainerAsync(
new CreateContainerParameters()
{
Image = _imageId,
Name = Guid.NewGuid().ToString(),
Tty = false
Tty = true
},
_cts.Token
);
Expand All @@ -204,33 +205,44 @@ await _dockerClient.Containers.StartContainerAsync(

containerLogsCts.CancelAfter(TimeSpan.FromSeconds(5));

// Will be cancelled after CancellationTokenSource interval, would run forever otherwise
await Assert.ThrowsAsync<TaskCanceledException>(() => _dockerClient.Containers.GetContainerLogsAsync(
var containerLogsTask = _dockerClient.Containers.GetContainerLogsAsync(
createContainerResponse.ID,
new ContainerLogsParameters
{
ShowStderr = true,
ShowStdout = true,
Timestamps = true,
Follow = true
Follow = false
},
containerLogsCts.Token,
new Progress<string>((m) => { _output.WriteLine(JsonConvert.SerializeObject(m)); logList.Add(m); })
));
new Progress<string>(m => { _output.WriteLine(m); logList.Add(m); })
);

await Task.Delay(TimeSpan.FromSeconds(5));

await _dockerClient.Containers.StopContainerAsync(
createContainerResponse.ID,
new ContainerStopParameters(),
_cts.Token
);

await containerLogsTask;
_output.WriteLine($"Line count: {logList.Count}");

Assert.NotEmpty(logList);
}

[Fact]
public async Task GetContainerLogs_Tty_True_Follow_True_Requires_Task_To_Be_Cancelled()
public async Task GetContainerLogs_Tty_False_Follow_True_Requires_Task_To_Be_Cancelled()
{
using var containerLogsCts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
var logList = new List<string>();

var createContainerResponse = await _dockerClient.Containers.CreateContainerAsync(
new CreateContainerParameters()
{
Image = _imageId,
Name = Guid.NewGuid().ToString(),
Tty = true
Tty = false
},
_cts.Token
);
Expand All @@ -241,9 +253,9 @@ await _dockerClient.Containers.StartContainerAsync(
_cts.Token
);

containerLogsCts.CancelAfter(TimeSpan.FromSeconds(10));
containerLogsCts.CancelAfter(TimeSpan.FromSeconds(5));

var containerLogsTask = _dockerClient.Containers.GetContainerLogsAsync(
await Assert.ThrowsAsync<TaskCanceledException>(() => _dockerClient.Containers.GetContainerLogsAsync(
createContainerResponse.ID,
new ContainerLogsParameters
{
Expand All @@ -253,17 +265,14 @@ await _dockerClient.Containers.StartContainerAsync(
Follow = true
},
containerLogsCts.Token,
new Progress<string>((m) => { _output.WriteLine(m); logList.Add(m); })
);

await Assert.ThrowsAsync<TaskCanceledException>(() => containerLogsTask);
new Progress<string>(m => _output.WriteLine(m))
));
}

[Fact]
public async Task GetContainerLogs_Tty_True_Follow_True_StreamLogs_TaskIsCancelled()
public async Task GetContainerLogs_Tty_True_Follow_True_Requires_Task_To_Be_Cancelled()
{
using var containerLogsCts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
var logList = new List<string>();

var createContainerResponse = await _dockerClient.Containers.CreateContainerAsync(
new CreateContainerParameters()
Expand Down Expand Up @@ -293,53 +302,14 @@ await _dockerClient.Containers.StartContainerAsync(
Follow = true
},
containerLogsCts.Token,
new Progress<string>((m) => { _output.WriteLine(m); logList.Add(m); })
);

await Task.Delay(TimeSpan.FromSeconds(10));

await _dockerClient.Containers.StopContainerAsync(
createContainerResponse.ID,
new ContainerStopParameters
{
WaitBeforeKillSeconds = 0
},
_cts.Token
);

await _dockerClient.Containers.RemoveContainerAsync(
createContainerResponse.ID,
new ContainerRemoveParameters
{
Force = true
},
_cts.Token
new Progress<string>(m => _output.WriteLine(m))
);

await Assert.ThrowsAsync<TaskCanceledException>(() => containerLogsTask);

_output.WriteLine(JsonConvert.SerializeObject(new
{
AsyncState = containerLogsTask.AsyncState,
CreationOptions = containerLogsTask.CreationOptions,
Exception = containerLogsTask.Exception,
Id = containerLogsTask.Id,
IsCanceled = containerLogsTask.IsCanceled,
IsCompleted = containerLogsTask.IsCompleted,
IsCompletedSuccessfully = containerLogsTask.IsCompletedSuccessfully,
Status = containerLogsTask.Status
}
));

_output.WriteLine($"Line count: {logList.Count}");

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

Assert.NotEmpty(logList);
}

[Fact]
public async Task GetContainerLogs_Tty_True_ReadsLogs()
public async Task GetContainerLogs_Tty_True_Follow_True_ReadsLogs_TaskIsCancelled()
{
using var containerLogsCts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
var logList = new List<string>();
Expand Down Expand Up @@ -369,47 +339,22 @@ await _dockerClient.Containers.StartContainerAsync(
ShowStderr = true,
ShowStdout = true,
Timestamps = true,
Follow = false
Follow = true
},
containerLogsCts.Token,
new Progress<string>((m) => { _output.WriteLine(m); logList.Add(m); })
new Progress<string>(m => { _output.WriteLine(m); logList.Add(m); })
);

await Task.Delay(TimeSpan.FromSeconds(10));
await Task.Delay(TimeSpan.FromSeconds(5));

await _dockerClient.Containers.StopContainerAsync(
createContainerResponse.ID,
new ContainerStopParameters
{
WaitBeforeKillSeconds = 0
},
_cts.Token
);

await _dockerClient.Containers.RemoveContainerAsync(
createContainerResponse.ID,
new ContainerRemoveParameters
{
Force = true
},
new ContainerStopParameters(),
_cts.Token
);

await containerLogsTask;

_output.WriteLine(JsonConvert.SerializeObject(new
{
AsyncState = containerLogsTask.AsyncState,
CreationOptions = containerLogsTask.CreationOptions,
Exception = containerLogsTask.Exception,
Id = containerLogsTask.Id,
IsCanceled = containerLogsTask.IsCanceled,
IsCompleted = containerLogsTask.IsCompleted,
IsCompletedSuccessfully = containerLogsTask.IsCompletedSuccessfully,
Status = containerLogsTask.Status
}
));

await Assert.ThrowsAsync<TaskCanceledException>(() => containerLogsTask);
_output.WriteLine($"Line count: {logList.Count}");

Assert.NotEmpty(logList);
Expand Down Expand Up @@ -445,7 +390,7 @@ await _dockerClient.Containers.GetContainerStatsAsync(
{
Stream = false
},
new Progress<ContainerStatsResponse>((m) => { _output.WriteLine(m.ID); containerStatsList.Add(m); }),
new Progress<ContainerStatsResponse>(m => { _output.WriteLine(m.ID); containerStatsList.Add(m); }),
tcs.Token
);

Expand Down Expand Up @@ -492,7 +437,7 @@ await _dockerClient.Containers.GetContainerStatsAsync(
{
Stream = true
},
new Progress<ContainerStatsResponse>((m) => { containerStatsList.Add(m); _output.WriteLine(JsonConvert.SerializeObject(m)); }),
new Progress<ContainerStatsResponse>(m => { containerStatsList.Add(m); _output.WriteLine(JsonConvert.SerializeObject(m)); }),
linkedCts.Token
);
}
Expand Down Expand Up @@ -536,7 +481,7 @@ await _dockerClient.Containers.GetContainerStatsAsync(
{
Stream = false
},
new Progress<ContainerStatsResponse>((m) => { _output.WriteLine(m.ID); containerStatsList.Add(m); }),
new Progress<ContainerStatsResponse>(m => { _output.WriteLine(m.ID); containerStatsList.Add(m); }),
tcs.Token
);

Expand Down Expand Up @@ -585,7 +530,7 @@ await _dockerClient.Containers.GetContainerStatsAsync(
{
Stream = true
},
new Progress<ContainerStatsResponse>((m) => { containerStatsList.Add(m); _output.WriteLine(JsonConvert.SerializeObject(m)); }),
new Progress<ContainerStatsResponse>(m => { containerStatsList.Add(m); _output.WriteLine(JsonConvert.SerializeObject(m)); }),
linkedTcs.Token
);
}
Expand Down
Loading

0 comments on commit 27cef85

Please sign in to comment.