37d8761721
build / build (push) Successful in 2m35s
- 变基到 CYWL-38 的 reconciliationMessages 之后:沿用 runtime_key_forbidden / runtime_unreachable 公共码,不再另造一套。 - HTTP 仍按本任务裁定用 502 区分上游拒绝/失败与绑定不可用 503。 - 页面文案统一由 reconciliationMessages 输出;按新 Key 来源改写「平台只读 Key」措辞。 - 同步 monitoring 原因码与相关测试;make verify 17 项全绿。
70 lines
2.3 KiB
Go
70 lines
2.3 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
"knowledge-governance/internal/reconciliation"
|
|
)
|
|
|
|
func TestParseCPUStatAndMemoryInfo(t *testing.T) {
|
|
idle, total, err := parseCPUStat("cpu 100 20 30 400 50 6 7 8 0 0\n")
|
|
if err != nil || idle != 450 || total != 621 {
|
|
t.Fatalf("idle=%d total=%d err=%v", idle, total, err)
|
|
}
|
|
used, memoryTotal, err := parseMemoryInfo("MemTotal: 1000 kB\nMemAvailable: 250 kB\n")
|
|
if err != nil || used != 750*1024 || memoryTotal != 1000*1024 {
|
|
t.Fatalf("used=%d total=%d err=%v", used, memoryTotal, err)
|
|
}
|
|
}
|
|
|
|
func TestCPUPercentUsesTwoMonotonicSamples(t *testing.T) {
|
|
value := cpuPercent(450, 621, 500, 721)
|
|
if value != 50 {
|
|
t.Fatalf("cpu percent=%v, want 50", value)
|
|
}
|
|
if cpuPercent(500, 721, 450, 621) != 0 {
|
|
t.Fatal("non-monotonic CPU counters must fail closed to zero")
|
|
}
|
|
}
|
|
|
|
type stubQueueReader struct {
|
|
summary *governance.WeKnoraQueueSummary
|
|
err error
|
|
}
|
|
|
|
func (reader stubQueueReader) Queues(context.Context) (*governance.WeKnoraQueueSummary, error) {
|
|
return reader.summary, reader.err
|
|
}
|
|
|
|
func TestRuntimeQueueProjectionStatesWhyQueueIsAbsent(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
err error
|
|
code string
|
|
}{
|
|
{name: "binding unavailable", err: reconciliation.ErrRuntimeUnavailable, code: "runtime_read_unavailable"},
|
|
{name: "key forbidden", err: reconciliation.ErrRuntimeForbidden, code: "runtime_key_forbidden"},
|
|
{name: "unreachable", err: reconciliation.ErrRuntimeUnreachable, code: "runtime_unreachable"},
|
|
{name: "unclassified", err: errors.New("boom"), code: ""},
|
|
}
|
|
for _, testCase := range cases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
queues, code := runtimeQueueProjection(context.Background(), stubQueueReader{err: testCase.err})
|
|
if queues != nil || code != testCase.code {
|
|
t.Fatalf("queues=%v code=%q, want nil/%q", queues, code, testCase.code)
|
|
}
|
|
})
|
|
}
|
|
summary := &governance.WeKnoraQueueSummary{Available: true}
|
|
queues, code := runtimeQueueProjection(context.Background(), stubQueueReader{summary: summary})
|
|
if queues != summary || code != "" {
|
|
t.Fatalf("available queue projection = %v/%q", queues, code)
|
|
}
|
|
if queues, code := runtimeQueueProjection(context.Background(), nil); queues != nil || code != "" {
|
|
t.Fatalf("unwired projection = %v/%q", queues, code)
|
|
}
|
|
}
|