Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Core.Tests/Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<None Update="__resources__\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="appsettings.user.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<None Update="appsettings.user.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down
82 changes: 82 additions & 0 deletions src/Core.Tests/GenericContainerWithOnOptionsInitializedTests.cs
Original file line number Diff line number Diff line change
@@ -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<GenericNamedOptions>
{
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);
}
}
}
}
62 changes: 34 additions & 28 deletions src/Core/ContainerResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@

namespace Squadron
{
/// <summary>
/// Base resource for container based resources
/// </summary>
/// <typeparam name="TOptions">The type of the options.</typeparam>
/// <summary>
/// Base resource for container based resources
/// </summary>
/// <typeparam name="TOptions">The type of the options.</typeparam>
public class ContainerResource<TOptions>
where TOptions : ContainerResourceOptions, new()
{
/// <summary>
/// Gets or sets the continer settings.
/// </summary>
/// <value>
/// The settings.
/// </value>
/// <summary>
/// Gets or sets the continer settings.
/// </summary>
/// <value>
/// The settings.
/// </value>
protected ContainerResourceSettings Settings { get; set; }

/// <summary>
/// The container manager
/// </summary>
/// <summary>
/// The container manager
/// </summary>
protected IDockerContainerManager Manager = null;

/// <summary>
/// The container initializer
/// </summary>
/// <summary>
/// The container initializer
/// </summary>
protected ContainerInitializer Initializer = null;

/// <summary>
Expand All @@ -42,12 +42,13 @@ public class ContainerResource<TOptions>

public ContainerInstance Instance => Manager.Instance;

/// <summary>
/// Initializes the resources
/// </summary>
/// <summary>
/// Initializes the resources
/// </summary>
public async virtual Task InitializeAsync()
{
ResourceOptions = new TOptions();
OnOptionsInitialized(ResourceOptions);
var builder = ContainerResourceBuilder.New();
ResourceOptions.Configure(builder);
AddNetworksToBuilder(builder);
Expand All @@ -73,7 +74,7 @@ private void AddNetworksToBuilder(ContainerResourceBuilder builder)
}

private void SetComposeVariables()
{
{
if (_composeVariables != null)
{
foreach (var envVar in _composeVariables)
Expand Down Expand Up @@ -108,13 +109,19 @@ private void ValidateSettings(ContainerResourceSettings settings)
throw new ArgumentException("Can not be 0", nameof(settings.InternalPort));
}

/// <summary>
/// Called when after settings are build
/// </summary>
/// <param name="settings">The settings.</param>
/// <summary>
/// Called when after settings are build
/// </summary>
/// <param name="settings">The settings.</param>
protected virtual void OnSettingsBuilded(ContainerResourceSettings settings)
{ }

/// <summary>
/// Called after the ResourceOptions were initialized
/// </summary>
/// <param name="options">The options.</param>
protected virtual void OnOptionsInitialized(TOptions options)
{ }

public async Task PauseContainer(TimeSpan pauseTime)
{}
Expand All @@ -129,10 +136,9 @@ public virtual Dictionary<string, string> GetComposeExports()
};
}


/// <summary>
/// Cleans up the resource
/// </summary>
/// <summary>
/// Cleans up the resource
/// </summary>
public async Task DisposeAsync()
{
try
Expand Down