149 lines
6.0 KiB
Go
149 lines
6.0 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"github.com/go-sql-driver/mysql"
|
|
"io/fs"
|
|
"os"
|
|
"sea-vhost/internal/migrations"
|
|
"sea-vhost/internal/scope"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestExistingSingleNodeMigrationMySQL(t *testing.T) {
|
|
testSingleNodeMigrationMySQL(t, false)
|
|
}
|
|
|
|
func TestInterruptedSingleNodeMigrationMySQL(t *testing.T) {
|
|
testSingleNodeMigrationMySQL(t, true)
|
|
}
|
|
|
|
func testSingleNodeMigrationMySQL(t *testing.T, interrupted bool) {
|
|
dsn := os.Getenv("SEA_TEST_MYSQL_DSN")
|
|
if dsn == "" {
|
|
t.Skip("dedicated MySQL schema not supplied")
|
|
}
|
|
cfg, err := mysql.ParseDSN(dsn)
|
|
if err != nil || !strings.HasPrefix(cfg.DBName, "svh_cluster_test_") {
|
|
t.Fatal("non-test schema denied")
|
|
}
|
|
database, err := Open(dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer database.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute)
|
|
defer cancel()
|
|
var sessionZone string
|
|
if err = database.db.QueryRowContext(ctx, "SELECT @@session.time_zone").Scan(&sessionZone); err != nil || sessionZone != "+00:00" {
|
|
t.Fatalf("platform session is not UTC: %q %v", sessionZone, err)
|
|
}
|
|
var tables int
|
|
if err = database.db.QueryRowContext(ctx, "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=DATABASE()").Scan(&tables); err != nil || tables != 0 {
|
|
t.Fatal("legacy fixture requires an empty disposable schema")
|
|
}
|
|
entries, err := fs.ReadDir(migrations.Files, ".")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, entry := range entries {
|
|
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") || entry.Name() >= "012_" {
|
|
continue
|
|
}
|
|
raw, err := migrations.Files.ReadFile(entry.Name())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, statement := range strings.Split(string(raw), ";") {
|
|
if strings.TrimSpace(statement) == "" {
|
|
continue
|
|
}
|
|
if _, err = database.db.ExecContext(ctx, statement); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err = database.db.ExecContext(ctx, "INSERT INTO schema_migrations(version) VALUES(?)", entry.Name()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// This fixture intentionally predates migration 014's bandwidth_mbps.
|
|
// Read only historical columns until the current migrations have run.
|
|
var plan Plan
|
|
if err = database.db.QueryRowContext(ctx, "SELECT id,process_limit FROM plans ORDER BY id LIMIT 1").Scan(&plan.ID, &plan.ProcessLimit); err != nil {
|
|
t.Fatalf("legacy plan fixture: %v", err)
|
|
}
|
|
tenant, err := database.CreateTenant(ctx, NewTenant{Slug: "legacy-test", Name: "旧版迁移测试", Email: "legacy@example.invalid", DisplayName: "迁移测试", Password: "legacy-test-only-12345", PlanID: plan.ID})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
expiry := time.Now().UTC().Add(-5 * 24 * time.Hour).Truncate(time.Second)
|
|
if _, err = database.db.ExecContext(ctx, `UPDATE tenants SET status='suspended',lifecycle_state='recycle',expires_at=?,grace_until=DATE_ADD(?,INTERVAL 3 DAY),purge_after=DATE_ADD(?,INTERVAL 10 DAY),backup_purge_after=DATE_ADD(?,INTERVAL 24 DAY) WHERE id=?`, expiry, expiry, expiry, expiry, tenant); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := database.db.ExecContext(ctx, `INSERT INTO projects(tenant_id,host_name,slug,runtime,runtime_version,root_path,run_path,assigned_domain,panel_site_id,backend_port,process_limit,status) VALUES(?,'sv3333333333','legacy-host','PHP','8.4','/www/wwwroot/seavHost/vHost/sv3333333333','/custom','sv3333333333.legacy.example.invalid',1001,20000,7,'stopped')`, tenant)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id, _ := result.LastInsertId()
|
|
_, err = database.db.ExecContext(ctx, `INSERT INTO sites(tenant_id,project_id,slug,assigned_domain,run_path,runtime,runtime_version,panel_site_id,backend_port,process_limit,status,is_primary) VALUES(?,?,'legacy-site','sv3333333333.legacy.example.invalid','/custom','PHP','8.4',1001,20000,7,'stopped',TRUE)`, tenant, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if interrupted {
|
|
raw, err := migrations.Files.ReadFile("012_control_nodes.sql")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, statement := range strings.Split(string(raw), ";") {
|
|
statement = strings.TrimSpace(statement)
|
|
if statement == "" {
|
|
continue
|
|
}
|
|
if _, err = database.db.ExecContext(ctx, statement); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Simulate a process dying after the old account lifecycle was reset,
|
|
// without recording the migration version. Expiry must not be lost.
|
|
if strings.HasPrefix(statement, "UPDATE tenants SET status='active'") {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if err = database.Migrate(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = database.Migrate(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
project, err := database.Project(ctx, tenant, uint64(id))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
owner, err := database.ProjectOwner(ctx, project.ID)
|
|
if err != nil || owner == nil || *owner != tenant {
|
|
t.Fatalf("legacy customer ownership was not backfilled: %v", err)
|
|
}
|
|
if project.NodeID != 1 || project.PlanID != plan.ID || project.ProcessLimit != int(plan.ProcessLimit) || project.Status != "stopped" || project.RunPath != "/custom" || project.Domain != "sv3333333333.legacy.example.invalid" {
|
|
t.Fatalf("legacy host changed unexpectedly: %+v", project)
|
|
}
|
|
sites, err := database.ListSites(ctx, tenant, project.ID)
|
|
if err != nil || len(sites) != 1 || sites[0].ProcessLimit != int(plan.ProcessLimit) || sites[0].Status != "stopped" {
|
|
t.Fatalf("legacy site migration: %+v %v", sites, err)
|
|
}
|
|
var copied time.Time
|
|
var state string
|
|
if err = database.db.QueryRowContext(ctx, "SELECT expires_at,lifecycle_state FROM projects WHERE id=?", id).Scan(&copied, &state); err != nil || !copied.Equal(expiry) || state != "recycle" {
|
|
t.Fatalf("legacy expiry not preserved: %v %s %v", copied, state, err)
|
|
}
|
|
if _, err = database.TenantOwner(ctx, tenant); err != nil {
|
|
t.Fatalf("host expiry still disables the user: %v", err)
|
|
}
|
|
p, _, err := database.PlanAndUsage(scope.WithProject(ctx, project.ID, 1), tenant)
|
|
if err != nil || p.ID != plan.ID {
|
|
t.Fatalf("host plan mapping: %+v %v", p, err)
|
|
}
|
|
t.Log("existing single-node host, stopped state, custom run directory, assigned domain, full per-host process allowance, expiration and user account separation preserved")
|
|
}
|