Files
pangolin/server/internal/store/mysql_integration_test.go
wangjia 0fd3bce7a5 fix(server): 修复腐烂的集成测试套件 + devices 注册回填 last_seen
这批 -tags integration 测试(usage/auth/codes/devices/store)因依赖冲突
长期编译不过、从未进 CI 跑过,代码静默腐烂。本次逐层修复:

- 依赖:testcontainers-go v0.34→v0.43(原 v0.34 配 docker v28.3.3 编译失败:
  archive.Compression/sockets.DialPipe undefined)。
- auth: LoginOutcome 重构成 Tokens 嵌套后,测试仍引用扁平 RefreshToken;
  users 测试 schema 缺 totp_enabled 列 → GetUserByEmail 报错 → SendCode 500。
- usage/auth/codes/devices: DSN 里 time_zone='+00:00' 作 URL query 透传时 '+'
  被解码成空格 → MySQL Error 1298 ' 00:00';改百分号编码。
- store: 测试 DSN 缺 multiStatements=true → 多语句迁移 Error 1064;
  WithConfigFile("") 在 v0.43 被拒,改写真 my.cnf 设 +08:00 真正考验 UTC 覆盖。
- devices(产品 bug):insertDeviceTx 把 last_seen 写进库却没回填返回值,
  刚注册的设备 API 响应 last_seen=null 与库不一致;TestFullChain 据此把关。

修复后完整 integration 套件 -p 1 串行全绿(25 包 + 5 testcontainers 包)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 21:19:10 +08:00

128 lines
4.2 KiB
Go

//go:build integration
package store_test
import (
"context"
"os"
"path/filepath"
"testing"
mysqlmodule "github.com/testcontainers/testcontainers-go/modules/mysql"
"github.com/wangjia/pangolin/server/internal/config"
"github.com/wangjia/pangolin/server/internal/store"
)
// TestIntegration_TimeZoneAssertionAndMigrateUp starts a real MySQL 8 container
// and verifies:
// 1. store.Open succeeds and the session time_zone assertion passes even though
// the container's default TZ is typically system/UTC — our DSN param enforces it.
// 2. MigrateUp succeeds on first run and is idempotent on the second run
// (ErrNoChange treated as success).
// 3. MigrateDown succeeds and returns the schema to baseline.
func TestIntegration_TimeZoneAssertionAndMigrateUp(t *testing.T) {
ctx := context.Background()
ctr, err := mysqlmodule.Run(ctx,
"mysql:8",
mysqlmodule.WithDatabase("pangolin"),
mysqlmodule.WithUsername("root"),
mysqlmodule.WithPassword("secret"),
)
if err != nil {
t.Fatalf("start mysql container: %v", err)
}
t.Cleanup(func() {
if err := ctr.Terminate(ctx); err != nil {
t.Logf("terminate container: %v", err)
}
})
// ConnectionString returns e.g. root:secret@tcp(localhost:PORT)/pangolin
dsn, err := ctr.ConnectionString(ctx, "multiStatements=true")
if err != nil {
t.Fatalf("connection string: %v", err)
}
cfg := &config.Config{DSN: dsn}
// ── 1. Open (includes UTC assertion) ─────────────────────────────────────
db, err := store.Open(cfg)
if err != nil {
t.Fatalf("store.Open: %v", err)
}
defer db.Close()
// ── 2. First MigrateUp ───────────────────────────────────────────────────
if err := store.MigrateUp(db, "mysql"); err != nil {
t.Fatalf("MigrateUp (first): %v", err)
}
v, dirty, err := store.MigrateVersion(db, "mysql")
if err != nil {
t.Fatalf("MigrateVersion after up: %v", err)
}
if dirty {
t.Errorf("schema is dirty after MigrateUp")
}
if v == 0 {
t.Errorf("version is still 0 after MigrateUp — no migrations applied?")
}
t.Logf("after MigrateUp: version=%d dirty=%v", v, dirty)
// ── 3. Idempotent second MigrateUp ───────────────────────────────────────
if err := store.MigrateUp(db, "mysql"); err != nil {
t.Fatalf("MigrateUp (idempotent): %v", err)
}
// ── 4. MigrateDown ───────────────────────────────────────────────────────
if err := store.MigrateDown(db, "mysql"); err != nil {
t.Fatalf("MigrateDown: %v", err)
}
v2, _, err := store.MigrateVersion(db, "mysql")
if err != nil {
t.Fatalf("MigrateVersion after down: %v", err)
}
t.Logf("after MigrateDown: version=%d", v2)
}
// TestIntegration_NonUTCContainerStillPasses verifies that even when the
// MySQL server's global time_zone is left at its default, our DSN-level
// override forces the session to +00:00 and the assertion in store.Open passes.
func TestIntegration_NonUTCContainerStillPasses(t *testing.T) {
ctx := context.Background()
// Deliberately start MySQL with a non-UTC global timezone (+08:00) via a
// my.cnf, so this test genuinely exercises store.Open's session-level UTC
// override + assertion. (offset zone needs no tz tables loaded.)
cnf := filepath.Join(t.TempDir(), "tz.cnf")
if err := os.WriteFile(cnf, []byte("[mysqld]\ndefault-time-zone='+08:00'\n"), 0o644); err != nil {
t.Fatalf("write my.cnf: %v", err)
}
ctr, err := mysqlmodule.Run(ctx,
"mysql:8",
mysqlmodule.WithDatabase("pangolin"),
mysqlmodule.WithUsername("root"),
mysqlmodule.WithPassword("secret"),
mysqlmodule.WithConfigFile(cnf),
)
if err != nil {
t.Fatalf("start mysql container: %v", err)
}
t.Cleanup(func() { _ = ctr.Terminate(ctx) })
dsn, err := ctr.ConnectionString(ctx, "multiStatements=true")
if err != nil {
t.Fatalf("connection string: %v", err)
}
cfg := &config.Config{DSN: dsn}
db, err := store.Open(cfg)
if err != nil {
t.Fatalf("store.Open with default container TZ: %v", err)
}
db.Close()
}