Files
knowledge-governance/internal/monitoring/health_test.go
T
姚章浩 1c80b07f2c
build / build (push) Successful in 1m59s
fix(ui): 修复F12-F16入口与追溯缺陷
Dockerfile 运行阶段补齐 dws_dingtalk_scan(F12),并新增 runtime-scripts 门禁
做脚本依赖闭包正负例;就绪探测每次解析当前端点/绑定/凭据并区分 404、未配置、
不可达(F13);路由写入来源路径与父目录,历史空值仅按同连接同 item 的意图读时
补齐(F14);目标筛选显示短 ID 与当前绑定/历史并提供快捷筛选(F15);刷新终态
移除后台运行、展示失败时间/旧快照/脱敏原因/作业 ID 与重试(F16)。

验证:make verify 17 项全过(identity 本地按设计 skipped);一次性 schema 的
Monitor 目录/路由集成用例通过;8088 部署与真实 UI 验收仍由父会话执行。
2026-11-05 10:38:26 +08:00

173 lines
8.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package monitoring
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"knowledge-governance/internal/governance"
"knowledge-governance/internal/readiness"
)
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)
}
}
// F13: 旧 env 绑定 404 是「业务就绪被阻断」,不是「服务不可达」;
// 同时必须把脱敏后的探测对象带出来,否则红灯无法归因。
func TestWeKnoraReadinessReportsBindingBlockWithoutLosingLiveness(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)
}))
defer server.Close()
provider := &RealProvider{
httpClient: server.Client(),
weknoraBaseURL: server.URL,
health: newHealthTracker(),
weknoraReadiness: func(context.Context) (string, error) {
target := "实例 weknora.test · 知识库 1b52c140"
return target, fmt.Errorf("%w(%s)", readiness.ErrKnowledgeBaseMissing, target)
},
}
health := provider.weKnoraProbe(context.Background(), testNow())
if health.Liveness != LiveUp || health.Readiness != ReadBlocked {
t.Fatalf("404 binding = %+v, want up/blocked", health)
}
if health.ProbeTarget != "实例 weknora.test · 知识库 1b52c140" {
t.Fatalf("probe target = %q", health.ProbeTarget)
}
if !strings.Contains(health.Reason, "1b52c140") || strings.Contains(health.Reason, "1b52c140-2e18-4676-afc5-4c7d8d190a8c") {
t.Fatalf("reason = %q, want short target only", health.Reason)
}
}