Skip to content

Commit

Permalink
feat(testcontainers#421) Added AzuriteTestcontainer - added binding w…
Browse files Browse the repository at this point in the history
…orkspace location to host
  • Loading branch information
vlaskal committed Aug 7, 2022
1 parent 96cadd5 commit 087ac7a
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace DotNet.Testcontainers.Builders
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using DotNet.Testcontainers.Configurations.Modules.Databases;
using DotNet.Testcontainers.Containers;
Expand Down Expand Up @@ -40,22 +41,67 @@ public static ITestcontainersBuilder<AzuriteTestcontainer> WithAzurite(this ITes
builder = builder.WithPortBinding(configuration.TablePort, AzuriteTestcontainerConfiguration.DefaultTablePort);
}

if (!string.IsNullOrWhiteSpace(configuration.Location))
{
builder = builder.WithBindMount(configuration.Location, AzuriteTestcontainerConfiguration.DefaultLocation);
}

builder = builder
.WithCommand(GetMainCommand(configuration))
.WithCommand(GetServiceEndpointArgs(configuration))
.WithCommand(GetWorkspaceLocation());

return builder;
}

private static string[] GetWorkspaceLocation()
{
return new[] { "--location", AzuriteTestcontainerConfiguration.DefaultLocation };
}

private static string[] GetServiceEndpointArgs(AzuriteTestcontainerConfiguration configuration)
{
var args = new List<string>();

if (configuration.RunBlobOnly || configuration.RunAllServices)
{
args.Add("--blobHost");
args.Add(AzuriteTestcontainerConfiguration.DefaultBlobEndpoint);
}

if (configuration.RunQueueOnly || configuration.RunAllServices)
{
args.Add("--queueHost");
args.Add(AzuriteTestcontainerConfiguration.DefaultQueueEndpoint);
}

if (configuration.RunTableOnly || configuration.RunAllServices)
{
args.Add("--tableHost");
args.Add(AzuriteTestcontainerConfiguration.DefaultTableEndpoint);
}

return args.Count > 0 ? args.ToArray() : null;
}

