Skip to content

Instantly share code, notes, and snippets.

@yreynhout
Last active May 9, 2020 20:45
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save yreynhout/c7569833d78c8db255ec8d7703bb3ae5 to your computer and use it in GitHub Desktop.
Save yreynhout/c7569833d78c8db255ec8d7703bb3ae5 to your computer and use it in GitHub Desktop.
Recipe for an EventStoreFixture using Docker.DotNet
using Xunit;
namespace TheDukesOfDocker
{
[CollectionDefinition(nameof(EventStoreCollection))]
public class EventStoreCollection : ICollectionFixture<EventStoreFixture>
{
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Docker.DotNet;
using Docker.DotNet.Models;
using EventStore.ClientAPI;
using EventStore.ClientAPI.SystemData;
using Xunit;
namespace TheDukesOfDocker
{
public class EventStoreFixture : IAsyncLifetime
{
public string EventStoreContainer { get; private set; } = $"es-{Guid.NewGuid().ToString("N")}";
const string EventStoreImage = "eventstore/eventstore";
public async Task InitializeAsync()
{
// I'm running docker on Ubuntu, you may have to connect to Docker Machine on Windows.
var config = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine"));
this.Client = config.CreateClient();
var images = await this.Client.Images.ListImagesAsync(new ImagesListParameters { MatchName = EventStoreImage });
if (images.Count == 0)
{
// No image found. Pulling latest ..
await this.Client.Images.CreateImageAsync(new ImagesCreateParameters { FromImage = EventStoreImage, Tag = "latest" }, null, IgnoreProgress.Forever);
}
var containers = await this.Client.Containers.ListContainersAsync(new ContainersListParameters { All = true });
await this.Client.Containers.CreateContainerAsync(
new CreateContainerParameters
{
Image = EventStoreImage,
Name = EventStoreContainer,
Tty = true,
HostConfig = new HostConfig
{
PortBindings = new Dictionary<string, IList<PortBinding>>
{
{
"2113/tcp",
new List<PortBinding> {
new PortBinding
{
HostPort = "2113"
}
}
},
{
"1113/tcp",
new List<PortBinding> {
new PortBinding
{
HostPort = "1113"
}
}
}
}
}
});
// Starting the container ...
await this.Client.Containers.StartContainerAsync(EventStoreContainer, new ContainerStartParameters { });
var endpoint = new Uri("tcp://127.0.0.1:1113");
var settings = ConnectionSettings
.Create()
.KeepReconnecting()
.KeepRetrying()
.SetDefaultUserCredentials(new UserCredentials("admin", "changeit"));
var connectionName = $"M={Environment.MachineName},P={Process.GetCurrentProcess().Id},T={DateTimeOffset.UtcNow.Ticks}";
this.Connection = EventStoreConnection.Create(settings, endpoint, connectionName);
await this.Connection.ConnectAsync();
}
public async Task DisposeAsync()
{
if(this.Client != null)
{
this.Connection?.Dispose();
await this.Client.Containers.StopContainerAsync(EventStoreContainer, new ContainerStopParameters { });
await this.Client.Containers.RemoveContainerAsync(EventStoreContainer, new ContainerRemoveParameters { Force = true });
this.Client.Dispose();
}
}
private DockerClient Client { get; set; }
public IEventStoreConnection Connection { get; private set; }
private class IgnoreProgress : IProgress<JSONMessage>
{
public static readonly IProgress<JSONMessage> Forever = new IgnoreProgress();
public void Report(JSONMessage value) { }
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>7.1</LangVersion>
<AssemblyName>TheDukesOfDocker</AssemblyName>
<RootNamespace>TheDukesOfDocker</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Docker.DotNet" Version="3.125.0" />
<PackageReference Include="EventStore.ClientAPI.NetCore" Version="4.0.3-rc" />
<PackageReference Include="Xunit" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
</Project>
using System;
using System.Text;
using System.Threading.Tasks;
using EventStore.ClientAPI;
using EventStore.ClientAPI.SystemData;
using Xunit;
namespace TheDukesOfDocker
{
[Collection(nameof(EventStoreCollection))]
public class Usage
{
private IEventStoreConnection Connection { get; }
public Usage(EventStoreFixture fixture)
{
this.Connection = fixture.Connection;
}
[Fact]
public async Task Show()
{
await this.Connection.AppendToStreamAsync("test", ExpectedVersion.Any, new UserCredentials("admin", "changeit"), new EventData[]
{
new EventData(Guid.NewGuid(), "MyMessage", true, Encoding.UTF8.GetBytes("{}"), null)
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment