github.com/mckael/restic@v0.8.3/internal/backend/local/layout_test.go (about)

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