github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/uniter/runner/jujuc/config-get_test.go (about)

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