Skip to content

Commit

Permalink
Add mock client for openstak
Browse files Browse the repository at this point in the history
Signed-off-by: Swapnil Mhamane <[email protected]>
  • Loading branch information
Swapnil Mhamane committed Nov 30, 2018
1 parent 3cbb0ba commit ee5326e
Show file tree
Hide file tree
Showing 875 changed files with 114,813 additions and 41,139 deletions.
412 changes: 323 additions & 89 deletions Gopkg.lock

Large diffs are not rendered by default.

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

[[constraint]]
name = "github.com/gophercloud/gophercloud"
revision = "c7ca48da8eafab13e1835090a1147e64cfc10174"
revision = "c7ca48da8eafab13e1835090a1147e64cfc10174"

[[override]]
name = "gopkg.in/fsnotify.v1"
source = "https://github.com/fsnotify/fsnotify.git"
5 changes: 3 additions & 2 deletions pkg/etcdutil/defrag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ var _ = Describe("Defrag", func() {
tlsConfig = NewTLSConfig("", "", "", true, true, endpoints)
Context("Defragmentation", func() {
BeforeEach(func() {
now := time.Now().Unix()
client, err := GetTLSClientForEtcd(tlsConfig)
defer client.Close()
Expect(err).ShouldNot(HaveOccurred())
for index := 0; index <= 1000; index++ {
ctx, cancel := context.WithTimeout(context.TODO(), etcdConnectionTimeout)
client.Put(ctx, fmt.Sprintf("%s%d", keyPrefix, index), valuePrefix)
client.Put(ctx, fmt.Sprintf("%s%d%d", keyPrefix, now, index), valuePrefix)
cancel()
}
for index := 0; index <= 500; index++ {
ctx, cancel := context.WithTimeout(context.TODO(), etcdConnectionTimeout)
client.Delete(ctx, fmt.Sprintf("%s%d", keyPrefix, index))
client.Delete(ctx, fmt.Sprintf("%s%d%d", keyPrefix, now, index))
cancel()
}
})
Expand Down
12 changes: 8 additions & 4 deletions pkg/snapstore/s3_snapstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"sort"
"strings"
"sync"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -32,9 +33,10 @@ import (
// Define a mock struct to be used in your unit tests of myFunc.
type mockS3Client struct {
s3iface.S3API
objects map[string]*[]byte
prefix string
multiPartUploads map[string]*[][]byte
objects map[string]*[]byte
prefix string
multiPartUploads map[string]*[][]byte
multiPartUploadsMutex sync.Mutex
}

// GetObject returns the object from map for mock test
Expand Down Expand Up @@ -79,10 +81,12 @@ func (m *mockS3Client) UploadPartWithContext(ctx aws.Context, in *s3.UploadPartI
return nil, fmt.Errorf("part number should be positive integer")
}
if *in.PartNumber > int64(len(*m.multiPartUploads[*in.UploadId])) {
m.multiPartUploadsMutex.Lock()
t := make([][]byte, *in.PartNumber)
copy(t, *m.multiPartUploads[*in.UploadId])
delete(m.multiPartUploads, *in.UploadId)
m.multiPartUploads[*in.UploadId] = &t
m.multiPartUploadsMutex.Unlock()
}
off, err := in.Body.Seek(0, io.SeekEnd)
if err != nil {
Expand Down Expand Up @@ -153,7 +157,7 @@ func (m *mockS3Client) ListObjects(in *s3.ListObjectsInput) (*s3.ListObjectsOutp
func (m *mockS3Client) ListObjectsPages(in *s3.ListObjectsInput, callback func(*s3.ListObjectsOutput, bool) bool) error {
var (
count int64 = 0
limit int64 = 1000
limit int64 = 1 // aws default is 1000.
lastPage bool = false
keys []string
out = &s3.ListObjectsOutput{
Expand Down
21 changes: 20 additions & 1 deletion pkg/snapstore/snapstore_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,32 @@
package snapstore_test

import (
"testing"

"github.com/gardener/etcd-backup-restore/pkg/snapstore"
th "github.com/gophercloud/gophercloud/testhelper"
fake "github.com/gophercloud/gophercloud/testhelper/client"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

"testing"
var (
testObj *testing.T
swiftStore snapstore.SnapStore
)

func TestSnapstore(t *testing.T) {
RegisterFailHandler(Fail)
testObj = t
RunSpecs(t, "Snapstore Suite")
}

var _ = BeforeSuite(func() {
th.SetupHTTP()
initializeMockSwiftServer(testObj)
swiftStore = snapstore.NewSwiftSnapstoreFromClient(bucket, prefix, "/tmp", 5, fake.ServiceClient())
})

var _ = AfterSuite(func() {
th.TeardownHTTP()
})
92 changes: 60 additions & 32 deletions pkg/snapstore/snapstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,37 @@
package snapstore_test

import (
"bytes"
"fmt"
"io"
"path"
"strings"
"time"

"github.com/sirupsen/logrus"

. "github.com/gardener/etcd-backup-restore/pkg/snapstore"
fake "github.com/gophercloud/gophercloud/testhelper/client"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var (
bucket string = "mock-bucket"
objectMap = map[string]*[]byte{}
prefix string = "v1"
)

var _ = Describe("Snapstore", func() {
var (
bucket string
snap1 Snapshot
snap2 Snapshot
snap3 Snapshot
expectedVal1 string
expectedVal2 string
m mockS3Client
expectedVal1 []byte
expectedVal2 []byte
snapstores map[string]SnapStore
)
// S3SnapStore is snapstore with local disk as backend

BeforeEach(func() {
bucket = "mock-bucket"
prefix := "v1"
now := time.Now().Unix()
snap1 = Snapshot{
CreatedOn: time.Unix(now, 0).UTC(),
Expand All @@ -64,63 +71,84 @@ var _ = Describe("Snapstore", func() {
snap2.GenerateSnapshotDirectory()
snap3.GenerateSnapshotName()
snap3.GenerateSnapshotDirectory()
expectedVal1 = "value1"
expectedVal2 = "value2"
expectedVal1Bytes := []byte(expectedVal1)
expectedVal2Bytes := []byte(expectedVal2)
m = mockS3Client{
objects: map[string]*[]byte{
path.Join(prefix, snap1.SnapDir, snap1.SnapName): &expectedVal1Bytes,
path.Join(prefix, snap2.SnapDir, snap2.SnapName): &expectedVal2Bytes,
},
prefix: prefix,
multiPartUploads: map[string]*[][]byte{},
}
expectedVal1 = []byte("value1")
expectedVal2 = []byte("value2")

snapstores = map[string]SnapStore{
"s3": NewS3FromClient(bucket, prefix, "/tmp", 5, &m),
"s3": NewS3FromClient(bucket, prefix, "/tmp", 5, &mockS3Client{
objects: objectMap,
prefix: prefix,
multiPartUploads: map[string]*[][]byte{},
}),
"swift": NewSwiftSnapstoreFromClient(bucket, prefix, "/tmp", 2, fake.ServiceClient()),
}
})

Describe("Fetch operation", func() {
It("fetches snapshot", func() {
for _, snapStore := range snapstores {
for key, snapStore := range snapstores {
logrus.Infof("Running tests for %s", key)
resetObjectMap()
objectMap[path.Join(prefix, snap1.SnapDir, snap1.SnapName)] = &expectedVal1
objectMap[path.Join(prefix, snap2.SnapDir, snap2.SnapName)] = &expectedVal2
rc, err := snapStore.Fetch(snap1)
Expect(err).ShouldNot(HaveOccurred())
temp := make([]byte, len(expectedVal1))
n, err := rc.Read(temp)
Expect(err).ShouldNot(HaveOccurred())
Expect(string(temp[:n])).To(Equal(expectedVal1))
if err != nil && err != io.EOF {
Fail(fmt.Sprintf("error should not have occurred: %v", err))
}
Expect(temp[:n]).To(Equal(expectedVal1))
}
})
})

Describe("Save snapshot", func() {
It("saves snapshot", func() {
for _, snapStore := range snapstores {
prevLen := len(m.objects)
err := snapStore.Save(snap3, strings.NewReader("value3"))
for key, snapStore := range snapstores {
logrus.Infof("Running tests for %s", key)
resetObjectMap()
dummyData := make([]byte, 6*1024*1024)
err := snapStore.Save(snap3, bytes.NewReader(dummyData))
Expect(err).ShouldNot(HaveOccurred())
Expect(len(m.objects)).To(Equal(prevLen + 1))
Expect(len(objectMap)).Should(BeNumerically(">", 0))
}
})
})

Describe("List snapshot", func() {
It("gives sorted list of snapshot", func() {
for _, snapStore := range snapstores {
for key, snapStore := range snapstores {
logrus.Infof("Running tests for %s", key)
resetObjectMap()
objectMap[path.Join(prefix, snap1.SnapDir, snap1.SnapName)] = &expectedVal1
objectMap[path.Join(prefix, snap2.SnapDir, snap2.SnapName)] = &expectedVal2
snapList, err := snapStore.List()
Expect(err).ShouldNot(HaveOccurred())
Expect(snapList.Len()).To(Equal(2))
Expect(snapList[0].SnapName).To(Equal(snap1.SnapName))
}
})
})

Describe("Delete snapshot", func() {
It("deletes snapshot", func() {
for _, snapStore := range snapstores {
prevLen := len(m.objects)
for key, snapStore := range snapstores {
logrus.Infof("Running tests for %s", key)
resetObjectMap()
objectMap[path.Join(prefix, snap1.SnapDir, snap1.SnapName)] = &expectedVal1
objectMap[path.Join(prefix, snap2.SnapDir, snap2.SnapName)] = &expectedVal2
prevLen := len(objectMap)
err := snapStore.Delete(snap2)
Expect(err).ShouldNot(HaveOccurred())
Expect(len(m.objects)).To(Equal(prevLen - 1))
Expect(len(objectMap)).To(Equal(prevLen - 1))
}
})
})
})

func resetObjectMap() {
for k := range objectMap {
delete(objectMap, k)
}
}
Loading

0 comments on commit ee5326e

Please sign in to comment.