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:
ashmrtn 2022-06-03 16:15:51 -07:00 committed by GitHub
parent 0aa0915adf
commit f68a4e3f46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 16 deletions

View File

@ -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 { if err := r.Close(ctx); err != nil {
fmt.Printf("Error closing repository: %v\n", err) fmt.Printf("Error closing repository: %v\n", err)
} }

View File

@ -24,11 +24,11 @@ type KopiaWrapper struct {
rep repo.Repository rep repo.Repository
} }
func New(s storage.Storage) KopiaWrapper { func New(s storage.Storage) *KopiaWrapper {
return KopiaWrapper{storage: s} 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) bst, err := blobStoreByProvider(ctx, kw.storage)
if err != nil { if err != nil {
return errors.Wrap(err, errInit.Error()) return errors.Wrap(err, errInit.Error())
@ -63,7 +63,7 @@ func (kw KopiaWrapper) Initialize(ctx context.Context) error {
return nil return nil
} }
func (kw KopiaWrapper) Connect(ctx context.Context) error { func (kw *KopiaWrapper) Connect(ctx context.Context) error {
bst, err := blobStoreByProvider(ctx, kw.storage) bst, err := blobStoreByProvider(ctx, kw.storage)
if err != nil { if err != nil {
return errors.Wrap(err, errInit.Error()) 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 { if kw.rep == nil {
return nil return nil
} }
@ -117,7 +117,7 @@ func (kw KopiaWrapper) Close(ctx context.Context) error {
return nil 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(). // TODO(ashmrtnz): issue #75: nil here should be storage.ConnectionOptions().
rep, err := repo.Open(ctx, defaultKopiaConfigFilePath, password, nil) rep, err := repo.Open(ctx, defaultKopiaConfigFilePath, password, nil)
if err != nil { if err != nil {

View 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))
}

View File

@ -49,19 +49,19 @@ func Initialize(
ctx context.Context, ctx context.Context,
acct Account, acct Account,
storage storage.Storage, storage storage.Storage,
) (Repository, error) { ) (*Repository, error) {
k := kopia.New(storage) k := kopia.New(storage)
if err := k.Initialize(ctx); err != nil { if err := k.Initialize(ctx); err != nil {
return Repository{}, err return nil, err
} }
r := Repository{ r := Repository{
ID: uuid.New(), ID: uuid.New(),
Version: "v1", Version: "v1",
Account: acct, Account: acct,
Storage: storage, Storage: storage,
dataLayer: &k, dataLayer: k,
} }
return r, nil return &r, nil
} }
// Connect will: // Connect will:
@ -73,22 +73,22 @@ func Connect(
ctx context.Context, ctx context.Context,
acct Account, acct Account,
storage storage.Storage, storage storage.Storage,
) (Repository, error) { ) (*Repository, error) {
k := kopia.New(storage) k := kopia.New(storage)
if err := k.Connect(ctx); err != nil { 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. // todo: ID and CreatedAt should get retrieved from a stored kopia config.
r := Repository{ r := Repository{
Version: "v1", Version: "v1",
Account: acct, Account: acct,
Storage: storage, 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 { if r.dataLayer == nil {
return nil return nil
} }