github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/storage/show_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 "strings" 8 "time" 9 10 "github.com/juju/cmd" 11 "github.com/juju/names" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/apiserver/params" 16 "github.com/juju/juju/cmd/envcmd" 17 "github.com/juju/juju/cmd/juju/storage" 18 _ "github.com/juju/juju/provider/dummy" 19 "github.com/juju/juju/testing" 20 ) 21 22 // epoch is the time we use for "since" in statuses. The time 23 // is always shown as a local time, so we override the local 24 // location to be UTC+8. 25 var epoch = time.Unix(0, 0) 26 27 type ShowSuite struct { 28 SubStorageSuite 29 mockAPI *mockShowAPI 30 } 31 32 var _ = gc.Suite(&ShowSuite{}) 33 34 func (s *ShowSuite) SetUpTest(c *gc.C) { 35 s.SubStorageSuite.SetUpTest(c) 36 37 s.mockAPI = &mockShowAPI{} 38 s.PatchValue(storage.GetStorageShowAPI, func(c *storage.ShowCommand) (storage.StorageShowAPI, error) { 39 return s.mockAPI, nil 40 }) 41 s.PatchValue(&time.Local, time.FixedZone("Australia/Perth", 3600*8)) 42 43 } 44 45 func runShow(c *gc.C, args []string) (*cmd.Context, error) { 46 return testing.RunCommand(c, envcmd.Wrap(&storage.ShowCommand{}), args...) 47 } 48 49 func (s *ShowSuite) TestShowNoMatch(c *gc.C) { 50 s.mockAPI.noMatch = true 51 s.assertValidShow( 52 c, 53 []string{"fluff/0"}, 54 ` 55 {} 56 `[1:], 57 ) 58 } 59 60 func (s *ShowSuite) TestShow(c *gc.C) { 61 s.assertValidShow( 62 c, 63 []string{"shared-fs/0"}, 64 // Default format is yaml 65 ` 66 shared-fs/0: 67 kind: filesystem 68 status: 69 current: attached 70 since: 01 Jan 1970 08:00:00\+08:00 71 persistent: true 72 attachments: 73 units: 74 transcode/0: 75 machine: \"1\" 76 location: a location 77 transcode/1: 78 machine: \"2\" 79 location: b location 80 `[1:], 81 ) 82 } 83 84 func (s *ShowSuite) TestShowInvalidId(c *gc.C) { 85 _, err := runShow(c, []string{"foo"}) 86 c.Assert(err, gc.ErrorMatches, ".*invalid storage id foo.*") 87 } 88 89 func (s *ShowSuite) TestShowJSON(c *gc.C) { 90 s.assertValidShow( 91 c, 92 []string{"shared-fs/0", "--format", "json"}, 93 `{"shared-fs/0":{"kind":"filesystem","status":{"current":"attached","since":"01 Jan 1970 08:00:00\+08:00"},"persistent":true,"attachments":{"units":{"transcode/0":{"machine":"1","location":"a location"},"transcode/1":{"machine":"2","location":"b location"}}}}} 94 `, 95 ) 96 } 97 98 func (s *ShowSuite) TestShowMultipleReturn(c *gc.C) { 99 s.assertValidShow( 100 c, 101 []string{"shared-fs/0", "db-dir/1000"}, 102 ` 103 db-dir/1000: 104 kind: block 105 status: 106 current: pending 107 since: .* 108 persistent: true 109 attachments: 110 units: 111 postgresql/0: {} 112 shared-fs/0: 113 kind: filesystem 114 status: 115 current: attached 116 since: 01 Jan 1970 08:00:00\+08:00 117 persistent: true 118 attachments: 119 units: 120 transcode/0: 121 machine: \"1\" 122 location: a location 123 transcode/1: 124 machine: \"2\" 125 location: b location 126 `[1:], 127 ) 128 } 129 130 func (s *ShowSuite) assertValidShow(c *gc.C, args []string, expected string) { 131 context, err := runShow(c, args) 132 c.Assert(err, jc.ErrorIsNil) 133 134 obtained := testing.Stdout(context) 135 c.Assert(obtained, gc.Matches, expected) 136 } 137 138 type mockShowAPI struct { 139 noMatch bool 140 } 141 142 func (s mockShowAPI) Close() error { 143 return nil 144 } 145 146 func (s mockShowAPI) Show(tags []names.StorageTag) ([]params.StorageDetailsResult, error) { 147 if s.noMatch { 148 return nil, nil 149 } 150 all := make([]params.StorageDetailsResult, len(tags)) 151 for i, tag := range tags { 152 if strings.Contains(tag.String(), "shared") { 153 all[i].Result = ¶ms.StorageDetails{ 154 StorageTag: tag.String(), 155 OwnerTag: "service-transcode", 156 Kind: params.StorageKindFilesystem, 157 Status: params.EntityStatus{ 158 Status: "attached", 159 Since: &epoch, 160 }, 161 Persistent: true, 162 Attachments: map[string]params.StorageAttachmentDetails{ 163 "unit-transcode-0": params.StorageAttachmentDetails{ 164 MachineTag: "machine-1", 165 Location: "a location", 166 }, 167 "unit-transcode-1": params.StorageAttachmentDetails{ 168 MachineTag: "machine-2", 169 Location: "b location", 170 }, 171 }, 172 } 173 } else { 174 all[i].Legacy = params.LegacyStorageDetails{ 175 StorageTag: tag.String(), 176 UnitTag: "unit-postgresql-0", 177 Kind: params.StorageKindBlock, 178 Status: "pending", 179 } 180 if i == 1 { 181 all[i].Legacy.Persistent = true 182 } 183 } 184 } 185 return all, nil 186 }