From 7c31fed068dc9f1cb8c7daf7d3e24ea4aa423235 Mon Sep 17 00:00:00 2001 From: Gianluca Arbezzano Date: Tue, 24 Jul 2018 17:51:12 +0200 Subject: [PATCH] Tests to cover the ability to expose the same ports This test is added based on a comment I get from reddit about the ability to reach two containers exposing the same port without any collision. Signed-off-by: Gianluca Arbezzano --- docker_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/docker_test.go b/docker_test.go index 6716ef256d..16769365f5 100644 --- a/docker_test.go +++ b/docker_test.go @@ -7,6 +7,55 @@ import ( "testing" ) +func TestTwoContainersExposingTheSamePort(t *testing.T) { + ctx := context.Background() + nginxA, err := RunContainer(ctx, "nginx", RequestContainer{ + ExportedPort: []string{ + "80/tpc", + }, + }) + if err != nil { + t.Error(err) + } + defer nginxA.Terminate(ctx, t) + + nginxB, err := RunContainer(ctx, "nginx", RequestContainer{ + ExportedPort: []string{ + "80/tpc", + }, + }) + if err != nil { + t.Error(err) + } + defer nginxB.Terminate(ctx, t) + + ipA, err := nginxA.GetIPAddress(ctx) + if err != nil { + t.Error(err) + } + + ipB, err := nginxA.GetIPAddress(ctx) + if err != nil { + t.Error(err) + } + + resp, err := http.Get(fmt.Sprintf("http://%s", ipA)) + if err != nil { + t.Error(err) + } + if resp.StatusCode != http.StatusOK { + t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode) + } + + resp, err = http.Get(fmt.Sprintf("http://%s", ipB)) + if err != nil { + t.Error(err) + } + if resp.StatusCode != http.StatusOK { + t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode) + } +} + func TestContainerCreation(t *testing.T) { ctx := context.Background() nginxC, err := RunContainer(ctx, "nginx", RequestContainer{