2018-05-12 19:40:31 +00:00
|
|
|
package archiver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func createTestFiles(t testing.TB, num int) (files []string, cleanup func()) {
|
|
|
|
tempdir, cleanup := test.TempDir(t)
|
|
|
|
|
|
|
|
for i := 0; i < 15; i++ {
|
|
|
|
filename := fmt.Sprintf("testfile-%d", i)
|
|
|
|
err := ioutil.WriteFile(filepath.Join(tempdir, filename), []byte(filename), 0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
files = append(files, filepath.Join(tempdir, filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, cleanup
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-10-07 18:23:38 +00:00
|
|
|
saveBlob := func(ctx context.Context, tpe restic.BlobType, buf *Buffer, cb func(SaveBlobResponse)) {
|
|
|
|
cb(SaveBlobResponse{})
|
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)
|
2022-08-19 21:08:13 +00:00
|
|
|
s.NodeFromFileInfo = func(snPath, filename string, fi os.FileInfo) (*restic.Node, error) {
|
|
|
|
return restic.NodeFromFileInfo(filename, fi)
|
|
|
|
}
|
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()
|
|
|
|
|
|
|
|
files, cleanup := createTestFiles(t, 15)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
startFn := func() {}
|
|
|
|
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-05-29 09:57:10 +00:00
|
|
|
ff := s.Save(ctx, filename, filename, f, fi, startFn, 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)
|
|
|
|
}
|
|
|
|
}
|