github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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.v1"
    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  purpose: print service configuration
   135  
   136  options:
   137  -a, --all  (= false)
   138      print all keys
   139  --format  (= smart)
   140      specify output format (json|smart|yaml)
   141  -o, --output (= "")
   142      specify an output file
   143  
   144  When no <key> is supplied, all keys with values or defaults are printed. If
   145  --all is set, all known keys are printed; those without defaults or values are
   146  reported as null. <key> and --all are mutually exclusive.
   147  `)
   148  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   149  }
   150  
   151  func (s *ConfigGetSuite) TestOutputPath(c *gc.C) {
   152  	hctx := s.GetHookContext(c, -1, "")
   153  	com, err := jujuc.NewCommand(hctx, cmdString("config-get"))
   154  	c.Assert(err, jc.ErrorIsNil)
   155  	ctx := testing.Context(c)
   156  	code := cmd.Main(com, ctx, []string{"--output", "some-file", "monsters"})
   157  	c.Assert(code, gc.Equals, 0)
   158  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   159  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
   160  	content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file"))
   161  	c.Assert(err, jc.ErrorIsNil)
   162  	c.Assert(string(content), gc.Equals, "False\n")
   163  }
   164  
   165  func (s *ConfigGetSuite) TestUnknownArg(c *gc.C) {
   166  	hctx := s.GetHookContext(c, -1, "")
   167  	com, err := jujuc.NewCommand(hctx, cmdString("config-get"))
   168  	c.Assert(err, jc.ErrorIsNil)
   169  	testing.TestInit(c, com, []string{"multiple", "keys"}, `unrecognized args: \["keys"\]`)
   170  }
   171  
   172  func (s *ConfigGetSuite) TestAllPlusKey(c *gc.C) {
   173  	hctx := s.GetHookContext(c, -1, "")
   174  	com, err := jujuc.NewCommand(hctx, cmdString("config-get"))
   175  	c.Assert(err, jc.ErrorIsNil)
   176  	ctx := testing.Context(c)
   177  	code := cmd.Main(com, ctx, []string{"--all", "--format", "json", "monsters"})
   178  	c.Assert(code, gc.Equals, 2)
   179  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "error: cannot use argument --all together with key \"monsters\"\n")
   180  }