github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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 "github.com/juju/cmd" 8 "github.com/juju/names" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/apiserver/params" 13 "github.com/juju/juju/cmd/envcmd" 14 "github.com/juju/juju/cmd/juju/storage" 15 _ "github.com/juju/juju/provider/dummy" 16 "github.com/juju/juju/testing" 17 ) 18 19 type ShowSuite struct { 20 SubStorageSuite 21 mockAPI *mockStorageAPI 22 } 23 24 var _ = gc.Suite(&ShowSuite{}) 25 26 func (s *ShowSuite) SetUpTest(c *gc.C) { 27 s.SubStorageSuite.SetUpTest(c) 28 29 s.mockAPI = &mockStorageAPI{} 30 s.PatchValue(storage.GetStorageShowAPI, func(c *storage.ShowCommand) (storage.StorageShowAPI, error) { 31 return s.mockAPI, nil 32 }) 33 34 } 35 36 func runShow(c *gc.C, args []string) (*cmd.Context, error) { 37 return testing.RunCommand(c, envcmd.Wrap(&storage.ShowCommand{}), args...) 38 } 39 40 func (s *ShowSuite) TestShow(c *gc.C) { 41 s.assertValidShow( 42 c, 43 []string{"shared-fs/0"}, 44 // Default format is yaml 45 `- storage-tag: storage-shared-fs-0 46 storage-name: storage-name 47 owner-tag: unitTag 48 location: witty 49 available-size: 30 50 total-size: 100 51 tags: 52 - tests 53 - well 54 - maybe 55 `, 56 ) 57 } 58 59 func (s *ShowSuite) TestShowJSON(c *gc.C) { 60 s.assertValidShow( 61 c, 62 []string{"shared-fs/0", "--format", "json"}, 63 `[{"storage-tag":"storage-shared-fs-0","storage-name":"storage-name","owner-tag":"unitTag","location":"witty","available-size":30,"total-size":100,"tags":["tests","well","maybe"]}] 64 `, 65 ) 66 } 67 68 func (s *ShowSuite) TestShowMultipleReturn(c *gc.C) { 69 s.assertValidShow( 70 c, 71 []string{"shared-fs/0", "db-dir/1000"}, 72 `- storage-tag: storage-shared-fs-0 73 storage-name: storage-name 74 owner-tag: unitTag 75 location: witty 76 available-size: 30 77 total-size: 100 78 tags: 79 - tests 80 - well 81 - maybe 82 - storage-tag: storage-db-dir-1000 83 storage-name: storage-name 84 owner-tag: unitTag 85 location: witty 86 available-size: 30 87 total-size: 100 88 tags: 89 - tests 90 - well 91 - maybe 92 `, 93 ) 94 } 95 96 func (s *ShowSuite) assertValidShow(c *gc.C, args []string, expected string) { 97 context, err := runShow(c, args) 98 c.Assert(err, jc.ErrorIsNil) 99 100 obtained := testing.Stdout(context) 101 c.Assert(obtained, gc.Equals, expected) 102 } 103 104 type mockStorageAPI struct { 105 } 106 107 func (s mockStorageAPI) Close() error { 108 return nil 109 } 110 111 func (s mockStorageAPI) Show(tags []names.StorageTag) ([]params.StorageInstance, error) { 112 results := make([]params.StorageInstance, len(tags)) 113 114 for i, tag := range tags { 115 results[i] = params.StorageInstance{ 116 StorageTag: tag.String(), 117 StorageName: "storage-name", 118 OwnerTag: "unitTag", 119 Location: "witty", 120 AvailableSize: 30, 121 TotalSize: 100, 122 Tags: []string{"tests", "well", "maybe"}, 123 } 124 } 125 return results, nil 126 }