github.com/mckael/restic@v0.8.3/internal/backend/b2/b2_test.go (about)

     1  package b2_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/restic/restic/internal/backend"
    11  	"github.com/restic/restic/internal/backend/b2"
    12  	"github.com/restic/restic/internal/backend/test"
    13  	"github.com/restic/restic/internal/restic"
    14  
    15  	rtest "github.com/restic/restic/internal/test"
    16  )
    17  
    18  func newB2TestSuite(t testing.TB) *test.Suite {
    19  	tr, err := backend.Transport(backend.TransportOptions{})
    20  	if err != nil {
    21  		t.Fatalf("cannot create transport for tests: %v", err)
    22  	}
    23  
    24  	return &test.Suite{
    25  		// do not use excessive data
    26  		MinimalData: true,
    27  
    28  		// wait for at most 10 seconds for removed files to disappear
    29  		WaitForDelayedRemoval: 10 * time.Second,
    30  
    31  		// NewConfig returns a config for a new temporary backend that will be used in tests.
    32  		NewConfig: func() (interface{}, error) {
    33  			b2cfg, err := b2.ParseConfig(os.Getenv("RESTIC_TEST_B2_REPOSITORY"))
    34  			if err != nil {
    35  				return nil, err
    36  			}
    37  
    38  			cfg := b2cfg.(b2.Config)
    39  			cfg.AccountID = os.Getenv("RESTIC_TEST_B2_ACCOUNT_ID")
    40  			cfg.Key = os.Getenv("RESTIC_TEST_B2_ACCOUNT_KEY")
    41  			cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano())
    42  			return cfg, nil
    43  		},
    44  
    45  		// CreateFn is a function that creates a temporary repository for the tests.
    46  		Create: func(config interface{}) (restic.Backend, error) {
    47  			cfg := config.(b2.Config)
    48  			return b2.Create(context.Background(), cfg, tr)
    49  		},
    50  
    51  		// OpenFn is a function that opens a previously created temporary repository.
    52  		Open: func(config interface{}) (restic.Backend, error) {
    53  			cfg := config.(b2.Config)
    54  			return b2.Open(context.Background(), cfg, tr)
    55  		},
    56  
    57  		// CleanupFn removes data created during the tests.
    58  		Cleanup: func(config interface{}) error {
    59  			cfg := config.(b2.Config)
    60  			be, err := b2.Open(context.Background(), cfg, tr)
    61  			if err != nil {
    62  				return err
    63  			}
    64  
    65  			return be.Delete(context.TODO())
    66  		},
    67  	}
    68  }
    69  
    70  func testVars(t testing.TB) {
    71  	vars := []string{
    72  		"RESTIC_TEST_B2_ACCOUNT_ID",
    73  		"RESTIC_TEST_B2_ACCOUNT_KEY",
    74  		"RESTIC_TEST_B2_REPOSITORY",
    75  	}
    76  
    77  	for _, v := range vars {
    78  		if os.Getenv(v) == "" {
    79  			t.Skipf("environment variable %v not set", v)
    80  			return
    81  		}
    82  	}
    83  }
    84  
    85  func TestBackendB2(t *testing.T) {
    86  	defer func() {
    87  		if t.Skipped() {
    88  			rtest.SkipDisallowed(t, "restic/backend/b2.TestBackendB2")
    89  		}
    90  	}()
    91  
    92  	testVars(t)
    93  	newB2TestSuite(t).RunTests(t)
    94  }
    95  
    96  func BenchmarkBackendb2(t *testing.B) {
    97  	testVars(t)
    98  	newB2TestSuite(t).RunBenchmarks(t)
    99  }