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