抽取 Gin、配置、日志、数据库、Redis 与出站 HTTP 基础能力。 新增统一接入指南,将能力清单和相对链接检查纳入 make verify。 对齐远端 module 路径,修正 Redis 读写超时特殊值映射。 验证:make verify、make race-check 均通过。
This commit is contained in:
commit
b4c3ac622c
55 files changed
+5862
No files matched your search
@@ -0,0 +1,93 @@
|
||||
package commandlog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.zhangdeman.cn/github/ginframework/internal/testutil/redisserver"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zaptest/observer"
|
||||
)
|
||||
|
||||
// TestNativePipelineObservationPreservesData 验证 Pipeline 逐条对应记录,日志脱敏不修改执行参数和返回正文。
|
||||
func TestNativePipelineObservationPreservesData(t *testing.T) {
|
||||
var (
|
||||
client = redisserver.New(t, 3)
|
||||
)
|
||||
var (
|
||||
core, logs = observer.New(zap.InfoLevel)
|
||||
)
|
||||
var (
|
||||
ctx = context.Background()
|
||||
)
|
||||
var (
|
||||
value = `{"nested":{"token":"private-token"},"data":"kept"}`
|
||||
)
|
||||
client.AddHook(New(zap.New(core), "cache"))
|
||||
var (
|
||||
pipeline = client.Pipeline()
|
||||
)
|
||||
pipeline.Set(ctx, "safe-key", value, 0)
|
||||
var (
|
||||
get = pipeline.Get(ctx, "safe-key")
|
||||
)
|
||||
var (
|
||||
missing = pipeline.Get(ctx, "missing-key")
|
||||
)
|
||||
if _, err := pipeline.Exec(ctx); !errors.Is(err, redis.Nil) {
|
||||
t.Fatalf("pipeline error changed: %v", err)
|
||||
}
|
||||
if get.Val() != value || !errors.Is(missing.Err(), redis.Nil) {
|
||||
t.Fatal("logging changed actual command result")
|
||||
}
|
||||
var (
|
||||
commands []observer.LoggedEntry
|
||||
)
|
||||
for _, entry := range logs.All() {
|
||||
if name := entry.ContextMap()["command"]; name == "set" || name == "get" {
|
||||
commands = append(commands, entry)
|
||||
}
|
||||
}
|
||||
if len(commands) != 3 {
|
||||
t.Fatalf("pipeline records=%d, want 3", len(commands))
|
||||
}
|
||||
for _, entry := range logs.All() {
|
||||
body, _ := json.Marshal(entry.ContextMap())
|
||||
if strings.Contains(string(body), "private-token") {
|
||||
t.Fatal("credential leaked into execution log")
|
||||
}
|
||||
}
|
||||
if commands[2].ContextMap()["result"] != "miss" {
|
||||
t.Fatal("Redis nil observation changed")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHookObservesReturnedError 验证官方回调返回前尚未 SetErr 时不会误记成功。
|
||||
func TestHookObservesReturnedError(t *testing.T) {
|
||||
var (
|
||||
core, logs = observer.New(zap.InfoLevel)
|
||||
)
|
||||
var (
|
||||
hook = New(zap.New(core), "cache")
|
||||
)
|
||||
var (
|
||||
cmd = redis.NewStringCmd(context.Background(), "get", "safe-key")
|
||||
)
|
||||
var (
|
||||
calls int
|
||||
)
|
||||
var (
|
||||
process = hook.ProcessHook(func(context.Context, redis.Cmder) error { calls++; return context.Canceled })
|
||||
)
|
||||
if err := process(context.Background(), cmd); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("error changed: %v", err)
|
||||
}
|
||||
if calls != 1 || cmd.Err() != nil || logs.Len() != 1 || logs.All()[0].ContextMap()["result"] != "failed" {
|
||||
t.Fatalf("hook altered command or observed wrong result: %v", logs.All())
|
||||
}
|
||||
}
|
||||