抽取 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 logger
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"git.zhangdeman.cn/github/ginframework/diagnostic"
|
||||
"git.zhangdeman.cn/github/ginframework/fault"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOutputLifecycleAndSafeFields(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
instance, err := ConfigureOutput(&OutputConfig{Name: "default", Path: directory, UseJSON: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if instance != Instance("default") {
|
||||
t.Fatal("logger not shared")
|
||||
}
|
||||
if _, err := ConfigureOutput(&OutputConfig{Name: "default", Path: directory}); err == nil {
|
||||
t.Fatal("replaced active logger")
|
||||
}
|
||||
source := map[string]any{"token": "private-value", "count": int64(9007199254740993)}
|
||||
instance.Info("Write example data", Fields(source))
|
||||
if err := CloseOutputs(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files, err := filepath.Glob(filepath.Join(directory, "*"))
|
||||
if err != nil || len(files) == 0 {
|
||||
t.Fatal("log file missing")
|
||||
}
|
||||
body, err := os.ReadFile(files[0])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(body), "private-value") || !strings.Contains(string(body), "9007199254740993") {
|
||||
t.Fatal("log values unsafe or imprecise")
|
||||
}
|
||||
if source["token"] != "private-value" {
|
||||
t.Fatal("logging mutated source")
|
||||
}
|
||||
if err := CloseOutputs(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOutputCreationPreservesCause(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "file")
|
||||
if err := os.WriteFile(path, []byte("file"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := ConfigureOutput(&OutputConfig{Name: "invalid", Path: filepath.Join(path, "child")})
|
||||
var (
|
||||
output *OutputError
|
||||
)
|
||||
if !errors.As(err, &output) || errors.Unwrap(err) == nil {
|
||||
t.Fatal("output failure lost cause")
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorContextUsesTrustedSnapshotWithoutCauseText(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
if _, err := ConfigureOutput(&OutputConfig{Name: "default", Path: directory, UseJSON: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := diagnostic.With(context.Background(), diagnostic.Snapshot{TraceID: "trusted-trace", RequestID: "trusted-request", URI: "/safe"})
|
||||
RecordErrorContext(ctx, "Read outbound response failed", fault.New(900709, "Read outbound response failed", errors.New("private-password")))
|
||||
if err := CloseOutputs(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files, err := filepath.Glob(filepath.Join(directory, "*"))
|
||||
if err != nil || len(files) != 1 {
|
||||
t.Fatal("log file missing")
|
||||
}
|
||||
raw, err := os.ReadFile(files[0])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var (
|
||||
entry map[string]any
|
||||
)
|
||||
if err = json.Unmarshal(bytes.TrimSpace(raw), &entry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if entry["trace_id"] != "trusted-trace" || entry["request_id"] != "trusted-request" || entry["uri"] != "/safe" || strings.Contains(string(raw), "private-password") {
|
||||
t.Fatalf("unsafe or missing diagnostics: %s", raw)
|
||||
}
|
||||
}
|
||||