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

Feature/862 add spanner module #865

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,6 @@ test-coverage/

# SonarQube
.sonarqube/

# Visual Studio Code
.vscode/
16 changes: 15 additions & 1 deletion Testcontainers.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Expand Down Expand Up @@ -121,6 +121,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Tests", "tes
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.WebDriver.Tests", "tests\Testcontainers.WebDriver.Tests\Testcontainers.WebDriver.Tests.csproj", "{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Spanner", "src\Testcontainers.Spanner\Testcontainers.Spanner.csproj", "{7026E057-9F15-4947-AB8C-4AF034323A3E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Spanner.Tests", "tests\Testcontainers.Spanner.Tests\Testcontainers.Spanner.Tests.csproj", "{A84920E9-D781-4457-BD32-43B32C2D451B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -346,6 +350,14 @@ Global
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}.Release|Any CPU.Build.0 = Release|Any CPU
{7026E057-9F15-4947-AB8C-4AF034323A3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7026E057-9F15-4947-AB8C-4AF034323A3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7026E057-9F15-4947-AB8C-4AF034323A3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7026E057-9F15-4947-AB8C-4AF034323A3E}.Release|Any CPU.Build.0 = Release|Any CPU
{A84920E9-D781-4457-BD32-43B32C2D451B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A84920E9-D781-4457-BD32-43B32C2D451B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A84920E9-D781-4457-BD32-43B32C2D451B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A84920E9-D781-4457-BD32-43B32C2D451B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{3F2E254F-C203-43FD-A078-DC3E2CBC0F9F} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
Expand Down Expand Up @@ -402,5 +414,7 @@ Global
{1A1983E6-5297-435F-B467-E8E1F11277D6} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{27CDB869-A150-4593-958F-6F26E5391E7C} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{7026E057-9F15-4947-AB8C-4AF034323A3E} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{A84920E9-D781-4457-BD32-43B32C2D451B} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions src/Testcontainers.Spanner/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
HofmeisterAn marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 24 additions & 0 deletions src/Testcontainers.Spanner/HttpResponseMessageExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Testcontainers.Spanner;
internal static class HttpResponseMessageExtensions
{
internal static async Task<HttpResponseMessage> ValidateAsync(this Task<HttpResponseMessage> response, string operation, CancellationToken ct = default)
{
ct.ThrowIfCancellationRequested();

var result = await response;

if (!result.IsSuccessStatusCode)
{
string resultContent = await result.Content.ReadAsStringAsync();
string message =
$"Failed to {operation} with status code {result.StatusCode} response: {resultContent}";
throw new InvalidOperationException(message);
}

return result;
}
}
101 changes: 101 additions & 0 deletions src/Testcontainers.Spanner/SpannerBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
namespace Testcontainers.Spanner;

/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class SpannerBuilder : ContainerBuilder<SpannerBuilder, SpannerContainer, SpannerConfiguration>
{
private const string DefaultProjectId = "my-project";
private const string SpannerEmulatorImage = "gcr.io/cloud-spanner-emulator/emulator:1.5.3";


internal const int InternalGrpcPort = 9010;
internal const int InternalRestPort = 9020;


/// <summary>
/// Initializes a new instance of the <see cref="SpannerBuilder" /> class.
/// </summary>
public SpannerBuilder()
: this(new SpannerConfiguration())
{

DockerResourceConfiguration = Init().DockerResourceConfiguration;
}

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

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

/// <summary>
/// Sets the ProjectId.
/// </summary>
/// <param name="projectId">The ProjectId.</param>
/// <returns>A configured instance of <see cref="SpannerBuilder" />.</returns>
public SpannerBuilder WithProjectId(string projectId)
=> Merge(DockerResourceConfiguration, new SpannerConfiguration(projectId: projectId));


/// <inheritdoc />
public override SpannerContainer Build()
{
Validate();
return new SpannerContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
}


/// <inheritdoc />
protected override SpannerBuilder Init()
{
return base.Init()
.WithImage(SpannerEmulatorImage)
.WithPortBinding(InternalGrpcPort, true)
.WithPortBinding(InternalRestPort, true)
.WithProjectId(DefaultProjectId)
.WithWaitStrategy(
Wait
.ForUnixContainer()
// The default wait for port implementation keeps waiting untill the test times out, therefor now using this custom flavor of the same concept
.UntilMessageIsLogged($".+REST server listening at 0.0.0.0:{InternalRestPort}")
.UntilMessageIsLogged($".+gRPC server listening at 0.0.0.0:{InternalGrpcPort}")
);

}


/// <inheritdoc />
protected override void Validate()
{
base.Validate();

_ = Guard.Argument(DockerResourceConfiguration.ProjectId, nameof(DockerResourceConfiguration.ProjectId))
.NotNull()
.NotEmpty();
}

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

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

/// <inheritdoc />
protected override SpannerBuilder Merge(SpannerConfiguration oldValue, SpannerConfiguration newValue)
{
return new SpannerBuilder(new SpannerConfiguration(oldValue, newValue));
}
}
58 changes: 58 additions & 0 deletions src/Testcontainers.Spanner/SpannerConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace Testcontainers.Spanner;

/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public class SpannerConfiguration : ContainerConfiguration
{
public string? ProjectId { get; }

/// <summary>
/// Initializes a new instance of the <see cref="SpannerConfiguration" /> class.
/// </summary>
/// <param name="projectId"></param>
public SpannerConfiguration(string? projectId = null)
{
ProjectId = projectId;
}

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

/// <summary>
/// Initializes a new instance of the <see cref="SpannerConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public SpannerConfiguration(SpannerConfiguration oldValue, SpannerConfiguration newValue)
: base(oldValue, newValue)
{
ProjectId = BuildConfiguration.Combine(oldValue.ProjectId, newValue.ProjectId);
}
}
34 changes: 34 additions & 0 deletions src/Testcontainers.Spanner/SpannerContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Testcontainers.Spanner;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class SpannerContainer : DockerContainer
{
private const string EnvironmentVariableEmulatorHost = "SPANNER_EMULATOR_HOST";
private readonly SpannerConfiguration _configuration;

public int GrpcPort => GetMappedPublicPort(SpannerBuilder.InternalGrpcPort);
public int RestPort => GetMappedPublicPort(SpannerBuilder.InternalRestPort);

/// <summary>
/// Initializes a new instance of the <see cref="SpannerContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
/// <param name="logger">The logger.</param>
public SpannerContainer(SpannerConfiguration configuration, ILogger logger)
: base(configuration, logger)
{
_configuration = configuration;
}

public override async Task StartAsync(CancellationToken ct = default)
{
await base.StartAsync(ct);

Environment.SetEnvironmentVariable(EnvironmentVariableEmulatorHost, $"{Hostname}:{GrpcPort}");
}
}
15 changes: 15 additions & 0 deletions src/Testcontainers.Spanner/Testcontainers.Spanner.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Cloud.ResourceManager.V3" Version="2.2.0" />
<PackageReference Include="Google.Cloud.Spanner.Admin.Instance.V1" Version="4.5.0" />
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(SolutionDir)src/Testcontainers/Testcontainers.csproj" />
</ItemGroup>
</Project>
8 changes: 8 additions & 0 deletions src/Testcontainers.Spanner/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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;
1 change: 1 addition & 0 deletions tests/Testcontainers.Spanner.Tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
HofmeisterAn marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: CollectionBehavior(DisableTestParallelization = true)] // The c.net client libraries alwys use the host from the environment variable, even if another one is passed