1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
12
"github.com/juju/testing"
13
gc "gopkg.in/check.v1"
14
"gopkg.in/juju/blobstore.v2"
17
var _ = gc.Suite(&gridfsSuite{})
19
type gridfsSuite struct {
20
testing.IsolationSuite
22
stor blobstore.ResourceStorage
25
func (s *gridfsSuite) SetUpSuite(c *gc.C) {
26
s.IsolationSuite.SetUpSuite(c)
27
s.MgoSuite.SetUpSuite(c)
30
func (s *gridfsSuite) TearDownSuite(c *gc.C) {
31
s.MgoSuite.TearDownSuite(c)
32
s.IsolationSuite.TearDownSuite(c)
35
func (s *gridfsSuite) SetUpTest(c *gc.C) {
36
s.IsolationSuite.SetUpTest(c)
37
s.MgoSuite.SetUpTest(c)
38
s.stor = blobstore.NewGridFS("juju", "test", s.Session)
41
func (s *gridfsSuite) TearDownTest(c *gc.C) {
42
s.MgoSuite.TearDownTest(c)
43
s.IsolationSuite.TearDownTest(c)
46
func assertPut(c *gc.C, stor blobstore.ResourceStorage, path, data string) {
47
r := strings.NewReader(data)
48
checksum, err := stor.Put(path, r, int64(len(data)))
49
c.Assert(err, gc.IsNil)
51
_, err = md5Hash.Write([]byte(data))
52
c.Assert(err, gc.IsNil)
53
c.Assert(checksum, gc.Equals, hex.EncodeToString(md5Hash.Sum(nil)))
54
assertGet(c, stor, path, data)
57
func (s *gridfsSuite) TestPut(c *gc.C) {
58
assertPut(c, s.stor, "/path/to/file", "hello world")
61
func (s *gridfsSuite) TestPutSameFileOverwrites(c *gc.C) {
62
assertPut(c, s.stor, "/path/to/file", "hello world")
63
assertPut(c, s.stor, "/path/to/file", "hello again")
66
func assertGet(c *gc.C, stor blobstore.ResourceStorage, path, expected string) {
67
r, err := stor.Get(path)
68
c.Assert(err, gc.IsNil)
70
data, err := ioutil.ReadAll(r)
71
c.Assert(err, gc.IsNil)
72
c.Assert(data, gc.DeepEquals, []byte(expected))
75
func (s *gridfsSuite) TestGetNonExistent(c *gc.C) {
76
_, err := s.stor.Get("missing")
77
c.Assert(err, gc.ErrorMatches, `failed to open GridFS file "missing": not found`)
80
func (s *gridfsSuite) TestGet(c *gc.C) {
82
r := strings.NewReader(data)
83
_, err := s.stor.Put("/path/to/file", r, int64(len(data)))
84
c.Assert(err, gc.IsNil)
85
assertGet(c, s.stor, "/path/to/file", data)
88
func (s *gridfsSuite) TestRemove(c *gc.C) {
89
path := "/path/to/file"
90
assertPut(c, s.stor, path, "hello world")
91
err := s.stor.Remove(path)
92
c.Assert(err, gc.IsNil)
93
_, err = s.stor.Get(path)
94
c.Assert(err, gc.ErrorMatches, `failed to open GridFS file "/path/to/file": not found`)
97
func (s *gridfsSuite) TestRemoveNonExistent(c *gc.C) {
98
err := s.stor.Remove("/path/to/file")
99
c.Assert(err, gc.IsNil)
102
func (s *gridfsSuite) TestNamespaceSeparation(c *gc.C) {
103
anotherStor := blobstore.NewGridFS("juju", "another", s.Session)
104
path := "/path/to/file"
105
assertPut(c, anotherStor, path, "hello world")
106
_, err := s.stor.Get(path)
107
c.Assert(err, gc.ErrorMatches, `failed to open GridFS file "/path/to/file": not found`)
110
func (s *gridfsSuite) TestNamespaceSeparationRemove(c *gc.C) {
111
anotherStor := blobstore.NewGridFS("juju", "another", s.Session)
112
path := "/path/to/file"
113
assertPut(c, s.stor, path, "hello world")
114
assertPut(c, anotherStor, path, "hello again")
115
err := s.stor.Remove(path)
116
c.Assert(err, gc.IsNil)
117
assertGet(c, anotherStor, "/path/to/file", "hello again")