github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/environs/storage/storage_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package storage_test 5 6 import ( 7 "bytes" 8 "fmt" 9 "io" 10 "io/ioutil" 11 stdtesting "testing" 12 13 jc "github.com/juju/testing/checkers" 14 "github.com/juju/utils" 15 gc "gopkg.in/check.v1" 16 17 "github.com/juju/juju/environs/filestorage" 18 "github.com/juju/juju/environs/storage" 19 "github.com/juju/juju/testing" 20 ) 21 22 func TestPackage(t *stdtesting.T) { 23 gc.TestingT(t) 24 } 25 26 var _ = gc.Suite(&datasourceSuite{}) 27 28 type datasourceSuite struct { 29 testing.FakeJujuHomeSuite 30 stor storage.Storage 31 baseURL string 32 } 33 34 func (s *datasourceSuite) SetUpTest(c *gc.C) { 35 s.FakeJujuHomeSuite.SetUpTest(c) 36 37 storageDir := c.MkDir() 38 stor, err := filestorage.NewFileStorageWriter(storageDir) 39 c.Assert(err, jc.ErrorIsNil) 40 s.stor = stor 41 s.baseURL, err = s.stor.URL("") 42 c.Assert(err, jc.ErrorIsNil) 43 } 44 45 func (s *datasourceSuite) TestFetch(c *gc.C) { 46 sampleData := "hello world" 47 s.stor.Put("foo/bar/data.txt", bytes.NewReader([]byte(sampleData)), int64(len(sampleData))) 48 ds := storage.NewStorageSimpleStreamsDataSource("test datasource", s.stor, "") 49 rc, url, err := ds.Fetch("foo/bar/data.txt") 50 c.Assert(err, jc.ErrorIsNil) 51 defer rc.Close() 52 c.Assert(url, gc.Equals, s.baseURL+"/foo/bar/data.txt") 53 data, err := ioutil.ReadAll(rc) 54 c.Assert(data, gc.DeepEquals, []byte(sampleData)) 55 } 56 57 func (s *datasourceSuite) TestFetchWithBasePath(c *gc.C) { 58 sampleData := "hello world" 59 s.stor.Put("base/foo/bar/data.txt", bytes.NewReader([]byte(sampleData)), int64(len(sampleData))) 60 ds := storage.NewStorageSimpleStreamsDataSource("test datasource", s.stor, "base") 61 rc, url, err := ds.Fetch("foo/bar/data.txt") 62 c.Assert(err, jc.ErrorIsNil) 63 defer rc.Close() 64 c.Assert(url, gc.Equals, s.baseURL+"/base/foo/bar/data.txt") 65 data, err := ioutil.ReadAll(rc) 66 c.Assert(data, gc.DeepEquals, []byte(sampleData)) 67 } 68 69 func (s *datasourceSuite) TestFetchWithRetry(c *gc.C) { 70 stor := &fakeStorage{shouldRetry: true} 71 ds := storage.NewStorageSimpleStreamsDataSource("test datasource", stor, "base") 72 ds.SetAllowRetry(true) 73 _, _, err := ds.Fetch("foo/bar/data.txt") 74 c.Assert(err, gc.ErrorMatches, "an error") 75 c.Assert(stor.getName, gc.Equals, "base/foo/bar/data.txt") 76 c.Assert(stor.invokeCount, gc.Equals, 10) 77 } 78 79 func (s *datasourceSuite) TestFetchWithNoRetry(c *gc.C) { 80 // NB shouldRetry below is true indicating the fake storage is capable of 81 // retrying, not that it will retry. 82 stor := &fakeStorage{shouldRetry: true} 83 ds := storage.NewStorageSimpleStreamsDataSource("test datasource", stor, "base") 84 _, _, err := ds.Fetch("foo/bar/data.txt") 85 c.Assert(err, gc.ErrorMatches, "an error") 86 c.Assert(stor.getName, gc.Equals, "base/foo/bar/data.txt") 87 c.Assert(stor.invokeCount, gc.Equals, 1) 88 } 89 90 func (s *datasourceSuite) TestURL(c *gc.C) { 91 sampleData := "hello world" 92 s.stor.Put("bar/data.txt", bytes.NewReader([]byte(sampleData)), int64(len(sampleData))) 93 ds := storage.NewStorageSimpleStreamsDataSource("test datasource", s.stor, "") 94 url, err := ds.URL("bar") 95 c.Assert(err, jc.ErrorIsNil) 96 expectedURL, _ := s.stor.URL("bar") 97 c.Assert(url, gc.Equals, expectedURL) 98 } 99 100 func (s *datasourceSuite) TestURLWithBasePath(c *gc.C) { 101 sampleData := "hello world" 102 s.stor.Put("base/bar/data.txt", bytes.NewReader([]byte(sampleData)), int64(len(sampleData))) 103 ds := storage.NewStorageSimpleStreamsDataSource("test datasource", s.stor, "base") 104 url, err := ds.URL("bar") 105 c.Assert(err, jc.ErrorIsNil) 106 expectedURL, _ := s.stor.URL("base/bar") 107 c.Assert(url, gc.Equals, expectedURL) 108 } 109 110 var _ = gc.Suite(&storageSuite{}) 111 112 type storageSuite struct{} 113 114 type fakeStorage struct { 115 getName string 116 listPrefix string 117 invokeCount int 118 shouldRetry bool 119 } 120 121 func (s *fakeStorage) Get(name string) (io.ReadCloser, error) { 122 s.getName = name 123 s.invokeCount++ 124 return nil, fmt.Errorf("an error") 125 } 126 127 func (s *fakeStorage) List(prefix string) ([]string, error) { 128 s.listPrefix = prefix 129 s.invokeCount++ 130 return nil, fmt.Errorf("an error") 131 } 132 133 func (s *fakeStorage) URL(name string) (string, error) { 134 return "", nil 135 } 136 137 func (s *fakeStorage) DefaultConsistencyStrategy() utils.AttemptStrategy { 138 return utils.AttemptStrategy{Min: 10} 139 } 140 141 func (s *fakeStorage) ShouldRetry(error) bool { 142 return s.shouldRetry 143 } 144 145 func (s *storageSuite) TestGetWithRetry(c *gc.C) { 146 stor := &fakeStorage{shouldRetry: true} 147 attempt := utils.AttemptStrategy{Min: 5} 148 storage.GetWithRetry(stor, "foo", attempt) 149 c.Assert(stor.getName, gc.Equals, "foo") 150 c.Assert(stor.invokeCount, gc.Equals, 5) 151 } 152 153 func (s *storageSuite) TestGet(c *gc.C) { 154 stor := &fakeStorage{shouldRetry: true} 155 storage.Get(stor, "foo") 156 c.Assert(stor.getName, gc.Equals, "foo") 157 c.Assert(stor.invokeCount, gc.Equals, 10) 158 } 159 160 func (s *storageSuite) TestGetNoRetryAllowed(c *gc.C) { 161 stor := &fakeStorage{} 162 storage.Get(stor, "foo") 163 c.Assert(stor.getName, gc.Equals, "foo") 164 c.Assert(stor.invokeCount, gc.Equals, 1) 165 } 166 167 func (s *storageSuite) TestListWithRetry(c *gc.C) { 168 stor := &fakeStorage{shouldRetry: true} 169 attempt := utils.AttemptStrategy{Min: 5} 170 storage.ListWithRetry(stor, "foo", attempt) 171 c.Assert(stor.listPrefix, gc.Equals, "foo") 172 c.Assert(stor.invokeCount, gc.Equals, 5) 173 } 174 175 func (s *storageSuite) TestList(c *gc.C) { 176 stor := &fakeStorage{shouldRetry: true} 177 storage.List(stor, "foo") 178 c.Assert(stor.listPrefix, gc.Equals, "foo") 179 c.Assert(stor.invokeCount, gc.Equals, 10) 180 } 181 182 func (s *storageSuite) TestListNoRetryAllowed(c *gc.C) { 183 stor := &fakeStorage{} 184 storage.List(stor, "foo") 185 c.Assert(stor.listPrefix, gc.Equals, "foo") 186 c.Assert(stor.invokeCount, gc.Equals, 1) 187 }