Skip to content

Commit

Permalink
[WIP] Use Testcontainers.Xunit in Testcontainers' own tests
Browse files Browse the repository at this point in the history
TODO: remaining modules
  • Loading branch information
0xced committed Dec 20, 2024
1 parent 1543575 commit f259fff
Show file tree
Hide file tree
Showing 94 changed files with 354 additions and 779 deletions.
58 changes: 11 additions & 47 deletions tests/Testcontainers.ActiveMq.Tests/ArtemisContainerTest.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
namespace Testcontainers.ActiveMq;

public abstract class ArtemisContainerTest : IAsyncLifetime
public abstract class ArtemisContainerTest(ITestOutputHelper testOutputHelper, string userName, string password, Func<ArtemisBuilder, ArtemisBuilder> configure = null)
: ContainerTest<ArtemisBuilder, ArtemisContainer>(testOutputHelper, configure)
{
private readonly ArtemisContainer _artemisContainer;

private readonly string _username;

private readonly string _password;

private ArtemisContainerTest(ArtemisContainer artemisContainer, string username, string password)
{
_artemisContainer = artemisContainer;
_username = username;
_password = password;
}

public Task InitializeAsync()
{
return _artemisContainer.StartAsync();
}

public Task DisposeAsync()
{
return _artemisContainer.DisposeAsync().AsTask();
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task EstablishesConnection()
{
// Given
var connectionFactory = new ConnectionFactory(_artemisContainer.GetBrokerAddress());
connectionFactory.UserName = _username;
connectionFactory.Password = _password;
var connectionFactory = new ConnectionFactory(Container.GetBrokerAddress());
connectionFactory.UserName = userName;
connectionFactory.Password = password;

// When
using var connection = await connectionFactory.CreateConnectionAsync()
Expand Down Expand Up @@ -69,33 +47,19 @@ await producer.SendAsync(producedMessage)
}

[UsedImplicitly]
public sealed class DefaultCredentialsConfiguration : ArtemisContainerTest
{
public DefaultCredentialsConfiguration()
: base(new ArtemisBuilder().Build(), ArtemisBuilder.DefaultUsername, ArtemisBuilder.DefaultPassword)
{
}
}
public sealed class DefaultCredentialsConfiguration(ITestOutputHelper testOutputHelper)
: ArtemisContainerTest(testOutputHelper, ArtemisBuilder.DefaultUsername, ArtemisBuilder.DefaultPassword);

[UsedImplicitly]
public sealed class CustomCredentialsConfiguration : ArtemisContainerTest
public sealed class CustomCredentialsConfiguration(ITestOutputHelper testOutputHelper)
: ArtemisContainerTest(testOutputHelper, Username, Password, builder => builder.WithUsername(Username).WithPassword(Password))
{
private static readonly string Username = Guid.NewGuid().ToString("D");

private static readonly string Password = Guid.NewGuid().ToString("D");

public CustomCredentialsConfiguration()
: base(new ArtemisBuilder().WithUsername(Username).WithPassword(Password).Build(), Username, Password)
{
}
}

[UsedImplicitly]
public sealed class NoAuthCredentialsConfiguration : ArtemisContainerTest
{
public NoAuthCredentialsConfiguration()
: base(new ArtemisBuilder().WithEnvironment("ANONYMOUS_LOGIN", bool.TrueString).Build(), string.Empty, string.Empty)
{
}
}
public sealed class NoAuthCredentialsConfiguration(ITestOutputHelper testOutputHelper)
: ArtemisContainerTest(testOutputHelper, string.Empty, string.Empty, builder => builder.WithEnvironment("ANONYMOUS_LOGIN", bool.TrueString));
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/Testcontainers.ActiveMq/Testcontainers.ActiveMq.csproj"/>
<ProjectReference Include="../../src/Testcontainers.Xunit/Testcontainers.Xunit.csproj" />
<ProjectReference Include="../Testcontainers.Commons/Testcontainers.Commons.csproj"/>
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion tests/Testcontainers.ActiveMq.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
global using Apache.NMS.ActiveMQ;
global using DotNet.Testcontainers.Commons;
global using JetBrains.Annotations;
global using Xunit;
global using Testcontainers.Xunit;
global using Xunit;
global using Xunit.Abstractions;
16 changes: 2 additions & 14 deletions tests/Testcontainers.ArangoDb.Tests/ArangoDbContainerTest.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
namespace Testcontainers.ArangoDb;

public sealed class ArangoDbContainerTest : IAsyncLifetime
public sealed class ArangoDbContainerTest(ITestOutputHelper testOutputHelper) : ContainerTest<ArangoDbBuilder, ArangoDbContainer>(testOutputHelper)
{
private readonly ArangoDbContainer _arangoDbContainer = new ArangoDbBuilder().Build();

public Task InitializeAsync()
{
return _arangoDbContainer.StartAsync();
}

public Task DisposeAsync()
{
return _arangoDbContainer.DisposeAsync().AsTask();
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task RetrievesDatabases()
{
// Given
var address = new Uri(_arangoDbContainer.GetTransportAddress());
var address = new Uri(Container.GetTransportAddress());

using var transport = HttpApiTransport.UsingBasicAuth(address, ArangoDbBuilder.DefaultUsername, ArangoDbBuilder.DefaultPassword);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/Testcontainers.ArangoDb/Testcontainers.ArangoDb.csproj"/>
<ProjectReference Include="../../src/Testcontainers.Xunit/Testcontainers.Xunit.csproj" />
<ProjectReference Include="../Testcontainers.Commons/Testcontainers.Commons.csproj"/>
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion tests/Testcontainers.ArangoDb.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
global using ArangoDBNetStandard;
global using ArangoDBNetStandard.Transport.Http;
global using DotNet.Testcontainers.Commons;
global using Xunit;
global using Testcontainers.Xunit;
global using Xunit;
global using Xunit.Abstractions;
56 changes: 19 additions & 37 deletions tests/Testcontainers.Azurite.Tests/AzuriteContainerTest.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
namespace Testcontainers.Azurite;

public abstract class AzuriteContainerTest : IAsyncLifetime
public abstract class AzuriteContainerTest(AzuriteContainerTest.AzuriteFixture fixture)
{
private readonly AzuriteContainer _azuriteContainer;

private AzuriteContainerTest(AzuriteContainer azuriteContainer)
{
_azuriteContainer = azuriteContainer;
}

public Task InitializeAsync()
{
return _azuriteContainer.StartAsync();
}

public Task DisposeAsync()
{
return _azuriteContainer.DisposeAsync().AsTask();
}
private readonly AzuriteContainer _azuriteContainer = fixture.Container;

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
Expand Down Expand Up @@ -72,48 +57,45 @@ private static bool HasError<TResponseEntity>(NullableResponse<TResponseEntity>
}
}

public class AzuriteFixture(IMessageSink messageSink) : ContainerFixture<AzuriteBuilder, AzuriteContainer>(messageSink);

[UsedImplicitly]
public sealed class AzuriteDefaultConfiguration : AzuriteContainerTest
public sealed class AzuriteInMemoryFixture(IMessageSink messageSink) : AzuriteFixture(messageSink)
{
public AzuriteDefaultConfiguration()
: base(new AzuriteBuilder().Build())
{
}
protected override AzuriteBuilder Configure(AzuriteBuilder builder) => builder.WithInMemoryPersistence();
}

[UsedImplicitly]
public sealed class AzuriteInMemoryConfiguration : AzuriteContainerTest
public sealed class AzuriteMemoryLimitFixture(IMessageSink messageSink) : AzuriteFixture(messageSink)
{
public AzuriteInMemoryConfiguration()
: base(new AzuriteBuilder().WithInMemoryPersistence().Build())
{
}
public const int MemoryLimitInMb = 64;

protected override AzuriteBuilder Configure(AzuriteBuilder builder) => builder.WithInMemoryPersistence(MemoryLimitInMb);
}

[UsedImplicitly]
public sealed class AzuriteMemoryLimitConfiguration : AzuriteContainerTest
{
private const int MemoryLimitInMb = 64;
public sealed class AzuriteDefaultConfiguration(AzuriteFixture fixture) : AzuriteContainerTest(fixture), IClassFixture<AzuriteFixture>;

private static readonly string[] LineEndings = { "\r\n", "\n" };
[UsedImplicitly]
public sealed class AzuriteInMemoryConfiguration(AzuriteInMemoryFixture fixture) : AzuriteContainerTest(fixture), IClassFixture<AzuriteInMemoryFixture>;

public AzuriteMemoryLimitConfiguration()
: base(new AzuriteBuilder().WithInMemoryPersistence(MemoryLimitInMb).Build())
{
}
[UsedImplicitly]
public sealed class AzuriteMemoryLimitConfiguration(AzuriteMemoryLimitFixture fixture) : AzuriteContainerTest(fixture), IClassFixture<AzuriteMemoryLimitFixture>
{
private static readonly string[] LineEndings = { "\r\n", "\n" };

[Fact]
public async Task MemoryLimitIsConfigured()
{
// Given
var (stdout, _) = await _azuriteContainer.GetLogsAsync(timestampsEnabled: false)
var (stdout, _) = await fixture.Container.GetLogsAsync(timestampsEnabled: false)
.ConfigureAwait(true);

// When
var firstLine = stdout.Split(LineEndings, StringSplitOptions.RemoveEmptyEntries).First();

// Then
Assert.StartsWith(string.Format(CultureInfo.InvariantCulture, "In-memory extent storage is enabled with a limit of {0:F2} MB", MemoryLimitInMb), firstLine);
Assert.StartsWith(string.Format(CultureInfo.InvariantCulture, "In-memory extent storage is enabled with a limit of {0:F2} MB", AzuriteMemoryLimitFixture.MemoryLimitInMb), firstLine);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/Testcontainers.Azurite/Testcontainers.Azurite.csproj"/>
<ProjectReference Include="../../src/Testcontainers.Xunit/Testcontainers.Xunit.csproj" />
<ProjectReference Include="../Testcontainers.Commons/Testcontainers.Commons.csproj"/>
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion tests/Testcontainers.Azurite.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
global using Azure.Storage.Queues;
global using DotNet.Testcontainers.Commons;
global using JetBrains.Annotations;
global using Xunit;
global using Testcontainers.Xunit;
global using Xunit;
global using Xunit.Abstractions;
16 changes: 2 additions & 14 deletions tests/Testcontainers.BigQuery.Tests/BigQueryContainerTest.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
namespace Testcontainers.BigQuery;

public sealed class BigQueryContainerTest : IAsyncLifetime
public sealed class BigQueryContainerTest(ContainerFixture<BigQueryBuilder, BigQueryContainer> fixture) : IClassFixture<ContainerFixture<BigQueryBuilder, BigQueryContainer>>
{
private readonly BigQueryContainer _bigQueryContainer = new BigQueryBuilder().Build();

public Task InitializeAsync()
{
return _bigQueryContainer.StartAsync();
}

public Task DisposeAsync()
{
return _bigQueryContainer.DisposeAsync().AsTask();
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task ExecuteQueryReturnsInsertRow()
Expand All @@ -26,7 +14,7 @@ public async Task ExecuteQueryReturnsInsertRow()
var utcNowWithoutMilliseconds = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day, utcNow.Hour, utcNow.Minute, utcNow.Second, DateTimeKind.Utc);

var bigQueryClientBuilder = new BigQueryClientBuilder();
bigQueryClientBuilder.BaseUri = _bigQueryContainer.GetEmulatorEndpoint();
bigQueryClientBuilder.BaseUri = fixture.Container.GetEmulatorEndpoint();
bigQueryClientBuilder.ProjectId = BigQueryBuilder.DefaultProjectId;
bigQueryClientBuilder.Credential = new Credential();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/Testcontainers.BigQuery/Testcontainers.BigQuery.csproj"/>
<ProjectReference Include="../../src/Testcontainers.Xunit/Testcontainers.Xunit.csproj" />
<ProjectReference Include="../Testcontainers.Commons/Testcontainers.Commons.csproj"/>
</ItemGroup>
</Project>
9 changes: 5 additions & 4 deletions tests/Testcontainers.BigQuery.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
global using System;
global using System.Linq;
global using System.Threading;
global using System.Threading.Tasks;
global using DotNet.Testcontainers.Commons;
global using Google.Cloud.BigQuery.V2;
global using Xunit;
global using System.Threading;
global using Google.Apis.Auth.OAuth2;
global using Google.Apis.Http;
global using Google.Apis.Http;
global using Google.Cloud.BigQuery.V2;
global using Testcontainers.Xunit;
global using Xunit;
16 changes: 2 additions & 14 deletions tests/Testcontainers.Bigtable.Tests/BigtableContainerTest.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
namespace Testcontainers.Bigtable;

public sealed class BigtableContainerTest : IAsyncLifetime
public sealed class BigtableContainerTest(ITestOutputHelper testOutputHelper) : ContainerTest<BigtableBuilder, BigtableContainer>(testOutputHelper)
{
private readonly BigtableContainer _bigtableContainer = new BigtableBuilder().Build();

public Task InitializeAsync()
{
return _bigtableContainer.StartAsync();
}

public Task DisposeAsync()
{
return _bigtableContainer.DisposeAsync().AsTask();
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task GetTableReturnsCreateTable()
Expand All @@ -38,7 +26,7 @@ public async Task GetTableReturnsCreateTable()
table.ColumnFamilies.Add(nameof(columnFamily), columnFamily);

var bigtableClientBuilder = new BigtableTableAdminClientBuilder();
bigtableClientBuilder.Endpoint = _bigtableContainer.GetEmulatorEndpoint();
bigtableClientBuilder.Endpoint = Container.GetEmulatorEndpoint();
bigtableClientBuilder.ChannelCredentials = ChannelCredentials.Insecure;

// When
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/Testcontainers.Bigtable/Testcontainers.Bigtable.csproj"/>
<ProjectReference Include="../../src/Testcontainers.Xunit/Testcontainers.Xunit.csproj" />
<ProjectReference Include="../Testcontainers.Commons/Testcontainers.Commons.csproj"/>
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion tests/Testcontainers.Bigtable.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
global using Google.Cloud.Bigtable.Admin.V2;
global using Google.Cloud.Bigtable.Common.V2;
global using Grpc.Core;
global using Xunit;
global using Testcontainers.Xunit;
global using Xunit;
global using Xunit.Abstractions;
Loading

0 comments on commit f259fff

Please sign in to comment.