211 lines
7.7 KiB
Go
211 lines
7.7 KiB
Go
package cluster
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/go-sql-driver/mysql"
|
|
"io"
|
|
"log"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"sea-vhost/internal/controlwire"
|
|
"sea-vhost/internal/executor"
|
|
"sea-vhost/internal/panel"
|
|
"sea-vhost/internal/scope"
|
|
"sea-vhost/internal/secrets"
|
|
"sea-vhost/internal/store"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestControllerTwoNodesMySQL(t *testing.T) {
|
|
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 := store.Open(dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer database.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
if err = database.Migrate(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plans, err := database.ListPlans(ctx)
|
|
if err != nil || len(plans) == 0 {
|
|
t.Fatal("plans unavailable")
|
|
}
|
|
tenant, err := database.CreateTenant(ctx, store.NewTenant{Slug: "rpc-test", Name: "RPC 测试用户", Email: "rpc@example.invalid", DisplayName: "RPC 测试", Password: "RPC-test-only-123456", PlanID: plans[0].ID})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manager := &Manager{Store: database, Box: secrets.New(strings.Repeat("test-only-secret", 4))}
|
|
client := manager.Panel()
|
|
type host struct {
|
|
node, project uint64
|
|
name, root, domain string
|
|
site, ftp, db int64
|
|
}
|
|
hosts := []host{}
|
|
for _, name := range []string{"sv1111111111", "sv2222222222"} {
|
|
root := filepath.ToSlash(t.TempDir())
|
|
if err = os.Mkdir(filepath.Join(root, name), 0750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := httptest.NewUnstartedServer(nil)
|
|
server.Config.ErrorLog = log.New(io.Discard, "", 0)
|
|
endpoint := "https://" + server.Listener.Addr().String()
|
|
node, err := database.CreateNode(ctx, store.Node{Name: name, Endpoint: endpoint, RootDomain: name + ".example.invalid", TenantRoot: root, FTPCertificateDomain: "ftp.example.invalid"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
identity, err := manager.Pair(ctx, node)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server.TLS, err = controlwire.ServerTLS(identity)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server.Config.Handler = (&executor.Gateway{NodeID: node, TenantRoot: root, Direct: panel.NewMock()}).Handler()
|
|
server.StartTLS()
|
|
t.Cleanup(server.Close)
|
|
if err = manager.Probe(ctx, node); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = database.SetNodeEnabled(ctx, node, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
project, err := database.InsertProject(ctx, store.NewProject{TenantID: tenant, NodeID: node, PlanID: plans[0].ID, HostName: name, Slug: name, Path: root + "/" + name, RunPath: "/public", Runtime: "PHP", Version: "8.4", Domain: name + ".example.invalid", ProcessLimit: 10, HostPasswordHash: "test-only"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := host{node: node, project: project, name: name, root: root + "/" + name, domain: name + ".example.invalid"}
|
|
op := scope.WithProject(ctx, project, node)
|
|
h.site, err = client.CreateSite(op, panel.SiteSpec{Name: name, Domain: h.domain, Path: h.root, RunPath: "/public", Runtime: "PHP", Version: "8.4", RunUser: name, Port: 20000, ProcessLimit: 10})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h.ftp, err = client.CreateFTP(op, name, "TestPassword123456789", h.root, 1<<20)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h.db, err = client.CreateDatabase(op, name, name, "TestPassword123456789", h.site)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = database.CompleteProject(op, project, h.site, h.ftp, h.db, name, name, name, []byte("cipher"), []byte("cipher"), "running", "", "", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rootFiles, err := manager.OpenFiles(op, h.root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
file, err := rootFiles.OpenFile("marker.txt", os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0660)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = file.Write([]byte(name)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = file.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = manager.Isolation().Ping(op); err != nil {
|
|
t.Fatalf("remote GET health routing: %v", err)
|
|
}
|
|
hosts = append(hosts, h)
|
|
}
|
|
if hosts[0].site != hosts[1].site || hosts[0].ftp != hosts[1].ftp || hosts[0].db != hosts[1].db {
|
|
t.Fatal("native ID collision was not exercised")
|
|
}
|
|
for _, h := range hosts {
|
|
op := scope.WithProject(ctx, h.project, h.node)
|
|
lease, err := database.LockNodeRegistry(op, h.node)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, err := lease.Read(op)
|
|
lease.Close()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var registry struct {
|
|
Sites map[string]json.RawMessage `json:"sites"`
|
|
}
|
|
if json.Unmarshal(raw, ®istry) != nil || len(registry.Sites) != 1 || !strings.Contains(string(raw), h.domain) {
|
|
t.Fatalf("controller registry mismatch: %s", raw)
|
|
}
|
|
targets, err := database.RuntimeTargets(op)
|
|
if err != nil || len(targets) != 1 || targets[0].Project.NodeID != h.node {
|
|
t.Fatalf("runtime target placement: %+v %v", targets, err)
|
|
}
|
|
if err = client.SetSiteState(op, h.site, h.domain, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
a := hosts[0]
|
|
b := hosts[1]
|
|
opA := scope.WithProject(ctx, a.project, a.node)
|
|
opB := scope.WithProject(ctx, b.project, b.node)
|
|
if _, err = client.CreateSite(opA, panel.SiteSpec{Name: a.name, Domain: "invalid.example.invalid", Path: "/outside/namespace", RunPath: "/", Runtime: "PHP", Version: "8.4", RunUser: a.name, Port: 20001, ProcessLimit: 10}); !errors.Is(err, ErrUncertain) {
|
|
t.Fatalf("write rejection should preserve an uncertain task: %v", err)
|
|
}
|
|
if err = client.SetSiteState(opA, a.site, a.domain, true); !errors.Is(err, ErrUncertain) {
|
|
t.Fatalf("unresolved node mutation was allowed: %v", err)
|
|
}
|
|
if _, err = database.CreateRuntimeJob(opA, store.RuntimeJob{Action: "check", Runtime: "all", TargetVersion: "-"}); err == nil {
|
|
t.Fatal("node-wide maintenance ignored unresolved host state")
|
|
}
|
|
// A tenant-triggered failure must not freeze another host on the same node.
|
|
peerName := "sv4444444444"
|
|
peerRoot := filepath.Join(filepath.Dir(a.root), peerName)
|
|
if err = os.Mkdir(peerRoot, 0750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
peer, err := database.InsertProject(ctx, store.NewProject{TenantID: tenant, NodeID: a.node, PlanID: plans[0].ID, HostName: peerName, Slug: peerName, Path: peerRoot, RunPath: "/public", Runtime: "PHP", Version: "8.4", Domain: peerName + ".example.invalid", ProcessLimit: 10, HostPasswordHash: "test-only"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
peerCtx := scope.WithProject(ctx, peer, a.node)
|
|
peerSite, err := client.CreateSite(peerCtx, panel.SiteSpec{Name: peerName, Domain: peerName + ".example.invalid", Path: peerRoot, RunPath: "/public", Runtime: "PHP", Version: "8.4", RunUser: peerName, Port: 20020, ProcessLimit: 10})
|
|
if err != nil {
|
|
t.Fatalf("host A failure blocked another host on the same node: %v", err)
|
|
}
|
|
if err = client.SetSiteState(peerCtx, peerSite, peerName+".example.invalid", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = client.SetSiteState(opB, b.site, b.domain, true); err != nil {
|
|
t.Fatalf("node A failure blocked node B: %v", err)
|
|
}
|
|
tasks, err := database.ControlTasks(ctx, a.node)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resolved := false
|
|
for _, task := range tasks {
|
|
if task.State == "uncertain" {
|
|
if err = database.ResolveControlTask(ctx, a.node, task.ID, "测试已核对拒绝发生在变更之前"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resolved = true
|
|
}
|
|
}
|
|
if !resolved {
|
|
t.Fatal("uncertain operation was not recorded centrally")
|
|
}
|
|
if err = client.SetSiteState(opA, a.site, a.domain, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Log("controller MySQL + two mTLS gateways: native ID collision, central snapshots, site/FTP/database operations, file routing, health routing, maintenance placement and independent failure recovery passed")
|
|
}
|