github.com/advanderveer/restic@v0.8.1-0.20171209104529-42a8c19aaea6/internal/backend/sftp/layout_test.go (about) 1 package sftp_test 2 3 import ( 4 "context" 5 "fmt" 6 "path/filepath" 7 "testing" 8 9 "github.com/restic/restic/internal/backend/sftp" 10 "github.com/restic/restic/internal/restic" 11 rtest "github.com/restic/restic/internal/test" 12 ) 13 14 func TestLayout(t *testing.T) { 15 if sftpServer == "" { 16 t.Skip("sftp server binary not available") 17 } 18 19 path, cleanup := rtest.TempDir(t) 20 defer cleanup() 21 22 var tests = []struct { 23 filename string 24 layout string 25 failureExpected bool 26 datafiles map[string]bool 27 }{ 28 {"repo-layout-default.tar.gz", "", false, map[string]bool{ 29 "aa464e9fd598fe4202492ee317ffa728e82fa83a1de1a61996e5bd2d6651646c": false, 30 "fc919a3b421850f6fa66ad22ebcf91e433e79ffef25becf8aef7c7b1eca91683": false, 31 "c089d62788da14f8b7cbf77188305c0874906f0b73d3fce5a8869050e8d0c0e1": false, 32 }}, 33 {"repo-layout-s3legacy.tar.gz", "", false, map[string]bool{ 34 "fc919a3b421850f6fa66ad22ebcf91e433e79ffef25becf8aef7c7b1eca91683": false, 35 "c089d62788da14f8b7cbf77188305c0874906f0b73d3fce5a8869050e8d0c0e1": false, 36 "aa464e9fd598fe4202492ee317ffa728e82fa83a1de1a61996e5bd2d6651646c": false, 37 }}, 38 } 39 40 for _, test := range tests { 41 t.Run(test.filename, func(t *testing.T) { 42 rtest.SetupTarTestFixture(t, path, filepath.Join("..", "testdata", test.filename)) 43 44 repo := filepath.Join(path, "repo") 45 be, err := sftp.Open(sftp.Config{ 46 Command: fmt.Sprintf("%q -e", sftpServer), 47 Path: repo, 48 Layout: test.layout, 49 }, nil, nil) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 if be == nil { 55 t.Fatalf("Open() returned nil but no error") 56 } 57 58 datafiles := make(map[string]bool) 59 for id := range be.List(context.TODO(), restic.DataFile) { 60 datafiles[id] = false 61 } 62 63 if len(datafiles) == 0 { 64 t.Errorf("List() returned zero data files") 65 } 66 67 for id := range test.datafiles { 68 if _, ok := datafiles[id]; !ok { 69 t.Errorf("datafile with id %v not found", id) 70 } 71 72 datafiles[id] = true 73 } 74 75 for id, v := range datafiles { 76 if !v { 77 t.Errorf("unexpected id %v found", id) 78 } 79 } 80 81 if err = be.Close(); err != nil { 82 t.Errorf("Close() returned error %v", err) 83 } 84 85 rtest.RemoveAll(t, filepath.Join(path, "repo")) 86 }) 87 } 88 }