private static string GetMainCommand(AzuriteTestcontainerConfiguration configuration)
{
if (configuration.RunBlobOnly)
{
builder = builder.WithCommand("azurite-blob", "--blobHost", AzuriteTestcontainerConfiguration.DefaultBlobEndpoint);
return "azurite-blob";
}

if (configuration.RunQueueOnly)
{
builder = builder.WithCommand("azurite-queue", "--queueHost", AzuriteTestcontainerConfiguration.DefaultQueueEndpoint);
return "azurite-queue";
}

if (configuration.RunTableOnly)
{
builder = builder.WithCommand("azurite-table", "--tableHost", AzuriteTestcontainerConfiguration.DefaultTableEndpoint);
return "azurite-table";
}

return builder;
return "azurite";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,27 @@ public class AzuriteTestcontainerConfiguration : IDisposable
/// <summary>
/// Default Blob service listening port. Default is 10000.
/// </summary>
[PublicAPI]
public const int DefaultBlobPort = 10000;

/// <summary>
/// Default Queue service listening port. Default is 10001.
/// </summary>
[PublicAPI]
public const int DefaultQueuePort = 10001;

/// <summary>
/// Default Table service listening port. Default 10002.
/// Default Table service listening port. Default is 10002.
/// </summary>
[PublicAPI]
public const int DefaultTablePort = 10002;

/// <summary>
/// Default Workspace location folder path. Default is /data.
/// </summary>
[PublicAPI]
public const string DefaultLocation = "/data";

internal const string DefaultBlobEndpoint = "0.0.0.0";
internal const string DefaultQueueEndpoint = "0.0.0.0";
internal const string DefaultTableEndpoint = "0.0.0.0";
Expand All @@ -31,6 +40,7 @@ public class AzuriteTestcontainerConfiguration : IDisposable
private bool runBlobOnly;
private bool runQueueOnly;
private bool runTableOnly;
private string location;

/// <summary>
/// Initializes a new instance of the <see cref="AzuriteTestcontainerConfiguration" /> class with default Azurite image.
Expand Down Expand Up @@ -151,6 +161,25 @@ public bool RunTableOnly
[PublicAPI]
public bool RunAllServices => !this.RunBlobOnly && !this.RunQueueOnly && !this.RunTableOnly;

/// <summary>
/// Gets or sets workspace location path.
/// </summary>
/// <remarks>
/// Corresponds to the default workspace location of the hosted service.
/// </remarks>
[PublicAPI]
public string Location
{
get => this.location;
set
{
Guard.Argument(value, nameof(this.Location))
.NotNull()
.NotEmpty();
this.location = value;
}
}

/// <summary>
/// Gets the environment configuration.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace DotNet.Testcontainers.Tests.Unit
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Azure;
using Azure.Data.Tables;
Expand All @@ -14,7 +16,11 @@ namespace DotNet.Testcontainers.Tests.Unit
[Collection(nameof(Testcontainers))]
public sealed class AzuriteTestcontainerTest : IAsyncLifetime
{
// We cannot use `Path.GetTempPath()` on macOS, see: https://github.com/common-workflow-language/cwltool/issues/328.
private static readonly string TempDir = Environment.GetEnvironmentVariable("AGENT_TEMPDIRECTORY") ?? Directory.GetCurrentDirectory();

private AzuriteTestcontainer container;
private string testDir;

public Task InitializeAsync()
{
Expand All @@ -23,6 +29,12 @@ public Task InitializeAsync()

public Task DisposeAsync()
{
if (this.testDir != null && Directory.Exists(this.testDir))
{
Directory.Delete(this.testDir, true);
this.testDir = null;
}

return this.container == null ? Task.CompletedTask : this.container.DisposeAsync().AsTask();
}

Expand All @@ -40,11 +52,16 @@ public async Task ConnectionToAllServicesEstablished()
var blobProperties = await blobServiceClient.GetPropertiesAsync();
var queueProperties = await queueServiceClient.GetPropertiesAsync();
var tableServiceProperties = await tableServiceClient.GetPropertiesAsync();
var workspaceCommandResult = await this.container.ExecAsync(new List<string> {"ls", AzuriteTestcontainerConfiguration.DefaultLocation});

// Then
Assert.True(blobProperties.GetRawResponse().Status is >= 200 and <= 299);
Assert.True(queueProperties.GetRawResponse().Status is >= 200 and <= 299);
Assert.True(tableServiceProperties.GetRawResponse().Status is >= 200 and <= 299);
Assert.True(workspaceCommandResult.ExitCode == 0);
Assert.Contains(GetDataFilename("blob"), workspaceCommandResult.Stdout);
Assert.Contains(GetDataFilename("queue"), workspaceCommandResult.Stdout);
Assert.Contains(GetDataFilename("table"), workspaceCommandResult.Stdout);
}

[Fact]
Expand All @@ -62,11 +79,16 @@ await this.StartAzuriteContainer(config =>

// When
var blobProperties = await blobServiceClient.GetPropertiesAsync();
var workspaceCommandResult = await this.container.ExecAsync(new List<string> { "ls", AzuriteTestcontainerConfiguration.DefaultLocation });

// Then
Assert.True(blobProperties.GetRawResponse().Status is >= 200 and <= 299);
await Assert.ThrowsAsync<RequestFailedException>(() => queueServiceClient.GetPropertiesAsync());
await Assert.ThrowsAsync<RequestFailedException>(() => tableServiceClient.GetPropertiesAsync());
Assert.True(workspaceCommandResult.ExitCode == 0);
Assert.Contains(GetDataFilename("blob"), workspaceCommandResult.Stdout);
Assert.DoesNotContain(GetDataFilename("queue"), workspaceCommandResult.Stdout);
Assert.DoesNotContain(GetDataFilename("table"), workspaceCommandResult.Stdout);
}

[Fact]
Expand All @@ -84,11 +106,16 @@ await this.StartAzuriteContainer(config =>

// When
var queueProperties = await queueServiceClient.GetPropertiesAsync();
var workspaceCommandResult = await this.container.ExecAsync(new List<string> { "ls", AzuriteTestcontainerConfiguration.DefaultLocation });

// Then
Assert.True(queueProperties.GetRawResponse().Status is >= 200 and <= 299);
await Assert.ThrowsAsync<RequestFailedException>(() => blobServiceClient.GetPropertiesAsync());
await Assert.ThrowsAsync<RequestFailedException>(() => tableServiceClient.GetPropertiesAsync());
Assert.True(workspaceCommandResult.ExitCode == 0);
Assert.DoesNotContain(GetDataFilename("blob"), workspaceCommandResult.Stdout);
Assert.Contains(GetDataFilename("queue"), workspaceCommandResult.Stdout);
Assert.DoesNotContain(GetDataFilename("table"), workspaceCommandResult.Stdout);
}

[Fact]
Expand All @@ -106,11 +133,36 @@ await this.StartAzuriteContainer(config =>

// When
var tableServiceProperties = await tableServiceClient.GetPropertiesAsync();
var workspaceCommandResult = await this.container.ExecAsync(new List<string> { "ls", AzuriteTestcontainerConfiguration.DefaultLocation });

// Then
Assert.True(tableServiceProperties.GetRawResponse().Status is >= 200 and <= 299);
await Assert.ThrowsAsync<RequestFailedException>(() => blobServiceClient.GetPropertiesAsync());
await Assert.ThrowsAsync<RequestFailedException>(() => queueServiceClient.GetPropertiesAsync());
Assert.True(workspaceCommandResult.ExitCode == 0);
Assert.DoesNotContain(GetDataFilename("blob"), workspaceCommandResult.Stdout);
Assert.DoesNotContain(GetDataFilename("queue"), workspaceCommandResult.Stdout);
Assert.Contains(GetDataFilename("table"), workspaceCommandResult.Stdout);
}

[Fact]
public async Task BindedLocationPathShouldContainsDataFiles()
{
// Given
this.testDir = Path.Combine(TempDir, Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(this.testDir);
await this.StartAzuriteContainer(config =>
{
config.Location = this.testDir;
});

// When

// Then
var files = Directory.GetFiles(this.testDir);
Assert.Contains(files, file => file.EndsWith(GetDataFilename("blob"), StringComparison.InvariantCultureIgnoreCase));
Assert.Contains(files, file => file.EndsWith(GetDataFilename("queue"), StringComparison.InvariantCultureIgnoreCase));
Assert.Contains(files, file => file.EndsWith(GetDataFilename("table"), StringComparison.InvariantCultureIgnoreCase));
}

private async Task StartAzuriteContainer(Action<AzuriteTestcontainerConfiguration> configure = null)
Expand All @@ -122,5 +174,10 @@ private async Task StartAzuriteContainer(Action<AzuriteTestcontainerConfiguratio
.Build();
await this.container.StartAsync();
}

private static string GetDataFilename(string service)
{
return $"__azurite_db_{service}__.json";
}
}
}

0 comments on commit 087ac7a

Please sign in to comment.