github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/apiserver/storage/storage_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package storage_test 5 6 import ( 7 "fmt" 8 9 "github.com/juju/errors" 10 "github.com/juju/names" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/apiserver/params" 15 "github.com/juju/juju/state" 16 ) 17 18 type storageSuite struct { 19 baseStorageSuite 20 } 21 22 var _ = gc.Suite(&storageSuite{}) 23 24 func (s *storageSuite) TestStorageListEmpty(c *gc.C) { 25 s.state.allStorageInstances = func() ([]state.StorageInstance, error) { 26 s.calls = append(s.calls, allStorageInstancesCall) 27 return []state.StorageInstance{}, nil 28 } 29 30 found, err := s.api.List() 31 c.Assert(err, jc.ErrorIsNil) 32 c.Assert(found.Results, gc.HasLen, 0) 33 s.assertCalls(c, []string{allStorageInstancesCall}) 34 } 35 36 func (s *storageSuite) TestStorageListFilesystem(c *gc.C) { 37 found, err := s.api.List() 38 c.Assert(err, jc.ErrorIsNil) 39 40 expectedCalls := []string{ 41 allStorageInstancesCall, 42 storageInstanceAttachmentsCall, 43 unitAssignedMachineCall, 44 storageInstanceCall, 45 storageInstanceFilesystemCall, 46 storageInstanceFilesystemAttachmentCall, 47 } 48 s.assertCalls(c, expectedCalls) 49 50 c.Assert(found.Results, gc.HasLen, 1) 51 wantedDetails := s.createTestStorageInfo() 52 wantedDetails.UnitTag = s.unitTag.String() 53 s.assertInstanceInfoError(c, found.Results[0], wantedDetails, "") 54 } 55 56 func (s *storageSuite) TestStorageListVolume(c *gc.C) { 57 s.storageInstance.kind = state.StorageKindBlock 58 found, err := s.api.List() 59 c.Assert(err, jc.ErrorIsNil) 60 61 expectedCalls := []string{ 62 allStorageInstancesCall, 63 storageInstanceVolumeCall, 64 storageInstanceAttachmentsCall, 65 unitAssignedMachineCall, 66 storageInstanceCall, 67 storageInstanceVolumeCall, 68 } 69 s.assertCalls(c, expectedCalls) 70 71 c.Assert(found.Results, gc.HasLen, 1) 72 wantedDetails := s.createTestStorageInfo() 73 wantedDetails.Kind = params.StorageKindBlock 74 wantedDetails.UnitTag = s.unitTag.String() 75 s.assertInstanceInfoError(c, found.Results[0], wantedDetails, "") 76 } 77 78 func (s *storageSuite) TestStorageListError(c *gc.C) { 79 msg := "list test error" 80 s.state.allStorageInstances = func() ([]state.StorageInstance, error) { 81 s.calls = append(s.calls, allStorageInstancesCall) 82 return []state.StorageInstance{}, errors.Errorf(msg) 83 } 84 85 found, err := s.api.List() 86 c.Assert(errors.Cause(err), gc.ErrorMatches, msg) 87 88 expectedCalls := []string{ 89 allStorageInstancesCall, 90 } 91 s.assertCalls(c, expectedCalls) 92 c.Assert(found.Results, gc.HasLen, 0) 93 } 94 95 func (s *storageSuite) TestStorageListInstanceError(c *gc.C) { 96 msg := "list test error" 97 s.state.storageInstance = func(sTag names.StorageTag) (state.StorageInstance, error) { 98 s.calls = append(s.calls, storageInstanceCall) 99 c.Assert(sTag, gc.DeepEquals, s.storageTag) 100 return nil, errors.Errorf(msg) 101 } 102 103 found, err := s.api.List() 104 c.Assert(err, jc.ErrorIsNil) 105 106 expectedCalls := []string{ 107 allStorageInstancesCall, 108 storageInstanceAttachmentsCall, 109 unitAssignedMachineCall, 110 storageInstanceCall, 111 } 112 s.assertCalls(c, expectedCalls) 113 c.Assert(found.Results, gc.HasLen, 1) 114 wanted := s.createTestStorageInfoWithError("", 115 fmt.Sprintf("getting storage attachment info: getting storage instance: %v", msg)) 116 s.assertInstanceInfoError(c, found.Results[0], wanted, msg) 117 } 118 119 func (s *storageSuite) TestStorageListAttachmentError(c *gc.C) { 120 s.state.storageInstanceAttachments = func(tag names.StorageTag) ([]state.StorageAttachment, error) { 121 s.calls = append(s.calls, storageInstanceAttachmentsCall) 122 c.Assert(tag, gc.DeepEquals, s.storageTag) 123 return []state.StorageAttachment{}, errors.Errorf("list test error") 124 } 125 126 found, err := s.api.List() 127 c.Assert(err, jc.ErrorIsNil) 128 129 expectedCalls := []string{ 130 allStorageInstancesCall, 131 storageInstanceAttachmentsCall, 132 } 133 s.assertCalls(c, expectedCalls) 134 c.Assert(found.Results, gc.HasLen, 1) 135 expectedErr := "list test error" 136 wanted := s.createTestStorageInfoWithError("", expectedErr) 137 s.assertInstanceInfoError(c, found.Results[0], wanted, expectedErr) 138 } 139 140 func (s *storageSuite) TestStorageListMachineError(c *gc.C) { 141 msg := "list test error" 142 s.state.unitAssignedMachine = func(u names.UnitTag) (names.MachineTag, error) { 143 s.calls = append(s.calls, unitAssignedMachineCall) 144 c.Assert(u, gc.DeepEquals, s.unitTag) 145 return names.MachineTag{}, errors.Errorf(msg) 146 } 147 148 found, err := s.api.List() 149 c.Assert(err, jc.ErrorIsNil) 150 151 expectedCalls := []string{ 152 allStorageInstancesCall, 153 storageInstanceAttachmentsCall, 154 unitAssignedMachineCall, 155 } 156 s.assertCalls(c, expectedCalls) 157 c.Assert(found.Results, gc.HasLen, 1) 158 wanted := s.createTestStorageInfoWithError("", 159 fmt.Sprintf("getting unit for storage attachment: %v", msg)) 160 s.assertInstanceInfoError(c, found.Results[0], wanted, msg) 161 } 162 163 func (s *storageSuite) TestStorageListFilesystemError(c *gc.C) { 164 msg := "list test error" 165 s.state.storageInstanceFilesystem = func(sTag names.StorageTag) (state.Filesystem, error) { 166 s.calls = append(s.calls, storageInstanceFilesystemCall) 167 c.Assert(sTag, gc.DeepEquals, s.storageTag) 168 return nil, errors.Errorf(msg) 169 } 170 171 found, err := s.api.List() 172 c.Assert(err, jc.ErrorIsNil) 173 174 expectedCalls := []string{ 175 allStorageInstancesCall, 176 storageInstanceAttachmentsCall, 177 unitAssignedMachineCall, 178 storageInstanceCall, 179 storageInstanceFilesystemCall, 180 } 181 s.assertCalls(c, expectedCalls) 182 c.Assert(found.Results, gc.HasLen, 1) 183 wanted := s.createTestStorageInfoWithError("", 184 fmt.Sprintf("getting storage attachment info: getting filesystem: %v", msg)) 185 s.assertInstanceInfoError(c, found.Results[0], wanted, msg) 186 } 187 188 func (s *storageSuite) TestStorageListFilesystemAttachmentError(c *gc.C) { 189 msg := "list test error" 190 s.state.unitAssignedMachine = func(u names.UnitTag) (names.MachineTag, error) { 191 s.calls = append(s.calls, unitAssignedMachineCall) 192 c.Assert(u, gc.DeepEquals, s.unitTag) 193 return s.machineTag, errors.Errorf(msg) 194 } 195 196 found, err := s.api.List() 197 c.Assert(err, jc.ErrorIsNil) 198 199 expectedCalls := []string{ 200 allStorageInstancesCall, 201 storageInstanceAttachmentsCall, 202 unitAssignedMachineCall, 203 } 204 s.assertCalls(c, expectedCalls) 205 c.Assert(found.Results, gc.HasLen, 1) 206 wanted := s.createTestStorageInfoWithError("", 207 fmt.Sprintf("getting unit for storage attachment: %v", msg)) 208 s.assertInstanceInfoError(c, found.Results[0], wanted, msg) 209 } 210 211 func (s *storageSuite) createTestStorageInfoWithError(code, msg string) params.StorageInfo { 212 wanted := s.createTestStorageInfo() 213 wanted.Error = ¶ms.Error{Code: code, 214 Message: fmt.Sprintf("getting attachments for storage data/0: %v", msg)} 215 return wanted 216 } 217 218 func (s *storageSuite) createTestStorageInfo() params.StorageInfo { 219 return params.StorageInfo{ 220 params.StorageDetails{ 221 StorageTag: s.storageTag.String(), 222 OwnerTag: s.unitTag.String(), 223 Kind: params.StorageKindFilesystem, 224 Status: "pending", 225 }, 226 nil, 227 } 228 } 229 230 func (s *storageSuite) assertInstanceInfoError(c *gc.C, obtained params.StorageInfo, wanted params.StorageInfo, expected string) { 231 if expected != "" { 232 c.Assert(errors.Cause(obtained.Error), gc.ErrorMatches, fmt.Sprintf(".*%v.*", expected)) 233 } else { 234 c.Assert(obtained.Error, gc.IsNil) 235 } 236 c.Assert(obtained, gc.DeepEquals, wanted) 237 } 238 239 func (s *storageSuite) TestShowStorageEmpty(c *gc.C) { 240 found, err := s.api.Show(params.Entities{}) 241 c.Assert(err, jc.ErrorIsNil) 242 // Nothing should have matched the filter :D 243 c.Assert(found.Results, gc.HasLen, 0) 244 } 245 246 func (s *storageSuite) TestShowStorageNoFilter(c *gc.C) { 247 found, err := s.api.Show(params.Entities{Entities: []params.Entity{}}) 248 c.Assert(err, jc.ErrorIsNil) 249 // Nothing should have matched the filter :D 250 c.Assert(found.Results, gc.HasLen, 0) 251 } 252 253 func (s *storageSuite) TestShowStorage(c *gc.C) { 254 entity := params.Entity{Tag: s.storageTag.String()} 255 256 found, err := s.api.Show(params.Entities{Entities: []params.Entity{entity}}) 257 c.Assert(err, jc.ErrorIsNil) 258 c.Assert(found.Results, gc.HasLen, 1) 259 260 one := found.Results[0] 261 c.Assert(one.Error, gc.IsNil) 262 263 expected := params.StorageDetails{ 264 StorageTag: s.storageTag.String(), 265 OwnerTag: s.unitTag.String(), 266 Kind: params.StorageKindFilesystem, 267 UnitTag: s.unitTag.String(), 268 Status: "pending", 269 } 270 c.Assert(one.Result, gc.DeepEquals, expected) 271 } 272 273 func (s *storageSuite) TestShowStorageInvalidId(c *gc.C) { 274 storageTag := "foo" 275 entity := params.Entity{Tag: storageTag} 276 277 found, err := s.api.Show(params.Entities{Entities: []params.Entity{entity}}) 278 c.Assert(err, jc.ErrorIsNil) 279 c.Assert(found.Results, gc.HasLen, 1) 280 281 instance := found.Results[0] 282 c.Assert(instance.Error, gc.ErrorMatches, `"foo" is not a valid tag`) 283 284 expected := params.StorageDetails{Kind: params.StorageKindUnknown} 285 c.Assert(instance.Result, gc.DeepEquals, expected) 286 }