github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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 "fmt" 8 "strings" 9 "time" 10 11 "github.com/juju/cmd" 12 "github.com/juju/cmd/cmdtesting" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 "gopkg.in/juju/names.v2" 16 17 "github.com/juju/juju/apiserver/params" 18 "github.com/juju/juju/cmd/juju/common" 19 "github.com/juju/juju/cmd/juju/storage" 20 _ "github.com/juju/juju/provider/dummy" 21 ) 22 23 type ShowSuite struct { 24 SubStorageSuite 25 mockAPI *mockShowAPI 26 } 27 28 var _ = gc.Suite(&ShowSuite{}) 29 30 func (s *ShowSuite) SetUpTest(c *gc.C) { 31 s.SubStorageSuite.SetUpTest(c) 32 33 s.mockAPI = &mockShowAPI{} 34 } 35 36 func (s *ShowSuite) runShow(c *gc.C, args []string) (*cmd.Context, error) { 37 return cmdtesting.RunCommand(c, storage.NewShowCommandForTest(s.mockAPI, s.store), args...) 38 } 39 40 func (s *ShowSuite) TestShowNoMatch(c *gc.C) { 41 s.mockAPI.noMatch = true 42 s.assertValidShow( 43 c, 44 []string{"fluff/0"}, 45 ` 46 {} 47 `[1:], 48 ) 49 } 50 51 func (s *ShowSuite) TestShow(c *gc.C) { 52 now := time.Now() 53 s.mockAPI.time = now 54 s.assertValidShow( 55 c, 56 []string{"shared-fs/0"}, 57 // Default format is yaml 58 fmt.Sprintf(` 59 shared-fs/0: 60 kind: filesystem 61 status: 62 current: attached 63 since: %s 64 persistent: true 65 attachments: 66 units: 67 transcode/0: 68 machine: "1" 69 location: a location 70 transcode/1: 71 machine: "2" 72 location: b location 73 `[1:], common.FormatTime(&now, false)), 74 ) 75 } 76 77 func (s *ShowSuite) TestShowInvalidId(c *gc.C) { 78 _, err := s.runShow(c, []string{"foo"}) 79 c.Assert(err, gc.ErrorMatches, ".*invalid storage id foo.*") 80 } 81 82 func (s *ShowSuite) TestShowJSON(c *gc.C) { 83 now := time.Now() 84 s.mockAPI.time = now 85 s.assertValidShow( 86 c, 87 []string{"shared-fs/0", "--format", "json"}, 88 fmt.Sprintf(`{"shared-fs/0":{"kind":"filesystem","status":{"current":"attached","since":"%s"},"persistent":true,"attachments":{"units":{"transcode/0":{"machine":"1","location":"a location"},"transcode/1":{"machine":"2","location":"b location"}}}}} 89 `, common.FormatTime(&now, false)), 90 ) 91 } 92 93 func (s *ShowSuite) TestShowMultipleReturn(c *gc.C) { 94 now := time.Now() 95 s.mockAPI.time = now 96 since := common.FormatTime(&now, false) 97 98 s.assertValidShow( 99 c, 100 []string{"shared-fs/0", "db-dir/1000"}, 101 fmt.Sprintf(` 102 db-dir/1000: 103 kind: block 104 status: 105 current: pending 106 since: %s 107 persistent: true 108 attachments: 109 units: 110 postgresql/0: {} 111 shared-fs/0: 112 kind: filesystem 113 status: 114 current: attached 115 since: %s 116 persistent: true 117 attachments: 118 units: 119 transcode/0: 120 machine: "1" 121 location: a location 122 transcode/1: 123 machine: "2" 124 location: b location 125 `[1:], since, since), 126 ) 127 } 128 129 func (s *ShowSuite) assertValidShow(c *gc.C, args []string, expected string) { 130 context, err := s.runShow(c, args) 131 c.Assert(err, jc.ErrorIsNil) 132 133 obtained := cmdtesting.Stdout(context) 134 c.Assert(obtained, gc.Equals, expected) 135 } 136 137 type mockShowAPI struct { 138 noMatch bool 139 time time.Time 140 } 141 142 func (s mockShowAPI) Close() error { 143 return nil 144 } 145 146 func (s mockShowAPI) StorageDetails(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: "application-transcode", 156 Kind: params.StorageKindFilesystem, 157 Status: params.EntityStatus{ 158 Status: "attached", 159 Since: &s.time, 160 }, 161 Persistent: true, 162 Attachments: map[string]params.StorageAttachmentDetails{ 163 "unit-transcode-0": { 164 MachineTag: "machine-1", 165 Location: "a location", 166 }, 167 "unit-transcode-1": { 168 MachineTag: "machine-2", 169 Location: "b location", 170 }, 171 }, 172 } 173 } else { 174 all[i].Result = ¶ms.StorageDetails{ 175 StorageTag: tag.String(), 176 Kind: params.StorageKindBlock, 177 Status: params.EntityStatus{ 178 Status: "pending", 179 Since: &s.time, 180 }, 181 Attachments: map[string]params.StorageAttachmentDetails{ 182 "unit-postgresql-0": {}, 183 }, 184 } 185 if i == 1 { 186 all[i].Result.Persistent = true 187 } 188 } 189 } 190 return all, nil 191 }