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