只锁治理库 scope 挡不住 WeKnora 直连和后台写入。把封存链路的 维护机制接到发布路径:持久化 owner/目标身份/原服务状态,关入口、 停写入者、验证排空与入口不可达,再写目标;写完先恢复原运行状态 与入口才释放占用。内容风险一旦落盘,重启恢复不再自动启动写入者。 新增 000071 迁移记录 level/指纹/服务/入口状态;未接线的目标直接 拒绝写,不用库级锁冒充系统维护。
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
package librarymaintenance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func baseConfig() Config {
|
||||
return Config{
|
||||
Instance: "kgmaint-weknora", Project: "kgmaint-weknora",
|
||||
Writers: []string{"app"},
|
||||
NonWriters: map[string]string{"postgres": "database store; not an initiator"},
|
||||
DockerSocket: "/var/run/docker.sock",
|
||||
IngressURL: "http://127.0.0.1:18080/health",
|
||||
Database: "127.0.0.1:25432/kgmaint_target",
|
||||
Index: "kgmaint_1028a_chunks", Bucket: "kgmaint-1028a",
|
||||
}
|
||||
}
|
||||
|
||||
// 只锁治理库 scope 的"维护"不许被配置成实例级:那会冒充系统维护。
|
||||
func TestNewRequiresExactInstanceIdentity(t *testing.T) {
|
||||
for name, mutate := range map[string]func(*Config){
|
||||
"no-instance": func(c *Config) { c.Instance = "" },
|
||||
"no-project": func(c *Config) { c.Project = "" },
|
||||
"no-writers": func(c *Config) { c.Writers = nil },
|
||||
"no-ingress": func(c *Config) { c.IngressURL = "" },
|
||||
"bad-ingress": func(c *Config) { c.IngressURL = "ftp://host" },
|
||||
"no-index": func(c *Config) { c.Index = "" },
|
||||
"no-bucket": func(c *Config) { c.Bucket = "" },
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
cfg := baseConfig()
|
||||
mutate(&cfg)
|
||||
if _, err := New(cfg); err == nil {
|
||||
t.Fatal("incomplete target accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 指纹必须随目标身份变化:换了索引/桶/入口就是另一个目标。
|
||||
func TestFingerprintTracksTargetIdentity(t *testing.T) {
|
||||
base, err := New(baseConfig())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(base.Description(), base.Target().Fingerprint) {
|
||||
t.Fatal("description omits fingerprint")
|
||||
}
|
||||
for name, mutate := range map[string]func(*Config){
|
||||
"index": func(c *Config) { c.Index = "other_chunks" },
|
||||
"bucket": func(c *Config) { c.Bucket = "other-bucket" },
|
||||
"ingress": func(c *Config) { c.IngressURL = "http://127.0.0.1:18081/health" },
|
||||
"writers": func(c *Config) { c.Writers = []string{"app", "worker"} },
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
cfg := baseConfig()
|
||||
mutate(&cfg)
|
||||
other, err := New(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if other.Target().Fingerprint == base.Target().Fingerprint {
|
||||
t.Fatalf("fingerprint did not change for %s", name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 入口"关了"必须是连不上;代理还返回 502 不算关。
|
||||
func TestIngressClosedRequiresConnectionRefused(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
}))
|
||||
cfg := baseConfig()
|
||||
cfg.IngressURL = server.URL + "/health"
|
||||
manager, err := New(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manager.timeout = 400 * time.Millisecond
|
||||
if err = manager.ingressClosed(context.Background()); err == nil {
|
||||
t.Fatal("a still-serving proxy was treated as a closed ingress")
|
||||
}
|
||||
server.Close()
|
||||
manager.client.CloseIdleConnections()
|
||||
if err = manager.ingressClosed(context.Background()); err != nil {
|
||||
t.Fatalf("closed ingress rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 入口"回来了"必须是 200;容器 running 不算。
|
||||
func TestIngressReadyRequiresHealthyResponse(t *testing.T) {
|
||||
status := http.StatusServiceUnavailable
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(status)
|
||||
}))
|
||||
defer server.Close()
|
||||
cfg := baseConfig()
|
||||
cfg.IngressURL = server.URL + "/health"
|
||||
manager, err := New(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manager.timeout = 300 * time.Millisecond
|
||||
if err = manager.VerifyRunning(context.Background()); err == nil {
|
||||
t.Fatal("unhealthy ingress reported ready")
|
||||
}
|
||||
status = http.StatusOK
|
||||
if err = manager.VerifyRunning(context.Background()); err != nil {
|
||||
t.Fatalf("healthy ingress rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 维护记录里的库身份不含凭据。
|
||||
func TestDatabaseIdentityDropsCredentials(t *testing.T) {
|
||||
identity, err := DatabaseIdentity("postgres://user:secret@127.0.0.1:25432/kgmaint_target?sslmode=disable")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if identity != "127.0.0.1:25432/kgmaint_target" {
|
||||
t.Fatalf("identity=%q", identity)
|
||||
}
|
||||
if strings.Contains(identity, "secret") {
|
||||
t.Fatal("credentials leaked into identity")
|
||||
}
|
||||
if _, err = DatabaseIdentity("not-a-dsn"); err == nil {
|
||||
t.Fatal("invalid DSN accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user