Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add DynamoDB module #768

Merged
merged 21 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Testcontainers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Minio", "src
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Minio.Tests", "tests\Testcontainers.Minio.Tests\Testcontainers.Minio.Tests.csproj", "{5DB1F35F-B714-4B62-84BE-16A33084D3E1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.DynamoDB.Tests", "tests\Testcontainers.DynamoDB.Tests\Testcontainers.DynamoDB.Tests.csproj", "{101515E6-74C1-40F9-85C8-871F742A378D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.DynamoDB", "src\Testcontainers.DynamoDB\Testcontainers.DynamoDB.csproj", "{2EAFA567-9F68-4C52-9DBC-8F3EC11BB2CE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -113,6 +117,14 @@ Global
{5DB1F35F-B714-4B62-84BE-16A33084D3E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DB1F35F-B714-4B62-84BE-16A33084D3E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DB1F35F-B714-4B62-84BE-16A33084D3E1}.Release|Any CPU.Build.0 = Release|Any CPU
{101515E6-74C1-40F9-85C8-871F742A378D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{101515E6-74C1-40F9-85C8-871F742A378D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{101515E6-74C1-40F9-85C8-871F742A378D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{101515E6-74C1-40F9-85C8-871F742A378D}.Release|Any CPU.Build.0 = Release|Any CPU
{2EAFA567-9F68-4C52-9DBC-8F3EC11BB2CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2EAFA567-9F68-4C52-9DBC-8F3EC11BB2CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2EAFA567-9F68-4C52-9DBC-8F3EC11BB2CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2EAFA567-9F68-4C52-9DBC-8F3EC11BB2CE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{EC76857B-A3B8-4B7A-A1B0-8D867A4D1733} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
Expand All @@ -131,5 +143,7 @@ Global
{27CDB869-A150-4593-958F-6F26E5391E7C} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{1266E1E6-5CEF-4161-8B45-83282455746E} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{5DB1F35F-B714-4B62-84BE-16A33084D3E1} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{101515E6-74C1-40F9-85C8-871F742A378D} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{2EAFA567-9F68-4C52-9DBC-8F3EC11BB2CE} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions src/Testcontainers.DynamoDB/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
86 changes: 86 additions & 0 deletions src/Testcontainers.DynamoDB/DynamoDBBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace Testcontainers.DynamoDB;

/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class DynamoDBBuilder : ContainerBuilder<DynamoDBBuilder, DynamoDBContainer, DynamoDBConfiguration>
dominikus1993 marked this conversation as resolved.
Show resolved Hide resolved
{
public const string DynamoDbImage = "amazon/dynamodb-local:1.21.0";

public const ushort DynamoDbPort = 8000;

/// <summary>
/// Initializes a new instance of the <see cref="DynamoDBBuilder" /> class.
/// </summary>
public DynamoDBBuilder()
: this(new DynamoDBConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}

/// <summary>
/// Initializes a new instance of the <see cref="DynamoDBBuilder" /> class.
/// </summary>
/// <param name="dockerResourceConfiguration">The Docker resource configuration.</param>
private DynamoDBBuilder(DynamoDBConfiguration dockerResourceConfiguration)
: base(dockerResourceConfiguration)
{
DockerResourceConfiguration = dockerResourceConfiguration;
}

/// <inheritdoc />
protected override DynamoDBConfiguration DockerResourceConfiguration { get; }


dominikus1993 marked this conversation as resolved.
Show resolved Hide resolved
/// <inheritdoc />
public override DynamoDBContainer Build()
{
Validate();
return new DynamoDBContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
}

/// <inheritdoc />
protected override DynamoDBBuilder Init()
{
return base.Init()
.WithImage(DynamoDbImage)
.WithPortBinding(DynamoDbPort, true)
.WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new WaitUntil(DynamoDbPort)));
dominikus1993 marked this conversation as resolved.
Show resolved Hide resolved
}

/// <inheritdoc />
protected override DynamoDBBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new DynamoDBConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override DynamoDBBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new DynamoDBConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override DynamoDBBuilder Merge(DynamoDBConfiguration oldValue, DynamoDBConfiguration newValue)
{
return new DynamoDBBuilder(new DynamoDBConfiguration(oldValue, newValue));
}

/// <inheritdoc cref="IWaitUntil" />
private sealed class WaitUntil : IWaitUntil
{
private readonly ushort _port;

public WaitUntil(ushort port)
{
_port = port;
}
/// <inheritdoc />
public async Task<bool> UntilAsync(IContainer container)
{
var execResult = await container.ExecAsync(new[] { "curl", $"http://localhost:{_port}" })
.ConfigureAwait(false);

return 0L.Equals(execResult.ExitCode);
}
}
}
53 changes: 53 additions & 0 deletions src/Testcontainers.DynamoDB/DynamoDBConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Testcontainers.DynamoDB;

/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class DynamoDBConfiguration : ContainerConfiguration
{
/// <summary>
/// Initializes a new instance of the <see cref="DynamoDBConfiguration" /> class.
/// </summary>
public DynamoDBConfiguration()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DynamoDBConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public DynamoDBConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="DynamoDBConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public DynamoDBConfiguration(IContainerConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="DynamoDBConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public DynamoDBConfiguration(DynamoDBConfiguration resourceConfiguration)
: this(new DynamoDBConfiguration(), resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="DynamoDBConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public DynamoDBConfiguration(DynamoDBConfiguration oldValue, DynamoDBConfiguration newValue)
: base(oldValue, newValue)
{
}
}
38 changes: 38 additions & 0 deletions src/Testcontainers.DynamoDB/DynamoDBContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Testcontainers.DynamoDB;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class DynamoDBContainer : DockerContainer
{

dominikus1993 marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// Initializes a new instance of the <see cref="DynamoDBContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
/// <param name="logger">The logger.</param>
public DynamoDBContainer(DynamoDBConfiguration configuration, ILogger logger)
: base(configuration, logger)
{
}

/// <summary>
/// Gets the DynamoDB endpoint.
/// </summary>
/// <returns>The DynamoDB endpoint.</returns>
private string GetEndpoint()
{
return new UriBuilder(Uri.UriSchemeHttp, Hostname, GetMappedPublicPort(DynamoDBBuilder.DynamoDbPort)).ToString();
}

/// <summary>
/// Gets the AmazonDynamoDBClient client.
/// </summary>
/// <returns>The AmazonDynamoDBClient.</returns>
public AmazonDynamoDBClient GetAmazonDynamoDBClient()
dominikus1993 marked this conversation as resolved.
Show resolved Hide resolved
{
var clientConfig = new AmazonDynamoDBConfig();
clientConfig.ServiceURL = this.GetEndpoint();
clientConfig.UseHttp = true;
return new AmazonDynamoDBClient(new BasicAWSCredentials("dummy", "dummy"), clientConfig);
}
}
13 changes: 13 additions & 0 deletions src/Testcontainers.DynamoDB/Testcontainers.DynamoDB.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.41" />
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(SolutionDir)src/Testcontainers/Testcontainers.csproj" />
</ItemGroup>
</Project>
dominikus1993 marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 11 additions & 0 deletions src/Testcontainers.DynamoDB/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
global using System;
global using Docker.DotNet.Models;
global using DotNet.Testcontainers;
global using DotNet.Testcontainers.Builders;
global using DotNet.Testcontainers.Configurations;
global using DotNet.Testcontainers.Containers;
global using JetBrains.Annotations;
global using Microsoft.Extensions.Logging;
global using Amazon.DynamoDBv2;
global using Amazon.Runtime;
global using System.Threading.Tasks;
dominikus1993 marked this conversation as resolved.
Show resolved Hide resolved
75 changes: 75 additions & 0 deletions tests/Testcontainers.DynamoDB.Tests/DynamoDbContainerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace Testcontainers.DynamoDB;

public sealed class MinioContainerTest : IAsyncLifetime
{
private readonly DynamoDBContainer dynamoDbContainer = new DynamoDBBuilder().Build();

public Task InitializeAsync()
{
return this.dynamoDbContainer.StartAsync();
}

public Task DisposeAsync()
{
return this.dynamoDbContainer.DisposeAsync().AsTask();
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task CreateTableReturnsCorrectTableDescription()
{
// Given
const string tableName = "TestDynamoDbTable";
using var client = this.dynamoDbContainer.GetAmazonDynamoDBClient();

// When
_ = await client.CreateTableAsync(new CreateTableRequest()
{
TableName = tableName,
AttributeDefinitions = new List<AttributeDefinition>() { new AttributeDefinition("Id", ScalarAttributeType.S), new AttributeDefinition("Name", ScalarAttributeType.S), },
KeySchema = new List<KeySchemaElement>() { new KeySchemaElement("Id", KeyType.HASH), new KeySchemaElement("Name", KeyType.RANGE), },
ProvisionedThroughput = new ProvisionedThroughput(1, 1),
TableClass = TableClass.STANDARD,
})
.ConfigureAwait(false);

var tableDescription = await client.DescribeTableAsync(tableName).ConfigureAwait(false);

// Then
Assert.NotNull(tableDescription);
Assert.Equal(HttpStatusCode.OK, tableDescription.HttpStatusCode);
Assert.Equal(tableName, tableDescription.Table.TableName);
Assert.Equal("Id", tableDescription.Table.KeySchema[0].AttributeName);
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task InsertElementToTableReturnsHttpStatusCodeOk()
{
// Given
var tableName = $"TestDynamoDbTable-{Guid.NewGuid():D}";
var itemId = Guid.NewGuid().ToString("D");
var itemName = Guid.NewGuid().ToString("D");

using var client = this.dynamoDbContainer.GetAmazonDynamoDBClient();

// When
_ = await client.CreateTableAsync(new CreateTableRequest()
{
TableName = tableName,
AttributeDefinitions = new List<AttributeDefinition>() { new AttributeDefinition("Id", ScalarAttributeType.S), new AttributeDefinition("Name", ScalarAttributeType.S), },
KeySchema = new List<KeySchemaElement>() { new KeySchemaElement("Id", KeyType.HASH), new KeySchemaElement("Name", KeyType.RANGE), },
ProvisionedThroughput = new ProvisionedThroughput(1, 1),
TableClass = TableClass.STANDARD,
})
.ConfigureAwait(false);

_ = await client.PutItemAsync(new PutItemRequest(tableName, new Dictionary<string, AttributeValue>() { { "Id", new AttributeValue() { S = itemId } }, { "Name", new AttributeValue() { S = itemName } } })).ConfigureAwait(false);

var getItemResponse = await client.GetItemAsync(new GetItemRequest(tableName, new Dictionary<string, AttributeValue>() { { "Id", new AttributeValue() { S = itemId } }, { "Name", new AttributeValue() { S = itemName } } }))
.ConfigureAwait(false);

// Then
Assert.Equal(HttpStatusCode.OK, getItemResponse.HttpStatusCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsPublishable>false</IsPublishable>
<RootNamespace>Testcontainers.DynamoDb.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="coverlet.msbuild" Version="3.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
<PackageReference Include="xunit" Version="2.4.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Testcontainers.DynamoDB\Testcontainers.DynamoDB.csproj" />
<ProjectReference Include="$(SolutionDir)tests/Testcontainers.Commons/Testcontainers.Commons.csproj" />
</ItemGroup>
</Project>
dominikus1993 marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 10 additions & 0 deletions tests/Testcontainers.DynamoDB.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
global using System;
global using System.IO;
global using System.Threading.Tasks;
global using Xunit;
global using System.Net;
global using DotNet.Testcontainers.Commons;
global using System.Collections.Generic;
global using Amazon.DynamoDBv2;
global using Amazon.DynamoDBv2.Model;
global using Amazon.Runtime;
dominikus1993 marked this conversation as resolved.
Show resolved Hide resolved