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