2017-04-10 20:35:03 +00:00
|
|
|
package sftp_test
|
|
|
|
|
|
|
|
import (
|
2017-06-03 15:39:57 +00:00
|
|
|
"context"
|
2017-04-10 20:35:03 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/backend/sftp"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2017-04-10 20:35:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLayout(t *testing.T) {
|
2017-05-01 20:55:09 +00:00
|
|
|
if sftpServer == "" {
|
2017-04-10 20:51:00 +00:00
|
|
|
t.Skip("sftp server binary not available")
|
|
|
|
}
|
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
path, cleanup := rtest.TempDir(t)
|
2017-04-10 20:35:03 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
filename string
|
|
|
|
layout string
|
|
|
|
failureExpected bool
|
2020-08-16 09:16:38 +00:00
|
|
|
packfiles map[string]bool
|
2017-04-10 20:35:03 +00:00
|
|
|
}{
|
2017-05-15 20:11:09 +00:00
|
|
|
{"repo-layout-default.tar.gz", "", false, map[string]bool{
|
2017-04-10 21:21:04 +00:00
|
|
|
"aa464e9fd598fe4202492ee317ffa728e82fa83a1de1a61996e5bd2d6651646c": false,
|
|
|
|
"fc919a3b421850f6fa66ad22ebcf91e433e79ffef25becf8aef7c7b1eca91683": false,
|
|
|
|
"c089d62788da14f8b7cbf77188305c0874906f0b73d3fce5a8869050e8d0c0e1": false,
|
|
|
|
}},
|
2017-05-15 20:11:09 +00:00
|
|
|
{"repo-layout-s3legacy.tar.gz", "", false, map[string]bool{
|
2017-04-10 21:21:04 +00:00
|
|
|
"fc919a3b421850f6fa66ad22ebcf91e433e79ffef25becf8aef7c7b1eca91683": false,
|
|
|
|
"c089d62788da14f8b7cbf77188305c0874906f0b73d3fce5a8869050e8d0c0e1": false,
|
|
|
|
"aa464e9fd598fe4202492ee317ffa728e82fa83a1de1a61996e5bd2d6651646c": false,
|
|
|
|
}},
|
2017-04-10 20:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.filename, func(t *testing.T) {
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.SetupTarTestFixture(t, path, filepath.Join("..", "testdata", test.filename))
|
2017-04-10 20:35:03 +00:00
|
|
|
|
|
|
|
repo := filepath.Join(path, "repo")
|
2020-09-19 20:01:32 +00:00
|
|
|
be, err := sftp.Open(context.TODO(), sftp.Config{
|
2017-05-01 20:55:09 +00:00
|
|
|
Command: fmt.Sprintf("%q -e", sftpServer),
|
2017-04-10 20:35:03 +00:00
|
|
|
Path: repo,
|
|
|
|
Layout: test.layout,
|
2018-01-17 22:14:47 +00:00
|
|
|
})
|
2017-04-10 20:35:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if be == nil {
|
|
|
|
t.Fatalf("Open() returned nil but no error")
|
|
|
|
}
|
|
|
|
|
2020-08-16 09:16:38 +00:00
|
|
|
packs := make(map[string]bool)
|
|
|
|
err = be.List(context.TODO(), restic.PackFile, func(fi restic.FileInfo) error {
|
|
|
|
packs[fi.Name] = false
|
2018-01-20 18:34:38 +00:00
|
|
|
return nil
|
|
|
|
})
|
2020-02-12 21:36:21 +00:00
|
|
|
rtest.OK(t, err)
|
2017-04-10 21:21:04 +00:00
|
|
|
|
2020-08-16 09:16:38 +00:00
|
|
|
if len(packs) == 0 {
|
|
|
|
t.Errorf("List() returned zero pack files")
|
2017-04-10 21:21:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 09:16:38 +00:00
|
|
|
for id := range test.packfiles {
|
|
|
|
if _, ok := packs[id]; !ok {
|
|
|
|
t.Errorf("packfile with id %v not found", id)
|
2017-04-10 21:21:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 09:16:38 +00:00
|
|
|
packs[id] = true
|
2017-04-10 21:21:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 09:16:38 +00:00
|
|
|
for id, v := range packs {
|
2017-04-10 21:21:04 +00:00
|
|
|
if !v {
|
|
|
|
t.Errorf("unexpected id %v found", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = be.Close(); err != nil {
|
|
|
|
t.Errorf("Close() returned error %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.RemoveAll(t, filepath.Join(path, "repo"))
|
2017-04-10 20:35:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|