github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/runner/jujuc/relation-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  	"fmt"
     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  
    16  	"github.com/juju/juju/testing"
    17  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    18  )
    19  
    20  type RelationGetSuite struct {
    21  	ContextSuite
    22  }
    23  
    24  var _ = gc.Suite(&RelationGetSuite{})
    25  
    26  func (s *RelationGetSuite) SetUpTest(c *gc.C) {
    27  	s.ContextSuite.SetUpTest(c)
    28  	s.rels[0].units["u/0"]["private-address"] = "foo: bar\n"
    29  	s.rels[1].units["m/0"] = Settings{"pew": "pew\npew\n"}
    30  	s.rels[1].units["u/1"] = Settings{"value": "12345"}
    31  }
    32  
    33  var relationGetTests = []struct {
    34  	summary  string
    35  	relid    int
    36  	unit     string
    37  	args     []string
    38  	code     int
    39  	out      string
    40  	checkctx func(*gc.C, *cmd.Context)
    41  }{
    42  	{
    43  		summary: "no default relation",
    44  		relid:   -1,
    45  		code:    2,
    46  		out:     `no relation id specified`,
    47  	}, {
    48  		summary: "explicit relation, not known",
    49  		relid:   -1,
    50  		code:    2,
    51  		args:    []string{"-r", "burble:123"},
    52  		out:     `invalid value "burble:123" for flag -r: unknown relation id`,
    53  	}, {
    54  		summary: "default relation, no unit chosen",
    55  		relid:   1,
    56  		code:    2,
    57  		out:     `no unit id specified`,
    58  	}, {
    59  		summary: "explicit relation, no unit chosen",
    60  		relid:   -1,
    61  		code:    2,
    62  		args:    []string{"-r", "burble:1"},
    63  		out:     `no unit id specified`,
    64  	}, {
    65  		summary: "missing key",
    66  		relid:   1,
    67  		unit:    "m/0",
    68  		args:    []string{"ker-plunk"},
    69  	}, {
    70  		summary: "missing unit",
    71  		relid:   1,
    72  		unit:    "bad/0",
    73  		code:    1,
    74  		out:     `unknown unit bad/0`,
    75  	}, {
    76  		summary: "all keys with explicit non-member",
    77  		relid:   1,
    78  		args:    []string{"-", "u/1"},
    79  		out:     `value: "12345"`,
    80  	}, {
    81  		summary: "specific key with implicit member",
    82  		relid:   1,
    83  		unit:    "m/0",
    84  		args:    []string{"pew"},
    85  		out:     "pew\npew\n",
    86  	}, {
    87  		summary: "specific key with explicit member",
    88  		relid:   1,
    89  		args:    []string{"pew", "m/0"},
    90  		out:     "pew\npew\n",
    91  	}, {
    92  		summary: "specific key with explicit non-member",
    93  		relid:   1,
    94  		args:    []string{"value", "u/1"},
    95  		out:     "12345",
    96  	}, {
    97  		summary: "specific key with explicit local",
    98  		relid:   0,
    99  		args:    []string{"private-address", "u/0"},
   100  		out:     "foo: bar\n",
   101  	}, {
   102  		summary: "all keys with implicit member",
   103  		relid:   1,
   104  		unit:    "m/0",
   105  		out:     "pew: |\n  pew\n  pew",
   106  	}, {
   107  		summary: "all keys with explicit member",
   108  		relid:   1,
   109  		args:    []string{"-", "m/0"},
   110  		out:     "pew: |\n  pew\n  pew",
   111  	}, {
   112  		summary: "all keys with explicit local",
   113  		relid:   0,
   114  		args:    []string{"-", "u/0"},
   115  		out:     "private-address: |\n  foo: bar",
   116  	}, {
   117  		summary: "explicit smart formatting 1",
   118  		relid:   1,
   119  		unit:    "m/0",
   120  		args:    []string{"--format", "smart"},
   121  		out:     "pew: |\n  pew\n  pew",
   122  	}, {
   123  		summary: "explicit smart formatting 2",
   124  		relid:   1,
   125  		unit:    "m/0",
   126  		args:    []string{"pew", "--format", "smart"},
   127  		out:     "pew\npew\n",
   128  	}, {
   129  		summary: "explicit smart formatting 3",
   130  		relid:   1,
   131  		args:    []string{"value", "u/1", "--format", "smart"},
   132  		out:     "12345",
   133  	}, {
   134  		summary: "explicit smart formatting 4",
   135  		relid:   1,
   136  		args:    []string{"missing", "u/1", "--format", "smart"},
   137  		out:     "",
   138  	},
   139  }
   140  
   141  func (s *RelationGetSuite) TestRelationGet(c *gc.C) {
   142  	for i, t := range relationGetTests {
   143  		c.Logf("test %d: %s", i, t.summary)
   144  		hctx := s.GetHookContext(c, t.relid, t.unit)
   145  		com, err := jujuc.NewCommand(hctx, cmdString("relation-get"))
   146  		c.Assert(err, jc.ErrorIsNil)
   147  		ctx := testing.Context(c)
   148  		code := cmd.Main(com, ctx, t.args)
   149  		c.Check(code, gc.Equals, t.code)
   150  		if code == 0 {
   151  			c.Check(bufferString(ctx.Stderr), gc.Equals, "")
   152  			expect := t.out
   153  			if expect != "" {
   154  				expect = expect + "\n"
   155  			}
   156  			c.Check(bufferString(ctx.Stdout), gc.Equals, expect)
   157  		} else {
   158  			c.Check(bufferString(ctx.Stdout), gc.Equals, "")
   159  			expect := fmt.Sprintf(`(.|\n)*error: %s\n`, t.out)
   160  			c.Check(bufferString(ctx.Stderr), gc.Matches, expect)
   161  		}
   162  	}
   163  }
   164  
   165  var relationGetFormatTests = []struct {
   166  	summary string
   167  	relid   int
   168  	unit    string
   169  	args    []string
   170  	out     interface{}
   171  }{
   172  	{
   173  		summary: "formatting 1",
   174  		relid:   1,
   175  		unit:    "m/0",
   176  		out:     map[string]interface{}{"pew": "pew\npew\n"},
   177  	}, {
   178  		summary: "formatting 2",
   179  		relid:   1,
   180  		unit:    "m/0",
   181  		args:    []string{"pew"},
   182  		out:     "pew\npew\n",
   183  	}, {
   184  		summary: "formatting 3",
   185  		relid:   1,
   186  		args:    []string{"value", "u/1"},
   187  		out:     "12345",
   188  	}, {
   189  		summary: "formatting 4",
   190  		relid:   1,
   191  		args:    []string{"missing", "u/1"},
   192  		out:     nil,
   193  	},
   194  }
   195  
   196  func (s *RelationGetSuite) TestRelationGetFormat(c *gc.C) {
   197  	testFormat := func(format string, checker gc.Checker) {
   198  		for i, t := range relationGetFormatTests {
   199  			c.Logf("test %d: %s %s", i, format, t.summary)
   200  			hctx := s.GetHookContext(c, t.relid, t.unit)
   201  			com, err := jujuc.NewCommand(hctx, cmdString("relation-get"))
   202  			c.Assert(err, jc.ErrorIsNil)
   203  			ctx := testing.Context(c)
   204  			args := append(t.args, "--format", format)
   205  			code := cmd.Main(com, ctx, args)
   206  			c.Check(code, gc.Equals, 0)
   207  			c.Check(bufferString(ctx.Stderr), gc.Equals, "")
   208  			stdout := bufferString(ctx.Stdout)
   209  			c.Check(stdout, checker, t.out)
   210  		}
   211  	}
   212  	testFormat("yaml", jc.YAMLEquals)
   213  	testFormat("json", jc.JSONEquals)
   214  }
   215  
   216  var helpTemplate = `
   217  usage: %s
   218  purpose: get relation settings
   219  
   220  options:
   221  --format  (= smart)
   222      specify output format (json|smart|yaml)
   223  -o, --output (= "")
   224      specify an output file
   225  -r, --relation  (= %s)
   226      specify a relation by id
   227  
   228  relation-get prints the value of a unit's relation setting, specified by key.
   229  If no key is given, or if the key is "-", all keys and values will be printed.
   230  %s`[1:]
   231  
   232  var relationGetHelpTests = []struct {
   233  	summary string
   234  	relid   int
   235  	unit    string
   236  	usage   string
   237  	rel     string
   238  }{
   239  	{
   240  		summary: "no default relation",
   241  		relid:   -1,
   242  		usage:   "relation-get [options] <key> <unit id>",
   243  	}, {
   244  		summary: "no default unit",
   245  		relid:   1,
   246  		usage:   "relation-get [options] <key> <unit id>",
   247  		rel:     "peer1:1",
   248  	}, {
   249  		summary: "default unit",
   250  		relid:   1,
   251  		unit:    "any/1",
   252  		usage:   `relation-get [options] [<key> [<unit id>]]`,
   253  		rel:     "peer1:1",
   254  	},
   255  }
   256  
   257  func (s *RelationGetSuite) TestHelp(c *gc.C) {
   258  	for i, t := range relationGetHelpTests {
   259  		c.Logf("test %d", i)
   260  		hctx := s.GetHookContext(c, t.relid, t.unit)
   261  		com, err := jujuc.NewCommand(hctx, cmdString("relation-get"))
   262  		c.Assert(err, jc.ErrorIsNil)
   263  		ctx := testing.Context(c)
   264  		code := cmd.Main(com, ctx, []string{"--help"})
   265  		c.Assert(code, gc.Equals, 0)
   266  		unitHelp := ""
   267  		if t.unit != "" {
   268  			unitHelp = fmt.Sprintf("Current default unit id is %q.\n", t.unit)
   269  		}
   270  		expect := fmt.Sprintf(helpTemplate, t.usage, t.rel, unitHelp)
   271  		c.Assert(bufferString(ctx.Stdout), gc.Equals, expect)
   272  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   273  	}
   274  }
   275  
   276  func (s *RelationGetSuite) TestOutputPath(c *gc.C) {
   277  	hctx := s.GetHookContext(c, 1, "m/0")
   278  	com, err := jujuc.NewCommand(hctx, cmdString("relation-get"))
   279  	c.Assert(err, jc.ErrorIsNil)
   280  	ctx := testing.Context(c)
   281  	code := cmd.Main(com, ctx, []string{"--output", "some-file", "pew"})
   282  	c.Assert(code, gc.Equals, 0)
   283  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   284  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
   285  	content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file"))
   286  	c.Assert(err, jc.ErrorIsNil)
   287  	c.Assert(string(content), gc.Equals, "pew\npew\n\n")
   288  }