github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/internal/backend/test/tests_test.go (about)

     1  package test_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  //go:generate go run generate_test_list.go
    15  
    16  type memConfig struct {
    17  	be restic.Backend
    18  }
    19  
    20  func newTestSuite(t testing.TB) *test.Suite {
    21  	return &test.Suite{
    22  		// NewConfig returns a config for a new temporary backend that will be used in tests.
    23  		NewConfig: func() (interface{}, error) {
    24  			return &memConfig{}, nil
    25  		},
    26  
    27  		// CreateFn is a function that creates a temporary repository for the tests.
    28  		Create: func(cfg interface{}) (restic.Backend, error) {
    29  			c := cfg.(*memConfig)
    30  			if c.be != nil {
    31  				ok, err := c.be.Test(context.TODO(), restic.Handle{Type: restic.ConfigFile})
    32  				if err != nil {
    33  					return nil, err
    34  				}
    35  
    36  				if ok {
    37  					return nil, errors.New("config already exists")
    38  				}
    39  			}
    40  
    41  			c.be = mem.New()
    42  			return c.be, nil
    43  		},
    44  
    45  		// OpenFn is a function that opens a previously created temporary repository.
    46  		Open: func(cfg interface{}) (restic.Backend, error) {
    47  			c := cfg.(*memConfig)
    48  			if c.be == nil {
    49  				c.be = mem.New()
    50  			}
    51  			return c.be, nil
    52  		},
    53  
    54  		// CleanupFn removes data created during the tests.
    55  		Cleanup: func(cfg interface{}) error {
    56  			// no cleanup needed
    57  			return nil
    58  		},
    59  	}
    60  }
    61  
    62  func TestSuiteBackendMem(t *testing.T) {
    63  	newTestSuite(t).RunTests(t)
    64  }
    65  
    66  func BenchmarkSuiteBackendMem(b *testing.B) {
    67  	newTestSuite(b).RunBenchmarks(b)
    68  }