github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/storage-list_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc_test 5 6 import ( 7 "encoding/json" 8 9 "github.com/juju/cmd" 10 "github.com/juju/cmd/cmdtesting" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 goyaml "gopkg.in/yaml.v2" 14 15 "github.com/juju/juju/worker/uniter/runner/jujuc" 16 "github.com/juju/juju/worker/uniter/runner/jujuc/jujuctesting" 17 ) 18 19 type storageListSuite struct { 20 storageSuite 21 } 22 23 var _ = gc.Suite(&storageListSuite{}) 24 25 func (s *storageListSuite) newHookContext() *jujuctesting.Context { 26 ctx, info := s.NewHookContext() 27 info.SetBlockStorage("data/0", "/dev/sda", s.Stub) 28 info.SetBlockStorage("data/1", "/dev/sdb", s.Stub) 29 info.SetBlockStorage("data/2", "/dev/sdc", s.Stub) 30 info.SetBlockStorage("other/0", "/dev/sdd", s.Stub) 31 info.SetBlockStorage("other/1", "/dev/sde", s.Stub) 32 return ctx 33 } 34 35 func (s *storageListSuite) TestOutputFormatYAML(c *gc.C) { 36 s.testOutputFormat(c, 37 []string{"--format", "yaml"}, 38 formatYaml, 39 []string{"data/0", "data/1", "data/2", "other/0", "other/1"}, 40 ) 41 } 42 43 func (s *storageListSuite) TestOutputFormatJSON(c *gc.C) { 44 s.testOutputFormat(c, 45 []string{"--format", "json"}, 46 formatJson, 47 []string{"data/0", "data/1", "data/2", "other/0", "other/1"}, 48 ) 49 } 50 51 func (s *storageListSuite) TestOutputFormatDefault(c *gc.C) { 52 // The default output format is "smart", which is 53 // a newline-separated list of strings. 54 s.testOutputFormat(c, 55 []string{}, 56 -1, // don't specify format 57 "data/0\ndata/1\ndata/2\nother/0\nother/1\n", 58 ) 59 } 60 61 func (s *storageListSuite) TestOutputFiltered(c *gc.C) { 62 s.testOutputFormat(c, 63 []string{"--format", "yaml", "data"}, 64 formatYaml, 65 []string{"data/0", "data/1", "data/2"}, 66 ) 67 } 68 69 func (s *storageListSuite) TestOutputNoMatches(c *gc.C) { 70 s.testOutputFormat(c, 71 []string{"--format", "yaml", "dat"}, 72 formatYaml, 73 []string{}, 74 ) 75 } 76 77 func (s *storageListSuite) testOutputFormat(c *gc.C, args []string, format int, expect interface{}) { 78 hctx := s.newHookContext() 79 com, err := jujuc.NewCommand(hctx, cmdString("storage-list")) 80 c.Assert(err, jc.ErrorIsNil) 81 ctx := cmdtesting.Context(c) 82 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, args) 83 c.Assert(code, gc.Equals, 0) 84 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 85 86 var out interface{} 87 var outSlice []string 88 switch format { 89 case formatYaml: 90 c.Assert(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &outSlice), gc.IsNil) 91 out = outSlice 92 case formatJson: 93 c.Assert(json.Unmarshal(bufferBytes(ctx.Stdout), &outSlice), gc.IsNil) 94 out = outSlice 95 default: 96 out = string(bufferBytes(ctx.Stdout)) 97 } 98 c.Assert(out, jc.DeepEquals, expect) 99 } 100 101 func (s *storageListSuite) TestHelp(c *gc.C) { 102 hctx := s.newHookContext() 103 com, err := jujuc.NewCommand(hctx, cmdString("storage-list")) 104 c.Assert(err, jc.ErrorIsNil) 105 ctx := cmdtesting.Context(c) 106 code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--help"}) 107 c.Assert(code, gc.Equals, 0) 108 c.Assert(bufferString(ctx.Stdout), gc.Equals, `Usage: storage-list [options] [<storage-name>] 109 110 Summary: 111 list storage attached to the unit 112 113 Options: 114 --format (= smart) 115 Specify output format (json|smart|yaml) 116 -o, --output (= "") 117 Specify an output file 118 119 Details: 120 storage-list will list the names of all storage instances 121 attached to the unit. These names can be passed to storage-get 122 via the "-s" flag to query the storage attributes. 123 124 A storage name may be specified, in which case only storage 125 instances for that named storage will be returned. 126 `) 127 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 128 }