@@ -21,13 +21,36 @@ import (
21
21
const (
22
22
cleanerRespOK = "OK"
23
23
envCleanerParentUUID = "DB_CLEANER_PARENT_UUID"
24
+ envCleanerDSN = "DB_CLEANER_DSN"
24
25
)
25
26
27
+ var (
28
+ originalWorkingDir string
29
+ getWorkingDirError error
30
+ )
31
+
32
+ func init () {
33
+ // We expect our tests to run from somewhere in the project tree where `go run` below in `startCleaner` will
34
+ // be able to resolve the command package. However, some of the tests modify the working directory during the run.
35
+ // So, we grab the working directory during package init, before tests are run, and then set that work dir on the
36
+ // subcommand process before it starts.
37
+ originalWorkingDir ,getWorkingDirError = os .Getwd ()
38
+ }
39
+
26
40
// startCleaner starts the cleaner in a subprocess. holdThis is an opaque reference that needs to be kept from being
27
41
// garbage collected until we are done with all test databases (e.g. the end of the process).
28
- func startCleaner (ctx context.Context ,parentUUID uuid.UUID ) (holdThis any ,err error ) {
29
- cmd := exec .Command ("go" ,"run" ,"github.com/coder/coder/v2/cmd/dbtestcleaner" )
30
- cmd .Env = append (os .Environ (),fmt .Sprintf ("%s=%s" ,envCleanerParentUUID ,parentUUID .String ()))
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" )
44
+ cmd .Env = append (os .Environ (),
45
+ fmt .Sprintf ("%s=%s" ,envCleanerParentUUID ,parentUUID .String ()),
46
+ fmt .Sprintf ("%s=%s" ,envCleanerDSN ,dsn ),
47
+ )
48
+
49
+ // c.f. comment on `func init()` in this file.
50
+ if getWorkingDirError != nil {
51
+ return nil ,xerrors .Errorf ("failed to get working directory during init: %w" ,getWorkingDirError )
52
+ }
53
+ cmd .Dir = originalWorkingDir
31
54
32
55
// Here we don't actually use the reference to the stdin pipe, because we never write anything to it. When this
33
56
// process exits, the pipe is closed by the OS and this triggers the cleaner to do its cleaning work. But, we do
@@ -79,6 +102,10 @@ type cleaner struct {
79
102
80
103
func (c * cleaner )init (ctx context.Context )error {
81
104
var err error
105
+ dsn := os .Getenv (envCleanerDSN )
106
+ if dsn == "" {
107
+ return xerrors .Errorf ("DSN not set via env %s: %w" ,envCleanerDSN ,err )
108
+ }
82
109
parentUUIDStr := os .Getenv (envCleanerParentUUID )
83
110
c .parentUUID ,err = uuid .Parse (parentUUIDStr )
84
111
if err != nil {
@@ -89,8 +116,6 @@ func (c *cleaner) init(ctx context.Context) error {
89
116
Leveled (slog .LevelDebug ).
90
117
With (slog .F ("parent_uuid" ,parentUUIDStr ))
91
118
92
- // TODO: support configurable username, password & port if required
93
- dsn := fmt .Sprintf ("postgres://postgres:postgres@localhost:5432/%s?sslmode=disable" ,CoderTestingDBName )
94
119
c .db ,err = sql .Open ("postgres" ,dsn )
95
120
if err != nil {
96
121
return xerrors .Errorf ("couldn't open DB: %w" ,err )