-
-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Google Cloud Storage API (fake-gcs-server) module (#1023)
Co-authored-by: Andre Hofmeister <[email protected]>
- Loading branch information
1 parent
dfbaef6
commit c7cedb8
Showing
11 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
root = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace Testcontainers.FakeGcsServer; | ||
|
||
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
[PublicAPI] | ||
public sealed class FakeGcsServerBuilder : ContainerBuilder<FakeGcsServerBuilder, FakeGcsServerContainer, FakeGcsServerConfiguration> | ||
{ | ||
public const string FakeGcsServerImage = "fsouza/fake-gcs-server:1.47.5"; | ||
|
||
public const ushort FakeGcsServerPort = 4443; | ||
|
||
public const string StartupScriptFilePath = "/testcontainers.sh"; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FakeGcsServerBuilder" /> class. | ||
/// </summary> | ||
public FakeGcsServerBuilder() | ||
: this(new FakeGcsServerConfiguration()) | ||
{ | ||
DockerResourceConfiguration = Init().DockerResourceConfiguration; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FakeGcsServerBuilder" /> class. | ||
/// </summary> | ||
/// <param name="dockerResourceConfiguration">The Docker resource configuration.</param> | ||
private FakeGcsServerBuilder(FakeGcsServerConfiguration dockerResourceConfiguration) | ||
: base(dockerResourceConfiguration) | ||
{ | ||
DockerResourceConfiguration = dockerResourceConfiguration; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override FakeGcsServerConfiguration DockerResourceConfiguration { get; } | ||
|
||
/// <inheritdoc /> | ||
public override FakeGcsServerContainer Build() | ||
{ | ||
Validate(); | ||
return new FakeGcsServerContainer(DockerResourceConfiguration, TestcontainersSettings.Logger); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override FakeGcsServerBuilder Init() | ||
{ | ||
return base.Init() | ||
.WithImage(FakeGcsServerImage) | ||
.WithPortBinding(FakeGcsServerPort, true) | ||
.WithEntrypoint("/bin/sh", "-c") | ||
.WithCommand("while [ ! -f " + StartupScriptFilePath + " ]; do sleep 0.1; done; " + StartupScriptFilePath) | ||
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("server started")) | ||
.WithStartupCallback((container, ct) => | ||
{ | ||
const char lf = '\n'; | ||
var startupScript = new StringBuilder(); | ||
startupScript.Append("#!/bin/sh"); | ||
startupScript.Append(lf); | ||
startupScript.Append("fake-gcs-server "); | ||
startupScript.Append("-backend memory "); | ||
startupScript.Append("-scheme http "); | ||
// If we do not remove the trailing slash, uploading an object will result in an | ||
// error: HttpStatusCode.NotFound. The HTTP request appears incorrect. The | ||
// container logs indicate the presence of an extra slash: `PUT //upload/storage/v1`. | ||
startupScript.Append("-external-url " + new UriBuilder(Uri.UriSchemeHttp, container.Hostname, container.GetMappedPublicPort(FakeGcsServerPort)).ToString().Trim('/')); | ||
return container.CopyAsync(Encoding.Default.GetBytes(startupScript.ToString()), StartupScriptFilePath, Unix.FileMode755, ct); | ||
}); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override FakeGcsServerBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
{ | ||
return Merge(DockerResourceConfiguration, new FakeGcsServerConfiguration(resourceConfiguration)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override FakeGcsServerBuilder Clone(IContainerConfiguration resourceConfiguration) | ||
{ | ||
return Merge(DockerResourceConfiguration, new FakeGcsServerConfiguration(resourceConfiguration)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override FakeGcsServerBuilder Merge(FakeGcsServerConfiguration oldValue, FakeGcsServerConfiguration newValue) | ||
{ | ||
return new FakeGcsServerBuilder(new FakeGcsServerConfiguration(oldValue, newValue)); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Testcontainers.FakeGcsServer/FakeGcsServerConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace Testcontainers.FakeGcsServer; | ||
|
||
/// <inheritdoc cref="ContainerConfiguration" /> | ||
[PublicAPI] | ||
public sealed class FakeGcsServerConfiguration : ContainerConfiguration | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FakeGcsServerConfiguration" /> class. | ||
/// </summary> | ||
public FakeGcsServerConfiguration() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FakeGcsServerConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public FakeGcsServerConfiguration(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="FakeGcsServerConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public FakeGcsServerConfiguration(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="FakeGcsServerConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public FakeGcsServerConfiguration(FakeGcsServerConfiguration resourceConfiguration) | ||
: this(new FakeGcsServerConfiguration(), resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FakeGcsServerConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="oldValue">The old Docker resource configuration.</param> | ||
/// <param name="newValue">The new Docker resource configuration.</param> | ||
public FakeGcsServerConfiguration(FakeGcsServerConfiguration oldValue, FakeGcsServerConfiguration newValue) | ||
: base(oldValue, newValue) | ||
{ | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Testcontainers.FakeGcsServer/FakeGcsServerContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace Testcontainers.FakeGcsServer; | ||
|
||
/// <inheritdoc cref="DockerContainer" /> | ||
[PublicAPI] | ||
public sealed class FakeGcsServerContainer : DockerContainer | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FakeGcsServerContainer" /> class. | ||
/// </summary> | ||
/// <param name="configuration">The container configuration.</param> | ||
/// <param name="logger">The logger.</param> | ||
public FakeGcsServerContainer(FakeGcsServerConfiguration configuration, ILogger logger) | ||
: base(configuration, logger) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets the FakeGcsServer connection string. | ||
/// </summary> | ||
/// <returns>The FakeGcsServer connection string.</returns> | ||
public string GetConnectionString() | ||
{ | ||
return new UriBuilder(Uri.UriSchemeHttp, Hostname, GetMappedPublicPort(FakeGcsServerBuilder.FakeGcsServerPort), "/storage/v1/").ToString(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Testcontainers.FakeGcsServer/Testcontainers.FakeGcsServer.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/> | ||
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" PrivateAssets="All"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(SolutionDir)src/Testcontainers/Testcontainers.csproj"/> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
global using System; | ||
global using System.Text; | ||
global using Docker.DotNet.Models; | ||
global using DotNet.Testcontainers.Builders; | ||
global using DotNet.Testcontainers.Configurations; | ||
global using DotNet.Testcontainers.Containers; | ||
global using JetBrains.Annotations; | ||
global using Microsoft.Extensions.Logging; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
root = true |
54 changes: 54 additions & 0 deletions
54
tests/Testcontainers.FakeGcsServer.Tests/FakeGcsServerContainerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
namespace Testcontainers.FakeGcsServer; | ||
|
||
public sealed class FakeGcsServerContainerTest : IAsyncLifetime | ||
{ | ||
private readonly FakeGcsServerContainer _fakeGcsServerContainer = new FakeGcsServerBuilder().Build(); | ||
|
||
public Task InitializeAsync() | ||
{ | ||
return _fakeGcsServerContainer.StartAsync(); | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
return _fakeGcsServerContainer.DisposeAsync().AsTask(); | ||
} | ||
|
||
[Fact] | ||
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))] | ||
public async Task DownloadObjectReturnsUploadObject() | ||
{ | ||
// Given | ||
const string helloWorld = "Hello, World!"; | ||
|
||
using var writeStream = new MemoryStream(Encoding.Default.GetBytes(helloWorld)); | ||
|
||
using var readStream = new MemoryStream(); | ||
|
||
var project = Guid.NewGuid().ToString("D"); | ||
|
||
var bucket = Guid.NewGuid().ToString("D"); | ||
|
||
var fileName = Path.GetRandomFileName(); | ||
|
||
var storageClientBuilder = new StorageClientBuilder(); | ||
storageClientBuilder.UnauthenticatedAccess = true; | ||
storageClientBuilder.BaseUri = _fakeGcsServerContainer.GetConnectionString(); | ||
|
||
// When | ||
var client = await storageClientBuilder.BuildAsync() | ||
.ConfigureAwait(false); | ||
|
||
_ = await client.CreateBucketAsync(project, bucket) | ||
.ConfigureAwait(false); | ||
|
||
_ = await client.UploadObjectAsync(bucket, fileName, "text/plain", writeStream) | ||
.ConfigureAwait(false); | ||
|
||
_ = await client.DownloadObjectAsync(bucket, fileName, readStream) | ||
.ConfigureAwait(false); | ||
|
||
// Then | ||
Assert.Equal(helloWorld, Encoding.Default.GetString(readStream.ToArray())); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/Testcontainers.FakeGcsServer.Tests/Testcontainers.FakeGcsServer.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2"/> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0"/> | ||
<PackageReference Include="xunit" Version="2.5.0"/> | ||
<PackageReference Include="Google.Cloud.Storage.V1" Version="4.6.0"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(SolutionDir)src/Testcontainers.FakeGcsServer/Testcontainers.FakeGcsServer.csproj"/> | ||
<ProjectReference Include="$(SolutionDir)tests/Testcontainers.Commons/Testcontainers.Commons.csproj"/> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
global using System; | ||
global using System.IO; | ||
global using System.Text; | ||
global using System.Threading.Tasks; | ||
global using DotNet.Testcontainers.Commons; | ||
global using Google.Cloud.Storage.V1; | ||
global using Xunit; |