From 3ec8c6065759e09cf0e093648d6b2e1954c72b76 Mon Sep 17 00:00:00 2001 From: Nick Sieger Date: Fri, 26 May 2023 15:28:15 -0500 Subject: [PATCH 1/2] e2e: add a cuke feature to test compose errors with port conflicts Signed-off-by: Nick Sieger --- e2e/cucumber-features/port-conflict.feature | 28 +++++++++++++++++++++ e2e/cucumber_test.go | 15 +++++++++++ 2 files changed, 43 insertions(+) create mode 100644 e2e/cucumber-features/port-conflict.feature diff --git a/e2e/cucumber-features/port-conflict.feature b/e2e/cucumber-features/port-conflict.feature new file mode 100644 index 000000000..aaacdade4 --- /dev/null +++ b/e2e/cucumber-features/port-conflict.feature @@ -0,0 +1,28 @@ +Feature: Report port conflicts + +Background: + Given a compose file + """ + services: + web: + image: nginx + ports: + - 31415:80 + """ + And I run "docker rm -f nginx-pi-31415" + +Scenario: Reports a port allocation conflict with another container + Given I run "docker run -d -p 31415:80 --name nginx-pi-31415 nginx" + When I run "compose up -d" + Then the output contains "port is already allocated" + And the exit code is 1 + +Scenario: Reports a port conflict with some other process + Given a process listening on port 31415 + When I run "compose up -d" + Then the output contains "address already in use" + And the exit code is 1 + +Scenario: Cleanup + Given I run "docker rm -f nginx-pi-31415" + diff --git a/e2e/cucumber_test.go b/e2e/cucumber_test.go index 151fba923..6d7cd152e 100644 --- a/e2e/cucumber_test.go +++ b/e2e/cucumber_test.go @@ -19,6 +19,7 @@ package cucumber import ( "context" "fmt" + "net" "os" "path/filepath" "regexp" @@ -87,6 +88,7 @@ func setup(s *godog.ScenarioContext) { s.Step(`output contains "(.*)"$`, th.outputContains(true)) s.Step(`output does not contain "(.*)"$`, th.outputContains(false)) s.Step(`exit code is (\d+)$`, th.exitCodeIs) + s.Step(`a process listening on port (\d+)$`, th.listenerOnPort) } type testHelper struct { @@ -174,3 +176,16 @@ func (th *testHelper) setDockerfile(dockerfileString string) error { } return nil } + +func (th *testHelper) listenerOnPort(port int) error { + l, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) + if err != nil { + return err + } + + th.T.Cleanup(func() { + _ = l.Close() + }) + + return nil +} From 2ca8ab914a41707406418f4a58de16ec62f420f0 Mon Sep 17 00:00:00 2001 From: Nick Sieger Date: Fri, 26 May 2023 15:29:03 -0500 Subject: [PATCH 2/2] e2e: make test re-runnable on the same machine Signed-off-by: Nick Sieger --- e2e/cucumber-features/ps.feature | 2 ++ 1 file changed, 2 insertions(+) diff --git a/e2e/cucumber-features/ps.feature b/e2e/cucumber-features/ps.feature index e4e256f9d..50917086b 100644 --- a/e2e/cucumber-features/ps.feature +++ b/e2e/cucumber-features/ps.feature @@ -16,6 +16,7 @@ Background: """ FROM golang:1.19-alpine """ + And I run "docker rm -f external-test" Scenario: external container from compose image exists When I run "compose build" @@ -24,4 +25,5 @@ Scenario: external container from compose image exists Then the exit code is 0 And I run "compose ps -a" Then the output does not contain "external-test" + And I run "docker rm -f external-test"