github.com/advanderveer/restic@v0.8.1-0.20171209104529-42a8c19aaea6/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  			for id := range be.List(context.TODO(), restic.DataFile) {
    53  				datafiles[id] = false
    54  			}
    55  
    56  			if len(datafiles) == 0 {
    57  				t.Errorf("List() returned zero data files")
    58  			}
    59  
    60  			for id := range test.datafiles {
    61  				if _, ok := datafiles[id]; !ok {
    62  					t.Errorf("datafile with id %v not found", id)
    63  				}
    64  
    65  				datafiles[id] = true
    66  			}
    67  
    68  			for id, v := range datafiles {
    69  				if !v {
    70  					t.Errorf("unexpected id %v found", id)
    71  				}
    72  			}
    73  
    74  			if err = be.Close(); err != nil {
    75  				t.Errorf("Close() returned error %v", err)
    76  			}
    77  
    78  			rtest.RemoveAll(t, filepath.Join(path, "repo"))
    79  		})
    80  	}
    81  }