|
| 1 | +// Package db: coverage for the genuinely-unreachable error branches in the |
| 2 | +// migration runner + ConnectPostgres, driven through the package-var seams |
| 3 | +// (readMigrationDir / readMigrationFile / sqlOpen). These paths can't be |
| 4 | +// reached with the real embedded FS (compiled in, always readable) or the |
| 5 | +// lib/pq driver (sql.Open is lazy and never errors on a DSN), so the seams |
| 6 | +// let a test inject the failure deterministically. |
| 7 | +// |
| 8 | +// Rule-17 coverage block: |
| 9 | +// Symptom: postgres.go 34-36 / 40-42 / 72-74 / 177-178 uncovered (94.1%) |
| 10 | +// Enumeration: grep 'postgres.go' cover.out | awk '$NF==0' |
| 11 | +// Sites found: 4 zero-count blocks |
| 12 | +// Sites touched: 4 (this file) |
| 13 | +// Coverage test: TestRunMigrations_FilenameListError, _FileReadError, |
| 14 | +// TestEmbeddedMigrationFilenames_ReadDirError, |
| 15 | +// TestConnectPostgres_PanicsOnOpenError, |
| 16 | +// TestStartPoolStatsExporter_CtxDoneAfterFirstTick |
| 17 | +package db |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "database/sql" |
| 22 | + "errors" |
| 23 | + "io/fs" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | +) |
| 28 | + |
| 29 | +// withSeams swaps the package-var seams for the duration of a test and |
| 30 | +// restores them after. Centralises the save/restore so a forgotten restore |
| 31 | +// can't leak into the next test in the serialized (-p 1) run. |
| 32 | +func withSeams(t *testing.T, dir func() ([]fs.DirEntry, error), file func(string) ([]byte, error), open func(string, string) (*sql.DB, error)) { |
| 33 | + t.Helper() |
| 34 | + origDir, origFile, origOpen := readMigrationDir, readMigrationFile, sqlOpen |
| 35 | + if dir != nil { |
| 36 | + readMigrationDir = dir |
| 37 | + } |
| 38 | + if file != nil { |
| 39 | + readMigrationFile = file |
| 40 | + } |
| 41 | + if open != nil { |
| 42 | + sqlOpen = open |
| 43 | + } |
| 44 | + t.Cleanup(func() { |
| 45 | + readMigrationDir = origDir |
| 46 | + readMigrationFile = origFile |
| 47 | + sqlOpen = origOpen |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +// TestEmbeddedMigrationFilenames_ReadDirError covers postgres.go:72-74 — the |
| 52 | +// fs.ReadDir failure path inside embeddedMigrationFilenames. |
| 53 | +func TestEmbeddedMigrationFilenames_ReadDirError(t *testing.T) { |
| 54 | + sentinel := errors.New("boom: embed dir unreadable") |
| 55 | + withSeams(t, func() ([]fs.DirEntry, error) { return nil, sentinel }, nil, nil) |
| 56 | + |
| 57 | + names, err := embeddedMigrationFilenames() |
| 58 | + if err == nil { |
| 59 | + t.Fatal("embeddedMigrationFilenames: want error, got nil") |
| 60 | + } |
| 61 | + if names != nil { |
| 62 | + t.Errorf("names should be nil on error, got %v", names) |
| 63 | + } |
| 64 | + if !errors.Is(err, sentinel) { |
| 65 | + t.Errorf("error should wrap sentinel: %v", err) |
| 66 | + } |
| 67 | + if !strings.Contains(err.Error(), "read dir") { |
| 68 | + t.Errorf("error wrapping lost 'read dir': %v", err) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// TestRunMigrations_FilenameListError covers postgres.go:34-36 — RunMigrations |
| 73 | +// returns early when embeddedMigrationFilenames (via the dir seam) errors. |
| 74 | +func TestRunMigrations_FilenameListError(t *testing.T) { |
| 75 | + sentinel := errors.New("boom: cannot list migrations") |
| 76 | + withSeams(t, func() ([]fs.DirEntry, error) { return nil, sentinel }, nil, nil) |
| 77 | + |
| 78 | + err := RunMigrations(nil) // db never touched — list fails first |
| 79 | + if err == nil { |
| 80 | + t.Fatal("RunMigrations: want error from filename list, got nil") |
| 81 | + } |
| 82 | + if !strings.Contains(err.Error(), "db.RunMigrations") { |
| 83 | + t.Errorf("error wrapping lost prefix: %v", err) |
| 84 | + } |
| 85 | + if !errors.Is(err, sentinel) { |
| 86 | + t.Errorf("error should wrap sentinel: %v", err) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// TestRunMigrations_FileReadError covers postgres.go:40-42 — RunMigrations |
| 91 | +// returns when reading a listed migration file fails. The dir seam yields a |
| 92 | +// non-empty name list so the loop body runs, and the file seam fails the read. |
| 93 | +func TestRunMigrations_FileReadError(t *testing.T) { |
| 94 | + sentinel := errors.New("boom: file vanished") |
| 95 | + withSeams(t, |
| 96 | + func() ([]fs.DirEntry, error) { return []fs.DirEntry{fakeDirEntry{name: "001_x.sql"}}, nil }, |
| 97 | + func(string) ([]byte, error) { return nil, sentinel }, |
| 98 | + nil, |
| 99 | + ) |
| 100 | + |
| 101 | + err := RunMigrations(nil) // db never reached — read fails before Exec |
| 102 | + if err == nil { |
| 103 | + t.Fatal("RunMigrations: want error from file read, got nil") |
| 104 | + } |
| 105 | + if !strings.Contains(err.Error(), "read 001_x.sql") { |
| 106 | + t.Errorf("error should name the failing file: %v", err) |
| 107 | + } |
| 108 | + if !errors.Is(err, sentinel) { |
| 109 | + t.Errorf("error should wrap sentinel: %v", err) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// fakeDirEntry is a minimal fs.DirEntry so the dir seam can return a |
| 114 | +// synthetic non-empty file list without touching a real FS. |
| 115 | +type fakeDirEntry struct{ name string } |
| 116 | + |
| 117 | +func (f fakeDirEntry) Name() string { return f.name } |
| 118 | +func (f fakeDirEntry) IsDir() bool { return false } |
| 119 | +func (f fakeDirEntry) Type() fs.FileMode { return 0 } |
| 120 | +func (f fakeDirEntry) Info() (fs.FileInfo, error) { return nil, nil } |
| 121 | + |
| 122 | +// TestConnectPostgres_PanicsOnOpenError covers postgres.go:177-178 — the |
| 123 | +// sql.Open failure panic. lib/pq's Open is lazy and never errors on a DSN, |
| 124 | +// so this branch is only reachable via the sqlOpen seam. |
| 125 | +func TestConnectPostgres_PanicsOnOpenError(t *testing.T) { |
| 126 | + sentinel := errors.New("boom: driver open failed") |
| 127 | + withSeams(t, nil, nil, func(string, string) (*sql.DB, error) { return nil, sentinel }) |
| 128 | + |
| 129 | + defer func() { |
| 130 | + r := recover() |
| 131 | + if r == nil { |
| 132 | + t.Fatal("ConnectPostgres: expected panic on open error") |
| 133 | + } |
| 134 | + e, ok := r.(*ErrDBConnect) |
| 135 | + if !ok { |
| 136 | + t.Fatalf("panic value: want *ErrDBConnect, got %T (%v)", r, r) |
| 137 | + } |
| 138 | + if !errors.Is(e.Cause, sentinel) { |
| 139 | + t.Errorf("ErrDBConnect.Cause should wrap sentinel, got %v", e.Cause) |
| 140 | + } |
| 141 | + }() |
| 142 | + ConnectPostgres("postgres://whatever") |
| 143 | +} |
| 144 | + |
| 145 | +// TestRunExporterLoop_CtxDoneArm drives the ctx.Done() branch |
| 146 | +// (pool_metrics.go ctx.Done case) SYNCHRONOUSLY on the test goroutine, so the |
| 147 | +// atomic coverage counter is recorded deterministically. A pre-cancelled |
| 148 | +// context + an empty (never-firing) tick channel forces the ctx.Done() arm. |
| 149 | +func TestRunExporterLoop_CtxDoneArm(t *testing.T) { |
| 150 | + dsn := testDSN() |
| 151 | + pool, err := sql.Open("postgres", dsn) |
| 152 | + if err != nil { |
| 153 | + t.Skipf("postgres open: %v", err) |
| 154 | + } |
| 155 | + defer pool.Close() |
| 156 | + |
| 157 | + ctx, cancel := context.WithCancel(context.Background()) |
| 158 | + cancel() // already done before the loop is entered |
| 159 | + tick := make(chan time.Time) |
| 160 | + |
| 161 | + // runs on this goroutine and returns immediately via the ctx.Done() arm. |
| 162 | + runExporterLoop(ctx, pool, "rbw-ctxdone", tick) |
| 163 | +} |
| 164 | + |
| 165 | +// TestRunExporterLoop_TickArm drives the ticker arm synchronously: a pre-fed |
| 166 | +// tick channel publishes one sample, then ctx cancellation exits the loop. |
| 167 | +func TestRunExporterLoop_TickArm(t *testing.T) { |
| 168 | + dsn := testDSN() |
| 169 | + pool, err := sql.Open("postgres", dsn) |
| 170 | + if err != nil { |
| 171 | + t.Skipf("postgres open: %v", err) |
| 172 | + } |
| 173 | + defer pool.Close() |
| 174 | + |
| 175 | + tick := make(chan time.Time, 1) |
| 176 | + tick <- time.Now() // one sample will publish |
| 177 | + |
| 178 | + ctx, cancel := context.WithCancel(context.Background()) |
| 179 | + // Cancel shortly after the single tick is consumed so the loop takes the |
| 180 | + // tick arm at least once, then exits via ctx.Done(). |
| 181 | + go func() { |
| 182 | + time.Sleep(50 * time.Millisecond) |
| 183 | + cancel() |
| 184 | + }() |
| 185 | + |
| 186 | + runExporterLoop(ctx, pool, "rbw-tick", tick) |
| 187 | +} |
| 188 | + |
| 189 | +// TestStartPoolStatsExporter_CtxDoneAfterFirstTick exercises the public entry |
| 190 | +// end-to-end (immediate publish + loop) and asserts clean shutdown on cancel. |
| 191 | +func TestStartPoolStatsExporter_CtxDoneAfterFirstTick(t *testing.T) { |
| 192 | + dsn := testDSN() |
| 193 | + pool, err := sql.Open("postgres", dsn) |
| 194 | + if err != nil { |
| 195 | + t.Skipf("postgres open: %v", err) |
| 196 | + } |
| 197 | + defer pool.Close() |
| 198 | + |
| 199 | + ctx, cancel := context.WithCancel(context.Background()) |
| 200 | + done := make(chan struct{}) |
| 201 | + go func() { |
| 202 | + StartPoolStatsExporter(ctx, pool, "rbw-ctxdone-e2e") |
| 203 | + close(done) |
| 204 | + }() |
| 205 | + time.Sleep(50 * time.Millisecond) |
| 206 | + cancel() |
| 207 | + |
| 208 | + select { |
| 209 | + case <-done: |
| 210 | + case <-time.After(2 * time.Second): |
| 211 | + t.Fatal("StartPoolStatsExporter did not return after ctx cancel") |
| 212 | + } |
| 213 | +} |
0 commit comments