github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	goyaml "gopkg.in/yaml.v1"
    13  
    14  	"github.com/juju/juju/testing"
    15  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    16  	jujuctesting "github.com/juju/juju/worker/uniter/runner/jujuc/testing"
    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  	return ctx
    31  }
    32  
    33  func (s *storageListSuite) TestOutputFormatYAML(c *gc.C) {
    34  	s.testOutputFormat(c,
    35  		[]string{"--format", "yaml"},
    36  		formatYaml,
    37  		[]string{"data/0", "data/1", "data/2"},
    38  	)
    39  }
    40  
    41  func (s *storageListSuite) TestOutputFormatJSON(c *gc.C) {
    42  	s.testOutputFormat(c,
    43  		[]string{"--format", "json"},
    44  		formatJson,
    45  		[]string{"data/0", "data/1", "data/2"},
    46  	)
    47  }
    48  
    49  func (s *storageListSuite) TestOutputFormatDefault(c *gc.C) {
    50  	// The default output format is "smart", which is
    51  	// a newline-separated list of strings.
    52  	s.testOutputFormat(c,
    53  		[]string{},
    54  		-1, // don't specify format
    55  		"data/0\ndata/1\ndata/2\n",
    56  	)
    57  }
    58  
    59  func (s *storageListSuite) testOutputFormat(c *gc.C, args []string, format int, expect interface{}) {
    60  	hctx := s.newHookContext()
    61  	com, err := jujuc.NewCommand(hctx, cmdString("storage-list"))
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	ctx := testing.Context(c)
    64  	code := cmd.Main(com, ctx, args)
    65  	c.Assert(code, gc.Equals, 0)
    66  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    67  
    68  	var out interface{}
    69  	var outSlice []string
    70  	switch format {
    71  	case formatYaml:
    72  		c.Assert(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &outSlice), gc.IsNil)
    73  		out = outSlice
    74  	case formatJson:
    75  		c.Assert(json.Unmarshal(bufferBytes(ctx.Stdout), &outSlice), gc.IsNil)
    76  		out = outSlice
    77  	default:
    78  		out = string(bufferBytes(ctx.Stdout))
    79  	}
    80  	c.Assert(out, jc.DeepEquals, expect)
    81  }
    82  
    83  func (s *storageListSuite) TestHelp(c *gc.C) {
    84  	hctx := s.newHookContext()
    85  	com, err := jujuc.NewCommand(hctx, cmdString("storage-list"))
    86  	c.Assert(err, jc.ErrorIsNil)
    87  	ctx := testing.Context(c)
    88  	code := cmd.Main(com, ctx, []string{"--help"})
    89  	c.Assert(code, gc.Equals, 0)
    90  	c.Assert(bufferString(ctx.Stdout), gc.Equals, `usage: storage-list [options]
    91  purpose: list storage attached to the unit
    92  
    93  options:
    94  --format  (= smart)
    95      specify output format (json|smart|yaml)
    96  -o, --output (= "")
    97      specify an output file
    98  
    99  storage-list will list the names of all storage instances
   100  attached to the unit. These names can be passed to storage-get
   101  via the "-s" flag to query the storage attributes.
   102  `)
   103  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   104  }