Skip to content

Instantly share code, notes, and snippets.

@jchannon
Last active November 24, 2015 17:34
Show Gist options
  • Save jchannon/7a4e1790e8b663c38e68 to your computer and use it in GitHub Desktop.
Save jchannon/7a4e1790e8b663c38e68 to your computer and use it in GitHub Desktop.
public class SomeFixture : IDisposable
{
private DockerClient client;
public SomeFixture()
{
Console.WriteLine("SomeFixture ctor: This should only be run once");
//Using https://github.com/ahmetalpbalkan/Docker.DotNet
client = new DockerClientConfiguration(new Uri("tcp://10.0.1.43:2376")).CreateClient();
Setup().Wait();
}
public void Dispose()
{
Console.WriteLine("SomeFixture: Disposing SomeFixture");
StopContainer().Wait();
}
private async Task Setup()
{
var stream = await client.Images.CreateImageAsync(new CreateImageParameters()
{
FromImage = "192.168.1.95:5000/mycontainer",
Tag = "latest",
}, null);
await client.Containers.StartContainerAsync("0a419b7732f5", null);
}
public async Task StopContainer()
{
var stopped = await client.Containers.StopContainerAsync("0a419b7732f5",
new StopContainerParameters()
{
Wait = TimeSpan.FromSeconds(30)
},
CancellationToken.None);
}
}
public class Class1 : IUseFixture<SomeFixture>
{
[Fact]
public async Task FirstTest()
{
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync("http://IP_OF_MY_SERVER_IN_DOCKER_CONTAINER/Foo"))
using (HttpContent content = response.Content)
{
// ... Read the string.
string result = await content.ReadAsStringAsync();
Assert.NotNull(result);
}
}
[Fact]
public async Task SecondTest()
{
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync("http://IP_OF_MY_SERVER_IN_DOCKER_CONTAINER/Bar"))
using (HttpContent content = response.Content)
{
// ... Read the string.
string result = await content.ReadAsStringAsync();
Assert.NotNull(result);
}
}
public void SetFixture(SomeFixture data)
{
//IUseFixture method called before each test
}
}
@jchannon
Copy link
Author

GOAL - to enable integration tests with Docker. Run multiple tests against one container. I need something to start the container once. I have that through a library but the calls are async which can't be called in a constructor.

I tried using a Task.Factory.StartNew and then call .Wait(), I tried just calling the methods with a .Result to force the wait but on OSX and Xamarin Studio when I debug it ends up stepping through the DisAssembly window and the test just stops.

I was hoping to use xUnit 2 but there is a problem with that on Mono so this is using xUnit 1.9.2.

All suggestions welcome!

@danbarua
Copy link

@jchannon
Copy link
Author

From a combined effort the above C# should now work for sharing a container across multiple tests.

(You may ask why not run the tests in the container but that's another discussion)

@bradwilson
Copy link

Unfortunately, there's no good answer for doing async code in ctor/Dispose. I've opened this issue to attempt to address it (at least with fixtures): xunit/xunit#424

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment