From 087ac7a3f6965713ee04e4a24b2abdebb3dd4d5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vlastimil=20Kalu=C5=BEa?= Date: Sun, 7 Aug 2022 21:09:55 +0200 Subject: [PATCH] feat(#421) Added AzuriteTestcontainer - added binding workspace location to host --- .../TestcontainersBuilderAzuriteExtension.cs | 54 ++++++++++++++++-- .../AzuriteTestcontainerConfiguration.cs | 31 +++++++++- .../Databases/AzuriteTestcontainerTest.cs | 57 +++++++++++++++++++ 3 files changed, 137 insertions(+), 5 deletions(-) diff --git a/src/Testcontainers/Builders/TestcontainersBuilderAzuriteExtension.cs b/src/Testcontainers/Builders/TestcontainersBuilderAzuriteExtension.cs index 23968edab..d07b256f0 100644 --- a/src/Testcontainers/Builders/TestcontainersBuilderAzuriteExtension.cs +++ b/src/Testcontainers/Builders/TestcontainersBuilderAzuriteExtension.cs @@ -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; @@ -40,22 +41,67 @@ public static ITestcontainersBuilder 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(); + + 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"; } } } diff --git a/src/Testcontainers/Configurations/Modules/Databases/AzuriteTestcontainerConfiguration.cs b/src/Testcontainers/Configurations/Modules/Databases/AzuriteTestcontainerConfiguration.cs index eaf40578f..c6817d8f6 100644 --- a/src/Testcontainers/Configurations/Modules/Databases/AzuriteTestcontainerConfiguration.cs +++ b/src/Testcontainers/Configurations/Modules/Databases/AzuriteTestcontainerConfiguration.cs @@ -10,18 +10,27 @@ public class AzuriteTestcontainerConfiguration : IDisposable /// /// Default Blob service listening port. Default is 10000. /// + [PublicAPI] public const int DefaultBlobPort = 10000; /// /// Default Queue service listening port. Default is 10001. /// + [PublicAPI] public const int DefaultQueuePort = 10001; /// - /// Default Table service listening port. Default 10002. + /// Default Table service listening port. Default is 10002. /// + [PublicAPI] public const int DefaultTablePort = 10002; + /// + /// Default Workspace location folder path. Default is /data. + /// + [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"; @@ -31,6 +40,7 @@ public class AzuriteTestcontainerConfiguration : IDisposable private bool runBlobOnly; private bool runQueueOnly; private bool runTableOnly; + private string location; /// /// Initializes a new instance of the class with default Azurite image. @@ -151,6 +161,25 @@ public bool RunTableOnly [PublicAPI] public bool RunAllServices => !this.RunBlobOnly && !this.RunQueueOnly && !this.RunTableOnly; + /// + /// Gets or sets workspace location path. + /// + /// + /// Corresponds to the default workspace location of the hosted service. + /// + [PublicAPI] + public string Location + { + get => this.location; + set + { + Guard.Argument(value, nameof(this.Location)) + .NotNull() + .NotEmpty(); + this.location = value; + } + } + /// /// Gets the environment configuration. /// diff --git a/tests/Testcontainers.Tests/Unit/Containers/Unix/Modules/Databases/AzuriteTestcontainerTest.cs b/tests/Testcontainers.Tests/Unit/Containers/Unix/Modules/Databases/AzuriteTestcontainerTest.cs index 8640e5157..ce7e217f4 100644 --- a/tests/Testcontainers.Tests/Unit/Containers/Unix/Modules/Databases/AzuriteTestcontainerTest.cs +++ b/tests/Testcontainers.Tests/Unit/Containers/Unix/Modules/Databases/AzuriteTestcontainerTest.cs @@ -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; @@ -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() { @@ -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(); } @@ -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 {"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] @@ -62,11 +79,16 @@ await this.StartAzuriteContainer(config => // When var blobProperties = await blobServiceClient.GetPropertiesAsync(); + var workspaceCommandResult = await this.container.ExecAsync(new List { "ls", AzuriteTestcontainerConfiguration.DefaultLocation }); // Then Assert.True(blobProperties.GetRawResponse().Status is >= 200 and <= 299); await Assert.ThrowsAsync(() => queueServiceClient.GetPropertiesAsync()); await Assert.ThrowsAsync(() => 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] @@ -84,11 +106,16 @@ await this.StartAzuriteContainer(config => // When var queueProperties = await queueServiceClient.GetPropertiesAsync(); + var workspaceCommandResult = await this.container.ExecAsync(new List { "ls", AzuriteTestcontainerConfiguration.DefaultLocation }); // Then Assert.True(queueProperties.GetRawResponse().Status is >= 200 and <= 299); await Assert.ThrowsAsync(() => blobServiceClient.GetPropertiesAsync()); await Assert.ThrowsAsync(() => 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] @@ -106,11 +133,36 @@ await this.StartAzuriteContainer(config => // When var tableServiceProperties = await tableServiceClient.GetPropertiesAsync(); + var workspaceCommandResult = await this.container.ExecAsync(new List { "ls", AzuriteTestcontainerConfiguration.DefaultLocation }); // Then Assert.True(tableServiceProperties.GetRawResponse().Status is >= 200 and <= 299); await Assert.ThrowsAsync(() => blobServiceClient.GetPropertiesAsync()); await Assert.ThrowsAsync(() => 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 configure = null) @@ -122,5 +174,10 @@ private async Task StartAzuriteContainer(Action