github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/service/addrelation_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package service
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/cmd/juju/common"
    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  	common.CmdBlockHelper
    19  }
    20  
    21  func (s *AddRelationSuite) SetUpTest(c *gc.C) {
    22  	s.RepoSuite.SetUpTest(c)
    23  	s.CmdBlockHelper = common.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, NewAddRelationCommand(), 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  	ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "wordpress")
   143  	err := runDeploy(c, ch, "wp", "--series", "quantal")
   144  	c.Assert(err, jc.ErrorIsNil)
   145  	ch = testcharms.Repo.CharmArchivePath(s.CharmsPath, "mysql")
   146  	err = runDeploy(c, ch, "ms", "--series", "quantal")
   147  	c.Assert(err, jc.ErrorIsNil)
   148  	ch = testcharms.Repo.CharmArchivePath(s.CharmsPath, "riak")
   149  	err = runDeploy(c, ch, "rk", "--series", "quantal")
   150  	c.Assert(err, jc.ErrorIsNil)
   151  	ch = testcharms.Repo.CharmArchivePath(s.CharmsPath, "logging")
   152  	err = runDeploy(c, ch, "lg", "--series", "quantal")
   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  	ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "wordpress")
   166  	err := runDeploy(c, ch, "wp", "--series", "quantal")
   167  	c.Assert(err, jc.ErrorIsNil)
   168  	ch = testcharms.Repo.CharmArchivePath(s.CharmsPath, "mysql")
   169  	err = runDeploy(c, ch, "ms", "--series", "quantal")
   170  	c.Assert(err, jc.ErrorIsNil)
   171  	ch = testcharms.Repo.CharmArchivePath(s.CharmsPath, "riak")
   172  	err = runDeploy(c, ch, "rk", "--series", "quantal")
   173  	c.Assert(err, jc.ErrorIsNil)
   174  	ch = testcharms.Repo.CharmArchivePath(s.CharmsPath, "logging")
   175  	err = runDeploy(c, ch, "lg", "--series", "quantal")
   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  }