github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/addrelation_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/cmd/envcmd"
    11  	jujutesting "github.com/juju/juju/juju/testing"
    12  	"github.com/juju/juju/testcharms"
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  type AddRelationSuite struct {
    17  	jujutesting.RepoSuite
    18  	CmdBlockHelper
    19  }
    20  
    21  func (s *AddRelationSuite) SetUpTest(c *gc.C) {
    22  	s.RepoSuite.SetUpTest(c)
    23  	s.CmdBlockHelper = NewCmdBlockHelper(s.APIState)
    24  	c.Assert(s.CmdBlockHelper, gc.NotNil)
    25  	s.AddCleanup(func(*gc.C) { s.CmdBlockHelper.Close() })
    26  }
    27  
    28  var _ = gc.Suite(&AddRelationSuite{})
    29  
    30  func runAddRelation(c *gc.C, args ...string) error {
    31  	_, err := testing.RunCommand(c, envcmd.Wrap(&AddRelationCommand{}), args...)
    32  	return err
    33  }
    34  
    35  var msWpAlreadyExists = `cannot add relation "wp:db ms:server": relation already exists`
    36  var msLgAlreadyExists = `cannot add relation "lg:info ms:juju-info": relation already exists`
    37  var wpLgAlreadyExists = `cannot add relation "lg:logging-directory wp:logging-dir": relation already exists`
    38  var wpLgAlreadyExistsJuju = `cannot add relation "lg:info wp:juju-info": relation already exists`
    39  
    40  var addRelationTests = []struct {
    41  	args []string
    42  	err  string
    43  }{
    44  	{
    45  		args: []string{"rk", "ms"},
    46  		err:  "no relations found",
    47  	}, {
    48  		err: "a relation must involve two services",
    49  	}, {
    50  		args: []string{"rk"},
    51  		err:  "a relation must involve two services",
    52  	}, {
    53  		args: []string{"rk:ring"},
    54  		err:  "a relation must involve two services",
    55  	}, {
    56  		args: []string{"ping:pong", "tic:tac", "icki:wacki"},
    57  		err:  "a relation must involve two services",
    58  	},
    59  
    60  	// Add a real relation, and check various ways of failing to re-add it.
    61  	{
    62  		args: []string{"ms", "wp"},
    63  	}, {
    64  		args: []string{"ms", "wp"},
    65  		err:  msWpAlreadyExists,
    66  	}, {
    67  		args: []string{"wp", "ms"},
    68  		err:  msWpAlreadyExists,
    69  	}, {
    70  		args: []string{"ms", "wp:db"},
    71  		err:  msWpAlreadyExists,
    72  	}, {
    73  		args: []string{"ms:server", "wp"},
    74  		err:  msWpAlreadyExists,
    75  	}, {
    76  		args: []string{"ms:server", "wp:db"},
    77  		err:  msWpAlreadyExists,
    78  	},
    79  
    80  	// Add a real relation using an implicit endpoint.
    81  	{
    82  		args: []string{"ms", "lg"},
    83  	}, {
    84  		args: []string{"ms", "lg"},
    85  		err:  msLgAlreadyExists,
    86  	}, {
    87  		args: []string{"lg", "ms"},
    88  		err:  msLgAlreadyExists,
    89  	}, {
    90  		args: []string{"ms:juju-info", "lg"},
    91  		err:  msLgAlreadyExists,
    92  	}, {
    93  		args: []string{"ms", "lg:info"},
    94  		err:  msLgAlreadyExists,
    95  	}, {
    96  		args: []string{"ms:juju-info", "lg:info"},
    97  		err:  msLgAlreadyExists,
    98  	},
    99  
   100  	// Add a real relation using an explicit endpoint, avoiding the potential implicit one.
   101  	{
   102  		args: []string{"wp", "lg"},
   103  	}, {
   104  		args: []string{"wp", "lg"},
   105  		err:  wpLgAlreadyExists,
   106  	}, {
   107  		args: []string{"lg", "wp"},
   108  		err:  wpLgAlreadyExists,
   109  	}, {
   110  		args: []string{"wp:logging-dir", "lg"},
   111  		err:  wpLgAlreadyExists,
   112  	}, {
   113  		args: []string{"wp", "lg:logging-directory"},
   114  		err:  wpLgAlreadyExists,
   115  	}, {
   116  		args: []string{"wp:logging-dir", "lg:logging-directory"},
   117  		err:  wpLgAlreadyExists,
   118  	},
   119  
   120  	// Check we can still use the implicit endpoint if specified explicitly.
   121  	{
   122  		args: []string{"wp:juju-info", "lg"},
   123  	}, {
   124  		args: []string{"wp:juju-info", "lg"},
   125  		err:  wpLgAlreadyExistsJuju,
   126  	}, {
   127  		args: []string{"lg", "wp:juju-info"},
   128  		err:  wpLgAlreadyExistsJuju,
   129  	}, {
   130  		args: []string{"wp:juju-info", "lg"},
   131  		err:  wpLgAlreadyExistsJuju,
   132  	}, {
   133  		args: []string{"wp", "lg:info"},
   134  		err:  wpLgAlreadyExistsJuju,
   135  	}, {
   136  		args: []string{"wp:juju-info", "lg:info"},
   137  		err:  wpLgAlreadyExistsJuju,
   138  	},
   139  }
   140  
   141  func (s *AddRelationSuite) TestAddRelation(c *gc.C) {
   142  	testcharms.Repo.CharmArchivePath(s.SeriesPath, "wordpress")
   143  	err := runDeploy(c, "local:wordpress", "wp")
   144  	c.Assert(err, jc.ErrorIsNil)
   145  	testcharms.Repo.CharmArchivePath(s.SeriesPath, "mysql")
   146  	err = runDeploy(c, "local:mysql", "ms")
   147  	c.Assert(err, jc.ErrorIsNil)
   148  	testcharms.Repo.CharmArchivePath(s.SeriesPath, "riak")
   149  	err = runDeploy(c, "local:riak", "rk")
   150  	c.Assert(err, jc.ErrorIsNil)
   151  	testcharms.Repo.CharmArchivePath(s.SeriesPath, "logging")
   152  	err = runDeploy(c, "local:logging", "lg")
   153  	c.Assert(err, jc.ErrorIsNil)
   154  
   155  	for i, t := range addRelationTests {
   156  		c.Logf("test %d: %v", i, t.args)
   157  		err := runAddRelation(c, t.args...)
   158  		if t.err != "" {
   159  			c.Assert(err, gc.ErrorMatches, t.err)
   160  		}
   161  	}
   162  }
   163  
   164  func (s *AddRelationSuite) TestBlockAddRelation(c *gc.C) {
   165  	testcharms.Repo.CharmArchivePath(s.SeriesPath, "wordpress")
   166  	err := runDeploy(c, "local:wordpress", "wp")
   167  	c.Assert(err, jc.ErrorIsNil)
   168  	testcharms.Repo.CharmArchivePath(s.SeriesPath, "mysql")
   169  	err = runDeploy(c, "local:mysql", "ms")
   170  	c.Assert(err, jc.ErrorIsNil)
   171  	testcharms.Repo.CharmArchivePath(s.SeriesPath, "riak")
   172  	err = runDeploy(c, "local:riak", "rk")
   173  	c.Assert(err, jc.ErrorIsNil)
   174  	testcharms.Repo.CharmArchivePath(s.SeriesPath, "logging")
   175  	err = runDeploy(c, "local:logging", "lg")
   176  	c.Assert(err, jc.ErrorIsNil)
   177  
   178  	// Block operation
   179  	s.BlockAllChanges(c, "TestBlockAddRelation")
   180  
   181  	for i, t := range addRelationTests {
   182  		c.Logf("test %d: %v", i, t.args)
   183  		err := runAddRelation(c, t.args...)
   184  		if len(t.args) == 2 {
   185  			// Only worry about Run being blocked.
   186  			// For len(t.args) != 2, an Init will fail
   187  			s.AssertBlocked(c, err, ".*TestBlockAddRelation.*")
   188  		}
   189  	}
   190  }