github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/cmd/juju/addrelation_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	gc "launchpad.net/gocheck"
     8  
     9  	jujutesting "launchpad.net/juju-core/juju/testing"
    10  	"launchpad.net/juju-core/testing"
    11  )
    12  
    13  type AddRelationSuite struct {
    14  	jujutesting.RepoSuite
    15  }
    16  
    17  var _ = gc.Suite(&AddRelationSuite{})
    18  
    19  func runAddRelation(c *gc.C, args ...string) error {
    20  	_, err := testing.RunCommand(c, &AddRelationCommand{}, args)
    21  	return err
    22  }
    23  
    24  var msWpAlreadyExists = `cannot add relation "wp:db ms:server": relation already exists`
    25  var msLgAlreadyExists = `cannot add relation "lg:info ms:juju-info": relation already exists`
    26  var wpLgAlreadyExists = `cannot add relation "lg:logging-directory wp:logging-dir": relation already exists`
    27  var wpLgAlreadyExistsJuju = `cannot add relation "lg:info wp:juju-info": relation already exists`
    28  
    29  var addRelationTests = []struct {
    30  	args []string
    31  	err  string
    32  }{
    33  	{
    34  		args: []string{"rk", "ms"},
    35  		err:  "no relations found",
    36  	}, {
    37  		err: "a relation must involve two services",
    38  	}, {
    39  		args: []string{"rk"},
    40  		err:  "a relation must involve two services",
    41  	}, {
    42  		args: []string{"rk:ring"},
    43  		err:  "a relation must involve two services",
    44  	}, {
    45  		args: []string{"ping:pong", "tic:tac", "icki:wacki"},
    46  		err:  "a relation must involve two services",
    47  	},
    48  
    49  	// Add a real relation, and check various ways of failing to re-add it.
    50  	{
    51  		args: []string{"ms", "wp"},
    52  	}, {
    53  		args: []string{"ms", "wp"},
    54  		err:  msWpAlreadyExists,
    55  	}, {
    56  		args: []string{"wp", "ms"},
    57  		err:  msWpAlreadyExists,
    58  	}, {
    59  		args: []string{"ms", "wp:db"},
    60  		err:  msWpAlreadyExists,
    61  	}, {
    62  		args: []string{"ms:server", "wp"},
    63  		err:  msWpAlreadyExists,
    64  	}, {
    65  		args: []string{"ms:server", "wp:db"},
    66  		err:  msWpAlreadyExists,
    67  	},
    68  
    69  	// Add a real relation using an implicit endpoint.
    70  	{
    71  		args: []string{"ms", "lg"},
    72  	}, {
    73  		args: []string{"ms", "lg"},
    74  		err:  msLgAlreadyExists,
    75  	}, {
    76  		args: []string{"lg", "ms"},
    77  		err:  msLgAlreadyExists,
    78  	}, {
    79  		args: []string{"ms:juju-info", "lg"},
    80  		err:  msLgAlreadyExists,
    81  	}, {
    82  		args: []string{"ms", "lg:info"},
    83  		err:  msLgAlreadyExists,
    84  	}, {
    85  		args: []string{"ms:juju-info", "lg:info"},
    86  		err:  msLgAlreadyExists,
    87  	},
    88  
    89  	// Add a real relation using an explicit endpoint, avoiding the potential implicit one.
    90  	{
    91  		args: []string{"wp", "lg"},
    92  	}, {
    93  		args: []string{"wp", "lg"},
    94  		err:  wpLgAlreadyExists,
    95  	}, {
    96  		args: []string{"lg", "wp"},
    97  		err:  wpLgAlreadyExists,
    98  	}, {
    99  		args: []string{"wp:logging-dir", "lg"},
   100  		err:  wpLgAlreadyExists,
   101  	}, {
   102  		args: []string{"wp", "lg:logging-directory"},
   103  		err:  wpLgAlreadyExists,
   104  	}, {
   105  		args: []string{"wp:logging-dir", "lg:logging-directory"},
   106  		err:  wpLgAlreadyExists,
   107  	},
   108  
   109  	// Check we can still use the implicit endpoint if specified explicitly.
   110  	{
   111  		args: []string{"wp:juju-info", "lg"},
   112  	}, {
   113  		args: []string{"wp:juju-info", "lg"},
   114  		err:  wpLgAlreadyExistsJuju,
   115  	}, {
   116  		args: []string{"lg", "wp:juju-info"},
   117  		err:  wpLgAlreadyExistsJuju,
   118  	}, {
   119  		args: []string{"wp:juju-info", "lg"},
   120  		err:  wpLgAlreadyExistsJuju,
   121  	}, {
   122  		args: []string{"wp", "lg:info"},
   123  		err:  wpLgAlreadyExistsJuju,
   124  	}, {
   125  		args: []string{"wp:juju-info", "lg:info"},
   126  		err:  wpLgAlreadyExistsJuju,
   127  	},
   128  }
   129  
   130  func (s *AddRelationSuite) TestAddRelation(c *gc.C) {
   131  	testing.Charms.BundlePath(s.SeriesPath, "wordpress")
   132  	err := runDeploy(c, "local:wordpress", "wp")
   133  	c.Assert(err, gc.IsNil)
   134  	testing.Charms.BundlePath(s.SeriesPath, "mysql")
   135  	err = runDeploy(c, "local:mysql", "ms")
   136  	c.Assert(err, gc.IsNil)
   137  	testing.Charms.BundlePath(s.SeriesPath, "riak")
   138  	err = runDeploy(c, "local:riak", "rk")
   139  	c.Assert(err, gc.IsNil)
   140  	testing.Charms.BundlePath(s.SeriesPath, "logging")
   141  	err = runDeploy(c, "local:logging", "lg")
   142  	c.Assert(err, gc.IsNil)
   143  
   144  	for i, t := range addRelationTests {
   145  		c.Logf("test %d: %v", i, t.args)
   146  		err := runAddRelation(c, t.args...)
   147  		if t.err != "" {
   148  			c.Assert(err, gc.ErrorMatches, t.err)
   149  		}
   150  	}
   151  }