github.com/mckael/restic@v0.8.3/internal/backend/mem/mem_backend_test.go (about) 1 package mem_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/restic/restic/internal/errors" 8 "github.com/restic/restic/internal/restic" 9 10 "github.com/restic/restic/internal/backend/mem" 11 "github.com/restic/restic/internal/backend/test" 12 ) 13 14 type memConfig struct { 15 be restic.Backend 16 } 17 18 func newTestSuite() *test.Suite { 19 return &test.Suite{ 20 // NewConfig returns a config for a new temporary backend that will be used in tests. 21 NewConfig: func() (interface{}, error) { 22 return &memConfig{}, nil 23 }, 24 25 // CreateFn is a function that creates a temporary repository for the tests. 26 Create: func(cfg interface{}) (restic.Backend, error) { 27 c := cfg.(*memConfig) 28 if c.be != nil { 29 ok, err := c.be.Test(context.TODO(), restic.Handle{Type: restic.ConfigFile}) 30 if err != nil { 31 return nil, err 32 } 33 34 if ok { 35 return nil, errors.New("config already exists") 36 } 37 } 38 39 c.be = mem.New() 40 return c.be, nil 41 }, 42 43 // OpenFn is a function that opens a previously created temporary repository. 44 Open: func(cfg interface{}) (restic.Backend, error) { 45 c := cfg.(*memConfig) 46 if c.be == nil { 47 c.be = mem.New() 48 } 49 return c.be, nil 50 }, 51 52 // CleanupFn removes data created during the tests. 53 Cleanup: func(cfg interface{}) error { 54 // no cleanup needed 55 return nil 56 }, 57 } 58 } 59 60 func TestSuiteBackendMem(t *testing.T) { 61 newTestSuite().RunTests(t) 62 } 63 64 func BenchmarkSuiteBackendMem(t *testing.B) { 65 newTestSuite().RunBenchmarks(t) 66 }