-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathTarOutputMemoryStreamTest.cs
229 lines (185 loc) · 7.98 KB
/
TarOutputMemoryStreamTest.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
namespace Testcontainers.Tests;
public abstract class TarOutputMemoryStreamTest
{
private const string TargetDirectoryPath = "/tmp";
private readonly TarOutputMemoryStream _tarOutputMemoryStream = new TarOutputMemoryStream(TargetDirectoryPath, NullLogger.Instance);
private readonly FileInfo _testFile = new FileInfo(Path.Combine(TestSession.TempDirectoryPath, Path.GetRandomFileName()));
protected TarOutputMemoryStreamTest()
{
using var fileStream = _testFile.Open(FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
fileStream.WriteByte(13);
}
[Fact]
public void TestFileExistsInTarFile()
{
// Given
var actual = new List<string>();
_tarOutputMemoryStream.Close();
_tarOutputMemoryStream.Seek(0, SeekOrigin.Begin);
// When
using var tarIn = TarArchive.CreateInputTarArchive(_tarOutputMemoryStream, Encoding.Default);
tarIn.ProgressMessageEvent += (_, entry, _) => actual.Add(entry.Name);
tarIn.ListContents();
// Then
Assert.Contains(actual, file => file.EndsWith(_testFile.Name));
}
[UsedImplicitly]
public sealed class FromResourceMapping : TarOutputMemoryStreamTest, IResourceMapping, IClassFixture<FromResourceMapping.HttpFixture>, IAsyncLifetime, IDisposable
{
private readonly string _testHttpUri;
private readonly string _testFileUri;
public FromResourceMapping(FromResourceMapping.HttpFixture httpFixture)
{
_testHttpUri = httpFixture.BaseAddress;
_testFileUri = new Uri(_testFile.FullName).ToString();
}
public MountType Type
=> MountType.Bind;
public AccessMode AccessMode
=> AccessMode.ReadOnly;
public string Source
=> string.Empty;
public string Target
=> string.Join("/", TargetDirectoryPath, _testFile.Name);
public UnixFileModes FileMode
=> Unix.FileMode644;
public Task InitializeAsync()
{
return _tarOutputMemoryStream.AddAsync(this);
}
public Task DisposeAsync()
{
return Task.CompletedTask;
}
public void Dispose()
{
_tarOutputMemoryStream.Dispose();
_testFile.Delete();
}
Task IFutureResource.CreateAsync(CancellationToken ct)
{
return Task.CompletedTask;
}
Task IFutureResource.DeleteAsync(CancellationToken ct)
{
return Task.CompletedTask;
}
public Task<byte[]> GetAllBytesAsync(CancellationToken ct = default)
{
return File.ReadAllBytesAsync(_testFile.FullName, ct);
}
[Fact]
public async Task TestFileExistsInContainer()
{
// Given
var targetFilePath1 = string.Join("/", string.Empty, "tmp", Guid.NewGuid(), _testFile.Name);
var targetFilePath2 = string.Join("/", string.Empty, "tmp", Guid.NewGuid(), _testFile.Name);
var targetFilePath3 = string.Join("/", string.Empty, "tmp", Guid.NewGuid(), _testFile.Name);
var targetDirectoryPath1 = string.Join("/", string.Empty, "tmp", Guid.NewGuid());
var targetDirectoryPath2 = string.Join("/", string.Empty, "tmp", Guid.NewGuid());
var targetDirectoryPath3 = string.Join("/", string.Empty, "tmp", Guid.NewGuid());
var targetDirectoryPath4 = string.Join("/", string.Empty, "tmp", Guid.NewGuid());
var targetDirectoryPath5 = string.Join("/", string.Empty, "tmp", Guid.NewGuid());
var targetFilePaths = new List<string>();
targetFilePaths.Add(targetFilePath1);
targetFilePaths.Add(targetFilePath2);
targetFilePaths.Add(targetFilePath3);
targetFilePaths.Add(string.Join("/", targetDirectoryPath1, _testFile.Name));
targetFilePaths.Add(string.Join("/", targetDirectoryPath2, _testFile.Name));
targetFilePaths.Add(string.Join("/", targetDirectoryPath3, _testFile.Name));
targetFilePaths.Add(string.Join("/", targetDirectoryPath4, _testFile.Name));
targetFilePaths.Add(string.Join("/", targetDirectoryPath5, _testFile.Name));
await using var container = new ContainerBuilder()
.WithImage(CommonImages.Alpine)
.WithEntrypoint(CommonCommands.SleepInfinity)
.WithResourceMapping(_testFile, new FileInfo(targetFilePath1))
.WithResourceMapping(_testFile.FullName, targetDirectoryPath1)
.WithResourceMapping(_testFile.Directory.FullName, targetDirectoryPath2)
.WithResourceMapping(_testHttpUri, targetFilePath2)
.WithResourceMapping(_testFileUri, targetDirectoryPath3)
.Build();
// When
var fileContent = await GetAllBytesAsync()
.ConfigureAwait(true);
await container.StartAsync()
.ConfigureAwait(true);
await container.CopyAsync(fileContent, targetFilePath3)
.ConfigureAwait(true);
await container.CopyAsync(_testFile.FullName, targetDirectoryPath4)
.ConfigureAwait(true);
await container.CopyAsync(_testFile.Directory.FullName, targetDirectoryPath5)
.ConfigureAwait(true);
// Then
var execResults = await Task.WhenAll(targetFilePaths.Select(containerFilePath => container.ExecAsync(new[] { "test", "-f", containerFilePath })))
.ConfigureAwait(true);
Assert.All(execResults, result => Assert.Equal(0, result.ExitCode));
}
public sealed class HttpFixture : IAsyncLifetime
{
private const ushort HttpPort = 80;
private readonly IContainer _container = new ContainerBuilder()
.WithImage(CommonImages.Alpine)
.WithEntrypoint("/bin/sh", "-c")
.WithCommand($"while true; do echo \"HTTP/1.1 200 OK\r\n\" | nc -l -p {HttpPort}; done")
.WithPortBinding(HttpPort, true)
.Build();
public string BaseAddress
=> new UriBuilder(Uri.UriSchemeHttp, _container.Hostname, _container.GetMappedPublicPort(HttpPort)).ToString();
public Task InitializeAsync()
{
return _container.StartAsync();
}
public Task DisposeAsync()
{
return _container.DisposeAsync().AsTask();
}
}
}
[UsedImplicitly]
public sealed class FromFile : TarOutputMemoryStreamTest, IAsyncLifetime, IDisposable
{
public Task InitializeAsync()
{
return _tarOutputMemoryStream.AddAsync(_testFile, Unix.FileMode644);
}
public Task DisposeAsync()
{
return Task.CompletedTask;
}
public void Dispose()
{
_tarOutputMemoryStream.Dispose();
_testFile.Delete();
}
}
[UsedImplicitly]
public sealed class FromDirectory : TarOutputMemoryStreamTest, IAsyncLifetime, IDisposable
{
public Task InitializeAsync()
{
return _tarOutputMemoryStream.AddAsync(_testFile.Directory, true, Unix.FileMode644);
}
public Task DisposeAsync()
{
return Task.CompletedTask;
}
public void Dispose()
{
_tarOutputMemoryStream.Dispose();
_testFile.Delete();
}
}
public sealed class UnixFileModeTest
{
[Theory]
[InlineData(Unix.FileMode644, "644")]
[InlineData(Unix.FileMode666, "666")]
[InlineData(Unix.FileMode700, "700")]
[InlineData(Unix.FileMode755, "755")]
[InlineData(Unix.FileMode777, "777")]
public void UnixFileModeResolvesToPosixFilePermission(UnixFileModes fileMode, string posixFilePermission)
{
Assert.Equal(Convert.ToInt32(posixFilePermission, 8), Convert.ToInt32(fileMode));
}
}
}