Files
Media-download-telegram-bot/internal/db/dbtest/dbtest.go
T
MichilisandClaude Fable 5.2 7d4893bc3f Phase 4: scale out
Remote workers, Postgres and S3, all chosen by configuration.

- Pull-based worker protocol over HTTP: claim (long poll), heartbeat,
  complete, fail, upload, source, probe. Workers need no inbound ports
- cmd/worker: standalone stateless worker with health endpoint and
  scheduled yt-dlp self-update
- Per-worker bearer tokens, stored hashed, revocable; identity always
  taken from the token; actions only on jobs the worker holds a lease for
- Tags: site_tags routes a site's jobs and lookups to workers that carry
  the required tags
- Drain, /workers, /ytdlp update, and "core worker add|revoke|list"
- Link lookups can run on remote workers (PROBE_MODE), so the core needs
  no yt-dlp and no residential address
- Postgres backend with pgx, same schema and queries; per-user advisory
  lock and job row lock where SQLite's serial writes were relied on
- The whole test suite runs on SQLite and on Postgres
- S3 storage backend with presigned worker uploads, sharing a conformance
  suite with the local backend
- Dockerfile with core, worker and faketelegram targets, docker-compose
  with optional Postgres and local Bot API server, systemd units
- deploy/acceptance/kill-worker.sh: kills a worker mid-job and checks
  that the other one delivers the file
- Fixes found on the way: /cancel could let the next playlist item start;
  a refund note could be missing when the sweep refunded first

Docker was not available on the build machine: the compose files were
validated with "docker compose config" only. The acceptance scenario was
run with real processes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 09:27:11 +00:00

82 lines
2.0 KiB
Go

// Package dbtest opens throwaway databases for tests.
//
// By default every test gets its own SQLite file. To run the same tests
// against Postgres, point them at a server:
//
// DLBOT_TEST_DB=postgres \
// DLBOT_TEST_PG_DSN=postgres://user@127.0.0.1:5432/dlbot_test \
// go test ./...
//
// Each test then gets its own schema, which is dropped afterwards.
package dbtest
import (
"context"
"crypto/rand"
"database/sql"
"encoding/hex"
"net/url"
"os"
"path/filepath"
"testing"
_ "github.com/jackc/pgx/v5/stdlib" // for schema setup
"github.com/noderunners/dlbot/internal/db"
)
// Postgres reports whether tests run against Postgres.
func Postgres() bool { return os.Getenv("DLBOT_TEST_DB") == "postgres" }
// NewDSN returns the driver and DSN of a fresh, empty database for one
// test. It is removed when the test ends.
func NewDSN(t testing.TB) (driver, dsn string) {
t.Helper()
if !Postgres() {
return "sqlite", filepath.Join(t.TempDir(), "test.db")
}
base := os.Getenv("DLBOT_TEST_PG_DSN")
if base == "" {
t.Fatal("DLBOT_TEST_DB=postgres needs DLBOT_TEST_PG_DSN")
}
var raw [6]byte
_, _ = rand.Read(raw[:])
schema := "t_" + hex.EncodeToString(raw[:])
admin, err := sql.Open("pgx", base)
if err != nil {
t.Fatalf("open postgres: %v", err)
}
if _, err := admin.Exec(`CREATE SCHEMA ` + schema); err != nil {
admin.Close()
t.Fatalf("create schema: %v", err)
}
t.Cleanup(func() {
_, _ = admin.Exec(`DROP SCHEMA ` + schema + ` CASCADE`)
admin.Close()
})
u, err := url.Parse(base)
if err != nil {
t.Fatalf("bad DLBOT_TEST_PG_DSN: %v", err)
}
q := u.Query()
q.Set("search_path", schema)
u.RawQuery = q.Encode()
return "postgres", u.String()
}
// Open returns a migrated database that is removed when the test ends.
func Open(t testing.TB) *db.DB {
t.Helper()
ctx := context.Background()
driver, dsn := NewDSN(t)
d, err := db.Open(ctx, driver, dsn)
if err != nil {
t.Fatalf("open db: %v", err)
}
t.Cleanup(func() { d.Close() })
if err := d.Migrate(ctx); err != nil {
t.Fatalf("migrate: %v", err)
}
return d
}