Skip to content

Instantly share code, notes, and snippets.

@jdmonty
Forked from jchannon/Test.cs
Last active August 29, 2015 14:21
Show Gist options
  • Save jdmonty/822eb28aa00d67a60b36 to your computer and use it in GitHub Desktop.
Save jdmonty/822eb28aa00d67a60b36 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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment