1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
"github.com/juju/errors"
10
jc "github.com/juju/testing/checkers"
11
gc "gopkg.in/check.v1"
12
"gopkg.in/juju/names.v2"
14
"github.com/juju/juju/apiserver/params"
15
"github.com/juju/juju/state"
16
"github.com/juju/juju/status"
19
type storageSuite struct {
23
var _ = gc.Suite(&storageSuite{})
25
func (s *storageSuite) TestStorageListEmpty(c *gc.C) {
26
s.state.allStorageInstances = func() ([]state.StorageInstance, error) {
27
s.calls = append(s.calls, allStorageInstancesCall)
28
return []state.StorageInstance{}, nil
31
found, err := s.api.ListStorageDetails(
32
params.StorageFilters{[]params.StorageFilter{{}}},
34
c.Assert(err, jc.ErrorIsNil)
35
c.Assert(found.Results, gc.HasLen, 1)
36
c.Assert(found.Results[0].Error, gc.IsNil)
37
c.Assert(found.Results[0].Result, gc.HasLen, 0)
38
s.assertCalls(c, []string{allStorageInstancesCall})
41
func (s *storageSuite) TestStorageListFilesystem(c *gc.C) {
42
found, err := s.api.ListStorageDetails(
43
params.StorageFilters{[]params.StorageFilter{{}}},
45
c.Assert(err, jc.ErrorIsNil)
47
expectedCalls := []string{
48
allStorageInstancesCall,
49
storageInstanceFilesystemCall,
50
storageInstanceAttachmentsCall,
51
unitAssignedMachineCall,
53
storageInstanceFilesystemCall,
54
storageInstanceFilesystemAttachmentCall,
56
s.assertCalls(c, expectedCalls)
58
c.Assert(found.Results, gc.HasLen, 1)
59
c.Assert(found.Results[0].Error, gc.IsNil)
60
c.Assert(found.Results[0].Result, gc.HasLen, 1)
61
wantedDetails := s.createTestStorageDetails()
62
c.Assert(found.Results[0].Result[0], jc.DeepEquals, wantedDetails)
65
func (s *storageSuite) TestStorageListVolume(c *gc.C) {
66
s.storageInstance.kind = state.StorageKindBlock
67
found, err := s.api.ListStorageDetails(
68
params.StorageFilters{[]params.StorageFilter{{}}},
70
c.Assert(err, jc.ErrorIsNil)
72
expectedCalls := []string{
73
allStorageInstancesCall,
74
storageInstanceVolumeCall,
75
storageInstanceAttachmentsCall,
76
unitAssignedMachineCall,
78
storageInstanceVolumeCall,
80
s.assertCalls(c, expectedCalls)
82
c.Assert(found.Results, gc.HasLen, 1)
83
c.Assert(found.Results[0].Error, gc.IsNil)
84
c.Assert(found.Results[0].Result, gc.HasLen, 1)
85
wantedDetails := s.createTestStorageDetails()
86
wantedDetails.Kind = params.StorageKindBlock
87
wantedDetails.Status.Status = status.StatusAttached
88
c.Assert(found.Results[0].Result[0], jc.DeepEquals, wantedDetails)
91
func (s *storageSuite) TestStorageListError(c *gc.C) {
92
msg := "list test error"
93
s.state.allStorageInstances = func() ([]state.StorageInstance, error) {
94
s.calls = append(s.calls, allStorageInstancesCall)
95
return []state.StorageInstance{}, errors.Errorf(msg)
98
found, err := s.api.ListStorageDetails(
99
params.StorageFilters{[]params.StorageFilter{{}}},
101
c.Assert(err, jc.ErrorIsNil)
102
c.Assert(found.Results, gc.HasLen, 1)
103
c.Assert(found.Results[0].Error, gc.ErrorMatches, msg)
105
expectedCalls := []string{allStorageInstancesCall}
106
s.assertCalls(c, expectedCalls)
109
func (s *storageSuite) TestStorageListInstanceError(c *gc.C) {
110
msg := "list test error"
111
s.state.storageInstance = func(sTag names.StorageTag) (state.StorageInstance, error) {
112
s.calls = append(s.calls, storageInstanceCall)
113
c.Assert(sTag, jc.DeepEquals, s.storageTag)
114
return nil, errors.Errorf(msg)
117
found, err := s.api.ListStorageDetails(
118
params.StorageFilters{[]params.StorageFilter{{}}},
120
c.Assert(err, jc.ErrorIsNil)
122
expectedCalls := []string{
123
allStorageInstancesCall,
124
storageInstanceFilesystemCall,
125
storageInstanceAttachmentsCall,
126
unitAssignedMachineCall,
129
s.assertCalls(c, expectedCalls)
130
c.Assert(found.Results, gc.HasLen, 1)
131
c.Assert(found.Results[0].Error, gc.ErrorMatches,
132
fmt.Sprintf("getting details for storage data/0: getting storage instance: %v", msg),
136
func (s *storageSuite) TestStorageListAttachmentError(c *gc.C) {
137
s.state.storageInstanceAttachments = func(tag names.StorageTag) ([]state.StorageAttachment, error) {
138
s.calls = append(s.calls, storageInstanceAttachmentsCall)
139
c.Assert(tag, jc.DeepEquals, s.storageTag)
140
return []state.StorageAttachment{}, errors.Errorf("list test error")
143
found, err := s.api.ListStorageDetails(
144
params.StorageFilters{[]params.StorageFilter{{}}},
146
c.Assert(err, jc.ErrorIsNil)
148
expectedCalls := []string{
149
allStorageInstancesCall,
150
storageInstanceFilesystemCall,
151
storageInstanceAttachmentsCall,
153
s.assertCalls(c, expectedCalls)
154
c.Assert(found.Results, gc.HasLen, 1)
155
c.Assert(found.Results[0].Error, gc.ErrorMatches,
156
"getting details for storage data/0: list test error")
159
func (s *storageSuite) TestStorageListMachineError(c *gc.C) {
160
msg := "list test error"
161
s.state.unitAssignedMachine = func(u names.UnitTag) (names.MachineTag, error) {
162
s.calls = append(s.calls, unitAssignedMachineCall)
163
c.Assert(u, jc.DeepEquals, s.unitTag)
164
return names.MachineTag{}, errors.Errorf(msg)
167
found, err := s.api.ListStorageDetails(
168
params.StorageFilters{[]params.StorageFilter{{}}},
170
c.Assert(err, jc.ErrorIsNil)
172
expectedCalls := []string{
173
allStorageInstancesCall,
174
storageInstanceFilesystemCall,
175
storageInstanceAttachmentsCall,
176
unitAssignedMachineCall,
178
s.assertCalls(c, expectedCalls)
179
c.Assert(found.Results, gc.HasLen, 1)
180
c.Assert(found.Results[0].Error, gc.ErrorMatches,
181
fmt.Sprintf("getting details for storage data/0: %v", msg),
185
func (s *storageSuite) TestStorageListFilesystemError(c *gc.C) {
186
msg := "list test error"
187
s.state.storageInstanceFilesystem = func(sTag names.StorageTag) (state.Filesystem, error) {
188
s.calls = append(s.calls, storageInstanceFilesystemCall)
189
c.Assert(sTag, jc.DeepEquals, s.storageTag)
190
return nil, errors.Errorf(msg)
193
found, err := s.api.ListStorageDetails(
194
params.StorageFilters{[]params.StorageFilter{{}}},
196
c.Assert(err, jc.ErrorIsNil)
198
expectedCalls := []string{
199
allStorageInstancesCall,
200
storageInstanceFilesystemCall,
202
s.assertCalls(c, expectedCalls)
203
c.Assert(found.Results, gc.HasLen, 1)
204
c.Assert(found.Results[0].Error, gc.ErrorMatches,
205
fmt.Sprintf("getting details for storage data/0: %v", msg),
209
func (s *storageSuite) TestStorageListFilesystemAttachmentError(c *gc.C) {
210
msg := "list test error"
211
s.state.unitAssignedMachine = func(u names.UnitTag) (names.MachineTag, error) {
212
s.calls = append(s.calls, unitAssignedMachineCall)
213
c.Assert(u, jc.DeepEquals, s.unitTag)
214
return s.machineTag, errors.Errorf(msg)
217
found, err := s.api.ListStorageDetails(
218
params.StorageFilters{[]params.StorageFilter{{}}},
220
c.Assert(err, jc.ErrorIsNil)
222
expectedCalls := []string{
223
allStorageInstancesCall,
224
storageInstanceFilesystemCall,
225
storageInstanceAttachmentsCall,
226
unitAssignedMachineCall,
228
s.assertCalls(c, expectedCalls)
229
c.Assert(found.Results, gc.HasLen, 1)
230
c.Assert(found.Results[0].Error, gc.ErrorMatches,
231
fmt.Sprintf("getting details for storage data/0: %v", msg),
235
func (s *storageSuite) createTestStorageDetails() params.StorageDetails {
236
return params.StorageDetails{
237
StorageTag: s.storageTag.String(),
238
OwnerTag: s.unitTag.String(),
239
Kind: params.StorageKindFilesystem,
240
Status: params.EntityStatus{
243
Attachments: map[string]params.StorageAttachmentDetails{
244
s.unitTag.String(): params.StorageAttachmentDetails{
245
s.storageTag.String(),
247
s.machineTag.String(),
254
func (s *storageSuite) assertInstanceInfoError(c *gc.C, obtained params.StorageDetailsResult, wanted params.StorageDetailsResult, expected string) {
256
c.Assert(errors.Cause(obtained.Error), gc.ErrorMatches, fmt.Sprintf(".*%v.*", expected))
257
c.Assert(obtained.Result, gc.IsNil)
259
c.Assert(obtained.Error, gc.IsNil)
260
c.Assert(obtained, jc.DeepEquals, wanted)
264
func (s *storageSuite) TestShowStorageEmpty(c *gc.C) {
265
found, err := s.api.StorageDetails(params.Entities{})
266
c.Assert(err, jc.ErrorIsNil)
267
c.Assert(found.Results, gc.HasLen, 0)
270
func (s *storageSuite) TestShowStorageInvalidTag(c *gc.C) {
271
// Only storage tags are permitted
272
found, err := s.api.StorageDetails(params.Entities{
273
Entities: []params.Entity{{Tag: "machine-1"}},
275
c.Assert(err, jc.ErrorIsNil)
276
c.Assert(found.Results, gc.HasLen, 1)
277
c.Assert(found.Results[0].Error, gc.ErrorMatches, `"machine-1" is not a valid storage tag`)
280
func (s *storageSuite) TestShowStorage(c *gc.C) {
281
entity := params.Entity{Tag: s.storageTag.String()}
283
found, err := s.api.StorageDetails(
284
params.Entities{Entities: []params.Entity{entity}},
286
c.Assert(err, jc.ErrorIsNil)
287
c.Assert(found.Results, gc.HasLen, 1)
289
one := found.Results[0]
290
c.Assert(one.Error, gc.IsNil)
292
expected := params.StorageDetails{
293
StorageTag: s.storageTag.String(),
294
OwnerTag: s.unitTag.String(),
295
Kind: params.StorageKindFilesystem,
296
Status: params.EntityStatus{
299
Attachments: map[string]params.StorageAttachmentDetails{
300
s.unitTag.String(): params.StorageAttachmentDetails{
301
s.storageTag.String(),
303
s.machineTag.String(),
308
c.Assert(one.Result, jc.DeepEquals, &expected)
311
func (s *storageSuite) TestShowStorageInvalidId(c *gc.C) {
313
entity := params.Entity{Tag: storageTag}
315
found, err := s.api.StorageDetails(params.Entities{Entities: []params.Entity{entity}})
316
c.Assert(err, jc.ErrorIsNil)
317
c.Assert(found.Results, gc.HasLen, 1)
318
s.assertInstanceInfoError(c, found.Results[0], params.StorageDetailsResult{}, `"foo" is not a valid tag`)