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
|
|
|
}
|
|
|
|
|
2024-11-02 19:27:38 +00:00
|
|
|
func startFileSaver(ctx context.Context, t testing.TB, fsInst fs.FS) (*fileSaver, context.Context, *errgroup.Group) {
|
2022-05-27 17:08:50 +00:00
|
|
|
wg, ctx := errgroup.WithContext(ctx)
|
2018-05-12 19:40:31 +00:00
|
|
|
|
2024-08-27 09:26:52 +00:00
|
|
|
saveBlob := func(ctx context.Context, tpe restic.BlobType, buf *buffer, _ string, cb func(saveBlobResponse)) {
|
|
|
|
cb(saveBlobResponse{
|
2022-11-05 12:42:17 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-08-27 09:26:52 +00:00
|
|
|
s := newFileSaver(ctx, wg, saveBlob, pol, workers, workers)
|
2024-11-02 19:27:38 +00:00
|
|
|
s.NodeFromFileInfo = func(snPath, filename string, meta ToNoder, ignoreXattrListError bool) (*restic.Node, error) {
|
|
|
|
return meta.ToNode(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{}
|
2024-08-28 08:58:07 +00:00
|
|
|
s, ctx, wg := startFileSaver(ctx, t, testFs)
|
2018-05-12 19:40:31 +00:00
|
|
|
|
2024-08-27 09:26:52 +00:00
|
|
|
var results []futureNode
|
2018-05-12 19:40:31 +00:00
|
|
|
|
|
|
|
for _, filename := range files {
|
2024-11-02 19:27:38 +00:00
|
|
|
f, err := testFs.OpenFile(filename, os.O_RDONLY, false)
|
2018-05-12 19:40:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-11-02 19:27:38 +00:00
|
|
|
ff := s.Save(ctx, filename, filename, f, 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)
|
|
|
|
}
|
|
|
|
}
|