github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"github.com/juju/cmd/cmdtesting"
    13  	jc "github.com/juju/testing/checkers"
    14  	gc "gopkg.in/check.v1"
    15  	goyaml "gopkg.in/yaml.v2"
    16  
    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 := cmdtesting.Context(c)
    44  		code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(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 := cmdtesting.Context(c)
    69  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(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  
    73  Summary:
    74  print information for storage instance with specified id
    75  
    76  Options:
    77  --format  (= smart)
    78      Specify output format (json|smart|yaml)
    79  -o, --output (= "")
    80      Specify an output file
    81  -s  (= data/0)
    82      specify a storage instance by id
    83  
    84  Details:
    85  When no <key> is supplied, all keys values are printed.
    86  `)
    87  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    88  }
    89  
    90  func (s *storageGetSuite) TestOutputPath(c *gc.C) {
    91  	hctx, _ := s.newHookContext()
    92  	com, err := jujuc.NewCommand(hctx, cmdString("storage-get"))
    93  	c.Assert(err, jc.ErrorIsNil)
    94  	ctx := cmdtesting.Context(c)
    95  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--format", "yaml", "--output", "some-file", "-s", "data/0"})
    96  	c.Assert(code, gc.Equals, 0)
    97  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    98  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
    99  	content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file"))
   100  	c.Assert(err, jc.ErrorIsNil)
   101  
   102  	var out map[string]interface{}
   103  	c.Assert(goyaml.Unmarshal(content, &out), gc.IsNil)
   104  	c.Assert(out, gc.DeepEquals, storageAttributes)
   105  }