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

Add qdrant support #981

Closed
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
147 changes: 82 additions & 65 deletions Testcontainers.sln

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dotnet.defaultSolution": "src.sln"
}
1 change: 1 addition & 0 deletions src/Testcontainers.Qdrant/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
139 changes: 139 additions & 0 deletions src/Testcontainers.Qdrant/QdrantBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
namespace Testcontainers.Qdrant;
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class QdrantBuilder : ContainerBuilder<QdrantBuilder, QdrantContainer, QdrantConfiguration>
{
public const string QdrantImage = "qdrant/qdrant:latest";

public const ushort QdrantPort = 6333;

public const string QdrantContainerName = "qdrant";

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

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

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


/// <summary>
/// Sets the Volume Mapping.
/// <a href="https://docs.docker.com/storage/volumes/">Docker Volume</a>
/// <a href="https://docs.docker.com/storage/bind-mounts/">Docker Bind Mount</a>
/// <a href="https://hub.docker.com/r/qdrant/qdrant/">Qdrant</a>
/// </summary>
/// <param name="hostPath">The mapped Host Path .</param>
/// <param name="containerPath">The mapped Container Path.</param>
/// <param name="accessMode">The AccessMode.</param>
/// <returns>A configured instance of <see cref="QdrantBuilder" />.</returns>
public QdrantBuilder WithVolume(string hostPath, string containerPath, AccessMode accessMode)
{
return
Merge(DockerResourceConfiguration,
new QdrantConfiguration(hostPath: hostPath, containerPath: containerPath, accessMode: accessMode))
;
}

public QdrantBuilder WithContainerName(string containerName)
{
return
Merge(DockerResourceConfiguration,
new QdrantConfiguration(containerName: containerName))
;
}

/// <inheritdoc />
public override QdrantContainer Build()
{
Validate();
return new QdrantContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
}

/// <inheritdoc />
protected override QdrantBuilder Init()
{
var containerBuilder = base.Init()
.WithImage(QdrantImage)
.WithPortBinding(QdrantPort, true)
.BuildWithContainerName(DockerResourceConfiguration.ContainerName)
.BuildWithVolume(DockerResourceConfiguration.HostPath, DockerResourceConfiguration.ContainerPath, DockerResourceConfiguration.AccessMode);
containerBuilder = containerBuilder.WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new WaitUntil()));
return containerBuilder;
}

/// <inheritdoc />
protected override QdrantBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new QdrantConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override QdrantBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new QdrantConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override QdrantBuilder Merge(QdrantConfiguration oldValue, QdrantConfiguration newValue)
{
return new QdrantBuilder(new QdrantConfiguration(oldValue, newValue));
}

/// <inheritdoc cref="IWaitUntil" />
private sealed class WaitUntil : IWaitUntil
{
private static readonly string[] LineEndings = { "\r\n", "\n" };

/// <inheritdoc />
public async Task<bool> UntilAsync(IContainer container)
{
var (stdout, stderr) = await container.GetLogsAsync(timestampsEnabled: false)
.ConfigureAwait(false);

var detectedLines = Array.Empty<string>()
.Concat(stdout.Split(LineEndings, StringSplitOptions.RemoveEmptyEntries))
.Concat(stderr.Split(LineEndings, StringSplitOptions.RemoveEmptyEntries))
.Count(line => line.Contains("Access web UI at http")
|| line.Contains("Qdrant HTTP listening on")
|| line.Contains("starting in Actix runtime"));

return detectedLines >= 2;
}
}
}

internal static class QdrantBuilderExtensions
{
public static QdrantBuilder BuildWithContainerName(this QdrantBuilder builder, string containerName)
{
if (containerName is not null)
{
builder = builder.WithName(containerName);
}
return builder;
}

public static QdrantBuilder BuildWithVolume(this QdrantBuilder builder, string hostPath, string containerPath, AccessMode accessMode)
{
if (hostPath is not null)
{
builder = builder.WithBindMount(hostPath, containerPath, accessMode);
}
return builder;
}
}
95 changes: 95 additions & 0 deletions src/Testcontainers.Qdrant/QdrantConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
namespace Testcontainers.Qdrant;

/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class QdrantConfiguration : ContainerConfiguration
{

/// <summary>
/// Initializes a new instance of the <see cref="QdrantConfiguration" /> class.
/// </summary>
/// <param name="containerName"></param>
/// <param name="hostPath"></param>
/// <param name="containerPath"></param>
/// <param name="accessMode"></param>

public QdrantConfiguration(
string containerName = null,
string hostPath = null,
string containerPath = null,
Nullable<AccessMode> accessMode = null
)
{
this.ContainerName = containerName;
this.HostPath = hostPath;
this.ContainerPath = containerPath;
if (accessMode.HasValue)
{
this.AccessMode = accessMode.Value;
}
}

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

/// <summary>
/// Initializes a new instance of the <see cref="QdrantConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public QdrantConfiguration(QdrantConfiguration oldValue, QdrantConfiguration newValue)
: base(oldValue, newValue)
{
ContainerName = BuildConfiguration.Combine(oldValue.ContainerName, newValue.ContainerName);
HostPath = BuildConfiguration.Combine(oldValue.HostPath, newValue.HostPath);
ContainerPath = BuildConfiguration.Combine(oldValue.ContainerPath, newValue.ContainerPath);
AccessMode = BuildConfiguration.Combine(oldValue.AccessMode, newValue.AccessMode);
}

/// <summary>
/// Gets the docker container name.
/// </summary>
public string ContainerName { get; }

/// <summary>
/// Gets the HostPath for mounting volumes.
/// </summary>
public string HostPath { get; }

/// <summary>
/// Gets the ContainerPath for mounting volumes.
/// </summary>
public string ContainerPath { get; }

/// <summary>
/// Get the AccessMode (ReadWrite or ReadOnly) for mounted volumes.
/// </summary>
public AccessMode AccessMode { get; } = AccessMode.ReadWrite;
}
28 changes: 28 additions & 0 deletions src/Testcontainers.Qdrant/QdrantContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Testcontainers.Qdrant;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class QdrantContainer : DockerContainer
{
private readonly QdrantConfiguration _configuration;

/// <summary>
/// Initializes a new instance of the <see cref="QdrantContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
/// <param name="logger">The logger.</param>
public QdrantContainer(QdrantConfiguration configuration, Microsoft.Extensions.Logging.ILogger logger)
: base(configuration, logger)
{
_configuration = configuration;
}

/// <summary>
/// Gets the Qdrant connection url.
/// </summary>
/// <returns>The Qdrant connection url.</returns>
public string GetConnectionUrl()
{
return $"http://{Hostname}:{GetMappedPublicPort(QdrantBuilder.QdrantPort)}";
}
}
13 changes: 13 additions & 0 deletions src/Testcontainers.Qdrant/Testcontainers.Qdrant.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(SolutionDir)src/Testcontainers/Testcontainers.csproj" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions src/Testcontainers.Qdrant/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Text;
global using System.Threading;
global using System.Threading.Tasks;
global using Docker.DotNet.Models;
global using DotNet.Testcontainers;
global using DotNet.Testcontainers.Builders;
global using DotNet.Testcontainers.Configurations;
global using DotNet.Testcontainers.Containers;
global using JetBrains.Annotations;
global using Microsoft.Extensions.Logging;
Loading