diff --git a/src/Core.Tests/Core.Tests.csproj b/src/Core.Tests/Core.Tests.csproj
index 4829dfbf..a96af5d2 100644
--- a/src/Core.Tests/Core.Tests.csproj
+++ b/src/Core.Tests/Core.Tests.csproj
@@ -21,8 +21,8 @@
Always
-
- Always
+
+ Always
Always
diff --git a/src/Core.Tests/GenericContainerWithOnOptionsInitializedTests.cs b/src/Core.Tests/GenericContainerWithOnOptionsInitializedTests.cs
new file mode 100644
index 00000000..525428ea
--- /dev/null
+++ b/src/Core.Tests/GenericContainerWithOnOptionsInitializedTests.cs
@@ -0,0 +1,82 @@
+using System;
+using System.Threading.Tasks;
+using FluentAssertions;
+using Xunit;
+
+namespace Squadron
+{
+ public class GenericContainerWithOnOptionsInitializedTests
+ {
+ private const string DefaultContainerName = "DefaultContainerName";
+
+ [Fact]
+ public async Task InitializeAsync_OptionsOverride_NameShouldBeOverridenValue()
+ {
+ // arrange
+ const string newContainerName = "SomeName";
+ var resource = new GenericNamedResource(newContainerName);
+
+ // act
+ await resource.InitializeAsync();
+
+ // assert
+ resource.Instance.Name.Should().Contain(newContainerName.ToLower());
+
+ // cleanup
+ await resource.DisposeAsync();
+ }
+
+ [Fact]
+ public async Task InitializeAsync_WithoutOverride_NameShouldBeDefault()
+ {
+ // arrange
+ var resource = new GenericNamedResource();
+
+ // act
+ await resource.InitializeAsync();
+
+ // assert
+ resource.Instance.Name.Should().Contain(DefaultContainerName.ToLower());
+
+ // cleanup
+ await resource.DisposeAsync();
+ }
+
+ class GenericNamedResource: GenericContainerResource
+ {
+ private readonly string? _containerName;
+
+ public GenericNamedResource(string? containerName = null)
+ {
+ _containerName = containerName;
+ }
+
+ protected override void OnOptionsInitialized(GenericNamedOptions options)
+ {
+ if (_containerName != null)
+ {
+ options.SetContainerName(_containerName);
+ }
+ }
+ }
+
+ class GenericNamedOptions : GenericContainerOptions
+ {
+ private string _containerName = DefaultContainerName;
+
+ public void SetContainerName(string containerName)
+ {
+ _containerName = containerName;
+ }
+
+ public override void Configure(ContainerResourceBuilder builder)
+ {
+ base.Configure(builder);
+ builder
+ .Name(_containerName)
+ .Image("sickp/alpine-sshd:latest")
+ .InternalPort(22);
+ }
+ }
+ }
+}
diff --git a/src/Core/ContainerResource.cs b/src/Core/ContainerResource.cs
index e17db693..f4069191 100644
--- a/src/Core/ContainerResource.cs
+++ b/src/Core/ContainerResource.cs
@@ -6,29 +6,29 @@
namespace Squadron
{
- ///
- /// Base resource for container based resources
- ///
- /// The type of the options.
+ ///
+ /// Base resource for container based resources
+ ///
+ /// The type of the options.
public class ContainerResource
where TOptions : ContainerResourceOptions, new()
{
- ///
- /// Gets or sets the continer settings.
- ///
- ///
- /// The settings.
- ///
+ ///
+ /// Gets or sets the continer settings.
+ ///
+ ///
+ /// The settings.
+ ///
protected ContainerResourceSettings Settings { get; set; }
- ///
- /// The container manager
- ///
+ ///
+ /// The container manager
+ ///
protected IDockerContainerManager Manager = null;
- ///
- /// The container initializer
- ///
+ ///
+ /// The container initializer
+ ///
protected ContainerInitializer Initializer = null;
///
@@ -42,12 +42,13 @@ public class ContainerResource
public ContainerInstance Instance => Manager.Instance;
- ///
- /// Initializes the resources
- ///
+ ///
+ /// Initializes the resources
+ ///
public async virtual Task InitializeAsync()
{
ResourceOptions = new TOptions();
+ OnOptionsInitialized(ResourceOptions);
var builder = ContainerResourceBuilder.New();
ResourceOptions.Configure(builder);
AddNetworksToBuilder(builder);
@@ -73,7 +74,7 @@ private void AddNetworksToBuilder(ContainerResourceBuilder builder)
}
private void SetComposeVariables()
- {
+ {
if (_composeVariables != null)
{
foreach (var envVar in _composeVariables)
@@ -108,13 +109,19 @@ private void ValidateSettings(ContainerResourceSettings settings)
throw new ArgumentException("Can not be 0", nameof(settings.InternalPort));
}
- ///
- /// Called when after settings are build
- ///
- /// The settings.
+ ///
+ /// Called when after settings are build
+ ///
+ /// The settings.
protected virtual void OnSettingsBuilded(ContainerResourceSettings settings)
{ }
+ ///
+ /// Called after the ResourceOptions were initialized
+ ///
+ /// The options.
+ protected virtual void OnOptionsInitialized(TOptions options)
+ { }
public async Task PauseContainer(TimeSpan pauseTime)
{}
@@ -129,10 +136,9 @@ public virtual Dictionary GetComposeExports()
};
}
-
- ///
- /// Cleans up the resource
- ///
+ ///
+ /// Cleans up the resource
+ ///
public async Task DisposeAsync()
{
try