3643e9bf82
build / build (push) Successful in 1m20s
删掉会自己开入口、只看服务健康的 FinalizeAfterOperatorRecovery。 新的 RecoverWindow 绑定原作业/目标指纹/持久化预备份,在维护关闭下 执行恢复与逐类校验,通过才恢复服务与入口;失败继续维护。 预备份身份与内容风险在第一笔写入前一起落盘;后台任务必须静默后才停写者; 写入者发布端口即拒绝开窗口;维护占用改为按实例互斥(000072)。
156 lines
4.9 KiB
Go
156 lines
4.9 KiB
Go
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")
|
|
}
|
|
}
|
|
|
|
// 写入者只要发布了宿主机端口,就存在绕开闸门的直连通道。
|
|
func TestPublishedPortsDetectsDirectBypass(t *testing.T) {
|
|
var state containerState
|
|
state.HostConfig.PortBindings = map[string][]struct {
|
|
HostIP string `json:"HostIp"`
|
|
HostPort string `json:"HostPort"`
|
|
}{"8080/tcp": {{HostIP: "127.0.0.1", HostPort: "18081"}}}
|
|
ports := state.publishedPorts()
|
|
if len(ports) != 1 || !strings.Contains(ports[0], "18081") {
|
|
t.Fatalf("publishedPorts=%v", ports)
|
|
}
|
|
var bare containerState
|
|
bare.HostConfig.PortBindings = map[string][]struct {
|
|
HostIP string `json:"HostIp"`
|
|
HostPort string `json:"HostPort"`
|
|
}{"8080/tcp": nil}
|
|
if got := bare.publishedPorts(); len(got) != 0 {
|
|
t.Fatalf("unpublished container reported ports: %v", got)
|
|
}
|
|
}
|