diff --git a/test/JanusGraph.Net.IntegrationTest/GremlinServerContainer.cs b/test/JanusGraph.Net.IntegrationTest/GremlinServerContainer.cs deleted file mode 100644 index dba3956..0000000 --- a/test/JanusGraph.Net.IntegrationTest/GremlinServerContainer.cs +++ /dev/null @@ -1,91 +0,0 @@ -#region License - -/* - * Copyright 2018 JanusGraph.Net Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#endregion - -using System; -using System.Net.WebSockets; -using System.Threading.Tasks; -using Docker.DotNet; -using Gremlin.Net.Driver; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Polly; -using TestContainers.Container.Abstractions; -using TestContainers.Container.Abstractions.Images; - -namespace JanusGraph.Net.IntegrationTest -{ - public class GremlinServerContainer : GenericContainer - { - public static readonly int GremlinServerPort = 8182; - - public string Host => GetDockerHostIpAddress(); - - public int Port => GetMappedPort(GremlinServerPort); - - public string ServerStartedCheckTraversal { get; set; } = "1+1==2"; - - /// - public GremlinServerContainer(string dockerImageName, IDockerClient dockerClient, ILoggerFactory loggerFactory) - : base(dockerImageName, dockerClient, loggerFactory) - { - } - - /// - [ActivatorUtilitiesConstructor] - public GremlinServerContainer(IImage dockerImage, IDockerClient dockerClient, ILoggerFactory loggerFactory) - : base(dockerImage, dockerClient, loggerFactory) - { - } - - protected override async Task ContainerStarted() - { - var result = await Policy.TimeoutAsync(TimeSpan.FromMinutes(2)) - .WrapAsync(Policy - .Handle() - .Or() - .WaitAndRetryForeverAsync(iteration => TimeSpan.FromSeconds(2))) - .ExecuteAndCaptureAsync(async () => - { - var serverStarted = await IsServerStartedAsync(); - - if (!serverStarted) - { - throw new InvalidOperationException("Server not fully started yet"); - } - }); - - if (result.Outcome == OutcomeType.Failure) - throw new Exception(result.FinalException.Message); - } - - private async Task IsServerStartedAsync() - { - try - { - using var client = new GremlinClient(new GremlinServer(Host, Port)); - return await client.SubmitWithSingleResultAsync(ServerStartedCheckTraversal); - } - catch (AggregateException e) when(e.InnerException != null) - { - throw e.InnerException; - } - } - } -} \ No newline at end of file diff --git a/test/JanusGraph.Net.IntegrationTest/JanusGraph.Net.IntegrationTest.csproj b/test/JanusGraph.Net.IntegrationTest/JanusGraph.Net.IntegrationTest.csproj index f9cae1b..8420b94 100644 --- a/test/JanusGraph.Net.IntegrationTest/JanusGraph.Net.IntegrationTest.csproj +++ b/test/JanusGraph.Net.IntegrationTest/JanusGraph.Net.IntegrationTest.csproj @@ -6,7 +6,7 @@ - + diff --git a/test/JanusGraph.Net.IntegrationTest/JanusGraphServerFixture.cs b/test/JanusGraph.Net.IntegrationTest/JanusGraphServerFixture.cs index ab040e2..13a29cf 100644 --- a/test/JanusGraph.Net.IntegrationTest/JanusGraphServerFixture.cs +++ b/test/JanusGraph.Net.IntegrationTest/JanusGraphServerFixture.cs @@ -19,47 +19,54 @@ #endregion using System; +using System.Net.WebSockets; using System.Threading.Tasks; +using DotNet.Testcontainers.Builders; +using DotNet.Testcontainers.Configurations; +using DotNet.Testcontainers.Containers; +using Gremlin.Net.Driver; +using Gremlin.Net.Driver.Remote; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; -using TestContainers.Container.Abstractions.Hosting; -using TestContainers.Container.Abstractions.Models; using Xunit; +using static Gremlin.Net.Process.Traversal.AnonymousTraversalSource; namespace JanusGraph.Net.IntegrationTest { public class JanusGraphServerFixture : IAsyncLifetime { - private readonly GremlinServerContainer _container; + private readonly TestcontainersContainer _container; + private const ushort JanusGraphServerPort = 8182; public JanusGraphServerFixture() { var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); var dockerImage = config["dockerImage"]; - _container = new ContainerBuilder() - .ConfigureDockerImageName(dockerImage) - .ConfigureLogging(builder => - { - builder.AddConsole(); - builder.SetMinimumLevel(LogLevel.Debug); - }) - .ConfigureContainer((context, container) => - { - container.ExposedPorts.Add(GremlinServerContainer.GremlinServerPort); - - container.BindMounts.Add(new Bind - { - HostPath = $"{AppContext.BaseDirectory}/load_data.groovy", - ContainerPath = "/docker-entrypoint-initdb.d/load_data.groovy", - AccessMode = AccessMode.ReadOnly - }); - }) + _container = new TestcontainersBuilder() + .WithImage(dockerImage) + .WithName("janusgraph") + .WithPortBinding(JanusGraphServerPort) + .WithBindMount($"{AppContext.BaseDirectory}/load_data.groovy", + "/docker-entrypoint-initdb.d/load_data.groovy", AccessMode.ReadOnly) + .WithWaitStrategy(Wait.ForUnixContainer().UntilOperationIsSucceeded(IsServerReady, 1000)) .Build(); - _container.ServerStartedCheckTraversal = "g.V().has('name', 'hercules').hasNext()"; } - public string Host => _container.Host; - public int Port => _container.Port; + private bool IsServerReady() + { + try + { + using var client = JanusGraphClientBuilder.BuildClientForServer(new GremlinServer(Host, Port)).Create(); + var g = Traversal().WithRemote(new DriverRemoteConnection(client)); + return g.V().Has("name", "hercules").HasNext(); + } + catch (AggregateException e) when (e.InnerException is WebSocketException) + { + return false; + } + } + + public string Host => _container.Hostname; + public int Port => _container.GetMappedPublicPort(JanusGraphServerPort); public Task InitializeAsync() {