Skip to content

Commit e2076be

Browse files
authored
test: change to explicitly compiling dbcleaner on posix (coder#20206)
relates to: coder/internal#1026 On POSIX systems (macOS and Linux) we compile the dbcleaner binary into a temp directory. This allows us to explicitly separate the compilation step and report the time it takes. We suspect this might be a contributing factor in the above linked flakes we see on macOS. This doesn't work on Windows because Go tests clean up the temp directory at the end of the test and the dbcleaner binary will still be executing. On Windows you cannot delete a file being executed nor the directory. However, we are not seeing any flakes on Windows so the old behavior seems to be OK.
1 parent f1d3f31 commit e2076be

6 files changed

Lines changed: 53 additions & 4 deletions

File tree

coderd/database/dbtestutil/broker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (b *Broker) init(t TBSubset) error {
150150
b.uuid = uuid.New()
151151
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
152152
defer cancel()
153-
b.cleanerFD, err = startCleaner(ctx, b.uuid, coderTestingParams.DSN())
153+
b.cleanerFD, err = startCleaner(ctx, t, b.uuid, coderTestingParams.DSN())
154154
if err != nil {
155155
return xerrors.Errorf("start test db cleaner: %w", err)
156156
}

coderd/database/dbtestutil/cleaner.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"io"
88
"os"
9-
"os/exec"
109
"os/signal"
1110
"time"
1211

@@ -39,8 +38,8 @@ func init() {
3938

4039
// startCleaner starts the cleaner in a subprocess. holdThis is an opaque reference that needs to be kept from being
4140
// garbage collected until we are done with all test databases (e.g. the end of the process).
42-
func startCleaner(ctx context.Context, parentUUID uuid.UUID, dsn string) (holdThis any, err error) {
43-
cmd := exec.Command("go", "run", "github.com/coder/coder/v2/coderd/database/dbtestutil/cleanercmd")
41+
func startCleaner(ctx context.Context, t TBSubset, parentUUID uuid.UUID, dsn string) (holdThis any, err error) {
42+
cmd := cleanerCmd(t)
4443
cmd.Env = append(os.Environ(),
4544
fmt.Sprintf("%s=%s", envCleanerParentUUID, parentUUID.String()),
4645
fmt.Sprintf("%s=%s", envCleanerDSN, dsn),
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//go:build !windows
2+
3+
package dbtestutil
4+
5+
import (
6+
"os/exec"
7+
"path/filepath"
8+
"time"
9+
)
10+
11+
const timeFormat = "2006-01-02 15:04:05.000"
12+
13+
// cleanerCmd builds the cleaner binary in a temporary directory and returns a command to execute it. We can do this on
14+
// POSIX because it's OK to delete the temporary directory after the test: the binary can still run. This is not
15+
// possible on Windows because cleaning the temporary directory will fail if the binary is still running.
16+
// c.f. cleaner_windows.go.
17+
func cleanerCmd(t TBSubset) *exec.Cmd {
18+
start := time.Now()
19+
t.Logf("[%s] starting cleaner binary build", start.Format(timeFormat))
20+
tempDir := t.TempDir()
21+
cleanerBinary := filepath.Join(tempDir, "cleaner")
22+
23+
buildCmd := exec.Command("go", "build", "-o", cleanerBinary, "github.com/coder/coder/v2/coderd/database/dbtestutil/cleanercmd")
24+
output, err := buildCmd.CombinedOutput()
25+
if err != nil {
26+
t.Logf("failed to build cleaner binary: %v", err)
27+
t.Logf("output: %s", string(output))
28+
// Fall back to go run if build fails
29+
return exec.Command("go", "run", "github.com/coder/coder/v2/coderd/database/dbtestutil/cleanercmd")
30+
}
31+
t.Logf("[%s] cleaner binary %s built in %s", time.Now().Format(timeFormat), cleanerBinary, time.Since(start))
32+
33+
return exec.Command(cleanerBinary)
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build windows
2+
3+
package dbtestutil
4+
5+
import "os/exec"
6+
7+
// cleanerCmd returns a command to execute the cleaner binary. We do this with go run on Windows because we can't
8+
// delete the temporary directory after the test: the binary will still be running. c.f. cleaner_posix.go.
9+
func cleanerCmd(_ TBSubset) *exec.Cmd {
10+
return exec.Command("go", "run", "github.com/coder/coder/v2/coderd/database/dbtestutil/cleanercmd")
11+
}

coderd/database/dbtestutil/postgres.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ type TBSubset interface {
166166
Cleanup(func())
167167
Helper()
168168
Logf(format string, args ...any)
169+
TempDir() string
169170
}
170171

171172
// Open creates a new PostgreSQL database instance.

coderd/database/gen/dump/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func (*mockTB) Logf(format string, args ...any) {
3535
_, _ = fmt.Printf(format, args...)
3636
}
3737

38+
func (*mockTB) TempDir() string {
39+
panic("not implemented")
40+
}
41+
3842
func main() {
3943
t := &mockTB{}
4044
defer func() {

0 commit comments

Comments
 (0)