Skip to content

Commit

Permalink
Switch to using testcontainers
Browse files Browse the repository at this point in the history
Closes #57
  • Loading branch information
roji committed Feb 27, 2024
1 parent 5f49a03 commit 8166022
Show file tree
Hide file tree
Showing 18 changed files with 283 additions and 135 deletions.
21 changes: 0 additions & 21 deletions .github/workflows/Build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,9 @@ jobs:
submodules: 'recursive'
fetch-depth: 0

- name: Start Milvus
run: |
wget https://github.com/milvus-io/milvus/releases/download/${{ matrix.milvus_version }}/milvus-standalone-docker-compose.yml -O docker-compose.yml
# Turn on authorization. This involves downloading milvus.yml and setting authorization to true, and then
# using a docker-compose.override.yml file to get it picked up.
wget https://raw.githubusercontent.com/milvus-io/milvus/${{ matrix.milvus_version }}/configs/milvus.yaml
sed -i 's/authorizationEnabled: false/authorizationEnabled: true/' milvus.yaml
cat <<EOF > docker-compose.override.yml
services:
standalone:
volumes:
- ${PWD}/milvus.yaml:/milvus/configs/milvus.yaml
EOF
sudo docker-compose up -d
- name: Setup .NET SDKs
uses: actions/setup-dotnet@v3

- name: Wait for Milvus to become available
run: sleep 30 # TODO: Find a better way to do this, e.g. try polling with curl until we detect that milvus is up

- name: Test
run: dotnet test --logger "GitHubActions;report-warnings=false"

Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageVersion Include="Testcontainers" Version="3.7.0" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.3.3" />
</ItemGroup>
</Project>
15 changes: 11 additions & 4 deletions Milvus.Client.Tests/AliasTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Milvus.Client.Tests;

[Collection("Milvus")]
public class AliasTests : IAsyncLifetime
{
[Fact]
Expand Down Expand Up @@ -50,8 +51,11 @@ public async Task Drop()

private MilvusCollection Collection { get; set; }

public AliasTests()
=> Collection = Client.GetCollection(CollectionName);
public AliasTests(MilvusFixture milvusFixture)
{
Client = milvusFixture.CreateClient();
Collection = Client.GetCollection(CollectionName);
}

public async Task InitializeAsync()
{
Expand All @@ -69,8 +73,11 @@ public async Task InitializeAsync()
});
}

private MilvusClient Client => TestEnvironment.Client;
private readonly MilvusClient Client;

public Task DisposeAsync()
=> Task.CompletedTask;
{
Client.Dispose();
return Task.CompletedTask;
}
}
19 changes: 16 additions & 3 deletions Milvus.Client.Tests/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Milvus.Client.Tests;

