Mountebank imposter ports not accessible when mountebank started using dotnet-testcontainers

830 views Asked by At

I am using dotnet-testcontainers https://github.com/HofmeisterAn/dotnet-testcontainers to spin up a container with mountebank in my xUnit test. I can successfully create a mountebank client and create an imposter successfully. The issue is that when the test is run, the app tries to make a call to the imposter on http://localhost:3000 and gets connection refused.

I can successfully open http://localhost:2525 and can see Mountebank default page. So mountebank is running fine. I also confirmed that the imposter was created successfully on port 3000 by looking at docker container log. I also tried to use Postman to make a call to my imposter at http:localhost:3000 and get connection refused.

What could be the issue? Is this an issue that port 3000 in the docker container is not exposed or something? Below is my code:

MountebankClient mbClient = new MountebankClient();

var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
                .WithImage("bbyars/mountebank")
                .WithName("mountebank")
                .WithPortBinding(2525, false)
                .WithHostname("localhost");

var testContainers = testcontainersBuilder.Build();
await testContainers.StartAsync();

var testImposter = mbClient.CreateHttpImposter(3000);
testImposter.AddStub().ReturnsBody(HttpStatusCode.OK, File.ReadAllText(@".\Stubs\testImposter.json"));
mbClient.Submit(testImposter);
1

There are 1 answers

0
rochitsen On BEST ANSWER

Might be useful for someone else who would face this issue. Figured out what the issue was. I was not mapping the host port and the imposter port in the container. Read about published ports https://docs.docker.com/config/containers/container-networking/ and used WithExposedPort(3000) and then did port binding of that port with host port using WithPortBinding(3000,3000) where the first port in this method is host port and second port is container port.

var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
        .WithImage("bbyars/mountebank")
        .WithName("mountebank")
        .WithPortBinding(2525, 2525)
        .WithExposedPort(3000)
        .WithExposedPort(3100)
        .WithPortBinding(3000, 3000)
        .WithPortBinding(3100, 3100)                 
        .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(2525))
        .WithHostname("localhost");