github.com/mckael/restic@v0.8.3/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  			})
    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  			err = be.List(context.TODO(), restic.DataFile, func(fi restic.FileInfo) error {
    60  				datafiles[fi.Name] = false
    61  				return nil
    62  			})
    63  
    64  			if len(datafiles) == 0 {
    65  				t.Errorf("List() returned zero data files")
    66  			}
    67  
    68  			for id := range test.datafiles {
    69  				if _, ok := datafiles[id]; !ok {
    70  					t.Errorf("datafile with id %v not found", id)
    71  				}
    72  
    73  				datafiles[id] = true
    74  			}
    75  
    76  			for id, v := range datafiles {
    77  				if !v {
    78  					t.Errorf("unexpected id %v found", id)
    79  				}
    80  			}
    81  
    82  			if err = be.Close(); err != nil {
    83  				t.Errorf("Close() returned error %v", err)
    84  			}
    85  
    86  			rtest.RemoveAll(t, filepath.Join(path, "repo"))
    87  		})
    88  	}
    89  }