Skip to content

Commit e016c30

Browse files
authored
Fix postgres ephemeral ports; don't use for TestPubsub_Disconnect (#7798)
Signed-off-by: Spike Curtis <spike@coder.com>
1 parent f14f011 commit e016c30

File tree

2 files changed

+9
-44
lines changed

2 files changed

+9
-44
lines changed

coderd/database/postgres/postgres.go

-34
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package postgres
33
import (
44
"database/sql"
55
"fmt"
6-
"net"
76
"os"
87
"strconv"
9-
"sync"
108
"time"
119

1210
"github.com/cenkalti/backoff/v4"
@@ -18,10 +16,6 @@ import (
1816
"github.com/coder/coder/cryptorand"
1917
)
2018

21-
// Required to prevent port collision during container creation.
22-
// Super unlikely, but it happened. See: https://github.com/coder/coder/runs/5375197003
23-
var openPortMutex sync.Mutex
24-
2519
// Open creates a new PostgreSQL database instance. With DB_FROM environment variable set, it clones a database
2620
// from the provided template. With the environment variable unset, it creates a new Docker container running postgres.
2721
func Open() (string, func(), error) {
@@ -68,17 +62,6 @@ func OpenContainerized(port int) (string, func(), error) {
6862
return "", nil, xerrors.Errorf("create tempdir: %w", err)
6963
}
7064

71-
openPortMutex.Lock()
72-
if port == 0 {
73-
// Pick an explicit port on the host to connect to 5432.
74-
// This is necessary so we can configure the port to only use ipv4.
75-
port, err = getFreePort()
76-
if err != nil {
77-
openPortMutex.Unlock()
78-
return "", nil, xerrors.Errorf("get free port: %w", err)
79-
}
80-
}
81-
8265
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
8366
Repository: "postgres",
8467
Tag: "13",
@@ -114,10 +97,8 @@ func OpenContainerized(port int) (string, func(), error) {
11497
config.RestartPolicy = docker.RestartPolicy{Name: "no"}
11598
})
11699
if err != nil {
117-
openPortMutex.Unlock()
118100
return "", nil, xerrors.Errorf("could not start resource: %w", err)
119101
}
120-
openPortMutex.Unlock()
121102

122103
hostAndPort := resource.GetHostPort("5432/tcp")
123104
dbURL := fmt.Sprintf("postgres://postgres:postgres@%s/postgres?sslmode=disable", hostAndPort)
@@ -166,18 +147,3 @@ func OpenContainerized(port int) (string, func(), error) {
166147
_ = os.RemoveAll(tempDir)
167148
}, nil
168149
}
169-
170-
// getFreePort asks the kernel for a free open port that is ready to use.
171-
func getFreePort() (port int, err error) {
172-
// Binding to port 0 tells the OS to grab a port for us:
173-
// https://stackoverflow.com/questions/1365265/on-localhost-how-do-i-pick-a-free-port-number
174-
listener, err := net.Listen("tcp", "localhost:0")
175-
if err != nil {
176-
return 0, err
177-
}
178-
179-
defer listener.Close()
180-
// This is always a *net.TCPAddr.
181-
// nolint:forcetypeassert
182-
return listener.Addr().(*net.TCPAddr).Port, nil
183-
}

coderd/database/pubsub_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"database/sql"
88
"fmt"
99
"math/rand"
10-
"net"
11-
"net/url"
1210
"strconv"
1311
"testing"
1412
"time"
@@ -117,11 +115,17 @@ func TestPubsub_ordering(t *testing.T) {
117115
}
118116
}
119117

118+
// disconnectTestPort is the hardcoded port for TestPubsub_Disconnect. In this test we need to be able to stop Postgres
119+
// and restart it on the same port. If we use an ephemeral port, there is a chance the OS will reallocate before we
120+
// start back up. The downside is that if the test crashes and leaves the container up, subsequent test runs will fail
121+
// until we manually kill the container.
122+
const disconnectTestPort = 26892
123+
124+
// nolint: paralleltest
120125
func TestPubsub_Disconnect(t *testing.T) {
121-
t.Parallel()
122126
// we always use a Docker container for this test, even in CI, since we need to be able to kill
123127
// postgres and bring it back on the same port.
124-
connectionURL, closePg, err := postgres.OpenContainerized(0)
128+
connectionURL, closePg, err := postgres.OpenContainerized(disconnectTestPort)
125129
require.NoError(t, err)
126130
defer closePg()
127131
db, err := sql.Open("postgres", connectionURL)
@@ -191,13 +195,8 @@ func TestPubsub_Disconnect(t *testing.T) {
191195

192196
// restart postgres on the same port --- since we only use LISTEN/NOTIFY it doesn't
193197
// matter that the new postgres doesn't have any persisted state from before.
194-
u, err := url.Parse(connectionURL)
195-
require.NoError(t, err)
196-
addr, err := net.ResolveTCPAddr("tcp", u.Host)
197-
require.NoError(t, err)
198-
newURL, closeNewPg, err := postgres.OpenContainerized(addr.Port)
198+
_, closeNewPg, err := postgres.OpenContainerized(disconnectTestPort)
199199
require.NoError(t, err)
200-
require.Equal(t, connectionURL, newURL)
201200
defer closeNewPg()
202201

203202
// now write messages until we DON'T hit an error -- pubsub is back up.

0 commit comments

Comments
 (0)