fix(backup): 宿主采集器按 du 口径去重硬链接并拒绝清单符号链接
备份目录 root:root 0700,API 不再遍历;宿主采集器只读 stat/du,原子写快照。统计包含目录自身、树内 (dev,ino) 硬链接去重,单库与整批各自独立与 du -sB1 对齐;MANIFEST 仅接受目录内常规非符号链接文件并 O_NOFOLLOW 打开。快照缺失/陈旧/不完整时批次与子库字节为 null,页面不画容量图、不使用部分字节。
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -2,152 +2,253 @@ package backupinventory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func writeFile(t *testing.T, filename string, content string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(filename), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filename, []byte(content), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
const hostRoot = "/srv/kb-backups/rds-logical"
|
||||
|
||||
func int64p(value int64) *int64 { return &value }
|
||||
func intp(value int) *int { return &value }
|
||||
|
||||
func validMetadata() metadata {
|
||||
latest := hostRoot + "/20261118T193001Z"
|
||||
broken := hostRoot + "/20261117T193001Z"
|
||||
without := hostRoot + "/20261116T193001Z"
|
||||
return metadata{
|
||||
SchemaVersion: SchemaVersion,
|
||||
CollectorVersion: "backup-metadata/1",
|
||||
Generation: "20261120T010000Z-abc123",
|
||||
CollectedAt: "2026-11-20T01:00:00Z",
|
||||
Host: "weknora-test",
|
||||
Source: "weknora-test:" + hostRoot,
|
||||
Root: hostRoot,
|
||||
Complete: true,
|
||||
Errors: []string{},
|
||||
Filesystem: metadataFilesystem{
|
||||
MountPoint: "/srv/kb-backups",
|
||||
MountSource: "/dev/sdb1",
|
||||
FSType: "ext4",
|
||||
ReadOnly: false,
|
||||
TotalBytes: 1000,
|
||||
FreeBytes: 400,
|
||||
AvailableBytes: 350,
|
||||
},
|
||||
IncompleteCount: 1,
|
||||
Skipped: []Skipped{{Name: "notes.txt", Reason: "不是批次目录"}},
|
||||
Batches: []metadataBatch{
|
||||
{
|
||||
Stamp: "20261118T193001Z",
|
||||
Path: latest,
|
||||
ManifestState: "ok",
|
||||
Kind: "rds-logical-backup",
|
||||
CreatedAt: "20261118T193001Z",
|
||||
StartedAt: "20261118T192908Z",
|
||||
DurationSeconds: 53,
|
||||
ManifestTotalBytes: int64p(300),
|
||||
Complete: true,
|
||||
ApparentBytes: int64p(300),
|
||||
ActualBytes: int64p(4096),
|
||||
FileCount: intp(3),
|
||||
Databases: []metadataDatabase{
|
||||
{
|
||||
Name: "weknora_v090prod", DirName: "weknora_v090prod.dir",
|
||||
Path: latest + "/weknora_v090prod.dir", Present: true, InManifest: true,
|
||||
ApparentBytes: int64p(250), ActualBytes: int64p(4096), FileCount: intp(2),
|
||||
},
|
||||
{
|
||||
Name: "weknora_test", DirName: "weknora_test.dir",
|
||||
Path: latest + "/weknora_test.dir", Present: true, InManifest: true, Failed: true,
|
||||
ApparentBytes: int64p(50), ActualBytes: int64p(0), FileCount: intp(1),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Stamp: "20261117T193001Z",
|
||||
Path: broken,
|
||||
ManifestState: "invalid", ManifestError: "清单无法解析:unexpected end",
|
||||
Complete: true, ApparentBytes: int64p(20), ActualBytes: int64p(2048), FileCount: intp(1),
|
||||
Databases: []metadataDatabase{{
|
||||
Name: "weknora_prod", DirName: "weknora_prod.dir",
|
||||
Path: broken + "/weknora_prod.dir", Present: true,
|
||||
ApparentBytes: int64p(20), ActualBytes: int64p(2048), FileCount: intp(1),
|
||||
}},
|
||||
},
|
||||
{
|
||||
Stamp: "20261116T193001Z",
|
||||
Path: without,
|
||||
ManifestState: "missing",
|
||||
Complete: true, ApparentBytes: int64p(30), ActualBytes: int64p(1024), FileCount: intp(1),
|
||||
Databases: []metadataDatabase{{
|
||||
Name: "weknora_archive", DirName: "weknora_archive.dir",
|
||||
Path: without + "/weknora_archive.dir", Present: true,
|
||||
ApparentBytes: int64p(30), ActualBytes: int64p(1024), FileCount: intp(1),
|
||||
}},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func actualOf(t *testing.T, filename string) int64 {
|
||||
func writeMetadata(t *testing.T, value metadata) string {
|
||||
t.Helper()
|
||||
info, err := os.Stat(filename)
|
||||
raw, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stat, ok := info.Sys().(*syscall.Stat_t)
|
||||
if !ok {
|
||||
t.Fatal("no stat")
|
||||
filename := filepath.Join(t.TempDir(), "metadata.json")
|
||||
if err := os.WriteFile(filename, raw, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return int64(stat.Blocks) * 512
|
||||
return filename
|
||||
}
|
||||
|
||||
func fixtureRoot(t *testing.T) string {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
latest := filepath.Join(root, "20261118T193001Z")
|
||||
writeFile(t, filepath.Join(latest, "MANIFEST.json"), `{
|
||||
"kind": "rds-logical-backup",
|
||||
"created_at": "20261118T193001Z",
|
||||
"started_at": "20261118T192908Z",
|
||||
"duration_seconds": 53,
|
||||
"databases": ["weknora_v090prod", "weknora_test"],
|
||||
"failed_databases": ["weknora_test"],
|
||||
"total_bytes": 300,
|
||||
"files": [
|
||||
{"file": "weknora_v090prod.dir/toc.dat", "bytes": 100},
|
||||
{"file": "weknora_v090prod.dir/0001.dat.gz", "bytes": 150},
|
||||
{"file": "weknora_test.dir/toc.dat", "bytes": 50}
|
||||
]
|
||||
}`)
|
||||
writeFile(t, filepath.Join(latest, "weknora_v090prod.dir", "toc.dat"), strings.Repeat("a", 100))
|
||||
writeFile(t, filepath.Join(latest, "weknora_v090prod.dir", "0001.dat.gz"), strings.Repeat("b", 150))
|
||||
writeFile(t, filepath.Join(latest, "weknora_test.dir", "toc.dat"), strings.Repeat("c", 50))
|
||||
|
||||
invalid := filepath.Join(root, "20261117T193001Z")
|
||||
writeFile(t, filepath.Join(invalid, "MANIFEST.json"), "{not-json")
|
||||
writeFile(t, filepath.Join(invalid, "weknora_prod.dir", "toc.dat"), strings.Repeat("d", 20))
|
||||
|
||||
missing := filepath.Join(root, "20261116T193001Z")
|
||||
writeFile(t, filepath.Join(missing, "weknora_archive.dir", "toc.dat"), strings.Repeat("e", 30))
|
||||
|
||||
incomplete := filepath.Join(root, "20261115T193001Z.incomplete")
|
||||
writeFile(t, filepath.Join(incomplete, "MANIFEST.json"), `{}`)
|
||||
|
||||
return root
|
||||
func readerAt(filename string, now time.Time) *Reader {
|
||||
reader := New(Config{MetadataPath: filename, HostRoot: hostRoot, Source: "configured", MaxAge: 15 * time.Minute})
|
||||
reader.now = func() time.Time { return now }
|
||||
return reader
|
||||
}
|
||||
|
||||
func TestSnapshotReadsBatchesManifestsAndActualUsage(t *testing.T) {
|
||||
root := fixtureRoot(t)
|
||||
reader := New(Config{
|
||||
Root: root,
|
||||
HostRoot: "/srv/kb-backups/rds-logical",
|
||||
Source: "weknora-test:/srv/kb-backups/rds-logical",
|
||||
})
|
||||
reader.device = func(path string) (uint64, error) {
|
||||
if path == "/" {
|
||||
return 10, nil
|
||||
}
|
||||
return 20, nil
|
||||
func TestSnapshotReadsHostMetadataAndPaths(t *testing.T) {
|
||||
filename := writeMetadata(t, validMetadata())
|
||||
now := time.Date(2026, 11, 20, 1, 5, 0, 0, time.UTC)
|
||||
snapshot := readerAt(filename, now).Snapshot(context.Background())
|
||||
if !snapshot.Wired || snapshot.Error != "" || !snapshot.Metadata.Valid || !snapshot.Metadata.Fresh || !snapshot.Metadata.Complete {
|
||||
t.Fatalf("应接受新鲜的完整快照: %+v", snapshot.Metadata)
|
||||
}
|
||||
reader.statfs = func(string) (statFS, error) {
|
||||
return statFS{Total: 1000, Free: 400, Available: 350, ReadOnly: true}, nil
|
||||
}
|
||||
|
||||
snapshot := reader.Snapshot(context.Background())
|
||||
if !snapshot.Wired || snapshot.Error != "" {
|
||||
t.Fatalf("应读到接线目录: %+v", snapshot)
|
||||
}
|
||||
if !snapshot.Mount.MountVerified || snapshot.Mount.LivesOnContainerRoot || !snapshot.Mount.ReadOnly {
|
||||
t.Fatalf("挂载判定不对: %+v", snapshot.Mount)
|
||||
if !snapshot.Mount.MountVerified || snapshot.Mount.LivesOnContainerRoot || snapshot.Mount.MountPoint != "/srv/kb-backups" {
|
||||
t.Fatalf("宿主挂载判定不对: %+v", snapshot.Mount)
|
||||
}
|
||||
if !snapshot.Capacity.Measured || snapshot.Capacity.TotalBytes != 1000 || snapshot.Capacity.AvailableBytes != 350 {
|
||||
t.Fatalf("容量不对: %+v", snapshot.Capacity)
|
||||
}
|
||||
if len(snapshot.Batches) != 3 || snapshot.IncompleteCount != 1 {
|
||||
t.Fatalf("批次数量不对: %d incomplete=%d", len(snapshot.Batches), snapshot.IncompleteCount)
|
||||
if snapshot.Metadata.AgeSeconds != 300 || snapshot.Metadata.Generation == "" || snapshot.Metadata.Host != "weknora-test" {
|
||||
t.Fatalf("元数据字段不对: %+v", snapshot.Metadata)
|
||||
}
|
||||
if len(snapshot.Batches) != 3 || snapshot.IncompleteCount != 1 || len(snapshot.Skipped) != 1 {
|
||||
t.Fatalf("批次计数不对: %+v", snapshot)
|
||||
}
|
||||
latest := snapshot.Batches[0]
|
||||
if latest.Stamp != "20261118T193001Z" || latest.ManifestState != ManifestOK {
|
||||
if latest.Stamp != "20261118T193001Z" || latest.ManifestState != ManifestOK || latest.SizesComplete != true {
|
||||
t.Fatalf("最近批次不对: %+v", latest)
|
||||
}
|
||||
if latest.CreatedAt != "20261118T193001Z" || latest.DurationSeconds != 53 || latest.ManifestBytes != 300 {
|
||||
t.Fatalf("清单字段不对: %+v", latest)
|
||||
}
|
||||
if latest.HostPath != "/srv/kb-backups/rds-logical/20261118T193001Z" {
|
||||
t.Fatalf("批次主机路径不对: %q", latest.HostPath)
|
||||
}
|
||||
if len(latest.Databases) != 2 {
|
||||
t.Fatalf("子库数量不对: %+v", latest.Databases)
|
||||
if latest.HostPath != hostRoot+"/20261118T193001Z" || latest.ManifestTotalBytes == nil || *latest.ManifestTotalBytes != 300 {
|
||||
t.Fatalf("批次路径/清单不对: %+v", latest)
|
||||
}
|
||||
prod := latest.Databases[0]
|
||||
if prod.Name != "weknora_v090prod" || !prod.Present || prod.Failed || !prod.InManifest {
|
||||
t.Fatalf("子库状态不对: %+v", prod)
|
||||
}
|
||||
if prod.Path != filepath.Join(root, "20261118T193001Z", "weknora_v090prod.dir") {
|
||||
t.Fatalf("子库容器路径不对: %q", prod.Path)
|
||||
}
|
||||
if prod.HostPath != "/srv/kb-backups/rds-logical/20261118T193001Z/weknora_v090prod.dir" {
|
||||
t.Fatalf("子库主机路径不对: %q", prod.HostPath)
|
||||
}
|
||||
expectedActual := actualOf(t, filepath.Join(root, "20261118T193001Z", "weknora_v090prod.dir", "toc.dat")) +
|
||||
actualOf(t, filepath.Join(root, "20261118T193001Z", "weknora_v090prod.dir", "0001.dat.gz"))
|
||||
if prod.ActualBytes != expectedActual {
|
||||
t.Fatalf("子库实际占盘不对: got %d want %d", prod.ActualBytes, expectedActual)
|
||||
}
|
||||
if prod.ApparentBytes != 250 || prod.FileCount != 2 {
|
||||
t.Fatalf("子库清单体积不对: %+v", prod)
|
||||
if prod.HostPath != hostRoot+"/20261118T193001Z/weknora_v090prod.dir" || prod.ActualBytes == nil || *prod.ActualBytes != 4096 {
|
||||
t.Fatalf("子库完整路径/占盘不对: %+v", prod)
|
||||
}
|
||||
if !latest.Databases[1].Failed {
|
||||
t.Fatalf("失败库没有标记: %+v", latest.Databases[1])
|
||||
}
|
||||
|
||||
broken := snapshot.Batches[1]
|
||||
if broken.Stamp != "20261117T193001Z" || broken.ManifestState != ManifestInvalid || broken.ManifestError == "" {
|
||||
t.Fatalf("损坏清单没有如实标记: %+v", broken)
|
||||
if snapshot.Batches[1].ManifestState != ManifestInvalid || snapshot.Batches[1].ManifestError == "" {
|
||||
t.Fatalf("损坏清单没有如实标记: %+v", snapshot.Batches[1])
|
||||
}
|
||||
if len(broken.Databases) != 1 || broken.Databases[0].Name != "weknora_prod" || !broken.Databases[0].Present {
|
||||
t.Fatalf("清单损坏时仍应按目录列出子库: %+v", broken.Databases)
|
||||
if snapshot.Batches[2].ManifestState != ManifestMissing {
|
||||
t.Fatalf("缺清单没有如实标记: %+v", snapshot.Batches[2])
|
||||
}
|
||||
withoutManifest := snapshot.Batches[2]
|
||||
if withoutManifest.ManifestState != ManifestMissing {
|
||||
t.Fatalf("缺清单没有如实标记: %+v", withoutManifest)
|
||||
}
|
||||
if snapshot.Totals.BatchCount != 3 || snapshot.Totals.LatestStamp != "20261118T193001Z" {
|
||||
if snapshot.Totals.ActualBytes == nil || *snapshot.Totals.ActualBytes != 4096+2048+1024 || !snapshot.Totals.Complete {
|
||||
t.Fatalf("汇总不对: %+v", snapshot.Totals)
|
||||
}
|
||||
if snapshot.ObservedAt == "" || snapshot.Source != "weknora-test:/srv/kb-backups/rds-logical" {
|
||||
t.Fatalf("观测时间/来源不对: %+v", snapshot)
|
||||
if snapshot.Source != "weknora-test:"+hostRoot || snapshot.Measurement != "host_collector" {
|
||||
t.Fatalf("来源/测量方式不对: %+v", snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotStaleIsUnknown(t *testing.T) {
|
||||
value := validMetadata()
|
||||
value.CollectedAt = "2026-11-20T00:00:00Z"
|
||||
filename := writeMetadata(t, value)
|
||||
snapshot := readerAt(filename, time.Date(2026, 11, 20, 1, 5, 0, 0, time.UTC)).Snapshot(context.Background())
|
||||
if snapshot.Metadata.Valid || snapshot.Metadata.Fresh || snapshot.Metadata.Complete {
|
||||
t.Fatalf("陈旧快照不能算有效: %+v", snapshot.Metadata)
|
||||
}
|
||||
if len(snapshot.Batches) != 0 || snapshot.Capacity.Measured || snapshot.Totals.ActualBytes != nil {
|
||||
t.Fatalf("陈旧快照不能给批次/容量/占盘: %+v", snapshot)
|
||||
}
|
||||
if !strings.Contains(snapshot.Error, "陈旧") {
|
||||
t.Fatalf("陈旧原因不清: %q", snapshot.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotMissingMetadata(t *testing.T) {
|
||||
reader := New(Config{MetadataPath: filepath.Join(t.TempDir(), "missing.json"), HostRoot: hostRoot})
|
||||
snapshot := reader.Snapshot(context.Background())
|
||||
if !snapshot.Wired || snapshot.Metadata.Present || snapshot.Metadata.Valid {
|
||||
t.Fatalf("缺失快照不能算有效: %+v", snapshot.Metadata)
|
||||
}
|
||||
if !strings.Contains(snapshot.Error, "不存在") {
|
||||
t.Fatalf("缺失原因不清: %q", snapshot.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotPermissionFailureNeverUsesPartialBytes(t *testing.T) {
|
||||
value := validMetadata()
|
||||
value.Complete = false
|
||||
value.Errors = nil
|
||||
value.Batches[0].Complete = false
|
||||
value.Batches[0].Errors = []string{"遍历失败:PermissionError: Permission denied ('.../weknora_v090prod.dir')"}
|
||||
// 模拟采集器在权限失败前已经数到的部分字节;API 绝不能把它们当占盘。
|
||||
value.Batches[0].ActualBytes = int64p(100)
|
||||
value.Batches[0].Databases[0].ActualBytes = int64p(100)
|
||||
filename := writeMetadata(t, value)
|
||||
snapshot := readerAt(filename, time.Date(2026, 11, 20, 1, 5, 0, 0, time.UTC)).Snapshot(context.Background())
|
||||
if !snapshot.Metadata.Valid || snapshot.Metadata.Complete {
|
||||
t.Fatalf("不完整快照应 valid=true/complete=false: %+v", snapshot.Metadata)
|
||||
}
|
||||
if snapshot.Capacity.Measured || snapshot.Totals.ActualBytes != nil || snapshot.Totals.ApparentBytes != nil {
|
||||
t.Fatalf("不完整快照不能给容量/总量: %+v", snapshot)
|
||||
}
|
||||
if len(snapshot.Batches) != 3 {
|
||||
t.Fatalf("路径仍应列出: %+v", snapshot.Batches)
|
||||
}
|
||||
latest := snapshot.Batches[0]
|
||||
if latest.ActualBytes != nil || latest.ApparentBytes != nil || latest.FileCount != nil || latest.SizesComplete {
|
||||
t.Fatalf("不完整批次不能保留部分字节: %+v", latest)
|
||||
}
|
||||
if latest.Databases[0].ActualBytes != nil || latest.Databases[0].ApparentBytes != nil {
|
||||
t.Fatalf("不完整子库不能保留部分字节: %+v", latest.Databases[0])
|
||||
}
|
||||
if !strings.Contains(snapshot.Metadata.Error, "Permission") {
|
||||
t.Fatalf("权限失败原因丢失: %q", snapshot.Metadata.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotTruncatedIsUnknown(t *testing.T) {
|
||||
value := validMetadata()
|
||||
value.Truncated = true
|
||||
filename := writeMetadata(t, value)
|
||||
snapshot := readerAt(filename, time.Date(2026, 11, 20, 1, 5, 0, 0, time.UTC)).Snapshot(context.Background())
|
||||
if snapshot.Metadata.Complete || snapshot.Capacity.Measured || snapshot.Totals.ActualBytes != nil {
|
||||
t.Fatalf("截断快照不能算完整/容量: %+v", snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotRejectsPathMismatch(t *testing.T) {
|
||||
value := validMetadata()
|
||||
value.Batches[0].Path = "/srv/kb-backups/rds-logical/../evil"
|
||||
filename := writeMetadata(t, value)
|
||||
snapshot := readerAt(filename, time.Date(2026, 11, 20, 1, 5, 0, 0, time.UTC)).Snapshot(context.Background())
|
||||
if snapshot.Metadata.Valid || len(snapshot.Batches) != 0 {
|
||||
t.Fatalf("路径不一致必须整块拒绝: %+v", snapshot.Metadata)
|
||||
}
|
||||
if !strings.Contains(snapshot.Error, "路径") {
|
||||
t.Fatalf("路径错误原因不清: %q", snapshot.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotRejectsUnsupportedSchema(t *testing.T) {
|
||||
value := validMetadata()
|
||||
value.SchemaVersion = 99
|
||||
filename := writeMetadata(t, value)
|
||||
snapshot := readerAt(filename, time.Date(2026, 11, 20, 1, 5, 0, 0, time.UTC)).Snapshot(context.Background())
|
||||
if snapshot.Metadata.Valid || !strings.Contains(snapshot.Error, "schema_version") {
|
||||
t.Fatalf("不支持的 schema 必须拒绝: %+v", snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,45 +262,19 @@ func TestSnapshotUnconfigured(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotMissingRoot(t *testing.T) {
|
||||
reader := New(Config{Root: filepath.Join(t.TempDir(), "missing")})
|
||||
snapshot := reader.Snapshot(context.Background())
|
||||
if !snapshot.Wired || snapshot.Error == "" || snapshot.Capacity.Measured {
|
||||
t.Fatalf("目录不存在应报错且不报容量: %+v", snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotRefusesSymlinkBatches(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
outside := t.TempDir()
|
||||
writeFile(t, filepath.Join(outside, "MANIFEST.json"), `{"databases":["escaped"]}`)
|
||||
if err := os.Symlink(outside, filepath.Join(root, "20261118T193001Z")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snapshot := New(Config{Root: root}).Snapshot(context.Background())
|
||||
if len(snapshot.Batches) != 0 {
|
||||
t.Fatalf("符号链接批次不能跟随: %+v", snapshot.Batches)
|
||||
}
|
||||
if len(snapshot.Skipped) != 1 || !strings.Contains(snapshot.Skipped[0].Reason, "符号链接") {
|
||||
t.Fatalf("跳过原因不对: %+v", snapshot.Skipped)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotDoesNotUseContainerRootCapacity(t *testing.T) {
|
||||
root := fixtureRoot(t)
|
||||
reader := New(Config{Root: root})
|
||||
reader.device = func(string) (uint64, error) { return 7, nil }
|
||||
reader.statfs = func(string) (statFS, error) {
|
||||
return statFS{Total: 999, Free: 1, Available: 1}, nil
|
||||
}
|
||||
snapshot := reader.Snapshot(context.Background())
|
||||
if snapshot.Mount.MountVerified || !snapshot.Mount.LivesOnContainerRoot {
|
||||
t.Fatalf("同设备必须判定为容器根盘: %+v", snapshot.Mount)
|
||||
}
|
||||
if snapshot.Capacity.Measured || snapshot.Capacity.TotalBytes != 0 || snapshot.Capacity.Error == "" {
|
||||
t.Fatalf("容器根盘不能冒充主机容量: %+v", snapshot.Capacity)
|
||||
}
|
||||
if len(snapshot.Batches) != 3 {
|
||||
t.Fatalf("容量不可用不应影响批次读取: %+v", snapshot.Batches)
|
||||
// 宿主采集器的权限失败回归:self-test 用注入 walker 模拟 PermissionError,
|
||||
// 断言不完整时字节为 null 且不产生部分统计。
|
||||
func TestCollectorSelfTest(t *testing.T) {
|
||||
python, err := exec.LookPath("python3")
|
||||
if err != nil {
|
||||
t.Skip("python3 not available")
|
||||
}
|
||||
command := exec.Command(python, "../../scripts/collect_backup_metadata.py", "--self-test")
|
||||
output, err := command.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("collector self-test failed: %v\n%s", err, output)
|
||||
}
|
||||
if !strings.Contains(string(output), "self-test ok") {
|
||||
t.Fatalf("collector self-test output unexpected: %s", output)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user