github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/worker/uniter/runner/jujuc/storage-get_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  	"io/ioutil"
     9  	"path/filepath"
    10  
    11  	"github.com/juju/cmd"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  	goyaml "gopkg.in/yaml.v1"
    15  
    16  	"github.com/juju/juju/testing"
    17  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    18  )
    19  
    20  type storageGetSuite struct {
    21  	storageSuite
    22  }
    23  
    24  var _ = gc.Suite(&storageGetSuite{})
    25  
    26  var storageGetTests = []struct {
    27  	args   []string
    28  	format int
    29  	out    interface{}
    30  }{
    31  	{[]string{"--format", "yaml"}, formatYaml, storageAttributes},
    32  	{[]string{"--format", "json"}, formatJson, storageAttributes},
    33  	{[]string{}, formatYaml, storageAttributes},
    34  	{[]string{"location"}, -1, "/dev/sda\n"},
    35  }
    36  
    37  func (s *storageGetSuite) TestOutputFormatKey(c *gc.C) {
    38  	for i, t := range storageGetTests {
    39  		c.Logf("test %d: %#v", i, t.args)
    40  		hctx, _ := s.newHookContext()
    41  		com, err := jujuc.NewCommand(hctx, cmdString("storage-get"))
    42  		c.Assert(err, jc.ErrorIsNil)
    43  		ctx := testing.Context(c)
    44  		code := cmd.Main(com, ctx, t.args)
    45  		c.Assert(code, gc.Equals, 0)
    46  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    47  
    48  		var out interface{}
    49  		var outMap map[string]interface{}
    50  		switch t.format {
    51  		case formatYaml:
    52  			c.Assert(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &outMap), gc.IsNil)
    53  			out = outMap
    54  		case formatJson:
    55  			c.Assert(json.Unmarshal(bufferBytes(ctx.Stdout), &outMap), gc.IsNil)
    56  			out = outMap
    57  		default:
    58  			out = string(bufferBytes(ctx.Stdout))
    59  		}
    60  		c.Assert(out, gc.DeepEquals, t.out)
    61  	}
    62  }
    63  
    64  func (s *storageGetSuite) TestHelp(c *gc.C) {
    65  	hctx, _ := s.newHookContext()
    66  	com, err := jujuc.NewCommand(hctx, cmdString("storage-get"))
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	ctx := testing.Context(c)
    69  	code := cmd.Main(com, ctx, []string{"--help"})
    70  	c.Assert(code, gc.Equals, 0)
    71  	c.Assert(bufferString(ctx.Stdout), gc.Equals, `usage: storage-get [options] [<key>]
    72  purpose: print information for storage instance with specified id
    73  
    74  options:
    75  --format  (= smart)
    76      specify output format (json|smart|yaml)
    77  -o, --output (= "")
    78      specify an output file
    79  -s  (= data/0)
    80      specify a storage instance by id
    81  
    82  When no <key> is supplied, all keys values are printed.
    83  `)
    84  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    85  }
    86  
    87  func (s *storageGetSuite) TestOutputPath(c *gc.C) {
    88  	hctx, _ := s.newHookContext()
    89  	com, err := jujuc.NewCommand(hctx, cmdString("storage-get"))
    90  	c.Assert(err, jc.ErrorIsNil)
    91  	ctx := testing.Context(c)
    92  	code := cmd.Main(com, ctx, []string{"--format", "yaml", "--output", "some-file", "-s", "data/0"})
    93  	c.Assert(code, gc.Equals, 0)
    94  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    95  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
    96  	content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file"))
    97  	c.Assert(err, jc.ErrorIsNil)
    98  
    99  	var out map[string]interface{}
   100  	c.Assert(goyaml.Unmarshal(content, &out), gc.IsNil)
   101  	c.Assert(out, gc.DeepEquals, storageAttributes)
   102  }