-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWritableGoogleFileSystemTests.cs
More file actions
212 lines (160 loc) · 6.22 KB
/
WritableGoogleFileSystemTests.cs
File metadata and controls
212 lines (160 loc) · 6.22 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
using System.Reflection;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Storage.V1;
using Ramstack.FileSystem.Physical;
using Ramstack.FileSystem.Specification.Tests;
using Ramstack.FileSystem.Specification.Tests.Utilities;
namespace Ramstack.FileSystem.Google;
[TestFixture]
[Category("Cloud:Google")]
[Ignore("This test requires a running Google Cloud Storage emulator")]
public class WritableGoogleFileSystemTests : VirtualFileSystemSpecificationTests
{
private readonly HashSet<string> _buckets = [];
private readonly TempFileStorage _storage = new TempFileStorage();
[OneTimeSetUp]
public async Task Setup()
{
using var fs = GetFileSystem();
await fs.CreateBucketAsync("ramstack-project");
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 bucket in _buckets.ToArray())
{
using var fs = CreateFileSystem(bucket);
try
{
await fs.DeleteDirectoryAsync("/");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
[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 byte[1024]);
// Simulates an internal buffer write error.
await underlying.DisposeAsync();
try
{
await stream.WriteAsync(new byte[1024]);
Assert.Fail();
}
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("ramstack-temp-bucket-1");
using var fs2 = CreateFileSystem("ramstack-temp-bucket-2");
await fs1.CreateBucketAsync("ramstack-project");
await fs2.CreateBucketAsync("ramstack-project");
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();
}
protected override GoogleFileSystem GetFileSystem() =>
CreateFileSystem("ramstack-test-bucket");
protected override DirectoryInfo GetDirectoryInfo() =>
new DirectoryInfo(_storage.Root);
private GoogleFileSystem CreateFileSystem(string bucket)
{
_buckets.Add(bucket);
var client = StorageClient.Create(
GoogleCredential.FromFile("credentials.json"));
return new GoogleFileSystem(client, bucketName: bucket);
}
}