github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/worker/uniter/jujuc/config-get_test.go (about)

     1  // Copyright 2012, 2013 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  	gc "launchpad.net/gocheck"
    12  	"launchpad.net/goyaml"
    13  
    14  	"github.com/juju/juju/cmd"
    15  	"github.com/juju/juju/testing"
    16  	"github.com/juju/juju/worker/uniter/jujuc"
    17  )
    18  
    19  type ConfigGetSuite struct {
    20  	ContextSuite
    21  }
    22  
    23  var _ = gc.Suite(&ConfigGetSuite{})
    24  
    25  var configGetKeyTests = []struct {
    26  	args []string
    27  	out  string
    28  }{
    29  	{[]string{"monsters"}, "False\n"},
    30  	{[]string{"--format", "yaml", "monsters"}, "false\n"},
    31  	{[]string{"--format", "json", "monsters"}, "false\n"},
    32  	{[]string{"spline-reticulation"}, "45\n"},
    33  	{[]string{"--format", "yaml", "spline-reticulation"}, "45\n"},
    34  	{[]string{"--format", "json", "spline-reticulation"}, "45\n"},
    35  	{[]string{"missing"}, ""},
    36  	{[]string{"--format", "yaml", "missing"}, ""},
    37  	{[]string{"--format", "json", "missing"}, "null\n"},
    38  }
    39  
    40  func (s *ConfigGetSuite) TestOutputFormatKey(c *gc.C) {
    41  	for i, t := range configGetKeyTests {
    42  		c.Logf("test %d: %#v", i, t.args)
    43  		hctx := s.GetHookContext(c, -1, "")
    44  		com, err := jujuc.NewCommand(hctx, "config-get")
    45  		c.Assert(err, gc.IsNil)
    46  		ctx := testing.Context(c)
    47  		code := cmd.Main(com, ctx, t.args)
    48  		c.Assert(code, gc.Equals, 0)
    49  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    50  		c.Assert(bufferString(ctx.Stdout), gc.Matches, t.out)
    51  	}
    52  }
    53  
    54  var (
    55  	configGetYamlMap = map[string]interface{}{
    56  		"monsters":            false,
    57  		"spline-reticulation": 45,
    58  		"title":               "My Title",
    59  		"username":            "admin001",
    60  	}
    61  	configGetJsonMap = map[string]interface{}{
    62  		"monsters":            false,
    63  		"spline-reticulation": 45.0,
    64  		"title":               "My Title",
    65  		"username":            "admin001",
    66  	}
    67  	configGetYamlMapAll = map[string]interface{}{
    68  		"empty":               nil,
    69  		"monsters":            false,
    70  		"spline-reticulation": 45,
    71  		"title":               "My Title",
    72  		"username":            "admin001",
    73  	}
    74  	configGetJsonMapAll = map[string]interface{}{
    75  		"empty":               nil,
    76  		"monsters":            false,
    77  		"spline-reticulation": 45.0,
    78  		"title":               "My Title",
    79  		"username":            "admin001",
    80  	}
    81  )
    82  
    83  const (
    84  	formatYaml = iota
    85  	formatJson
    86  )
    87  
    88  var configGetAllTests = []struct {
    89  	args   []string
    90  	format int
    91  	out    map[string]interface{}
    92  }{
    93  	{nil, formatYaml, configGetYamlMap},
    94  	{[]string{"--format", "yaml"}, formatYaml, configGetYamlMap},
    95  	{[]string{"--format", "json"}, formatJson, configGetJsonMap},
    96  	{[]string{"--all", "--format", "yaml"}, formatYaml, configGetYamlMapAll},
    97  	{[]string{"--all", "--format", "json"}, formatJson, configGetJsonMapAll},
    98  	{[]string{"-a", "--format", "yaml"}, formatYaml, configGetYamlMapAll},
    99  	{[]string{"-a", "--format", "json"}, formatJson, configGetJsonMapAll},
   100  }
   101  
   102  func (s *ConfigGetSuite) TestOutputFormatAll(c *gc.C) {
   103  	for i, t := range configGetAllTests {
   104  		c.Logf("test %d: %#v", i, t.args)
   105  		hctx := s.GetHookContext(c, -1, "")
   106  		com, err := jujuc.NewCommand(hctx, "config-get")
   107  		c.Assert(err, gc.IsNil)
   108  		ctx := testing.Context(c)
   109  		code := cmd.Main(com, ctx, t.args)
   110  		c.Assert(code, gc.Equals, 0)
   111  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   112  
   113  		out := map[string]interface{}{}
   114  		switch t.format {
   115  		case formatYaml:
   116  			c.Assert(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &out), gc.IsNil)
   117  		case formatJson:
   118  			c.Assert(json.Unmarshal(bufferBytes(ctx.Stdout), &out), gc.IsNil)
   119  		}
   120  		c.Assert(out, gc.DeepEquals, t.out)
   121  	}
   122  }
   123  
   124  func (s *ConfigGetSuite) TestHelp(c *gc.C) {
   125  	hctx := s.GetHookContext(c, -1, "")
   126  	com, err := jujuc.NewCommand(hctx, "config-get")
   127  	c.Assert(err, gc.IsNil)
   128  	ctx := testing.Context(c)
   129  	code := cmd.Main(com, ctx, []string{"--help"})
   130  	c.Assert(code, gc.Equals, 0)
   131  	c.Assert(bufferString(ctx.Stdout), gc.Equals, `usage: config-get [options] [<key>]
   132  purpose: print service configuration
   133  
   134  options:
   135  -a, --all  (= false)
   136      print all keys
   137  --format  (= smart)
   138      specify output format (json|smart|yaml)
   139  -o, --output (= "")
   140      specify an output file
   141  
   142  When no <key> is supplied, all keys with values or defaults are printed. If
   143  --all is set, all known keys are printed; those without defaults or values are
   144  reported as null. <key> and --all are mutually exclusive.
   145  `)
   146  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   147  }
   148  
   149  func (s *ConfigGetSuite) TestOutputPath(c *gc.C) {
   150  	hctx := s.GetHookContext(c, -1, "")
   151  	com, err := jujuc.NewCommand(hctx, "config-get")
   152  	c.Assert(err, gc.IsNil)
   153  	ctx := testing.Context(c)
   154  	code := cmd.Main(com, ctx, []string{"--output", "some-file", "monsters"})
   155  	c.Assert(code, gc.Equals, 0)
   156  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   157  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
   158  	content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file"))
   159  	c.Assert(err, gc.IsNil)
   160  	c.Assert(string(content), gc.Equals, "False\n")
   161  }
   162  
   163  func (s *ConfigGetSuite) TestUnknownArg(c *gc.C) {
   164  	hctx := s.GetHookContext(c, -1, "")
   165  	com, err := jujuc.NewCommand(hctx, "config-get")
   166  	c.Assert(err, gc.IsNil)
   167  	testing.TestInit(c, com, []string{"multiple", "keys"}, `unrecognized args: \["keys"\]`)
   168  }
   169  
   170  func (s *ConfigGetSuite) TestAllPlusKey(c *gc.C) {
   171  	hctx := s.GetHookContext(c, -1, "")
   172  	com, err := jujuc.NewCommand(hctx, "config-get")
   173  	c.Assert(err, gc.IsNil)
   174  	ctx := testing.Context(c)
   175  	code := cmd.Main(com, ctx, []string{"--all", "--format", "json", "monsters"})
   176  	c.Assert(code, gc.Equals, 2)
   177  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "error: cannot use argument --all together with key \"monsters\"\n")
   178  }