github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/storage/list_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 "github.com/juju/cmd" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/apiserver/params" 12 "github.com/juju/juju/cmd/envcmd" 13 "github.com/juju/juju/cmd/juju/storage" 14 _ "github.com/juju/juju/provider/dummy" 15 "github.com/juju/juju/testing" 16 ) 17 18 type ListSuite struct { 19 SubStorageSuite 20 mockAPI *mockListAPI 21 } 22 23 var _ = gc.Suite(&ListSuite{}) 24 25 func (s *ListSuite) SetUpTest(c *gc.C) { 26 s.SubStorageSuite.SetUpTest(c) 27 28 s.mockAPI = &mockListAPI{} 29 s.PatchValue(storage.GetStorageListAPI, func(c *storage.ListCommand) (storage.StorageListAPI, error) { 30 return s.mockAPI, nil 31 }) 32 33 } 34 35 func runList(c *gc.C, args []string) (*cmd.Context, error) { 36 return testing.RunCommand(c, envcmd.Wrap(&storage.ListCommand{}), args...) 37 } 38 39 func (s *ListSuite) TestList(c *gc.C) { 40 s.assertValidList( 41 c, 42 nil, 43 // Default format is tabular 44 ` 45 \[Storage\] 46 UNIT ID LOCATION STATUS MESSAGE 47 postgresql/0 db-dir/1100 hither attached 48 transcode/0 db-dir/1000 pending 49 transcode/0 shared-fs/0 there attached 50 transcode/1 shared-fs/0 here attached 51 52 `[1:], 53 "", 54 ) 55 } 56 57 func (s *ListSuite) TestListYAML(c *gc.C) { 58 s.assertValidList( 59 c, 60 []string{"--format", "yaml"}, 61 ` 62 storage: 63 db-dir/1000: 64 kind: block 65 status: 66 current: pending 67 since: .* 68 persistent: false 69 attachments: 70 units: 71 transcode/0: {} 72 db-dir/1100: 73 kind: block 74 status: 75 current: attached 76 since: .* 77 persistent: true 78 attachments: 79 units: 80 postgresql/0: 81 location: hither 82 shared-fs/0: 83 kind: filesystem 84 status: 85 current: attached 86 since: .* 87 persistent: true 88 attachments: 89 units: 90 transcode/0: 91 location: there 92 transcode/1: 93 location: here 94 `[1:], 95 "", 96 ) 97 } 98 99 func (s *ListSuite) TestListOwnerStorageIdSort(c *gc.C) { 100 s.mockAPI.includeErrors = true 101 s.assertValidList( 102 c, 103 nil, 104 // Default format is tabular 105 ` 106 \[Storage\] 107 UNIT ID LOCATION STATUS MESSAGE 108 postgresql/0 db-dir/1100 hither attached 109 transcode/0 db-dir/1000 pending 110 transcode/0 shared-fs/0 there attached 111 transcode/1 shared-fs/0 here attached 112 113 `[1:], 114 "error for storage-db-dir-1010\n", 115 ) 116 } 117 118 func (s *ListSuite) assertValidList(c *gc.C, args []string, expectedValid, expectedErr string) { 119 context, err := runList(c, args) 120 c.Assert(err, jc.ErrorIsNil) 121 122 obtainedErr := testing.Stderr(context) 123 c.Assert(obtainedErr, gc.Matches, expectedErr) 124 125 obtainedValid := testing.Stdout(context) 126 c.Assert(obtainedValid, gc.Matches, expectedValid) 127 } 128 129 type mockListAPI struct { 130 includeErrors bool 131 } 132 133 func (s mockListAPI) Close() error { 134 return nil 135 } 136 137 func (s mockListAPI) List() ([]params.StorageDetailsResult, error) { 138 // postgresql/0 has "db-dir/1100" 139 // transcode/1 has "db-dir/1000" 140 // transcode/0 and transcode/1 share "shared-fs/0" 141 // 142 // there is also a storage instance "db-dir/1010" which 143 // returns an error when listed. 144 results := []params.StorageDetailsResult{{ 145 Legacy: params.LegacyStorageDetails{ 146 StorageTag: "storage-db-dir-1000", 147 OwnerTag: "unit-transcode-0", 148 UnitTag: "unit-transcode-0", 149 Kind: params.StorageKindBlock, 150 Status: "pending", 151 }, 152 }, { 153 Result: ¶ms.StorageDetails{ 154 StorageTag: "storage-db-dir-1100", 155 OwnerTag: "unit-postgresql-0", 156 Kind: params.StorageKindBlock, 157 Status: params.EntityStatus{ 158 Status: params.StatusAttached, 159 Since: &epoch, 160 }, 161 Persistent: true, 162 Attachments: map[string]params.StorageAttachmentDetails{ 163 "unit-postgresql-0": params.StorageAttachmentDetails{ 164 Location: "hither", 165 }, 166 }, 167 }, 168 }, { 169 Result: ¶ms.StorageDetails{ 170 StorageTag: "storage-shared-fs-0", 171 OwnerTag: "service-transcode", 172 Kind: params.StorageKindFilesystem, 173 Status: params.EntityStatus{ 174 Status: params.StatusAttached, 175 Since: &epoch, 176 }, 177 Persistent: true, 178 Attachments: map[string]params.StorageAttachmentDetails{ 179 "unit-transcode-0": params.StorageAttachmentDetails{ 180 Location: "there", 181 }, 182 "unit-transcode-1": params.StorageAttachmentDetails{ 183 Location: "here", 184 }, 185 }, 186 }, 187 }} 188 if s.includeErrors { 189 results = append(results, params.StorageDetailsResult{ 190 Error: ¶ms.Error{ 191 Message: "error for storage-db-dir-1010", 192 }, 193 }) 194 } 195 return results, nil 196 }