1db21fb899
- 生产/测试 WeKnora 地址、租户 ID、知识库 ID 由静态 env 收敛为版本化 UI 设置,保留修订号与操作人审计 - 新增 serviceverify:保存前真调目标端点探测,端点无效才拒绝;网络抖动或无凭据仅告警 - 迁移 000042(生产 WeKnora 端点)/000043(服务目标);补齐设置候选与校验契约用例
144 lines
7.0 KiB
Go
144 lines
7.0 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
)
|
|
|
|
func testNow() time.Time { return time.Date(2026, 9, 12, 9, 0, 0, 0, time.UTC) }
|
|
|
|
func TestClassifySeparatesNotApplicableNotConfiguredAndFailures(t *testing.T) {
|
|
now := testNow()
|
|
notDeployed := classify("redis", "Redis / MQ", false, false, ProbeOutcome{ObservedAt: now})
|
|
if notDeployed.Liveness != LiveUnknown || notDeployed.Readiness != ReadNotApplicable {
|
|
t.Fatalf("not deployed = %+v", notDeployed)
|
|
}
|
|
notConfigured := classify("mineru", "MinerU", false, true, ProbeOutcome{ObservedAt: now})
|
|
if notConfigured.Liveness != LiveUnknown || notConfigured.Readiness != ReadNotConfigured {
|
|
t.Fatalf("not configured = %+v", notConfigured)
|
|
}
|
|
healthy := classify("postgres", "PostgreSQL", true, true, ProbeOutcome{ObservedAt: now})
|
|
if healthy.Liveness != LiveUp || healthy.Readiness != ReadReady {
|
|
t.Fatalf("healthy = %+v", healthy)
|
|
}
|
|
// DNS failure: down + blocked with sanitized reason, never the raw host.
|
|
dns := &net.DNSError{Err: "no such host", Name: "weknora.internal.example"}
|
|
down := classify("weknora", "WeKnora", true, true, ProbeOutcome{ObservedAt: now, Reachability: dns})
|
|
if down.Liveness != LiveDown || down.Reason == "" || strings.Contains(down.Reason, "weknora.internal") {
|
|
t.Fatalf("dns down = %+v", down)
|
|
}
|
|
// Timeout.
|
|
timeout := classify("weknora", "WeKnora", true, true, ProbeOutcome{ObservedAt: now, Reachability: context.DeadlineExceeded})
|
|
if timeout.Liveness != LiveDown || timeout.Readiness != ReadBlocked {
|
|
t.Fatalf("timeout = %+v", timeout)
|
|
}
|
|
// Reachable but capacity full => live up, readiness blocked, distinct reason.
|
|
capacity := classify("mineru", "MinerU", true, true, ProbeOutcome{ObservedAt: now, Reachability: errCapacityFull})
|
|
if capacity.Liveness != LiveUp || capacity.Readiness != ReadBlocked || capacity.Reason == "" {
|
|
t.Fatalf("capacity = %+v", capacity)
|
|
}
|
|
// Reachable but permission denied on target KB (readiness error only).
|
|
perm := classify("weknora", "WeKnora", true, true, ProbeOutcome{ObservedAt: now, WorkReadiness: errNoPermission})
|
|
if perm.Liveness != LiveUp || perm.Readiness != ReadBlocked {
|
|
t.Fatalf("permission = %+v", perm)
|
|
}
|
|
}
|
|
|
|
func TestHealthTrackerRemembersSuccessAndFailStreak(t *testing.T) {
|
|
tracker := newHealthTracker()
|
|
now := testNow()
|
|
first := tracker.apply("postgres", classify("postgres", "PostgreSQL", true, true, ProbeOutcome{ObservedAt: now}))
|
|
if first.LastSuccessAt == nil || !first.LastSuccessAt.Equal(now) || first.ConsecutiveFailures != 0 {
|
|
t.Fatalf("first = %+v", first)
|
|
}
|
|
later := now.Add(time.Minute)
|
|
bad := tracker.apply("postgres", classify("postgres", "PostgreSQL", true, true, ProbeOutcome{ObservedAt: later, Reachability: context.DeadlineExceeded}))
|
|
if !bad.LastSuccessAt.Equal(now) || bad.ConsecutiveFailures != 1 {
|
|
t.Fatalf("after one failure = %+v", bad)
|
|
}
|
|
bad2 := tracker.apply("postgres", classify("postgres", "PostgreSQL", true, true, ProbeOutcome{ObservedAt: bad.ObservedAt.Add(time.Minute), Reachability: context.DeadlineExceeded}))
|
|
if bad2.ConsecutiveFailures != 2 {
|
|
t.Fatalf("streak = %d", bad2.ConsecutiveFailures)
|
|
}
|
|
good := tracker.apply("postgres", classify("postgres", "PostgreSQL", true, true, ProbeOutcome{ObservedAt: bad2.ObservedAt.Add(time.Minute)}))
|
|
if good.ConsecutiveFailures != 0 || !good.LastSuccessAt.Equal(bad2.ObservedAt.Add(time.Minute)) {
|
|
t.Fatalf("recovered = %+v", good)
|
|
}
|
|
// Unknown probes (not configured) must not reset or extend a real streak.
|
|
neutral := tracker.apply("postgres", classify("postgres", "PostgreSQL", false, true, ProbeOutcome{ObservedAt: good.ObservedAt.Add(time.Minute)}))
|
|
if neutral.ConsecutiveFailures != 0 {
|
|
t.Fatalf("neutral streak = %d", neutral.ConsecutiveFailures)
|
|
}
|
|
}
|
|
|
|
func TestSanitizeProbeErrorTruncatesLongMessages(t *testing.T) {
|
|
long := errors.New(fmt.Sprintf("%0200d", 1))
|
|
if got := sanitizeProbeError(long); len(got) > 160 {
|
|
t.Fatalf("len=%d", len(got))
|
|
}
|
|
}
|
|
|
|
// TestProductionWeKnoraProbeReportsRealEndpointState freezes the production
|
|
// WeKnora contract: the console shows the endpoint's real state, an unset
|
|
// address is an explicit not-configured state, and no reason ever leaks the URL.
|
|
func TestProductionWeKnoraProbeReportsRealEndpointState(t *testing.T) {
|
|
// Unset address: explicit not_configured, never a fake "reachable".
|
|
provider := &RealProvider{httpClient: &http.Client{Timeout: time.Second}, health: newHealthTracker()}
|
|
health := provider.productionWeKnoraProbe(context.Background(), testNow())
|
|
if health.Key != "weknora-production" || health.Name != "生产 WeKnora" || health.Configured {
|
|
t.Fatalf("production rows must be identified: %+v", health)
|
|
}
|
|
if health.Liveness != LiveUnknown || health.Readiness != ReadNotConfigured {
|
|
t.Fatalf("unconfigured production endpoint = %+v", health)
|
|
}
|
|
|
|
// Reachable: /health is actually called on the configured address.
|
|
probed := ""
|
|
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
probed = request.URL.Path
|
|
writer.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer server.Close()
|
|
provider = &RealProvider{httpClient: server.Client(), productionWeKnoraBaseURL: server.URL, health: newHealthTracker()}
|
|
health = provider.productionWeKnoraProbe(context.Background(), testNow())
|
|
if probed != "/health" || health.Liveness != LiveUp || health.Readiness != ReadReady || !health.Configured {
|
|
t.Fatalf("reachable production endpoint = %+v (probed %q)", health, probed)
|
|
}
|
|
|
|
// Reachable but refusing new work: up/blocked with a sanitized reason.
|
|
full := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
writer.WriteHeader(http.StatusTooManyRequests)
|
|
}))
|
|
defer full.Close()
|
|
provider = &RealProvider{httpClient: full.Client(), productionWeKnoraBaseURL: full.URL, health: newHealthTracker()}
|
|
health = provider.productionWeKnoraProbe(context.Background(), testNow())
|
|
if health.Liveness != LiveUp || health.Readiness != ReadBlocked || health.Reason == "" || strings.Contains(health.Reason, full.URL) {
|
|
t.Fatalf("capacity-blocked production endpoint = %+v", health)
|
|
}
|
|
}
|
|
|
|
// TestOverallReadinessTreatsProductionEndpointAsAdvisory keeps the platform's
|
|
// own readiness independent from the production endpoint's availability.
|
|
func TestOverallReadinessTreatsProductionEndpointAsAdvisory(t *testing.T) {
|
|
health := []governance.ServiceHealth{
|
|
{Key: "postgres", Name: "PostgreSQL", Configured: true, Applicable: true, Liveness: LiveUp, Readiness: ReadReady},
|
|
{Key: "weknora-production", Name: "生产 WeKnora", Configured: true, Applicable: true, Liveness: LiveDown, Readiness: ReadBlocked, Reason: "网络不可达"},
|
|
}
|
|
if ok, blocked := overallReadiness(health); !ok || len(blocked) != 0 {
|
|
t.Fatalf("production endpoint must not block readiness: ok=%v blocked=%v", ok, blocked)
|
|
}
|
|
health[0].Readiness = ReadBlocked
|
|
if ok, blocked := overallReadiness(health); ok || len(blocked) != 1 || blocked[0] != "postgres" {
|
|
t.Fatalf("real dependency failure must still block: ok=%v blocked=%v", ok, blocked)
|
|
}
|
|
}
|