github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/action-get_test.go (about)

     1  // Copyright 2014 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  	"fmt"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/cmd/cmdtesting"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    16  	"github.com/juju/juju/worker/uniter/runner/jujuc/jujuctesting"
    17  )
    18  
    19  type ActionGetSuite struct {
    20  	jujuctesting.ContextSuite
    21  }
    22  
    23  var _ = gc.Suite(&ActionGetSuite{})
    24  
    25  type actionGetContext struct {
    26  	actionParams map[string]interface{}
    27  	jujuc.Context
    28  }
    29  
    30  func (ctx *actionGetContext) ActionParams() (map[string]interface{}, error) {
    31  	return ctx.actionParams, nil
    32  }
    33  
    34  type nonActionContext struct {
    35  	jujuc.Context
    36  }
    37  
    38  func (ctx *nonActionContext) ActionParams() (map[string]interface{}, error) {
    39  	return nil, fmt.Errorf("ActionParams queried from non-Action hook context")
    40  }
    41  
    42  func (s *ActionGetSuite) TestNonActionRunFail(c *gc.C) {
    43  	hctx := &nonActionContext{}
    44  	com, err := jujuc.NewCommand(hctx, cmdString("action-get"))
    45  	c.Assert(err, jc.ErrorIsNil)
    46  	ctx := cmdtesting.Context(c)
    47  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{})
    48  	c.Check(code, gc.Equals, 1)
    49  	c.Check(bufferString(ctx.Stdout), gc.Equals, "")
    50  	expect := fmt.Sprintf(`(\n)*ERROR %s\n`, "ActionParams queried from non-Action hook context")
    51  	c.Check(bufferString(ctx.Stderr), gc.Matches, expect)
    52  }
    53  
    54  func (s *ActionGetSuite) TestActionGet(c *gc.C) {
    55  	var actionGetTestMaps = []map[string]interface{}{
    56  		{
    57  			"outfile": "foo.bz2",
    58  		},
    59  
    60  		{
    61  			"outfile": map[string]interface{}{
    62  				"filename": "foo.bz2",
    63  				"format":   "bzip",
    64  			},
    65  		},
    66  
    67  		{
    68  			"outfile": map[string]interface{}{
    69  				"type": map[string]interface{}{
    70  					"1": "raw",
    71  					"2": "gzip",
    72  					"3": "bzip",
    73  				},
    74  			},
    75  		},
    76  
    77  		// A map with a non-string key is not usable.
    78  		{
    79  			"outfile": map[interface{}]interface{}{
    80  				5: map[string]interface{}{
    81  					"1": "raw",
    82  					"2": "gzip",
    83  					"3": "bzip",
    84  				},
    85  			},
    86  		},
    87  
    88  		// A map with an inner map[interface{}]interface{} is OK if
    89  		// the keys are strings.
    90  		{
    91  			"outfile": map[interface{}]interface{}{
    92  				"type": map[string]interface{}{
    93  					"1": "raw",
    94  					"2": "gzip",
    95  					"3": "bzip",
    96  				},
    97  			},
    98  		},
    99  	}
   100  
   101  	var actionGetTests = []struct {
   102  		summary      string
   103  		args         []string
   104  		actionParams map[string]interface{}
   105  		code         int
   106  		out          string
   107  		errMsg       string
   108  	}{{
   109  		summary: "a simple empty map with nil key",
   110  		args:    []string{},
   111  		out:     "{}\n",
   112  	}, {
   113  		summary: "a simple empty map with nil key",
   114  		args:    []string{"--format", "yaml"},
   115  		out:     "{}\n",
   116  	}, {
   117  		summary: "a simple empty map with nil key",
   118  		args:    []string{"--format", "json"},
   119  		out:     "null\n",
   120  	}, {
   121  		summary: "a nonexistent key",
   122  		args:    []string{"foo"},
   123  	}, {
   124  		summary: "a nonexistent key",
   125  		args:    []string{"--format", "yaml", "foo"},
   126  	}, {
   127  		summary: "a nonexistent key",
   128  		args:    []string{"--format", "json", "foo"},
   129  		out:     "null\n",
   130  	}, {
   131  		summary:      "a nonexistent inner key",
   132  		args:         []string{"outfile.type"},
   133  		actionParams: actionGetTestMaps[1],
   134  	}, {
   135  		summary:      "a nonexistent inner key",
   136  		args:         []string{"--format", "yaml", "outfile.type"},
   137  		actionParams: actionGetTestMaps[1],
   138  	}, {
   139  		summary:      "a nonexistent inner key",
   140  		args:         []string{"--format", "json", "outfile.type"},
   141  		actionParams: actionGetTestMaps[1],
   142  		out:          "null\n",
   143  	}, {
   144  		summary:      "a nonexistent inner key",
   145  		args:         []string{"outfile.type.1"},
   146  		actionParams: actionGetTestMaps[1],
   147  	}, {
   148  		summary:      "a nonexistent inner key",
   149  		args:         []string{"--format", "yaml", "outfile.type.1"},
   150  		actionParams: actionGetTestMaps[1],
   151  	}, {
   152  		summary:      "a nonexistent inner key",
   153  		args:         []string{"--format", "json", "outfile.type.1"},
   154  		actionParams: actionGetTestMaps[1],
   155  		out:          "null\n",
   156  	}, {
   157  		summary:      "a map with a non-string key",
   158  		args:         []string{"outfile.type"},
   159  		actionParams: actionGetTestMaps[3],
   160  	}, {
   161  		summary:      "a map with a non-string key",
   162  		args:         []string{"--format", "yaml", "outfile.type"},
   163  		actionParams: actionGetTestMaps[3],
   164  	}, {
   165  		summary:      "a map with a non-string key",
   166  		args:         []string{"--format", "json", "outfile.type"},
   167  		actionParams: actionGetTestMaps[3],
   168  		out:          "null\n",
   169  	}, {
   170  		summary:      "a simple map of one value to one key",
   171  		args:         []string{},
   172  		actionParams: actionGetTestMaps[0],
   173  		out:          "outfile: foo.bz2\n",
   174  	}, {
   175  		summary:      "a simple map of one value to one key",
   176  		args:         []string{"--format", "yaml"},
   177  		actionParams: actionGetTestMaps[0],
   178  		out:          "outfile: foo.bz2\n",
   179  	}, {
   180  		summary:      "a simple map of one value to one key",
   181  		args:         []string{"--format", "json"},
   182  		actionParams: actionGetTestMaps[0],
   183  		out:          `{"outfile":"foo.bz2"}` + "\n",
   184  	}, {
   185  		summary:      "an entire map",
   186  		args:         []string{},
   187  		actionParams: actionGetTestMaps[2],
   188  		out: "outfile:\n" +
   189  			"  type:\n" +
   190  			"    \"1\": raw\n" +
   191  			"    \"2\": gzip\n" +
   192  			"    \"3\": bzip\n",
   193  	}, {
   194  		summary:      "an entire map",
   195  		args:         []string{"--format", "yaml"},
   196  		actionParams: actionGetTestMaps[2],
   197  		out: "outfile:\n" +
   198  			"  type:\n" +
   199  			"    \"1\": raw\n" +
   200  			"    \"2\": gzip\n" +
   201  			"    \"3\": bzip\n",
   202  	}, {
   203  		summary:      "an entire map",
   204  		args:         []string{"--format", "json"},
   205  		actionParams: actionGetTestMaps[2],
   206  		out:          `{"outfile":{"type":{"1":"raw","2":"gzip","3":"bzip"}}}` + "\n",
   207  	}, {
   208  		summary:      "an inner map value which is itself a map",
   209  		args:         []string{"outfile.type"},
   210  		actionParams: actionGetTestMaps[2],
   211  		out: "\"1\": raw\n" +
   212  			"\"2\": gzip\n" +
   213  			"\"3\": bzip\n",
   214  	}, {
   215  		summary:      "an inner map value which is itself a map",
   216  		args:         []string{"--format", "yaml", "outfile.type"},
   217  		actionParams: actionGetTestMaps[2],
   218  		out: "\"1\": raw\n" +
   219  			"\"2\": gzip\n" +
   220  			"\"3\": bzip\n",
   221  	}, {
   222  		summary:      "an inner map value which is itself a map",
   223  		args:         []string{"--format", "json", "outfile.type"},
   224  		actionParams: actionGetTestMaps[2],
   225  		out:          `{"1":"raw","2":"gzip","3":"bzip"}` + "\n",
   226  	}, {
   227  		summary:      "a map with an inner map keyed by interface{}",
   228  		args:         []string{"outfile.type"},
   229  		actionParams: actionGetTestMaps[4],
   230  		out: "\"1\": raw\n" +
   231  			"\"2\": gzip\n" +
   232  			"\"3\": bzip\n",
   233  	}, {
   234  		summary:      "a map with an inner map keyed by interface{}",
   235  		args:         []string{"--format", "yaml", "outfile.type"},
   236  		actionParams: actionGetTestMaps[4],
   237  		out: "\"1\": raw\n" +
   238  			"\"2\": gzip\n" +
   239  			"\"3\": bzip\n",
   240  	}, {
   241  		summary:      "a map with an inner map keyed by interface{}",
   242  		args:         []string{"--format", "json", "outfile.type"},
   243  		actionParams: actionGetTestMaps[4],
   244  		out:          `{"1":"raw","2":"gzip","3":"bzip"}` + "\n",
   245  	}, {
   246  		summary: "too many arguments",
   247  		args:    []string{"multiple", "keys"},
   248  		code:    2,
   249  		errMsg:  `unrecognized args: \["keys"\]`,
   250  	}}
   251  
   252  	for i, t := range actionGetTests {
   253  		c.Logf("test %d: %s\n args: %#v", i, t.summary, t.args)
   254  		hctx := &actionGetContext{}
   255  		hctx.actionParams = t.actionParams
   256  		com, err := jujuc.NewCommand(hctx, cmdString("action-get"))
   257  		c.Assert(err, jc.ErrorIsNil)
   258  		ctx := cmdtesting.Context(c)
   259  		code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, t.args)
   260  		c.Check(code, gc.Equals, t.code)
   261  		if code == 0 {
   262  			c.Check(bufferString(ctx.Stdout), gc.Equals, t.out)
   263  			c.Check(bufferString(ctx.Stderr), gc.Equals, "")
   264  		} else {
   265  			c.Check(bufferString(ctx.Stdout), gc.Equals, "")
   266  			expect := fmt.Sprintf(`(\n)*ERROR %s\n`, t.errMsg)
   267  			c.Check(bufferString(ctx.Stderr), gc.Matches, expect)
   268  		}
   269  	}
   270  }
   271  
   272  func (s *ActionGetSuite) TestHelp(c *gc.C) {
   273  	hctx := &actionGetContext{}
   274  	com, err := jujuc.NewCommand(hctx, cmdString("action-get"))
   275  	c.Assert(err, jc.ErrorIsNil)
   276  	ctx := cmdtesting.Context(c)
   277  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--help"})
   278  	c.Assert(code, gc.Equals, 0)
   279  	c.Assert(bufferString(ctx.Stdout), gc.Equals, `Usage: action-get [options] [<key>[.<key>.<key>...]]
   280  
   281  Summary:
   282  get action parameters
   283  
   284  Options:
   285  --format  (= smart)
   286      Specify output format (json|smart|yaml)
   287  -o, --output (= "")
   288      Specify an output file
   289  
   290  Details:
   291  action-get will print the value of the parameter at the given key, serialized
   292  as YAML.  If multiple keys are passed, action-get will recurse into the param
   293  map as needed.
   294  `)
   295  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   296  }