2018-05-12 19:40:31 +00:00
|
|
|
package archiver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-08-19 21:08:13 +00:00
|
|
|
"os"
|
2018-05-12 19:40:31 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/chunker"
|
|
|
|
"github.com/restic/restic/internal/fs"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
"github.com/restic/restic/internal/test"
|
2022-05-27 17:08:50 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2018-05-12 19:40:31 +00:00
|
|
|
)
|
|
|
|
|
2022-12-09 12:42:33 +00:00
|
|
|
func createTestFiles(t testing.TB, num int) (files []string) {
|
|
|
|
tempdir := test.TempDir(t)
|
2018-05-12 19:40:31 +00:00
|
|
|
|
2023-05-18 17:31:42 +00:00
|
|
|
for i := 0; i < num; i++ {
|
2018-05-12 19:40:31 +00:00
|
|
|
filename := fmt.Sprintf("testfile-%d", i)
|
2022-12-02 18:36:43 +00:00
|
|
|
err := os.WriteFile(filepath.Join(tempdir, filename), []byte(filename), 0600)
|
2018-05-12 19:40:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
files = append(files, filepath.Join(tempdir, filename))
|
|
|
|
}
|
|
|
|
|
2022-12-09 12:42:33 +00:00
|
|
|
return files
|
2018-05-12 19:40:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
func startFileSaver(ctx context.Context, t testing.TB) (*FileSaver, context.Context, *errgroup.Group) {
|
|
|
|
wg, ctx := errgroup.WithContext(ctx)
|
2018-05-12 19:40:31 +00:00
|
|
|
|
2024-02-10 22:41:11 +00:00
|
|
|
saveBlob := func(ctx context.Context, tpe restic.BlobType, buf *Buffer, _ string, cb func(SaveBlobResponse)) {
|
2022-11-05 12:42:17 +00:00
|
|
|
cb(SaveBlobResponse{
|
|
|
|
id: restic.Hash(buf.Data),
|
|
|
|
length: len(buf.Data),
|
|
|
|
sizeInRepo: len(buf.Data),
|
|
|
|
known: false,
|
|
|
|
})
|
2018-05-12 19:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
workers := uint(runtime.NumCPU())
|
|
|
|
pol, err := chunker.RandomPolynomial()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
s := NewFileSaver(ctx, wg, saveBlob, pol, workers, workers)
|
2024-01-31 19:48:03 +00:00
|
|
|
s.NodeFromFileInfo = func(snPath, filename string, fi os.FileInfo, ignoreXattrListError bool) (*restic.Node, error) {
|
2024-08-26 21:03:25 +00:00
|
|
|
return fs.NodeFromFileInfo(filename, fi, ignoreXattrListError)
|
2022-08-19 21:08:13 +00:00
|
|
|
}
|
2018-05-12 19:40:31 +00:00
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
return s, ctx, wg
|
2018-05-12 19:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileSaver(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-12-09 12:42:33 +00:00
|
|
|
files := createTestFiles(t, 15)
|
2018-05-12 19:40:31 +00:00
|
|
|
|
|
|
|
startFn := func() {}
|
2022-10-22 10:05:49 +00:00
|
|
|
completeReadingFn := func() {}
|
2018-05-12 19:40:31 +00:00
|
|
|
completeFn := func(*restic.Node, ItemStats) {}
|
|
|
|
|
|
|
|
testFs := fs.Local{}
|
2022-05-27 17:08:50 +00:00
|
|
|
s, ctx, wg := startFileSaver(ctx, t)
|
2018-05-12 19:40:31 +00:00
|
|
|
|
2022-05-29 09:57:10 +00:00
|
|
|
var results []FutureNode
|
2018-05-12 19:40:31 +00:00
|
|
|
|
|
|
|
for _, filename := range files {
|
|
|
|
f, err := testFs.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-10-22 10:05:49 +00:00
|
|
|
ff := s.Save(ctx, filename, filename, f, fi, startFn, completeReadingFn, completeFn)
|
2018-05-12 19:40:31 +00:00
|
|
|
results = append(results, ff)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range results {
|
2022-05-29 09:57:10 +00:00
|
|
|
fnr := file.take(ctx)
|
|
|
|
if fnr.err != nil {
|
|
|
|
t.Errorf("unable to save file: %v", fnr.err)
|
2018-05-12 19:40:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
s.TriggerShutdown()
|
2018-05-12 19:40:31 +00:00
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
err := wg.Wait()
|
2018-05-12 19:40:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|