launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/worker/uniter/jujuc/relation-set_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  	gc "launchpad.net/gocheck"
     9  
    10  	"launchpad.net/juju-core/cmd"
    11  	"launchpad.net/juju-core/testing"
    12  	"launchpad.net/juju-core/worker/uniter/jujuc"
    13  )
    14  
    15  type RelationSetSuite struct {
    16  	ContextSuite
    17  }
    18  
    19  var _ = gc.Suite(&RelationSetSuite{})
    20  
    21  var helpTests = []struct {
    22  	relid  int
    23  	expect string
    24  }{{-1, ""}, {0, "peer0:0"}}
    25  
    26  func (s *RelationSetSuite) TestHelp(c *gc.C) {
    27  	for i, t := range helpTests {
    28  		c.Logf("test %d", i)
    29  		hctx := s.GetHookContext(c, t.relid, "")
    30  		com, err := jujuc.NewCommand(hctx, "relation-set")
    31  		c.Assert(err, gc.IsNil)
    32  		ctx := testing.Context(c)
    33  		code := cmd.Main(com, ctx, []string{"--help"})
    34  		c.Assert(code, gc.Equals, 0)
    35  		c.Assert(bufferString(ctx.Stdout), gc.Equals, fmt.Sprintf(`
    36  usage: relation-set [options] key=value [key=value ...]
    37  purpose: set relation settings
    38  
    39  options:
    40  --format (= "")
    41      deprecated format flag
    42  -r  (= %s)
    43      specify a relation by id
    44  `[1:], t.expect))
    45  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    46  	}
    47  }
    48  
    49  var relationSetInitTests = []struct {
    50  	ctxrelid int
    51  	args     []string
    52  	err      string
    53  	relid    int
    54  	settings map[string]string
    55  }{
    56  	{
    57  	// compatibility: 0 args is valid.
    58  	}, {
    59  		ctxrelid: -1,
    60  		err:      `no relation id specified`,
    61  	}, {
    62  		ctxrelid: -1,
    63  		args:     []string{"-r", "one"},
    64  		err:      `invalid value "one" for flag -r: invalid relation id`,
    65  	}, {
    66  		ctxrelid: 1,
    67  		args:     []string{"-r", "one"},
    68  		err:      `invalid value "one" for flag -r: invalid relation id`,
    69  	}, {
    70  		ctxrelid: -1,
    71  		args:     []string{"-r", "ignored:one"},
    72  		err:      `invalid value "ignored:one" for flag -r: invalid relation id`,
    73  	}, {
    74  		ctxrelid: 1,
    75  		args:     []string{"-r", "ignored:one"},
    76  		err:      `invalid value "ignored:one" for flag -r: invalid relation id`,
    77  	}, {
    78  		ctxrelid: -1,
    79  		args:     []string{"-r", "2"},
    80  		err:      `invalid value "2" for flag -r: unknown relation id`,
    81  	}, {
    82  		ctxrelid: 1,
    83  		args:     []string{"-r", "ignored:2"},
    84  		err:      `invalid value "ignored:2" for flag -r: unknown relation id`,
    85  	}, {
    86  		ctxrelid: -1,
    87  		err:      `no relation id specified`,
    88  	}, {
    89  		ctxrelid: 1,
    90  		args:     []string{"-r", "ignored:0"},
    91  		relid:    0,
    92  	}, {
    93  		ctxrelid: 1,
    94  		args:     []string{"-r", "0"},
    95  		relid:    0,
    96  	}, {
    97  		ctxrelid: -1,
    98  		args:     []string{"-r", "1"},
    99  		relid:    1,
   100  	}, {
   101  		ctxrelid: 0,
   102  		args:     []string{"-r", "1"},
   103  		relid:    1,
   104  	}, {
   105  		ctxrelid: 1,
   106  		args:     []string{"haha"},
   107  		err:      `expected "key=value", got "haha"`,
   108  	}, {
   109  		ctxrelid: 1,
   110  		args:     []string{"=haha"},
   111  		err:      `expected "key=value", got "=haha"`,
   112  	}, {
   113  		ctxrelid: 1,
   114  		args:     []string{"foo="},
   115  		relid:    1,
   116  		settings: map[string]string{"foo": ""},
   117  	}, {
   118  		ctxrelid: 1,
   119  		args:     []string{"foo='"},
   120  		relid:    1,
   121  		settings: map[string]string{"foo": "'"},
   122  	}, {
   123  		ctxrelid: 1,
   124  		args:     []string{"foo=bar"},
   125  		relid:    1,
   126  		settings: map[string]string{"foo": "bar"},
   127  	}, {
   128  		ctxrelid: 1,
   129  		args:     []string{"foo=bar=baz=qux"},
   130  		relid:    1,
   131  		settings: map[string]string{"foo": "bar=baz=qux"},
   132  	}, {
   133  		ctxrelid: 1,
   134  		args:     []string{"foo=foo: bar"},
   135  		relid:    1,
   136  		settings: map[string]string{"foo": "foo: bar"},
   137  	}, {
   138  		ctxrelid: 0,
   139  		args:     []string{"-r", "1", "foo=bar"},
   140  		relid:    1,
   141  		settings: map[string]string{"foo": "bar"},
   142  	}, {
   143  		ctxrelid: 1,
   144  		args:     []string{"foo=123", "bar=true", "baz=4.5", "qux="},
   145  		relid:    1,
   146  		settings: map[string]string{"foo": "123", "bar": "true", "baz": "4.5", "qux": ""},
   147  	},
   148  }
   149  
   150  func (s *RelationSetSuite) TestInit(c *gc.C) {
   151  	for i, t := range relationSetInitTests {
   152  		c.Logf("test %d", i)
   153  		hctx := s.GetHookContext(c, t.ctxrelid, "")
   154  		com, err := jujuc.NewCommand(hctx, "relation-set")
   155  		c.Assert(err, gc.IsNil)
   156  		err = testing.InitCommand(com, t.args)
   157  		if t.err == "" {
   158  			c.Assert(err, gc.IsNil)
   159  			rset := com.(*jujuc.RelationSetCommand)
   160  			c.Assert(rset.RelationId, gc.Equals, t.relid)
   161  			settings := t.settings
   162  			if settings == nil {
   163  				settings = map[string]string{}
   164  			}
   165  			c.Assert(rset.Settings, gc.DeepEquals, settings)
   166  		} else {
   167  			c.Assert(err, gc.ErrorMatches, t.err)
   168  		}
   169  	}
   170  }
   171  
   172  // Tests start with a relation with the settings {"base": "value"}
   173  var relationSetRunTests = []struct {
   174  	change map[string]string
   175  	expect Settings
   176  }{
   177  	{
   178  		map[string]string{"base": ""},
   179  		Settings{},
   180  	}, {
   181  		map[string]string{"foo": "bar"},
   182  		Settings{"base": "value", "foo": "bar"},
   183  	}, {
   184  		map[string]string{"base": "changed"},
   185  		Settings{"base": "changed"},
   186  	},
   187  }
   188  
   189  func (s *RelationSetSuite) TestRun(c *gc.C) {
   190  	hctx := s.GetHookContext(c, 0, "")
   191  	for i, t := range relationSetRunTests {
   192  		c.Logf("test %d", i)
   193  
   194  		pristine := Settings{"pristine": "untouched"}
   195  		hctx.rels[0].units["u/0"] = pristine
   196  		basic := Settings{"base": "value"}
   197  		hctx.rels[1].units["u/0"] = basic
   198  
   199  		// Run the command.
   200  		com, err := jujuc.NewCommand(hctx, "relation-set")
   201  		c.Assert(err, gc.IsNil)
   202  		rset := com.(*jujuc.RelationSetCommand)
   203  		rset.RelationId = 1
   204  		rset.Settings = t.change
   205  		ctx := testing.Context(c)
   206  		err = com.Run(ctx)
   207  		c.Assert(err, gc.IsNil)
   208  
   209  		// Check changes.
   210  		c.Assert(hctx.rels[0].units["u/0"], gc.DeepEquals, pristine)
   211  		c.Assert(hctx.rels[1].units["u/0"], gc.DeepEquals, t.expect)
   212  	}
   213  }
   214  
   215  func (s *RelationSetSuite) TestRunDeprecationWarning(c *gc.C) {
   216  	hctx := s.GetHookContext(c, 0, "")
   217  	com, _ := jujuc.NewCommand(hctx, "relation-set")
   218  	// The rel= is needed to make this a valid command.
   219  	ctx, err := testing.RunCommand(c, com, []string{"--format", "foo", "rel="})
   220  
   221  	c.Assert(err, gc.IsNil)
   222  	c.Assert(testing.Stdout(ctx), gc.Equals, "")
   223  	c.Assert(testing.Stderr(ctx), gc.Equals, "--format flag deprecated for command \"relation-set\"")
   224  }