mirror of https://github.com/docker/docs.git
if address already in use in unit tests, try a different port
This commit is contained in:
parent
7ff2e6b797
commit
2a53717e8f
|
@ -5,9 +5,12 @@ import (
|
||||||
"github.com/dotcloud/docker/utils"
|
"github.com/dotcloud/docker/utils"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -277,24 +280,50 @@ func TestGet(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func findAvailalblePort(runtime *Runtime, port int) (*Container, error) {
|
||||||
|
strPort := strconv.Itoa(port)
|
||||||
|
container, err := NewBuilder(runtime).Create(&Config{
|
||||||
|
Image: GetTestImage(runtime).Id,
|
||||||
|
Cmd: []string{"sh", "-c", "echo well hello there | nc -l -p " + strPort},
|
||||||
|
PortSpecs: []string{strPort},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := container.Start(); err != nil {
|
||||||
|
if strings.Contains(err.Error(), "address already in use") {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return container, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Run a container with a TCP port allocated, and test that it can receive connections on localhost
|
// Run a container with a TCP port allocated, and test that it can receive connections on localhost
|
||||||
func TestAllocatePortLocalhost(t *testing.T) {
|
func TestAllocatePortLocalhost(t *testing.T) {
|
||||||
runtime, err := newTestRuntime()
|
runtime, err := newTestRuntime()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
container, err := NewBuilder(runtime).Create(&Config{
|
port := 5554
|
||||||
Image: GetTestImage(runtime).Id,
|
|
||||||
Cmd: []string{"sh", "-c", "echo well hello there | nc -l -p 5555"},
|
var container *Container
|
||||||
PortSpecs: []string{"5555"},
|
for {
|
||||||
},
|
port += 1
|
||||||
)
|
log.Println("Trying port", port)
|
||||||
if err != nil {
|
t.Log("Trying port", port)
|
||||||
t.Fatal(err)
|
container, err = findAvailalblePort(runtime, port)
|
||||||
}
|
if container != nil {
|
||||||
if err := container.Start(); err != nil {
|
break
|
||||||
t.Fatal(err)
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Println("Port", port, "already in use")
|
||||||
|
t.Log("Port", port, "already in use")
|
||||||
}
|
}
|
||||||
|
|
||||||
defer container.Kill()
|
defer container.Kill()
|
||||||
|
|
||||||
setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
|
setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
|
||||||
|
@ -308,7 +337,7 @@ func TestAllocatePortLocalhost(t *testing.T) {
|
||||||
|
|
||||||
conn, err := net.Dial("tcp",
|
conn, err := net.Dial("tcp",
|
||||||
fmt.Sprintf(
|
fmt.Sprintf(
|
||||||
"localhost:%s", container.NetworkSettings.PortMapping["5555"],
|
"localhost:%s", container.NetworkSettings.PortMapping[strconv.Itoa(port)],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue