Download internal/reqlog/store_test.go from a3216/ds2api: direct link, hf CLI and curl.
- Browser
- Download file 3.96 kB
-
https://huggingface.co/spaces/a3216/ds2api/resolve/main/internal/reqlog/store_test.go
- Command line
-
hf download hf://spaces/a3216/ds2api/internal/reqlog/store_test.go
-
curl -L -o store_test.go https://huggingface.co/spaces/a3216/ds2api/resolve/main/internal/reqlog/store_test.go
3.96 kB
| package reqlog | |
| import ( | |
| "bytes" | |
| "os" | |
| "path/filepath" | |
| "testing" | |
| "time" | |
| ) | |
| func TestStoreQueryAndStats(t *testing.T) { | |
| dir := t.TempDir() | |
| s := New(Options{Dir: dir, MemoryLimit: 3, MaxFileBytes: 1 << 20, MaxFiles: 2}) | |
| defer func() { _ = s.Close() }() | |
| now := time.Now() | |
| s.Add(Entry{Time: now.Format(time.RFC3339Nano), Level: LevelInfo, Method: "POST", Path: "/v1/chat/completions", Status: 200, LatencyMS: 10, Account: "a@example.com", Model: "deepseek-v4", ErrorCode: ""}) | |
| s.Add(Entry{Time: now.Format(time.RFC3339Nano), Level: LevelWarn, Method: "POST", Path: "/v1/chat/completions", Status: 429, ErrorCode: "account_muted"}) | |
| s.Add(Entry{Time: now.Format(time.RFC3339Nano), Level: LevelError, Method: "GET", Path: "/v1/models", Status: 502, ErrorCode: "upstream_error"}) | |
| s.Add(Entry{Time: now.Format(time.RFC3339Nano), Level: LevelInfo, Method: "GET", Path: "/v1/models", Status: 200}) | |
| // 内存窗口只保留最近 3 条,最旧的 200 请求被淘汰。 | |
| if got := s.Query(Query{Limit: 10}); len(got) != 3 { | |
| t.Fatalf("expected 3 entries in window, got %d", len(got)) | |
| } | |
| if got := s.Query(Query{Limit: 10, Keyword: "account_muted"}); len(got) != 1 { | |
| t.Fatalf("keyword filter failed: %#v", got) | |
| } | |
| if got := s.Query(Query{Limit: 10, Status: 200}); len(got) != 1 { | |
| t.Fatalf("status filter failed: %#v", got) | |
| } | |
| stats := s.Stats() | |
| if stats["errors"].(int) != 1 || stats["client_errors"].(int) != 1 { | |
| t.Fatalf("unexpected stats: %#v", stats) | |
| } | |
| files := s.Files() | |
| if len(files) != 1 { | |
| t.Fatalf("expected one rotated file, got %#v", files) | |
| } | |
| name := files[0]["name"].(string) | |
| entries, err := s.Tail(name, 2) | |
| if err != nil { | |
| t.Fatalf("tail failed: %v", err) | |
| } | |
| if len(entries) != 2 { | |
| t.Fatalf("expected 2 tail entries, got %d", len(entries)) | |
| } | |
| } | |
| func TestStoreRejectsPathTraversal(t *testing.T) { | |
| s := New(Options{Dir: t.TempDir(), MemoryLimit: 10, MaxFileBytes: 1 << 20, MaxFiles: 1}) | |
| defer func() { _ = s.Close() }() | |
| if _, err := s.ResolveFile("../etc/passwd"); err == nil { | |
| t.Fatal("expected traversal to be rejected") | |
| } | |
| if _, err := s.ResolveFile("nested/requests.jsonl"); err == nil { | |
| t.Fatal("expected nested path to be rejected") | |
| } | |
| } | |
| func TestStoreZip(t *testing.T) { | |
| dir := t.TempDir() | |
| s := New(Options{Dir: dir, MemoryLimit: 10, MaxFileBytes: 1 << 20, MaxFiles: 3}) | |
| defer func() { _ = s.Close() }() | |
| s.Add(Entry{Time: time.Now().Format(time.RFC3339Nano), Method: "GET", Path: "/v1/models", Status: 200}) | |
| var buf bytes.Buffer | |
| if err := s.WriteZip(&buf, nil); err != nil { | |
| t.Fatalf("zip failed: %v", err) | |
| } | |
| if buf.Len() == 0 { | |
| t.Fatal("expected non-empty zip") | |
| } | |
| } | |
| func TestKeyStoreLifecycle(t *testing.T) { | |
| path := filepath.Join(t.TempDir(), "keys.json") | |
| ks := NewKeyStore(path) | |
| plain, item, err := ks.Create("ops", time.Hour) | |
| if err != nil { | |
| t.Fatalf("create failed: %v", err) | |
| } | |
| if plain == "" || item.Prefix == "" || item.Revoked { | |
| t.Fatalf("unexpected created key: %#v", item) | |
| } | |
| if _, ok := ks.Verify(plain); !ok { | |
| t.Fatal("expected key to verify") | |
| } | |
| if _, ok := ks.Verify("ds2log_deadbeef"); ok { | |
| t.Fatal("expected unknown key to fail") | |
| } | |
| // 重新加载后仍然可用,且磁盘上不保存明文。 | |
| reloaded := NewKeyStore(path) | |
| if _, ok := reloaded.Verify(plain); !ok { | |
| t.Fatal("expected key to survive reload") | |
| } | |
| raw, err := os.ReadFile(path) | |
| if err != nil { | |
| t.Fatalf("read keys file: %v", err) | |
| } | |
| if bytes.Contains(raw, []byte(plain)) { | |
| t.Fatal("plaintext key must not be persisted") | |
| } | |
| if err := ks.Delete(item.ID); err != nil { | |
| t.Fatalf("delete failed: %v", err) | |
| } | |
| if _, ok := ks.Verify(plain); ok { | |
| t.Fatal("expected deleted key to fail") | |
| } | |
| } | |
| func TestKeyStoreExpiry(t *testing.T) { | |
| ks := NewKeyStore("") | |
| plain, _, err := ks.Create("short", time.Millisecond) | |
| if err != nil { | |
| t.Fatalf("create failed: %v", err) | |
| } | |
| time.Sleep(5 * time.Millisecond) | |
| if _, ok := ks.Verify(plain); ok { | |
| t.Fatal("expected expired key to fail") | |
| } | |
| } | |