Skip to content

Instantly share code, notes, and snippets.

@danbarua
Forked from jchannon/Test.cs
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danbarua/70303787bcff15fca2f6 to your computer and use it in GitHub Desktop.
Save danbarua/70303787bcff15fca2f6 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");
//Stop container - CAN'T USE AWAIT HERE THOUGH!!!! :(
var stopped = await client.Containers.StopContainerAsync ("39e3317fd258",
new StopContainerParameters(){
Wait = TimeSpan.FromSeconds(30)
},
CancellationToken.None);
}
private async Task Setup()
{
//Pull container - CAN'T USE AWAIT HERE THOUGH!!!! :(
var stream = await client.Images.CreateImageAsync(new CreateImageParameters()
{
FromImage = "192.168.1.95:5000/mycontainer",
Tag = "latest",
}, null);
//Start container - CAN'T USE AWAIT HERE THOUGH!!!! :(
await client.Containers.StartContainerAsync ("39e3317fd258", null);
}
}
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment