Skip to content

Commit ec865bb

Browse files
authored
Merge branch 'main' into provisionerd
2 parents d323034 + 38867b0 commit ec865bb

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

.github/workflows/coder.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ jobs:
164164
run:
165165
DB=true gotestsum --jsonfile="gotests.json" --packages="./..." --
166166
-covermode=atomic -coverprofile="gotests.coverage" -timeout=3m
167-
-count=1 -race -parallel=1
167+
-count=1 -race -parallel=2
168168

169169
- uses: codecov/codecov-action@v2
170170
if: github.actor != 'dependabot[bot]'

database/postgres/postgres.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"fmt"
66
"io/ioutil"
7+
"net"
78
"os"
89
"time"
910

@@ -22,6 +23,13 @@ func Open() (string, func(), error) {
2223
if err != nil {
2324
return "", nil, xerrors.Errorf("create tempdir: %w", err)
2425
}
26+
// Pick an explicit port on the host to connect to 5432.
27+
// This is necessary so we can configure the port to only use ipv4.
28+
port, err := getFreePort()
29+
if err != nil {
30+
return "", nil, xerrors.Errorf("Unable to get free port: %w", err)
31+
}
32+
2533
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
2634
Repository: "postgres",
2735
Tag: "11",
@@ -33,6 +41,15 @@ func Open() (string, func(), error) {
3341
"PGDATA=/tmp",
3442
"listen_addresses = '*'",
3543
},
44+
PortBindings: map[docker.Port][]docker.PortBinding{
45+
"5432/tcp": {{
46+
// Manually specifying a host IP tells Docker just to use an IPV4 address.
47+
// If we don't do this, we hit a fun bug:
48+
// https://github.com/moby/moby/issues/42442
49+
// where the ipv4 and ipv6 ports might be _different_ and collide with other running docker containers.
50+
HostIP: "0.0.0.0",
51+
HostPort: fmt.Sprintf("%d", port)}},
52+
},
3653
Mounts: []string{
3754
// The postgres image has a VOLUME parameter in it's image.
3855
// If we don't mount at this point, Docker will allocate a
@@ -76,3 +93,16 @@ func Open() (string, func(), error) {
7693
_ = os.RemoveAll(tempDir)
7794
}, nil
7895
}
96+
97+
// getFreePort asks the kernel for a free open port that is ready to use.
98+
func getFreePort() (port int, err error) {
99+
// Binding to port 0 tells the OS to grab a port for us:
100+
// https://stackoverflow.com/questions/1365265/on-localhost-how-do-i-pick-a-free-port-number
101+
listener, err := net.Listen("tcp", "localhost:0")
102+
if err != nil {
103+
return 0, err
104+
}
105+
106+
defer listener.Close()
107+
return listener.Addr().(*net.TCPAddr).Port, nil
108+
}

0 commit comments

Comments
 (0)