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

     1  package gs_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/restic/restic/internal/backend/gs"
    11  	"github.com/restic/restic/internal/backend/test"
    12  	"github.com/restic/restic/internal/errors"
    13  	"github.com/restic/restic/internal/restic"
    14  	rtest "github.com/restic/restic/internal/test"
    15  )
    16  
    17  func newGSTestSuite(t testing.TB) *test.Suite {
    18  	return &test.Suite{
    19  		// do not use excessive data
    20  		MinimalData: true,
    21  
    22  		// NewConfig returns a config for a new temporary backend that will be used in tests.
    23  		NewConfig: func() (interface{}, error) {
    24  			gscfg, err := gs.ParseConfig(os.Getenv("RESTIC_TEST_GS_REPOSITORY"))
    25  			if err != nil {
    26  				return nil, err
    27  			}
    28  
    29  			cfg := gscfg.(gs.Config)
    30  			cfg.ProjectID = os.Getenv("RESTIC_TEST_GS_PROJECT_ID")
    31  			cfg.JSONKeyPath = os.Getenv("RESTIC_TEST_GS_APPLICATION_CREDENTIALS")
    32  			cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano())
    33  			return cfg, nil
    34  		},
    35  
    36  		// CreateFn is a function that creates a temporary repository for the tests.
    37  		Create: func(config interface{}) (restic.Backend, error) {
    38  			cfg := config.(gs.Config)
    39  
    40  			be, err := gs.Create(cfg)
    41  			if err != nil {
    42  				return nil, err
    43  			}
    44  
    45  			exists, err := be.Test(context.TODO(), restic.Handle{Type: restic.ConfigFile})
    46  			if err != nil {
    47  				return nil, err
    48  			}
    49  
    50  			if exists {
    51  				return nil, errors.New("config already exists")
    52  			}
    53  
    54  			return be, nil
    55  		},
    56  
    57  		// OpenFn is a function that opens a previously created temporary repository.
    58  		Open: func(config interface{}) (restic.Backend, error) {
    59  			cfg := config.(gs.Config)
    60  			return gs.Open(cfg)
    61  		},
    62  
    63  		// CleanupFn removes data created during the tests.
    64  		Cleanup: func(config interface{}) error {
    65  			cfg := config.(gs.Config)
    66  
    67  			be, err := gs.Open(cfg)
    68  			if err != nil {
    69  				return err
    70  			}
    71  
    72  			return be.Delete(context.TODO())
    73  		},
    74  	}
    75  }
    76  
    77  func TestBackendGS(t *testing.T) {
    78  	defer func() {
    79  		if t.Skipped() {
    80  			rtest.SkipDisallowed(t, "restic/backend/gs.TestBackendGS")
    81  		}
    82  	}()
    83  
    84  	vars := []string{
    85  		"RESTIC_TEST_GS_PROJECT_ID",
    86  		"RESTIC_TEST_GS_APPLICATION_CREDENTIALS",
    87  		"RESTIC_TEST_GS_REPOSITORY",
    88  	}
    89  
    90  	for _, v := range vars {
    91  		if os.Getenv(v) == "" {
    92  			t.Skipf("environment variable %v not set", v)
    93  			return
    94  		}
    95  	}
    96  
    97  	t.Logf("run tests")
    98  	newGSTestSuite(t).RunTests(t)
    99  }
   100  
   101  func BenchmarkBackendGS(t *testing.B) {
   102  	vars := []string{
   103  		"RESTIC_TEST_GS_PROJECT_ID",
   104  		"RESTIC_TEST_GS_APPLICATION_CREDENTIALS",
   105  		"RESTIC_TEST_GS_REPOSITORY",
   106  	}
   107  
   108  	for _, v := range vars {
   109  		if os.Getenv(v) == "" {
   110  			t.Skipf("environment variable %v not set", v)
   111  			return
   112  		}
   113  	}
   114  
   115  	t.Logf("run tests")
   116  	newGSTestSuite(t).RunBenchmarks(t)
   117  }