Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaf3ff82

Browse files
authored
test: track postgres database creation by package and test name (#20492)
Adds columns to track package and test name to test_databases table, and populates them as databases are created using the Broker.In order to seamlessly work with existing `coder_database` databases with the old schema, the SQL that creates the table and columns is additive and idempotent, so we run it every time we initialize the Broker (once per test binary execution).We include a transaction level advisorly lock to prevent deadlocks before attempting to alter the schema. I was seeing deadlocks without this.
1 parent50ba223 commitaf3ff82

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

‎Makefile‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,3 +1182,8 @@ endif
11821182

11831183
dogfood/coder/nix.hash: flake.nix flake.lock
11841184
sha256sum flake.nix flake.lock>./dogfood/coder/nix.hash
1185+
1186+
# Count the number of test databases created per test package.
1187+
count-test-databases:
1188+
PGPASSWORD=postgres psql -h localhost -U postgres -d coder_testing -P pager=off -c'SELECT test_package, count(*) as count from test_databases GROUP BY test_package ORDER BY count DESC'
1189+
.PHONY: count-test-databases

‎coderd/database/dbtestutil/broker.go‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
_"embed"
77
"fmt"
88
"os"
9+
"runtime"
10+
"strings"
911
"sync"
1012
"time"
1113

@@ -45,6 +47,8 @@ func (b *Broker) Create(t TBSubset, opts ...OpenOption) (ConnectionParams, error
4547
host=defaultConnectionParams.Host
4648
port=defaultConnectionParams.Port
4749
)
50+
packageName:=getTestPackageName(t)
51+
testName:=t.Name()
4852

4953
// Use a time-based prefix to make it easier to find the database
5054
// when debugging.
@@ -55,9 +59,9 @@ func (b *Broker) Create(t TBSubset, opts ...OpenOption) (ConnectionParams, error
5559
}
5660
dbName:=now+"_"+dbSuffix
5761

58-
// TODO: add package and test name
5962
_,err=b.coderTestingDB.Exec(
60-
"INSERT INTO test_databases (name, process_uuid) VALUES ($1, $2)",dbName,b.uuid)
63+
"INSERT INTO test_databases (name, process_uuid, test_package, test_name) VALUES ($1, $2, $3, $4)",
64+
dbName,b.uuid,packageName,testName)
6165
iferr!=nil {
6266
returnConnectionParams{},xerrors.Errorf("insert test_database row: %w",err)
6367
}
@@ -104,10 +108,10 @@ func (b *Broker) clean(t TBSubset, dbName string) func() {
104108
func (b*Broker)init(tTBSubset)error {
105109
b.Lock()
106110
deferb.Unlock()
107-
b.refCount++
108-
t.Cleanup(b.decRef)
109111
ifb.coderTestingDB!=nil {
110112
// already initialized
113+
b.refCount++
114+
t.Cleanup(b.decRef)
111115
returnnil
112116
}
113117

@@ -124,8 +128,8 @@ func (b *Broker) init(t TBSubset) error {
124128
returnxerrors.Errorf("open postgres connection: %w",err)
125129
}
126130

127-
//creating the db can succeed even if the database doesn't exist. Pingitto find out.
128-
err=coderTestingDB.Ping()
131+
//coderTestingSQLInit is idempotent, so we can runitevery time.
132+
_,err=coderTestingDB.Exec(coderTestingSQLInit)
129133
varpqErr*pq.Error
130134
ifxerrors.As(err,&pqErr)&&pqErr.Code=="3D000" {
131135
// database does not exist.
@@ -145,6 +149,8 @@ func (b *Broker) init(t TBSubset) error {
145149
returnxerrors.Errorf("ping '%s' database: %w",CoderTestingDBName,err)
146150
}
147151
b.coderTestingDB=coderTestingDB
152+
b.refCount++
153+
t.Cleanup(b.decRef)
148154

149155
ifb.uuid==uuid.Nil {
150156
b.uuid=uuid.New()
@@ -186,3 +192,42 @@ func (b *Broker) decRef() {
186192
b.coderTestingDB=nil
187193
}
188194
}
195+
196+
// getTestPackageName returns the package name of the test that called it.
197+
funcgetTestPackageName(tTBSubset)string {
198+
packageName:="unknown"
199+
// Ask runtime.Callers for up to 100 program counters, including runtime.Callers itself.
200+
pc:=make([]uintptr,100)
201+
n:=runtime.Callers(0,pc)
202+
ifn==0 {
203+
// No PCs available. This can happen if the first argument to
204+
// runtime.Callers is large.
205+
//
206+
// Return now to avoid processing the zero Frame that would
207+
// otherwise be returned by frames.Next below.
208+
t.Logf("could not determine test package name: no PCs available")
209+
returnpackageName
210+
}
211+
212+
pc=pc[:n]// pass only valid pcs to runtime.CallersFrames
213+
frames:=runtime.CallersFrames(pc)
214+
215+
// Loop to get frames.
216+
// A fixed number of PCs can expand to an indefinite number of Frames.
217+
for {
218+
frame,more:=frames.Next()
219+
220+
ifstrings.HasPrefix(frame.Function,"github.com/coder/coder/v2/") {
221+
packageName=strings.SplitN(strings.TrimPrefix(frame.Function,"github.com/coder/coder/v2/"),".",2)[0]
222+
}
223+
ifstrings.HasPrefix(frame.Function,"testing") {
224+
break
225+
}
226+
227+
// Check whether there are more frames to process after this one.
228+
if!more {
229+
break
230+
}
231+
}
232+
returnpackageName
233+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dbtestutil
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
funcTestGetTestPackageName(t*testing.T) {
10+
t.Parallel()
11+
packageName:=getTestPackageName(t)
12+
require.Equal(t,"coderd/database/dbtestutil",packageName)
13+
}

‎coderd/database/dbtestutil/coder_testing.sql‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
BEGIN TRANSACTION;
2+
SELECT pg_advisory_xact_lock(7283699);
3+
14
CREATETABLEIF NOT EXISTS test_databases (
25
nametextPRIMARY KEY,
36
created_attimestamp with time zoneNOT NULL DEFAULTCURRENT_TIMESTAMP,
@@ -6,3 +9,10 @@ CREATE TABLE IF NOT EXISTS test_databases (
69
);
710

811
CREATEINDEXIF NOT EXISTS test_databases_process_uuidON test_databases (process_uuid, dropped_at);
12+
13+
ALTERTABLE test_databases ADD COLUMN IF NOT EXISTS test_nametext;
14+
COMMENT ON COLUMN test_databases.test_name IS'Name of the test that created the database';
15+
ALTERTABLE test_databases ADD COLUMN IF NOT EXISTS test_packagetext;
16+
COMMENT ON COLUMN test_databases.test_package IS'Package of the test that created the database';
17+
18+
COMMIT;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp