|
| 1 | +package pgfileurl_test |
| 2 | + |
| 3 | +import ( |
| 4 | +"database/sql" |
| 5 | +"os" |
| 6 | +"path/filepath" |
| 7 | +"testing" |
| 8 | + |
| 9 | +"github.com/stretchr/testify/require" |
| 10 | + |
| 11 | +"cdr.dev/slog/sloggers/slogtest" |
| 12 | + |
| 13 | +"github.com/coder/coder/v2/coderd/database/dbtestutil" |
| 14 | +"github.com/coder/coder/v2/coderd/database/pgfileurl" |
| 15 | +) |
| 16 | + |
| 17 | +funcTestRegister(t*testing.T) { |
| 18 | +t.Parallel() |
| 19 | + |
| 20 | +t.Run("ReadsURLFromFile",func(t*testing.T) { |
| 21 | +t.Parallel() |
| 22 | + |
| 23 | +connectionURL,err:=dbtestutil.Open(t) |
| 24 | +require.NoError(t,err) |
| 25 | + |
| 26 | +// Write connection URL to a temp file. |
| 27 | +tmpDir:=t.TempDir() |
| 28 | +filePath:=filepath.Join(tmpDir,"pg_url") |
| 29 | +err=os.WriteFile(filePath, []byte(connectionURL),0o600) |
| 30 | +require.NoError(t,err) |
| 31 | + |
| 32 | +// Register the file URL driver. |
| 33 | +driverName,err:=pgfileurl.Register("postgres",filePath,slogtest.Make(t,nil)) |
| 34 | +require.NoError(t,err) |
| 35 | +require.Contains(t,driverName,"postgres-fileurl-") |
| 36 | + |
| 37 | +// Open a connection using the file URL driver. |
| 38 | +// The DSN is ignored since the URL is read from the file. |
| 39 | +db,err:=sql.Open(driverName,"ignored") |
| 40 | +require.NoError(t,err) |
| 41 | +deferdb.Close() |
| 42 | + |
| 43 | +// Verify we can query the database. |
| 44 | +varresultint |
| 45 | +err=db.QueryRow("SELECT 1").Scan(&result) |
| 46 | +require.NoError(t,err) |
| 47 | +require.Equal(t,1,result) |
| 48 | +}) |
| 49 | + |
| 50 | +t.Run("ReReadsFileOnNewConnection",func(t*testing.T) { |
| 51 | +t.Parallel() |
| 52 | + |
| 53 | +connectionURL,err:=dbtestutil.Open(t) |
| 54 | +require.NoError(t,err) |
| 55 | + |
| 56 | +// Write connection URL to a temp file. |
| 57 | +tmpDir:=t.TempDir() |
| 58 | +filePath:=filepath.Join(tmpDir,"pg_url") |
| 59 | +err=os.WriteFile(filePath, []byte(connectionURL),0o600) |
| 60 | +require.NoError(t,err) |
| 61 | + |
| 62 | +// Register the file URL driver. |
| 63 | +driverName,err:=pgfileurl.Register("postgres",filePath,slogtest.Make(t,nil)) |
| 64 | +require.NoError(t,err) |
| 65 | + |
| 66 | +// Open a connection and verify it works. |
| 67 | +db,err:=sql.Open(driverName,"") |
| 68 | +require.NoError(t,err) |
| 69 | +deferdb.Close() |
| 70 | + |
| 71 | +// Force only one connection in the pool so we can test reconnection. |
| 72 | +db.SetMaxOpenConns(1) |
| 73 | +db.SetMaxIdleConns(0) |
| 74 | + |
| 75 | +varresultint |
| 76 | +err=db.QueryRow("SELECT 1").Scan(&result) |
| 77 | +require.NoError(t,err) |
| 78 | +require.Equal(t,1,result) |
| 79 | + |
| 80 | +// Update the file with an invalid URL. |
| 81 | +err=os.WriteFile(filePath, []byte("postgres://invalid:5432/db"),0o600) |
| 82 | +require.NoError(t,err) |
| 83 | + |
| 84 | +// Close existing connections to force a new one. |
| 85 | +db.SetMaxIdleConns(0) |
| 86 | + |
| 87 | +// The next query should fail because it will read the invalid URL. |
| 88 | +// We need to force a new connection by closing the pool. |
| 89 | +db2,err:=sql.Open(driverName,"") |
| 90 | +require.NoError(t,err) |
| 91 | +deferdb2.Close() |
| 92 | + |
| 93 | +err=db2.Ping() |
| 94 | +require.Error(t,err) |
| 95 | +}) |
| 96 | + |
| 97 | +t.Run("FileNotFound",func(t*testing.T) { |
| 98 | +t.Parallel() |
| 99 | + |
| 100 | +driverName,err:=pgfileurl.Register("postgres","/nonexistent/path",slogtest.Make(t,nil)) |
| 101 | +require.NoError(t,err)// Registration succeeds |
| 102 | + |
| 103 | +db,err:=sql.Open(driverName,"") |
| 104 | +require.NoError(t,err) |
| 105 | +deferdb.Close() |
| 106 | + |
| 107 | +// Connection should fail when trying to read the file. |
| 108 | +err=db.Ping() |
| 109 | +require.Error(t,err) |
| 110 | +require.Contains(t,err.Error(),"read database URL file") |
| 111 | +}) |
| 112 | +} |