Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ClamAV module #1172

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
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
198 changes: 106 additions & 92 deletions Testcontainers.sln

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Testcontainers.ClamAv/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
67 changes: 67 additions & 0 deletions src/Testcontainers.ClamAv/ClamAvBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

namespace Testcontainers.ClamAv;

/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class ClamAvBuilder : ContainerBuilder<ClamAvBuilder, ClamAvContainer, ClamAvConfiguration>
{
public const string ClamAvImage = "clamav/clamav:1.0.6";

public const ushort ClamAvPort = 3310;

/// <summary>
/// Initializes a new instance of the <see cref="RedisBuilder" /> class.
/// </summary>
public ClamAvBuilder()
: this(new ClamAvConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}

/// <summary>
/// Initializes a new instance of the <see cref="RedisBuilder" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
private ClamAvBuilder(ClamAvConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}

/// <inheritdoc />
protected override ClamAvConfiguration DockerResourceConfiguration { get; }

public override ClamAvContainer Build()
{
Validate();
return new ClamAvContainer(DockerResourceConfiguration);
}

protected override ClamAvBuilder Init()
{
return base.Init()
.WithImage(ClamAvImage)
.WithPortBinding(ClamAvPort, true)
.WithEnvironment("CLAMAV_NO_CLAMD", "false")
.WithEnvironment("CLAMAV_NO_FRESHCLAMD", "true")
.WithEnvironment("CLAMAV_NO_MILTERD", "true")
.WithEnvironment("CLAMD_STARTUP_TIMEOUT", "1800")
.WithEnvironment("FRESHCLAM_CHECKS", "1")
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("socket found, clamd started."));
}

protected override ClamAvBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new ClamAvConfiguration(resourceConfiguration));
}

protected override ClamAvBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new ClamAvConfiguration(resourceConfiguration));
}

protected override ClamAvBuilder Merge(ClamAvConfiguration oldValue, ClamAvConfiguration newValue)
{
return new ClamAvBuilder(new ClamAvConfiguration(oldValue, newValue));
}
}
53 changes: 53 additions & 0 deletions src/Testcontainers.ClamAv/ClamAvConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Testcontainers.ClamAv;

/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class ClamAvConfiguration : ContainerConfiguration
{
/// <summary>
/// Initializes a new instance of the <see cref="ClamAvConfiguration" /> class.
/// </summary>
public ClamAvConfiguration()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ClamAvConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public ClamAvConfiguration(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="ClamAvConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public ClamAvConfiguration(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="ClamAvConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public ClamAvConfiguration(ClamAvConfiguration resourceConfiguration)
: this(new ClamAvConfiguration(), resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="ClamAvConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public ClamAvConfiguration(ClamAvConfiguration oldValue, ClamAvConfiguration newValue)
: base(oldValue, newValue)
{
}
}
21 changes: 21 additions & 0 deletions src/Testcontainers.ClamAv/ClamAvContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Net;

namespace Testcontainers.ClamAv;

[PublicAPI]
public sealed class ClamAvContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="RedisContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
public ClamAvContainer(ClamAvConfiguration configuration)
: base(configuration)
{
}

public DnsEndPoint GetDnsEndPoint()
{
return new DnsEndPoint(Hostname, GetMappedPublicPort(ClamAvBuilder.ClamAvPort));
}
}
12 changes: 12 additions & 0 deletions src/Testcontainers.ClamAv/Testcontainers.ClamAv.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0;netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" VersionOverride="2023.3.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Testcontainers/Testcontainers.csproj" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions src/Testcontainers.ClamAv/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
global using System;
global using System.IO;
global using System.Text;
global using System.Threading;
global using System.Threading.Tasks;
global using Docker.DotNet.Models;
global using DotNet.Testcontainers.Builders;
global using DotNet.Testcontainers.Configurations;
global using DotNet.Testcontainers.Containers;
global using JetBrains.Annotations;
1 change: 1 addition & 0 deletions tests/Testcontainers.ClamAv.Tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
57 changes: 57 additions & 0 deletions tests/Testcontainers.ClamAv.Tests/ClamAvContainerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Net.Sockets;
using System.Text;

namespace Testcontainers.ClamAv.Tests;

public sealed class ClamAvContainerTest : IAsyncLifetime
{
private readonly ClamAvContainer _clamAvContainer = new ClamAvBuilder().Build();

public Task InitializeAsync()
{
return _clamAvContainer.StartAsync();
}

public Task DisposeAsync()
{
return _clamAvContainer.DisposeAsync().AsTask();
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public void PingCommandReturnsPong()
{
string response = RunCommand("zPING\0"u8.ToArray());
Assert.Equal("PONG\0", response);
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public void VersionCommandReturnsVersionString()
{
string response = RunCommand("zVERSION\0"u8.ToArray());

// ClamAV 1.0.6/27266/Sun May 5 08:28:11 2024
// where 27266 is the signatures version and it is followed by the date of the signatures
Assert.StartsWith("ClamAV ", response);
}

private string RunCommand(byte[] command)
{
var endpoint = _clamAvContainer.GetDnsEndPoint();
using TcpClient client = new(endpoint.Host, endpoint.Port);
NetworkStream stream = client.GetStream();

// send command
stream.Write(command);

// read response
byte[] buffer = new byte[1024];
int length = stream.Read(buffer, 0, buffer.Length);

Assert.NotEqual(0, length);
string response = Encoding.UTF8.GetString(buffer, 0, length);

return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Testcontainers.ClamAv\Testcontainers.ClamAv.csproj" />
<ProjectReference Include="..\Testcontainers.Commons\Testcontainers.Commons.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions tests/Testcontainers.ClamAv.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
global using System;
global using System.Threading.Tasks;
global using DotNet.Testcontainers.Commons;
global using JetBrains.Annotations;
global using Xunit;
Loading