Fix pointer receivers for KopiaWrapper handles (#131)
* Fix pointer receivers * Some basic tests about closing KopiaWrapper * Return pointers to structs
This commit is contained in:
parent
0aa0915adf
commit
f68a4e3f46
@ -164,7 +164,7 @@ func makeS3Config() (storage.S3Config, storage.CommonConfig, error) {
|
||||
})
|
||||
}
|
||||
|
||||
func closeRepo(ctx context.Context, r repository.Repository) {
|
||||
func closeRepo(ctx context.Context, r *repository.Repository) {
|
||||
if err := r.Close(ctx); err != nil {
|
||||
fmt.Printf("Error closing repository: %v\n", err)
|
||||
}
|
||||
|
||||
@ -24,11 +24,11 @@ type KopiaWrapper struct {
|
||||
rep repo.Repository
|
||||
}
|
||||
|
||||
func New(s storage.Storage) KopiaWrapper {
|
||||
return KopiaWrapper{storage: s}
|
||||
func New(s storage.Storage) *KopiaWrapper {
|
||||
return &KopiaWrapper{storage: s}
|
||||
}
|
||||
|
||||
func (kw KopiaWrapper) Initialize(ctx context.Context) error {
|
||||
func (kw *KopiaWrapper) Initialize(ctx context.Context) error {
|
||||
bst, err := blobStoreByProvider(ctx, kw.storage)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, errInit.Error())
|
||||
@ -63,7 +63,7 @@ func (kw KopiaWrapper) Initialize(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (kw KopiaWrapper) Connect(ctx context.Context) error {
|
||||
func (kw *KopiaWrapper) Connect(ctx context.Context) error {
|
||||
bst, err := blobStoreByProvider(ctx, kw.storage)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, errInit.Error())
|
||||
@ -102,7 +102,7 @@ func blobStoreByProvider(ctx context.Context, s storage.Storage) (blob.Storage,
|
||||
}
|
||||
}
|
||||
|
||||
func (kw KopiaWrapper) Close(ctx context.Context) error {
|
||||
func (kw *KopiaWrapper) Close(ctx context.Context) error {
|
||||
if kw.rep == nil {
|
||||
return nil
|
||||
}
|
||||
@ -117,7 +117,7 @@ func (kw KopiaWrapper) Close(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (kw KopiaWrapper) open(ctx context.Context, password string) error {
|
||||
func (kw *KopiaWrapper) open(ctx context.Context, password string) error {
|
||||
// TODO(ashmrtnz): issue #75: nil here should be storage.ConnectionOptions().
|
||||
rep, err := repo.Open(ctx, defaultKopiaConfigFilePath, password, nil)
|
||||
if err != nil {
|
||||
|
||||
78
src/internal/kopia/kopia_test.go
Normal file
78
src/internal/kopia/kopia_test.go
Normal file
@ -0,0 +1,78 @@
|
||||
package kopia
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
ctesting "github.com/alcionai/corso/internal/testing"
|
||||
)
|
||||
|
||||
func openKopiaRepo(ctx context.Context, prefix string) (*KopiaWrapper, error) {
|
||||
storage, err := ctesting.NewS3Storage(prefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
k := New(storage)
|
||||
if err = k.Initialize(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// ---------------
|
||||
// unit tests
|
||||
// ---------------
|
||||
type KopiaUnitSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func (suite *KopiaUnitSuite) TestCloseWithoutOpenDoesNotCrash() {
|
||||
ctx := context.Background()
|
||||
timeOfTest := time.Now().UTC().Format("2016-01-02T15:04:05")
|
||||
suite.T().Logf("TestCloseWithoutOpenDoesNotCrash() run at %s", timeOfTest)
|
||||
|
||||
k := KopiaWrapper{}
|
||||
assert.NotPanics(suite.T(), func() {
|
||||
k.Close(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------
|
||||
// integration tests that use kopia
|
||||
// ---------------
|
||||
type KopiaIntegrationSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func TestKopiaIntegrationSuite(t *testing.T) {
|
||||
runIntegrationTests := os.Getenv("INTEGRATION_TESTING")
|
||||
if runIntegrationTests != "true" {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
suite.Run(t, new(KopiaIntegrationSuite))
|
||||
}
|
||||
|
||||
func (suite *KopiaIntegrationSuite) SetupSuite() {
|
||||
require.NoError(suite.T(), ctesting.CheckS3EnvVars())
|
||||
}
|
||||
|
||||
func (suite *KopiaIntegrationSuite) TestCloseTwiceDoesNotCrash() {
|
||||
ctx := context.Background()
|
||||
timeOfTest := time.Now().UTC().Format("2016-01-02T15:04:05")
|
||||
suite.T().Logf("TestCloseTwiceDoesNotCrash() run at %s", timeOfTest)
|
||||
|
||||
k, err := openKopiaRepo(ctx, "init-s3-"+timeOfTest)
|
||||
assert.NoError(suite.T(), err)
|
||||
assert.NoError(suite.T(), k.Close(ctx))
|
||||
assert.Nil(suite.T(), k.rep)
|
||||
assert.NoError(suite.T(), k.Close(ctx))
|
||||
}
|
||||
@ -49,19 +49,19 @@ func Initialize(
|
||||
ctx context.Context,
|
||||
acct Account,
|
||||
storage storage.Storage,
|
||||
) (Repository, error) {
|
||||
) (*Repository, error) {
|
||||
k := kopia.New(storage)
|
||||
if err := k.Initialize(ctx); err != nil {
|
||||
return Repository{}, err
|
||||
return nil, err
|
||||
}
|
||||
r := Repository{
|
||||
ID: uuid.New(),
|
||||
Version: "v1",
|
||||
Account: acct,
|
||||
Storage: storage,
|
||||
dataLayer: &k,
|
||||
dataLayer: k,
|
||||
}
|
||||
return r, nil
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
// Connect will:
|
||||
@ -73,22 +73,22 @@ func Connect(
|
||||
ctx context.Context,
|
||||
acct Account,
|
||||
storage storage.Storage,
|
||||
) (Repository, error) {
|
||||
) (*Repository, error) {
|
||||
k := kopia.New(storage)
|
||||
if err := k.Connect(ctx); err != nil {
|
||||
return Repository{}, err
|
||||
return nil, err
|
||||
}
|
||||
// todo: ID and CreatedAt should get retrieved from a stored kopia config.
|
||||
r := Repository{
|
||||
Version: "v1",
|
||||
Account: acct,
|
||||
Storage: storage,
|
||||
dataLayer: &k,
|
||||
dataLayer: k,
|
||||
}
|
||||
return r, nil
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (r Repository) Close(ctx context.Context) error {
|
||||
func (r *Repository) Close(ctx context.Context) error {
|
||||
if r.dataLayer == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user