-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWritableAmazonFileSystemTests.cs
More file actions
244 lines (186 loc) · 7.06 KB
/
WritableAmazonFileSystemTests.cs
File metadata and controls
244 lines (186 loc) · 7.06 KB
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
using System.Reflection;
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Ramstack.FileSystem.Physical;
using Ramstack.FileSystem.Specification.Tests;
using Ramstack.FileSystem.Specification.Tests.Utilities;
namespace Ramstack.FileSystem.Amazon;
[TestFixture]
[Category("Cloud:Amazon")]
public class WritableAmazonFileSystemTests : VirtualFileSystemSpecificationTests
{
private readonly HashSet<string> _list = [];
private readonly TempFileStorage _storage = new TempFileStorage();
[OneTimeSetUp]
public async Task Setup()
{
using var fs = GetFileSystem();
await fs.CreateBucketAsync();
foreach (var path in Directory.EnumerateFiles(_storage.Root, "*", SearchOption.AllDirectories))
{
await using var stream = File.OpenRead(path);
await fs.WriteAsync(path[_storage.Root.Length..], stream, overwrite: true);
}
}
[OneTimeTearDown]
public async Task Cleanup()
{
_storage.Dispose();
foreach (var name in _list.ToArray())
{
using var fs = CreateFileSystem(name);
try
{
await fs.DeleteDirectoryAsync("/");
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
[Test]
public async Task File_OpenWrite_InternalBufferWriteError_DoesNotCreateFile()
{
using var fs = GetFileSystem();
await using (var stream = await fs.OpenWriteAsync("/error.log"))
{
var underlying = (FileStream)stream.GetType().GetField("_stream", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(stream)!;
Assert.That(underlying, Is.Not.Null);
await stream.WriteAsync(new ReadOnlyMemory<byte>(new byte[1024]));
// Forces to upload buffer.
await stream.FlushAsync();
// Simulates an internal buffer write error.
await underlying.DisposeAsync();
try
{
await stream.WriteAsync(new ReadOnlyMemory<byte>(new byte[1024]));
}
catch
{
// Ignore
}
}
Assert.That(
await fs.FileExistsAsync("/error.log"),
Is.False);
}
[Test]
public async Task File_WriteHugeFile()
{
const int TotalLines = 500;
using var fs = GetFileSystem();
var value = new string('a', 100 * 1024);
{
await using var stream = await fs.OpenWriteAsync("/test.txt");
await using var writer = new StreamWriter(stream);
for (var i = 0; i < TotalLines; i++)
await writer.WriteLineAsync(value);
}
{
var file = fs.GetFile("/test.txt");
Assert.That(
await file.ExistsAsync(),
Is.True);
Assert.That(
await file.GetLengthAsync(),
Is.EqualTo(TotalLines * value.Length + TotalLines * Environment.NewLine.Length));
await using var stream = await file.OpenReadAsync();
using var reader = new StreamReader(stream);
var count = 0;
while (!reader.EndOfStream)
{
Assert.That(
await reader.ReadLineAsync(),
Is.EqualTo(value));
count++;
}
Assert.That(count, Is.EqualTo(TotalLines));
await file.DeleteAsync();
Assert.That(
await file.ExistsAsync(),
Is.False);
}
}
[Test]
public async Task File_CopyTo_File_DifferentFileSystems()
{
using var fs1 = new PhysicalFileSystem(_storage.Root);
using var fs2 = GetFileSystem();
var source = fs1.GetFile("/0000.txt");
var destination = fs2.GetFile("/0000.txt");
var content = Guid.NewGuid().ToString();
await using (var stream = await source.OpenWriteAsync())
await using (var writer = new StreamWriter(stream))
await writer.WriteAsync(content);
Assert.That(await destination.ExistsAsync(), Is.False);
await source.CopyToAsync(destination);
Assert.That(await destination.ExistsAsync(), Is.True);
using var reader = await destination.OpenTextAsync();
Assert.That(
await reader.ReadToEndAsync(),
Is.EqualTo(content));
await source.DeleteAsync();
await destination.DeleteAsync();
}
[Test]
public async Task File_CopyTo_File_DifferentStorages()
{
using var fs1 = CreateFileSystem("temp-storage");
using var fs2 = GetFileSystem();
await fs1.CreateBucketAsync();
var source = fs1.GetFile("/1111.txt");
var destination = fs2.GetFile("/1111.txt");
var content = Guid.NewGuid().ToString();
await using (var stream = await source.OpenWriteAsync())
await using (var writer = new StreamWriter(stream))
await writer.WriteAsync(content);
Assert.That(await destination.ExistsAsync(), Is.False);
await source.CopyToAsync(destination);
Assert.That(await destination.ExistsAsync(), Is.True);
using var reader = await destination.OpenTextAsync();
Assert.That(
await reader.ReadToEndAsync(),
Is.EqualTo(content));
await source.DeleteAsync();
await destination.DeleteAsync();
}
[Test]
public async Task Directory_BatchDeleting()
{
// 1. The maximum list page size is 1000 items.
// 2. The delete batch supports a maximum of 1000 keys.
//
// We'll set the count slightly higher to test pagination
// and batching functionality.
const int Count = 1100;
using var fs = GetFileSystem();
for (var i = 0; i < Count; i++)
await fs.WriteAsync($"/temp/{i:0000}", Stream.Null);
Assert.That(
await fs.GetFilesAsync("/temp").CountAsync(),
Is.EqualTo(Count));
await fs.DeleteDirectoryAsync("/temp");
Assert.That(
await fs.GetFilesAsync("/temp").AnyAsync(),
Is.False);
}
protected override AmazonS3FileSystem GetFileSystem() =>
CreateFileSystem("storage");
protected override DirectoryInfo GetDirectoryInfo() =>
new DirectoryInfo(_storage.Root);
private AmazonS3FileSystem CreateFileSystem(string storageName)
{
_list.Add(storageName);
return new AmazonS3FileSystem(
new BasicAWSCredentials("rustfsadmin", "rustfsadmin"),
new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.USEast1,
ServiceURL = "http://localhost:9000",
ForcePathStyle = true
},
bucketName: storageName);
}
}