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