chore: seed migration objects batch 5

This commit is contained in:
dev
2026-10-18 13:37:24 +08:00
parent 53c9c0afd8
commit c8c40b1dd2
239 changed files with 16941 additions and 1544 deletions
@@ -1,61 +1,52 @@
package storager
import (
"crypto/md5"
"errors"
"fmt"
"io"
"bytes"
"os"
"path/filepath"
"testing"
)
func TestMergePartFile(t *testing.T) {
srcFile, _ := os.Open("1.zip")
defer srcFile.Close()
fileInfo, err := srcFile.Stat()
if err != nil {
t.Error(err)
tempDir := t.TempDir()
partDir := filepath.Join(tempDir, "parts")
if err := os.MkdirAll(partDir, 0o755); err != nil {
t.Fatal(err)
}
hash := md5.New()
_, err = io.Copy(hash, srcFile)
sum := hash.Sum(nil)
fileHash := fmt.Sprintf("%x", sum)
var singleSize = int64(1024 * 1024 * 100)
chunks := int(fileInfo.Size() / singleSize)
if fileInfo.Size()%singleSize != 0 {
chunks += 1
parts := map[string][]byte{
"1": []byte("first-"),
"2": []byte("second-"),
"10": []byte("tenth"),
}
srcFile.Seek(0, io.SeekStart)
for j := 0; j < chunks; j++ {
partSize := singleSize
if j == chunks-1 {
partSize = fileInfo.Size() - int64(j)*singleSize
for name, content := range parts {
if err := os.WriteFile(filepath.Join(partDir, name), content, 0o644); err != nil {
t.Fatal(err)
}
partData := make([]byte, partSize)
_, err = io.ReadFull(srcFile, partData)
pf, _ := os.Create(fmt.Sprintf("tmp/%d", j+1))
_, err = pf.Write(partData)
if err != nil {
t.Error(err)
return
}
pf.Close()
}
err = MergePartFile("tmp/", "2.zip")
destination := filepath.Join(tempDir, "merged.bin")
if err := os.WriteFile(destination, []byte("stale-data"), 0o644); err != nil {
t.Fatal(err)
}
if err := MergePartFile(partDir, destination); err != nil {
t.Fatalf("合并分片失败: %v", err)
}
actual, err := os.ReadFile(destination)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}
expected := []byte("first-second-tenth")
if !bytes.Equal(actual, expected) {
t.Fatalf("合并内容不符 expected=%q actual=%q", expected, actual)
}
}
f2, _ := os.Open("2.zip")
hash2 := md5.New()
_, err = io.Copy(hash2, f2)
sum2 := hash.Sum(nil)
fileHash2 := fmt.Sprintf("%x", sum2)
if fileHash != fileHash2 {
t.Error(errors.New("hash mismatch"))
func TestMergePartFileRejectsEmptyDirectory(t *testing.T) {
partDir := t.TempDir()
err := MergePartFile(partDir, filepath.Join(t.TempDir(), "merged.bin"))
if err == nil {
t.Fatal("空分片目录应返回错误")
}
}