Skip to content

Commit

Permalink
Merge pull request #82 from swapnilgm/gcs-mock-test
Browse files Browse the repository at this point in the history
Add mock test for GCS
  • Loading branch information
swapnilgm authored Jan 4, 2019
2 parents 7581cfc + 932c106 commit 26f7194
Show file tree
Hide file tree
Showing 137 changed files with 6,966 additions and 1,784 deletions.
64 changes: 40 additions & 24 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ required = ["github.com/coreos/bbolt"]

[[constraint]]
name = "cloud.google.com/go"
version = "0.19.0"
version = "0.33.1"

[[constraint]]
name = "github.com/googleapis/google-cloud-go-testing"
revision = "f773598aef764db07257d386141bed45e60dc228"

[[constraint]]
name = "github.com/gophercloud/gophercloud"
Expand Down
15 changes: 10 additions & 5 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,26 @@ Copyright (c) 2013-2014 Onsi Fakhouri.
MIT License (https://github.com/onsi/gomega/blob/master/LICENSE)

Microsoft Azure SDK for Go.
https://github.com/Azure/azure-storage-blob-go
Copyright 2017 Microsoft
https://github.com/Azure/azure-storage-blob-go.
Copyright 2017 Microsoft.
MIT License (https://github.com/Azure/azure-storage-blob-go/blob/master/LICENSE)

Microsoft Azure SDK for Go.
https://github.com/Azure/azure-pipeline-go
Copyright 2017 Microsoft
Copyright 2017 Microsoft.
MIT License (https://github.com/Azure/azure-pipeline-go/blob/master/LICENSE)

Google Cloud Client Libraries for Go
Google Cloud Client Libraries for Go.
https://github.com/GoogleCloudPlatform/google-cloud-go
Copyright 2017 Google Inc. All Rights Reserved.
Apache 2 license (https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/LICENSE)

Gophercloud: an OpenStack SDK for Go
Testing Support for the Google Cloud Client Libraries for Go.
https://github.com/googleapis/google-cloud-go-testing
Copyright 2018 Google Inc. All Rights Reserved.
Apache 2 license (https://github.com/googleapis/google-cloud-go-testing/blob/master/LICENSE)

Gophercloud: an OpenStack SDK for Go.
https://github.com/gophercloud/gophercloud
Copyright 2012-2013 Rackspace, Inc.
Apache 2 license (https://github.com/gophercloud/gophercloud/blob/master/LICENSE)
Expand Down
44 changes: 23 additions & 21 deletions pkg/snapstore/gcs_snapstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,57 @@ import (
"sync"

"cloud.google.com/go/storage"
"github.com/googleapis/google-cloud-go-testing/storage/stiface"
"github.com/sirupsen/logrus"
"google.golang.org/api/iterator"
)

// GCSSnapStore is snapstore with local disk as backend
// GCSSnapStore is snapstore with GCS object store as backend.
type GCSSnapStore struct {
client stiface.Client
prefix string
client *storage.Client
bucket string
ctx context.Context
// maxParallelChunkUploads hold the maximum number of parallel chunk uploads allowed.
maxParallelChunkUploads int
tempDir string
}

const (
gcsNoOfChunk int64 = 32 //Default configuration in swift installation
gcsNoOfChunk int64 = 32
)

// NewGCSSnapStore create new S3SnapStore from shared configuration with specified bucket
// NewGCSSnapStore create new GCSSnapStore from shared configuration with specified bucket.
func NewGCSSnapStore(bucket, prefix, tempDir string, maxParallelChunkUploads int) (*GCSSnapStore, error) {
ctx := context.TODO()
gcsClient, err := storage.NewClient(ctx)
cli, err := storage.NewClient(ctx)
if err != nil {
return nil, err
}
gcsClient := stiface.AdaptClient(cli)

return NewGCSSnapStoreFromClient(bucket, prefix, tempDir, maxParallelChunkUploads, gcsClient), nil
}

// NewGCSSnapStoreFromClient create new GCSSnapStore from shared configuration with specified bucket.
func NewGCSSnapStoreFromClient(bucket, prefix, tempDir string, maxParallelChunkUploads int, cli stiface.Client) *GCSSnapStore {
return &GCSSnapStore{
prefix: prefix,
client: gcsClient,
client: cli,
bucket: bucket,
ctx: ctx,
maxParallelChunkUploads: maxParallelChunkUploads,
tempDir: tempDir,
}, nil

}
}

// Fetch should open reader for the snapshot file from store
// Fetch should open reader for the snapshot file from store.
func (s *GCSSnapStore) Fetch(snap Snapshot) (io.ReadCloser, error) {
objectName := path.Join(s.prefix, snap.SnapDir, snap.SnapName)
return s.client.Bucket(s.bucket).Object(objectName).NewReader(s.ctx)
ctx := context.TODO()
return s.client.Bucket(s.bucket).Object(objectName).NewReader(ctx)
}

// Save will write the snapshot to store
// Save will write the snapshot to store.
func (s *GCSSnapStore) Save(snap Snapshot, r io.Reader) error {
// Save it locally
tmpfile, err := ioutil.TempFile(s.tempDir, tmpBackupFilePrefix)
if err != nil {
return fmt.Errorf("failed to create snapshot tempfile: %v", err)
Expand Down Expand Up @@ -127,7 +131,7 @@ func (s *GCSSnapStore) Save(snap Snapshot, r io.Reader) error {
}
logrus.Info("All chunk uploaded successfully. Uploading composite object.")
bh := s.client.Bucket(s.bucket)
var subObjects []*storage.ObjectHandle
var subObjects []stiface.ObjectHandle
for partNumber := int64(1); partNumber <= noOfChunks; partNumber++ {
name := path.Join(s.prefix, snap.SnapDir, snap.SnapName, fmt.Sprintf("%010d", partNumber))
obj := bh.Object(name)
Expand Down Expand Up @@ -190,11 +194,9 @@ func (s *GCSSnapStore) componentUploader(wg *sync.WaitGroup, stopCh <-chan struc
}
}

// List will list the snapshots from store
// List will list the snapshots from store.
func (s *GCSSnapStore) List() (SnapList, error) {
// recursively list all "files", not directory

it := s.client.Bucket(s.bucket).Objects(s.ctx, &storage.Query{Prefix: s.prefix})
it := s.client.Bucket(s.bucket).Objects(context.TODO(), &storage.Query{Prefix: s.prefix})

var attrs []*storage.ObjectAttrs
for {
Expand Down Expand Up @@ -225,8 +227,8 @@ func (s *GCSSnapStore) List() (SnapList, error) {
return snapList, nil
}

// Delete should delete the snapshot file from store
// Delete should delete the snapshot file from store.
func (s *GCSSnapStore) Delete(snap Snapshot) error {
objectName := path.Join(s.prefix, snap.SnapDir, snap.SnapName)
return s.client.Bucket(s.bucket).Object(objectName).Delete(s.ctx)
return s.client.Bucket(s.bucket).Object(objectName).Delete(context.TODO())
}
Loading

0 comments on commit 26f7194

Please sign in to comment.