[Collection("Milvus")]
public class CollectionTests : IAsyncLifetime
{
[Fact]
Expand Down Expand Up @@ -300,7 +301,8 @@ public async Task Compact()
[Fact]
public async Task Create_in_non_existing_database_fails()
{
using MilvusClient databaseClient = TestEnvironment.CreateClientForDatabase("non_existing_db");
// using MilvusClient databaseClient = TestEnvironment.CreateClientForDatabase("non_existing_db");
using MilvusClient databaseClient = Fixture.CreateClient("non_existing_db");

var exception = await Assert.ThrowsAsync<MilvusException>(() => databaseClient.CreateCollectionAsync(
"foo",
Expand All @@ -310,13 +312,24 @@ public async Task Create_in_non_existing_database_fails()
Assert.Equal("ErrorCode: UnexpectedError Reason: database:non_existing_db not found", exception.Message);
}

public CollectionTests(MilvusFixture milvusFixture)
{
ArgumentNullException.ThrowIfNull(milvusFixture);
Fixture = milvusFixture;
Client = milvusFixture.CreateClient();
}

public Task InitializeAsync()
=> Client.GetCollection(CollectionName).DropAsync();

public Task DisposeAsync()
=> Task.CompletedTask;
{
Client.Dispose();
return Task.CompletedTask;
}

private const string CollectionName = nameof(CollectionTests);

private MilvusClient Client => TestEnvironment.Client;
private readonly MilvusFixture Fixture;
private readonly MilvusClient Client;
}
39 changes: 31 additions & 8 deletions Milvus.Client.Tests/DataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Milvus.Client.Tests;

public class DataTests : IClassFixture<DataTests.DataCollectionFixture>
[Collection("Milvus")]
public class DataTests : IClassFixture<DataTests.DataCollectionFixture>, IAsyncLifetime
{
[Fact]
public async Task Insert_Drop()
Expand Down Expand Up @@ -137,7 +138,7 @@ public async Task Insert_dynamic_field()
{
await Collection.DropAsync();

await TestEnvironment.Client.CreateCollectionAsync(
await Client.CreateCollectionAsync(
Collection.Name,
new CollectionSchema
{
Expand Down Expand Up @@ -177,13 +178,21 @@ private async Task<MutationResult> InsertDataAsync(long id1, long id2)

public class DataCollectionFixture : IAsyncLifetime
{
public MilvusCollection Collection { get; } = TestEnvironment.Client.GetCollection(CollectionName);
private readonly MilvusClient Client;

public DataCollectionFixture(MilvusFixture milvusFixture)
{
Client = milvusFixture.CreateClient();
Collection = Client.GetCollection(CollectionName);
}

public MilvusCollection Collection;

public async Task InitializeAsync()
{
await Collection.DropAsync();

await TestEnvironment.Client.CreateCollectionAsync(
await Client.CreateCollectionAsync(
Collection.Name,
new[]
{
Expand All @@ -198,14 +207,28 @@ await TestEnvironment.Client.CreateCollectionAsync(
}

public Task DisposeAsync()
=> Task.CompletedTask;
{
Client.Dispose();
return Task.CompletedTask;
}
}

private readonly DataCollectionFixture _dataCollectionFixture;
private const string CollectionName = nameof(DataTests);
private MilvusCollection Collection => _dataCollectionFixture.Collection;
private MilvusClient Client => TestEnvironment.Client;
private readonly MilvusClient Client;

public DataTests(DataCollectionFixture dataCollectionFixture)
=> _dataCollectionFixture = dataCollectionFixture;
public DataTests(MilvusFixture milvusFixture, DataCollectionFixture dataCollectionFixture)
{
Client = milvusFixture.CreateClient();
_dataCollectionFixture = dataCollectionFixture;
}

public Task InitializeAsync() => Task.CompletedTask;

public Task DisposeAsync()
{
Client.Dispose();
return Task.CompletedTask;
}
}
13 changes: 6 additions & 7 deletions Milvus.Client.Tests/DatabaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Milvus.Client.Tests;

public class DatabaseTests : IAsyncLifetime
[Collection("Milvus")]
public class DatabaseTests(MilvusFixture milvusFixture) : IAsyncLifetime
{
[Fact]
public async Task Create_List_Drop()
Expand Down Expand Up @@ -36,7 +37,7 @@ public async Task Search_on_non_default_database()
{
string databaseName = nameof(Search_on_non_default_database);

using var databaseClient = TestEnvironment.CreateClientForDatabase(databaseName);
using var databaseClient = milvusFixture.CreateClient(databaseName);

// If the database exists, drop it using the regular client and recreate it.
if ((await DefaultClient.ListDatabasesAsync()).Contains(databaseName))
Expand Down Expand Up @@ -105,8 +106,6 @@ await collection.WaitForCollectionLoadAsync(

public async Task InitializeAsync()
{
DatabaseClient = TestEnvironment.CreateClientForDatabase(DatabaseName);

if ((await DefaultClient.ListDatabasesAsync()).Contains(DatabaseName))
{
// First drop all collections from a possible previous test run, otherwise dropping fails
Expand All @@ -121,13 +120,13 @@ public async Task InitializeAsync()

public Task DisposeAsync()
{
DefaultClient.Dispose();
DatabaseClient.Dispose();
return Task.CompletedTask;
}

private MilvusClient DefaultClient => TestEnvironment.Client;

private MilvusClient DatabaseClient { get; set; } = null!;
private readonly MilvusClient DefaultClient = milvusFixture.CreateClient();
private readonly MilvusClient DatabaseClient = milvusFixture.CreateClient(DatabaseName);

private const string DatabaseName = nameof(DatabaseTests);
}
15 changes: 11 additions & 4 deletions Milvus.Client.Tests/IndexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Milvus.Client.Tests;

[Collection("Milvus")]
public class IndexTests : IAsyncLifetime
{
[Fact]
Expand Down Expand Up @@ -186,14 +187,20 @@ await Client.CreateCollectionAsync(
}

public Task DisposeAsync()
=> Task.CompletedTask;
{
Client.Dispose();
return Task.CompletedTask;
}

private const string CollectionName = nameof(IndexTests);

private MilvusClient Client => TestEnvironment.Client;
private readonly MilvusClient Client;

private MilvusCollection Collection { get; }

public IndexTests()
=> Collection = Client.GetCollection(CollectionName);
public IndexTests(MilvusFixture milvusFixture)
{
Client = milvusFixture.CreateClient();
Collection = Client.GetCollection(CollectionName);
}
}
8 changes: 2 additions & 6 deletions Milvus.Client.Tests/Milvus.Client.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<NoWarn>$(NoWarn);CA1062</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,17 +19,12 @@
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
<PackageReference Include="Testcontainers" />
<PackageReference Include="GitHubActionsTestLogger" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Milvus.Client\Milvus.Client.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions Milvus.Client.Tests/MilvusFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Globalization;
using Microsoft.Extensions.Configuration;
using Milvus.Client.Tests.TestContainer;
using Xunit;

namespace Milvus.Client.Tests;

[CollectionDefinition("Milvus")]
public sealed class QdrantCollection : ICollectionFixture<MilvusFixture>;

public sealed class MilvusFixture : IAsyncLifetime
{
private readonly MilvusContainer _container = new MilvusBuilder().Build();

public string Host => _container.Hostname;
public int Port => _container.GetMappedPublicPort(MilvusBuilder.MilvusGrpcPort);
public string Username => "root";
public string Password => "Milvus";

public MilvusClient CreateClient()
=> new(Host, Username, Password, Port, ssl: false);

public MilvusClient CreateClient(string database)
=> new(Host, Username, Password, Port, ssl: false, database);

public Task InitializeAsync() => _container.StartAsync();
public Task DisposeAsync() => _container.DisposeAsync().AsTask();
}
21 changes: 11 additions & 10 deletions Milvus.Client.Tests/MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

namespace Milvus.Client.Tests;

public class MiscTests
[Collection("Milvus")]
public class MiscTests(MilvusFixture milvusFixture) : IDisposable
{
// If this test is failing for you, that means you haven't enabled authorization in Milvus; follow the instructions
// in https://milvus.io/docs/authenticate.md.
[Fact]
public async Task Auth_failure_with_wrong_password()
{
using var badClient = new MilvusClient(
TestEnvironment.Host, username: TestEnvironment.Username, password: "incorrect_password");
milvusFixture.Host, username: milvusFixture.Username, password: "incorrect_password");

try
{
Expand Down Expand Up @@ -44,17 +45,17 @@ public async Task GetVersion()
}

[Fact]
public async Task Dispose()
public async Task Dispose_client()
{
var client = TestEnvironment.CreateClient();

MilvusHealthState state = await client.HealthAsync();
MilvusHealthState state = await Client.HealthAsync();
Assert.True(state.IsHealthy);

client.Dispose();
Client.Dispose();

await Assert.ThrowsAsync<ObjectDisposedException>(() => client.HealthAsync());
}
await Assert.ThrowsAsync<ObjectDisposedException>(() => Client.HealthAsync());
}

private readonly MilvusClient Client = milvusFixture.CreateClient();

private MilvusClient Client => TestEnvironment.Client;
public void Dispose() => Client.Dispose();
}
Loading

0 comments on commit 8166022

Please sign in to comment.