github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/resource/state/stub_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state 5 6 import ( 7 "io" 8 "io/ioutil" 9 "time" 10 11 "github.com/juju/errors" 12 "github.com/juju/names" 13 "github.com/juju/testing" 14 charmresource "gopkg.in/juju/charm.v6-unstable/resource" 15 "gopkg.in/mgo.v2/txn" 16 17 "github.com/juju/juju/resource" 18 ) 19 20 type stubRawState struct { 21 stub *testing.Stub 22 23 ReturnPersistence Persistence 24 ReturnStorage Storage 25 ReturnUnits []names.UnitTag 26 } 27 28 func (s *stubRawState) Persistence() Persistence { 29 s.stub.AddCall("Persistence") 30 s.stub.NextErr() 31 32 return s.ReturnPersistence 33 } 34 35 func (s *stubRawState) Storage() Storage { 36 s.stub.AddCall("Storage") 37 s.stub.NextErr() 38 39 return s.ReturnStorage 40 } 41 42 func (s *stubRawState) VerifyService(serviceID string) error { 43 s.stub.AddCall("VerifyService", serviceID) 44 if err := s.stub.NextErr(); err != nil { 45 return errors.Trace(err) 46 } 47 48 return nil 49 } 50 51 func (s *stubRawState) Units(serviceID string) ([]names.UnitTag, error) { 52 s.stub.AddCall("Units", serviceID) 53 if err := s.stub.NextErr(); err != nil { 54 return nil, errors.Trace(err) 55 } 56 57 return s.ReturnUnits, nil 58 } 59 60 type stubPersistence struct { 61 stub *testing.Stub 62 63 ReturnListResources resource.ServiceResources 64 ReturnListPendingResources []resource.Resource 65 ReturnGetResource resource.Resource 66 ReturnGetResourcePath string 67 ReturnStageResource *stubStagedResource 68 ReturnNewResolvePendingResourceOps [][]txn.Op 69 70 CallsForNewResolvePendingResourceOps map[string]string 71 } 72 73 func (s *stubPersistence) ListResources(serviceID string) (resource.ServiceResources, error) { 74 s.stub.AddCall("ListResources", serviceID) 75 if err := s.stub.NextErr(); err != nil { 76 return resource.ServiceResources{}, errors.Trace(err) 77 } 78 79 return s.ReturnListResources, nil 80 } 81 82 func (s *stubPersistence) ListPendingResources(serviceID string) ([]resource.Resource, error) { 83 s.stub.AddCall("ListPendingResources", serviceID) 84 if err := s.stub.NextErr(); err != nil { 85 return nil, errors.Trace(err) 86 } 87 88 return s.ReturnListPendingResources, nil 89 } 90 91 func (s *stubPersistence) GetResource(serviceID string) (resource.Resource, string, error) { 92 s.stub.AddCall("GetResource", serviceID) 93 if err := s.stub.NextErr(); err != nil { 94 return resource.Resource{}, "", errors.Trace(err) 95 } 96 97 return s.ReturnGetResource, s.ReturnGetResourcePath, nil 98 } 99 100 func (s *stubPersistence) StageResource(res resource.Resource, storagePath string) (StagedResource, error) { 101 s.stub.AddCall("StageResource", res, storagePath) 102 if err := s.stub.NextErr(); err != nil { 103 return nil, errors.Trace(err) 104 } 105 106 return s.ReturnStageResource, nil 107 } 108 109 func (s *stubPersistence) SetResource(res resource.Resource) error { 110 s.stub.AddCall("SetResource", res) 111 if err := s.stub.NextErr(); err != nil { 112 return errors.Trace(err) 113 } 114 115 return nil 116 } 117 118 func (s *stubPersistence) SetCharmStoreResource(id, serviceID string, chRes charmresource.Resource, lastPolled time.Time) error { 119 s.stub.AddCall("SetCharmStoreResource", id, serviceID, chRes, lastPolled) 120 if err := s.stub.NextErr(); err != nil { 121 return errors.Trace(err) 122 } 123 124 return nil 125 } 126 127 func (s *stubPersistence) SetUnitResource(unitID string, res resource.Resource) error { 128 s.stub.AddCall("SetUnitResource", unitID, res) 129 if err := s.stub.NextErr(); err != nil { 130 return errors.Trace(err) 131 } 132 133 return nil 134 } 135 136 func (s *stubPersistence) SetUnitResourceProgress(unitID string, res resource.Resource, progress int64) error { 137 s.stub.AddCall("SetUnitResourceProgress", unitID, res, progress) 138 if err := s.stub.NextErr(); err != nil { 139 return errors.Trace(err) 140 } 141 142 return nil 143 } 144 145 func (s *stubPersistence) NewResolvePendingResourceOps(resID, pendingID string) ([]txn.Op, error) { 146 s.stub.AddCall("NewResolvePendingResourceOps", resID, pendingID) 147 if err := s.stub.NextErr(); err != nil { 148 return nil, errors.Trace(err) 149 } 150 151 if s.CallsForNewResolvePendingResourceOps == nil { 152 s.CallsForNewResolvePendingResourceOps = make(map[string]string) 153 } 154 s.CallsForNewResolvePendingResourceOps[resID] = pendingID 155 156 if len(s.ReturnNewResolvePendingResourceOps) == 0 { 157 return nil, nil 158 } 159 ops := s.ReturnNewResolvePendingResourceOps[0] 160 s.ReturnNewResolvePendingResourceOps = s.ReturnNewResolvePendingResourceOps[1:] 161 return ops, nil 162 } 163 164 type stubStagedResource struct { 165 stub *testing.Stub 166 } 167 168 func (s *stubStagedResource) Unstage() error { 169 s.stub.AddCall("Unstage") 170 if err := s.stub.NextErr(); err != nil { 171 return errors.Trace(err) 172 } 173 174 return nil 175 } 176 177 func (s *stubStagedResource) Activate() error { 178 s.stub.AddCall("Activate") 179 if err := s.stub.NextErr(); err != nil { 180 return errors.Trace(err) 181 } 182 183 return nil 184 } 185 186 type stubStorage struct { 187 stub *testing.Stub 188 189 ReturnGet resource.Content 190 } 191 192 func (s *stubStorage) PutAndCheckHash(path string, r io.Reader, length int64, hash string) error { 193 s.stub.AddCall("PutAndCheckHash", path, r, length, hash) 194 if err := s.stub.NextErr(); err != nil { 195 return errors.Trace(err) 196 } 197 198 return nil 199 } 200 201 func (s *stubStorage) Get(path string) (io.ReadCloser, int64, error) { 202 s.stub.AddCall("Get", path) 203 if err := s.stub.NextErr(); err != nil { 204 return nil, 0, errors.Trace(err) 205 } 206 207 if readCloser, ok := s.ReturnGet.Data.(io.ReadCloser); ok { 208 return readCloser, s.ReturnGet.Size, nil 209 } 210 return ioutil.NopCloser(s.ReturnGet.Data), s.ReturnGet.Size, nil 211 } 212 213 func (s *stubStorage) Remove(path string) error { 214 s.stub.AddCall("Remove", path) 215 if err := s.stub.NextErr(); err != nil { 216 return errors.Trace(err) 217 } 218 219 return nil 220 } 221 222 type stubReader struct { 223 stub *testing.Stub 224 225 ReturnRead int 226 } 227 228 func (s *stubReader) Read(buf []byte) (int, error) { 229 s.stub.AddCall("Read", buf) 230 if err := s.stub.NextErr(); err != nil { 231 return 0, err 232 } 233 234 return s.ReturnRead, nil 235 }