forked from dotnet/Docker.DotNet
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathISystemOperations.Tests.cs
278 lines (226 loc) · 10.3 KB
/
ISystemOperations.Tests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Docker.DotNet.Models;
using Newtonsoft.Json;
using Xunit;
using Xunit.Abstractions;
namespace Docker.DotNet.Tests
{
[Collection(nameof(TestCollection))]
public class ISystemOperationsTests
{
private readonly CancellationTokenSource _cts;
private readonly TestOutput _output;
private readonly string _repositoryName;
private readonly string _tag;
private readonly DockerClient _dockerClient;
public ISystemOperationsTests(TestFixture testFixture, ITestOutputHelper outputHelper)
{
_output = new TestOutput(outputHelper);
_dockerClient = testFixture.DockerClient;
// Do not wait forever in case it gets stuck
_cts = CancellationTokenSource.CreateLinkedTokenSource(testFixture.Cts.Token);
_cts.CancelAfter(TimeSpan.FromMinutes(5));
_cts.Token.Register(() => throw new TimeoutException("SystemOperationsTests timeout"));
_repositoryName = testFixture.Repository;
_tag = testFixture.Tag;
}
[Fact]
public void Docker_IsRunning()
{
var dockerProcess = Process.GetProcesses().FirstOrDefault(process => process.ProcessName.Equals("docker", StringComparison.InvariantCultureIgnoreCase) || process.ProcessName.Equals("dockerd", StringComparison.InvariantCultureIgnoreCase));
Assert.NotNull(dockerProcess); // docker is not running
}
[Fact]
public async Task GetSystemInfoAsync_Succeeds()
{
var info = await _dockerClient.System.GetSystemInfoAsync();
Assert.NotNull(info.Architecture);
}
[Fact]
public async Task GetVersionAsync_Succeeds()
{
var version = await _dockerClient.System.GetVersionAsync();
Assert.NotNull(version.APIVersion);
}
[Fact]
public async Task MonitorEventsAsync_EmptyContainersList_CanBeCancelled()
{
var progress = new Progress<Message>();
using var cts = new CancellationTokenSource();
await cts.CancelAsync();
await Task.Delay(1);
await Assert.ThrowsAsync<TaskCanceledException>(() => _dockerClient.System.MonitorEventsAsync(new ContainerEventsParameters(), progress, cts.Token));
}
[Fact]
public async Task MonitorEventsAsync_NullParameters_Throws()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => _dockerClient.System.MonitorEventsAsync(null, null));
}
[Fact]
public async Task MonitorEventsAsync_NullProgress_Throws()
{
await Assert.ThrowsAsync<ArgumentNullException>(() => _dockerClient.System.MonitorEventsAsync(new ContainerEventsParameters(), null));
}
[Fact]
public async Task MonitorEventsAsync_Succeeds()
{
var newTag = $"MonitorTests-{Guid.NewGuid().ToString().Substring(1, 10)}";
var wasProgressCalled = false;
var progressMessage = new Progress<Message>((m) =>
{
_output.WriteLine($"MonitorEventsAsync_Succeeds: Message - {m.Action} - {m.Status} {m.From} - {m.Type}");
wasProgressCalled = true;
Assert.NotNull(m);
});
using var cts = CancellationTokenSource.CreateLinkedTokenSource(_cts.Token);
var task = _dockerClient.System.MonitorEventsAsync(
new ContainerEventsParameters(),
progressMessage,
cts.Token);
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));
await cts.CancelAsync();
await Assert.ThrowsAsync<OperationCanceledException>(() => task);
Assert.True(wasProgressCalled);
}
[Fact]
public async Task MonitorEventsAsync_IsCancelled_NoStreamCorruption()
{
var rand = new Random();
var sw = new Stopwatch();
for (int i = 0; i < 20; ++i)
{
try
{
// (1) Create monitor task
using var cts = CancellationTokenSource.CreateLinkedTokenSource(_cts.Token);
string newImageTag = Guid.NewGuid().ToString();
var monitorTask = _dockerClient.System.MonitorEventsAsync(
new ContainerEventsParameters(),
new Progress<Message>((value) => _output.WriteLine($"DockerSystemEvent: {JsonConvert.SerializeObject(value)}")),
cts.Token);
// (2) Wait for some time to make sure we get into blocking IO call
await Task.Delay(100, CancellationToken.None);
// (3) Invoke another request that will attempt to grab the same buffer
var listImagesTask1 = _dockerClient.Images.TagImageAsync(
$"{_repositoryName}:{_tag}",
new ImageTagParameters
{
RepositoryName = _repositoryName,
Tag = newImageTag,
}, CancellationToken.None);
// (4) Wait for a short bit again and cancel the monitor task - if we get lucky, we the list images call will grab the same buffer while
sw.Restart();
var iterations = rand.Next(15000000);
for (var j = 0; j < iterations; j++)
{
// noop
}
_output.WriteLine($"Waited for {sw.Elapsed.TotalMilliseconds} ms");
await cts.CancelAsync();
await listImagesTask1;
await _dockerClient.Images.TagImageAsync(
$"{_repositoryName}:{_tag}",
new ImageTagParameters
{
RepositoryName = _repositoryName,
Tag = newImageTag,
}, CancellationToken.None);
await monitorTask;
}
catch (OperationCanceledException)
{
// Exceptions other than this causes test to fail
}
}
}
[Fact]
public async Task MonitorEventsFiltered_Succeeds()
{
string newTag = $"MonitorTests-{Guid.NewGuid().ToString().Substring(1, 10)}";
string newImageRespositoryName = Guid.NewGuid().ToString();
await _dockerClient.Images.TagImageAsync(
$"{_repositoryName}:{_tag}",
new ImageTagParameters
{
RepositoryName = newImageRespositoryName,
Tag = newTag
},
_cts.Token
);
ImageInspectResponse image = await _dockerClient.Images.InspectImageAsync(
$"{newImageRespositoryName}:{newTag}",
_cts.Token
);
var progressCalledCounter = 0;
var eventsParams = new ContainerEventsParameters()
{
Filters = new Dictionary<string, IDictionary<string, bool>>()
{
{
"event", new Dictionary<string, bool>()
{
{
"tag", true
},
{
"untag", true
}
}
},
{
"type", new Dictionary<string, bool>()
{
{
"image", true
}
}
},
{
"image", new Dictionary<string, bool>()
{
{
image.ID, true
}
}
}
}
};
var progress = new Progress<Message>((m) =>
{
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.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 = $"{_repositoryName}:{_tag}" });
await _dockerClient.Containers.RemoveContainerAsync(createContainerResponse.ID, new ContainerRemoveParameters(), cts.Token);
await Task.Delay(TimeSpan.FromSeconds(1));
await cts.CancelAsync();
await Assert.ThrowsAsync<OperationCanceledException>(() => task);
Assert.Equal(2, progressCalledCounter);
Assert.True(task.IsCanceled);
}
[Fact]
public async Task PingAsync_Succeeds()
{
await _dockerClient.System.PingAsync();
}
}
}