github.com/mckael/restic@v0.8.3/internal/cache/backend_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"math/rand"
     7  	"testing"
     8  
     9  	"github.com/restic/restic/internal/backend"
    10  	"github.com/restic/restic/internal/backend/mem"
    11  	"github.com/restic/restic/internal/restic"
    12  	"github.com/restic/restic/internal/test"
    13  )
    14  
    15  func loadAndCompare(t testing.TB, be restic.Backend, h restic.Handle, data []byte) {
    16  	buf, err := backend.LoadAll(context.TODO(), be, h)
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  
    21  	if len(buf) != len(data) {
    22  		t.Fatalf("wrong number of bytes read, want %v, got %v", len(data), len(buf))
    23  	}
    24  
    25  	if !bytes.Equal(buf, data) {
    26  		t.Fatalf("wrong data returned, want:\n  %02x\ngot:\n  %02x", data[:16], buf[:16])
    27  	}
    28  }
    29  
    30  func save(t testing.TB, be restic.Backend, h restic.Handle, data []byte) {
    31  	err := be.Save(context.TODO(), h, bytes.NewReader(data))
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  }
    36  
    37  func remove(t testing.TB, be restic.Backend, h restic.Handle) {
    38  	err := be.Remove(context.TODO(), h)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  }
    43  
    44  func randomData(n int) (restic.Handle, []byte) {
    45  	data := test.Random(rand.Int(), n)
    46  	id := restic.Hash(data)
    47  	copy(id[:], data)
    48  	h := restic.Handle{
    49  		Type: restic.IndexFile,
    50  		Name: id.String(),
    51  	}
    52  	return h, data
    53  }
    54  
    55  func TestBackend(t *testing.T) {
    56  	be := mem.New()
    57  
    58  	c, cleanup := TestNewCache(t)
    59  	defer cleanup()
    60  
    61  	wbe := c.Wrap(be)
    62  
    63  	h, data := randomData(5234142)
    64  
    65  	// save directly in backend
    66  	save(t, be, h, data)
    67  	if c.Has(h) {
    68  		t.Errorf("cache has file too early")
    69  	}
    70  
    71  	// load data via cache
    72  	loadAndCompare(t, wbe, h, data)
    73  	if !c.Has(h) {
    74  		t.Errorf("cache doesn't have file after load")
    75  	}
    76  
    77  	// remove via cache
    78  	remove(t, wbe, h)
    79  	if c.Has(h) {
    80  		t.Errorf("cache has file after remove")
    81  	}
    82  
    83  	// save via cache
    84  	save(t, wbe, h, data)
    85  	if !c.Has(h) {
    86  		t.Errorf("cache doesn't have file after load")
    87  	}
    88  
    89  	// load data directly from backend
    90  	loadAndCompare(t, be, h, data)
    91  
    92  	// load data via cache
    93  	loadAndCompare(t, be, h, data)
    94  
    95  	// remove directly
    96  	remove(t, be, h)
    97  	if !c.Has(h) {
    98  		t.Errorf("file not in cache any more")
    99  	}
   100  
   101  	// run stat
   102  	_, err := wbe.Stat(context.TODO(), h)
   103  	if err == nil {
   104  		t.Errorf("expected error for removed file not found, got nil")
   105  	}
   106  
   107  	if !wbe.IsNotExist(err) {
   108  		t.Errorf("Stat() returned error that does not match IsNotExist(): %v", err)
   109  	}
   110  
   111  	if c.Has(h) {
   112  		t.Errorf("removed file still in cache after stat")
   113  	}
   114  }