Skip to content

Commit de6c007

Browse files
committed
Try to fix tests
1 parent 4a52771 commit de6c007

9 files changed

Lines changed: 109 additions & 67 deletions

File tree

src/Storage.Tests/BucketShould.cs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
1-
using FluentAssertions;
2-
using Storage.Tests.Mocks;
1+
using Storage.Tests.Utils;
32

43
namespace Storage.Tests;
54

6-
public sealed class BucketShould : IClassFixture<StorageFixture>
5+
public sealed class BucketShould(StorageFixture fixture) : IClassFixture<StorageFixture>
76
{
8-
private readonly CancellationToken _cancellation;
9-
private readonly StorageFixture _fixture;
10-
private readonly S3Client _client;
11-
12-
public BucketShould(StorageFixture fixture)
13-
{
14-
_cancellation = CancellationToken.None;
15-
_client = fixture.S3Client;
16-
_fixture = fixture;
17-
}
7+
private readonly CancellationToken _cancellation = CancellationToken.None;
8+
private readonly S3Client _client = fixture.S3Client;
189

1910
[Fact]
2011
public async Task CreateBucket()
2112
{
22-
var settings = _fixture.Settings;
13+
var settings = fixture.Settings;
2314

2415
// don't use using here
2516
var client =
2617
new S3Client(
2718
new S3Settings
2819
{
2920
AccessKey = settings.AccessKey,
30-
Bucket = _fixture.Create<string>(),
21+
Bucket = fixture.Create<string>(),
3122
EndPoint = settings.EndPoint,
3223
Port = settings.Port,
3324
SecretKey = settings.SecretKey,
3425
UseHttps = settings.UseHttps,
3526
},
36-
_fixture.HttpClient);
27+
fixture.HttpClient);
3728

3829
var bucketCreateResult = await client.CreateBucket(_cancellation);
3930

@@ -55,21 +46,21 @@ public async Task BeExists()
5546
[Fact]
5647
public async Task BeNotExists()
5748
{
58-
var settings = _fixture.Settings;
49+
var settings = fixture.Settings;
5950

6051
// don't dispose it
6152
var client =
6253
new S3Client(
6354
new S3Settings
6455
{
6556
AccessKey = settings.AccessKey,
66-
Bucket = _fixture.Create<string>(),
57+
Bucket = fixture.Create<string>(),
6758
EndPoint = settings.EndPoint,
6859
Port = settings.Port,
6960
SecretKey = settings.SecretKey,
7061
UseHttps = settings.UseHttps,
7162
},
72-
_fixture.HttpClient);
63+
fixture.HttpClient);
7364

7465
var bucketExistsResult = await client.IsBucketExists(_cancellation);
7566

@@ -88,21 +79,21 @@ public Task NotThrowIfCreateBucketAlreadyExists()
8879
[Fact]
8980
public Task NotThrowIfDeleteNotExistsBucket()
9081
{
91-
var settings = _fixture.Settings;
82+
var settings = fixture.Settings;
9283

9384
// don't use using here
9485
var client =
9586
new S3Client(
9687
new S3Settings
9788
{
9889
AccessKey = settings.AccessKey,
99-
Bucket = _fixture.Create<string>(),
90+
Bucket = fixture.Create<string>(),
10091
EndPoint = settings.EndPoint,
10192
Port = settings.Port,
10293
SecretKey = settings.SecretKey,
10394
UseHttps = settings.UseHttps,
10495
},
105-
_fixture.HttpClient);
96+
fixture.HttpClient);
10697

10798
return client
10899
.Invoking(c => c.DeleteBucket(_cancellation))

src/Storage.Tests/ClientShould.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
using System.Text.Json;
2-
using FluentAssertions;
3-
using Storage.Tests.Mocks;
1+
using Storage.Tests.Utils;
42

53
namespace Storage.Tests;
64

7-
public sealed class ClientShould : IClassFixture<StorageFixture>
5+
public sealed class ClientShould(StorageFixture fixture) : IClassFixture<StorageFixture>
86
{
9-
private readonly S3Client _client;
10-
private readonly StorageFixture _fixture;
11-
12-
public ClientShould(StorageFixture fixture)
13-
{
14-
_fixture = fixture;
15-
_client = fixture.S3Client;
16-
}
7+
private readonly S3Client _client = fixture.S3Client;
178

189
[Fact]
1910
public void DeserializeSettingsJson()
2011
{
21-
var expected = _fixture.Settings;
12+
var expected = fixture.Settings;
2213

2314
var json = JsonSerializer.Serialize(expected);
2415
var actual = JsonSerializer.Deserialize<S3Settings>(json);
@@ -31,6 +22,6 @@ public void HasValidInfo()
3122
{
3223
_client
3324
.Bucket
34-
.Should().Be(_fixture.Settings.Bucket);
25+
.Should().Be(fixture.Settings.Bucket);
3526
}
3627
}

src/Storage.Tests/GlobalUsings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global using System.Text.Json;
2+
global using FluentAssertions;
3+
global using Xunit;

src/Storage.Tests/ObjectShould.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Net;
2-
using FluentAssertions;
3-
using Storage.Tests.Mocks;
4-
using static Storage.Tests.Mocks.StorageFixture;
2+
using Storage.Tests.Utils;
3+
using static Storage.Tests.Utils.StorageFixture;
54

65
namespace Storage.Tests;
76

src/Storage.Tests/Storage.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<PrivateAssets>all</PrivateAssets>
1818
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1919
</PackageReference>
20+
<PackageReference Include="Testcontainers.Minio" Version="3.8.0" />
2021
<PackageReference Include="xunit" Version="2.7.1" />
2122
<PackageReference Include="JunitXml.TestLogger" Version="3.1.12" />
2223
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">

src/Storage.Tests/Usings.cs

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,26 @@
1-
using System.Globalization;
2-
using AutoFixture;
1+
using AutoFixture;
2+
using Testcontainers.Minio;
33

4-
namespace Storage.Tests.Mocks;
4+
namespace Storage.Tests.Utils;
55

6-
public sealed class StorageFixture : IDisposable
6+
public sealed class StorageFixture : IDisposable, IAsyncDisposable
77
{
88
public const string StreamContentType = "application/octet-stream";
99

1010
private const int DefaultByteArraySize = 1 * 1024 * 1024; // 7Mb
1111

12+
private readonly MinioContainer? _container;
1213
private Fixture? _fixture;
1314

1415
public StorageFixture()
1516
{
16-
var environmentPort = Environment.GetEnvironmentVariable("STORAGE_PORT");
17-
int? port = string.IsNullOrEmpty(environmentPort)
18-
? 5300
19-
: environmentPort is "null"
20-
? null
21-
: int.Parse(environmentPort, CultureInfo.InvariantCulture);
22-
23-
var environmentHttps = Environment.GetEnvironmentVariable("STORAGE_HTTPS");
24-
var https = !string.IsNullOrEmpty(environmentHttps) && bool.Parse(environmentHttps);
25-
26-
Settings = new S3Settings
27-
{
28-
AccessKey = Environment.GetEnvironmentVariable("STORAGE_KEY") ?? "ROOTUSER",
29-
Bucket = Environment.GetEnvironmentVariable("STORAGE_BUCKET") ?? "reconfig",
30-
EndPoint = Environment.GetEnvironmentVariable("STORAGE_ENDPOINT") ?? "localhost",
31-
Port = port,
32-
SecretKey = Environment.GetEnvironmentVariable("STORAGE_SECRET") ?? "ChangeMe123",
33-
UseHttps = https,
34-
};
17+
_container = TestHelper.CreateContainer();
3518

19+
Settings = TestHelper.CreateSettings(_container);
3620
HttpClient = new HttpClient();
3721
S3Client = new S3Client(Settings);
22+
23+
TestHelper.EnsureBucketExists(S3Client);
3824
}
3925

4026
internal S3Settings Settings { get; }
@@ -69,14 +55,22 @@ public static MemoryStream GetEmptyByteStream(long? size)
6955
: new MemoryStream();
7056
}
7157

58+
public T Create<T>()
59+
{
60+
return Mocks.Create<T>();
61+
}
62+
7263
public void Dispose()
7364
{
7465
HttpClient.Dispose();
7566
S3Client.Dispose();
7667
}
7768

78-
public T Create<T>()
69+
public async ValueTask DisposeAsync()
7970
{
80-
return Mocks.Create<T>();
71+
if (_container != null)
72+
{
73+
await _container.DisposeAsync();
74+
}
8175
}
8276
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Globalization;
2+
using Testcontainers.Minio;
3+
4+
namespace Storage.Tests.Utils;
5+
6+
public static class TestHelper
7+
{
8+
public const string SecretKey = "ChangeMe123";
9+
public const string Username = "ROOTUSER";
10+
11+
public static S3Settings CreateSettings(MinioContainer? container = null)
12+
{
13+
var environmentPort = Environment.GetEnvironmentVariable("STORAGE_PORT");
14+
int? port = string.IsNullOrEmpty(environmentPort)
15+
? container?.GetMappedPublicPort(MinioBuilder.MinioPort) ?? 5300
16+
: environmentPort is "null"
17+
? null
18+
: int.Parse(environmentPort, CultureInfo.InvariantCulture);
19+
20+
var environmentHttps = Environment.GetEnvironmentVariable("STORAGE_HTTPS");
21+
var https = !string.IsNullOrEmpty(environmentHttps) && bool.Parse(environmentHttps);
22+
23+
return new S3Settings
24+
{
25+
AccessKey = Environment.GetEnvironmentVariable("STORAGE_KEY") ?? Username,
26+
Bucket = Environment.GetEnvironmentVariable("STORAGE_BUCKET") ?? "reconfig",
27+
EndPoint = Environment.GetEnvironmentVariable("STORAGE_ENDPOINT") ?? "localhost",
28+
Port = port,
29+
SecretKey = Environment.GetEnvironmentVariable("STORAGE_SECRET") ?? SecretKey,
30+
UseHttps = https,
31+
};
32+
}
33+
34+
public static MinioContainer CreateContainer(bool run = true)
35+
{
36+
var container = new MinioBuilder()
37+
.WithPassword(SecretKey)
38+
.WithUsername(Username)
39+
.Build();
40+
41+
if (run)
42+
{
43+
container.StartAsync().Wait();
44+
}
45+
46+
return container;
47+
}
48+
49+
public static void EnsureBucketExists(S3Client client)
50+
{
51+
EnsureBucketExists(client, CancellationToken.None).Wait();
52+
}
53+
54+
public static async Task EnsureBucketExists(S3Client client, CancellationToken cancellation)
55+
{
56+
if (await client.IsBucketExists(cancellation))
57+
{
58+
return;
59+
}
60+
61+
await client.CreateBucket(cancellation);
62+
}
63+
}

src/Storage/Utils/HttpHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System.Buffers;
2+
using System.Collections.Frozen;
23
using System.Text;
34

45
namespace Storage.Utils;
56

67
internal readonly struct HttpHelper
78
{
8-
private static readonly HashSet<char> _validUrlCharacters =
9-
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~".ToHashSet();
9+
private static readonly FrozenSet<char> _validUrlCharacters =
10+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~".ToFrozenSet();
1011

1112
private readonly string _headerEnd;
1213
private readonly string _headerStart;

0 commit comments

Comments
 (0)