Merge pull request #10627 from nicksieger/test-cucumber-port-conflict

This commit is contained in:
Nick Sieger 2023-05-27 08:34:50 -05:00 committed by GitHub
commit b4924dee83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 0 deletions

View File

@ -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"

View File

@ -16,6 +16,7 @@ Background:
""" """
FROM golang:1.19-alpine FROM golang:1.19-alpine
""" """
And I run "docker rm -f external-test"
Scenario: external container from compose image exists Scenario: external container from compose image exists
When I run "compose build" When I run "compose build"
@ -24,4 +25,5 @@ Scenario: external container from compose image exists
Then the exit code is 0 Then the exit code is 0
And I run "compose ps -a" And I run "compose ps -a"
Then the output does not contain "external-test" Then the output does not contain "external-test"
And I run "docker rm -f external-test"

View File

@ -19,6 +19,7 @@ package cucumber
import ( import (
"context" "context"
"fmt" "fmt"
"net"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -87,6 +88,7 @@ func setup(s *godog.ScenarioContext) {
s.Step(`output contains "(.*)"$`, th.outputContains(true)) s.Step(`output contains "(.*)"$`, th.outputContains(true))
s.Step(`output does not contain "(.*)"$`, th.outputContains(false)) s.Step(`output does not contain "(.*)"$`, th.outputContains(false))
s.Step(`exit code is (\d+)$`, th.exitCodeIs) s.Step(`exit code is (\d+)$`, th.exitCodeIs)
s.Step(`a process listening on port (\d+)$`, th.listenerOnPort)
} }
type testHelper struct { type testHelper struct {
@ -174,3 +176,16 @@ func (th *testHelper) setDockerfile(dockerfileString string) error {
} }
return nil 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
}