Files
knowledge-governance/internal/httpapi/backup_inventory_test.go
Zhero & Rin 144b49cb53
build / build (push) Successful in 3m22s
feat(backup): 配额改按备份根占用并在超限时清理最旧批次
- 采集器上报 backup_root/usage/quota(加性字段),API 用 BACKUP_HOST_ROOT 校验并分别降级占用/配额
- 页面配额行改为备份占用 X / 配额 50 GB,4 条告警改用备份口径,解除本页归档依赖
- logical-backup.sh 预检按字节清理(保留最新、空间不足零删除),dump 后复查保护新批次
- backup_layout 写入 KB_BACKUP_QUOTA_BYTES;--show 输出根占用;新增 prune 工具与自检
2026-11-25 22:46:32 +08:00

104 lines
4.0 KiB
Go
Raw Permalink 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 httpapi
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"knowledge-governance/internal/backupinventory"
)
type fakeBackupInventory struct {
snapshot backupinventory.Snapshot
}
func (fake fakeBackupInventory) Available() bool { return fake.snapshot.Wired }
func (fake fakeBackupInventory) Snapshot(context.Context) backupinventory.Snapshot {
return fake.snapshot
}
func TestBackupInventoryProjectionPassesRealFields(t *testing.T) {
actual := int64(1024)
databaseActual := int64(512)
config := Config{BackupInventory: fakeBackupInventory{snapshot: backupinventory.Snapshot{
Wired: true,
Root: "/srv/kb-backups/rds-logical",
HostRoot: "/srv/kb-backups/rds-logical",
Source: "weknora-test:/srv/kb-backups/rds-logical",
Metadata: backupinventory.MetadataInfo{Configured: true, Present: true, Valid: true, Fresh: true, Complete: true},
BackupRoot: "/srv/kb-backups",
Usage: backupinventory.Usage{
Root: "/srv/kb-backups", Measured: true, Complete: true, ActualBytes: &actual,
},
QuotaBytes: func() *int64 { value := int64(53687091200); return &value }(),
Mount: backupinventory.MountInfo{
MountVerified: true,
ReadOnly: true,
Evidence: "宿主采集器:/dev/sdb1 挂在 /srv/kb-backups(ext4)",
},
Capacity: backupinventory.Capacity{Measured: true, TotalBytes: 100, FreeBytes: 40, AvailableBytes: 35, UsedBytes: 60},
Batches: []backupinventory.Batch{{
Stamp: "20261118T193001Z",
HostPath: "/srv/kb-backups/rds-logical/20261118T193001Z",
ManifestState: backupinventory.ManifestOK,
ActualBytes: &actual,
Databases: []backupinventory.Database{{
Name: "weknora_v090prod",
HostPath: "/srv/kb-backups/rds-logical/20261118T193001Z/weknora_v090prod.dir",
Present: true,
ActualBytes: &databaseActual,
}},
}},
Totals: backupinventory.Totals{BatchCount: 1, ActualBytes: &actual, LatestStamp: "20261118T193001Z", Complete: true},
}}}
recorder := httptest.NewRecorder()
config.handleBackupInventory(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/backup-schedule/inventory", nil))
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d", recorder.Code)
}
var payload struct {
Data backupinventory.Snapshot `json:"data"`
}
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
t.Fatal(err)
}
if !payload.Data.Wired || !payload.Data.Mount.MountVerified || !payload.Data.Capacity.Measured {
t.Fatalf("接线/挂载/容量字段丢失: %+v", payload.Data)
}
if len(payload.Data.Batches) != 1 ||
payload.Data.Batches[0].Databases[0].HostPath != "/srv/kb-backups/rds-logical/20261118T193001Z/weknora_v090prod.dir" {
t.Fatalf("批次/子库真实路径丢失: %+v", payload.Data.Batches)
}
if payload.Data.Batches[0].ActualBytes == nil || *payload.Data.Batches[0].ActualBytes != 1024 {
t.Fatalf("实际占盘字段丢失: %+v", payload.Data.Batches[0])
}
if payload.Data.BackupRoot != "/srv/kb-backups" || !payload.Data.Usage.Measured ||
payload.Data.Usage.ActualBytes == nil || *payload.Data.Usage.ActualBytes != 1024 {
t.Fatalf("备份根占用字段丢失: %+v", payload.Data.Usage)
}
if payload.Data.QuotaBytes == nil || *payload.Data.QuotaBytes != 53687091200 {
t.Fatalf("配额字段丢失: %v", payload.Data.QuotaBytes)
}
}
func TestBackupInventoryUnconfiguredIsNotAnError(t *testing.T) {
config := Config{BackupInventory: backupinventory.New(backupinventory.Config{})}
recorder := httptest.NewRecorder()
config.handleBackupInventory(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/backup-schedule/inventory", nil))
if recorder.Code != http.StatusOK {
t.Fatalf("未接线不应 500: %d", recorder.Code)
}
var payload struct {
Data backupinventory.Snapshot `json:"data"`
}
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
t.Fatal(err)
}
if payload.Data.Wired || payload.Data.Error == "" || payload.Data.Batches == nil {
t.Fatalf("未接线必须给原因且批次为空数组: %+v", payload.Data)
}
}