github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/state/storage/gridfs_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package storage_test 5 6 import ( 7 "crypto/md5" 8 "encoding/hex" 9 "io/ioutil" 10 "strings" 11 12 gitjujutesting "github.com/juju/testing" 13 gc "launchpad.net/gocheck" 14 15 "github.com/juju/juju/state/storage" 16 "github.com/juju/juju/testing" 17 ) 18 19 var _ = gc.Suite(&gridfsSuite{}) 20 21 type gridfsSuite struct { 22 testing.BaseSuite 23 gitjujutesting.MgoSuite 24 stor storage.ResourceStorage 25 } 26 27 func (s *gridfsSuite) SetUpSuite(c *gc.C) { 28 s.BaseSuite.SetUpSuite(c) 29 s.MgoSuite.SetUpSuite(c) 30 } 31 32 func (s *gridfsSuite) TearDownSuite(c *gc.C) { 33 s.MgoSuite.TearDownSuite(c) 34 s.BaseSuite.TearDownSuite(c) 35 } 36 37 func (s *gridfsSuite) SetUpTest(c *gc.C) { 38 s.BaseSuite.SetUpTest(c) 39 s.MgoSuite.SetUpTest(c) 40 s.stor = storage.NewGridFS("test", s.Session) 41 } 42 43 func (s *gridfsSuite) TearDownTest(c *gc.C) { 44 s.MgoSuite.TearDownTest(c) 45 s.BaseSuite.TearDownTest(c) 46 } 47 48 func assertPut(c *gc.C, stor storage.ResourceStorage, path, data string) { 49 r := strings.NewReader(data) 50 checksum, err := stor.Put(path, r, int64(len(data))) 51 c.Assert(err, gc.IsNil) 52 md5Hash := md5.New() 53 _, err = md5Hash.Write([]byte(data)) 54 c.Assert(err, gc.IsNil) 55 c.Assert(checksum, gc.Equals, hex.EncodeToString(md5Hash.Sum(nil))) 56 assertGet(c, stor, path, data) 57 } 58 59 func (s *gridfsSuite) TestPut(c *gc.C) { 60 assertPut(c, s.stor, "/path/to/file", "hello world") 61 } 62 63 func (s *gridfsSuite) TestPutSameFileOverwrites(c *gc.C) { 64 assertPut(c, s.stor, "/path/to/file", "hello world") 65 assertPut(c, s.stor, "/path/to/file", "hello again") 66 } 67 68 func assertGet(c *gc.C, stor storage.ResourceStorage, path, expected string) { 69 r, err := stor.Get(path) 70 c.Assert(err, gc.IsNil) 71 defer r.Close() 72 data, err := ioutil.ReadAll(r) 73 c.Assert(err, gc.IsNil) 74 c.Assert(data, gc.DeepEquals, []byte(expected)) 75 } 76 77 func (s *gridfsSuite) TestGetNonExistent(c *gc.C) { 78 _, err := s.stor.Get("missing") 79 c.Assert(err, gc.ErrorMatches, `failed to open GridFS file "missing": not found`) 80 } 81 82 func (s *gridfsSuite) TestGet(c *gc.C) { 83 data := "hello world" 84 r := strings.NewReader(data) 85 _, err := s.stor.Put("/path/to/file", r, int64(len(data))) 86 c.Assert(err, gc.IsNil) 87 assertGet(c, s.stor, "/path/to/file", data) 88 } 89 90 func (s *gridfsSuite) TestRemove(c *gc.C) { 91 path := "/path/to/file" 92 assertPut(c, s.stor, path, "hello world") 93 err := s.stor.Remove(path) 94 c.Assert(err, gc.IsNil) 95 _, err = s.stor.Get(path) 96 c.Assert(err, gc.ErrorMatches, `failed to open GridFS file "/path/to/file": not found`) 97 } 98 99 func (s *gridfsSuite) TestRemoveNonExistent(c *gc.C) { 100 err := s.stor.Remove("/path/to/file") 101 c.Assert(err, gc.IsNil) 102 } 103 104 func (s *gridfsSuite) TestNamespaceSeparation(c *gc.C) { 105 anotherStor := storage.NewGridFS("another", s.Session) 106 path := "/path/to/file" 107 assertPut(c, anotherStor, path, "hello world") 108 _, err := s.stor.Get(path) 109 c.Assert(err, gc.ErrorMatches, `failed to open GridFS file "/path/to/file": not found`) 110 } 111 112 func (s *gridfsSuite) TestNamespaceSeparationRemove(c *gc.C) { 113 anotherStor := storage.NewGridFS("another", s.Session) 114 path := "/path/to/file" 115 assertPut(c, s.stor, path, "hello world") 116 assertPut(c, anotherStor, path, "hello again") 117 err := s.stor.Remove(path) 118 c.Assert(err, gc.IsNil) 119 assertGet(c, anotherStor, "/path/to/file", "hello again") 120 }