1
// Copyright 2012, 2013 Canonical Ltd.
1
// Copyright 2012 Canonical Ltd.
2
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
7
"github.com/juju/errors"
8
"github.com/juju/testing"
8
9
jc "github.com/juju/testing/checkers"
9
10
gc "gopkg.in/check.v1"
11
jujutesting "github.com/juju/juju/juju/testing"
12
"github.com/juju/juju/rpc"
13
"github.com/juju/juju/testcharms"
14
"github.com/juju/juju/testing"
12
"github.com/juju/juju/apiserver/common"
13
coretesting "github.com/juju/juju/testing"
17
16
type RemoveRelationSuite struct {
19
testing.CmdBlockHelper
17
testing.IsolationSuite
18
mockAPI *mockRemoveAPI
22
21
func (s *RemoveRelationSuite) SetUpTest(c *gc.C) {
23
s.RepoSuite.SetUpTest(c)
24
s.CmdBlockHelper = testing.NewCmdBlockHelper(s.APIState)
25
c.Assert(s.CmdBlockHelper, gc.NotNil)
26
s.AddCleanup(func(*gc.C) { s.CmdBlockHelper.Close() })
22
s.IsolationSuite.SetUpTest(c)
23
s.mockAPI = &mockRemoveAPI{Stub: &testing.Stub{}}
24
s.mockAPI.removeRelationFunc = func(endpoints ...string) error {
25
return s.mockAPI.NextErr()
29
29
var _ = gc.Suite(&RemoveRelationSuite{})
31
func runRemoveRelation(c *gc.C, args ...string) error {
32
_, err := testing.RunCommand(c, NewRemoveRelationCommand(), args...)
31
func (s *RemoveRelationSuite) runRemoveRelation(c *gc.C, args ...string) error {
32
_, err := coretesting.RunCommand(c, NewRemoveRelationCommandForTest(s.mockAPI), args...)
36
func (s *RemoveRelationSuite) setupRelationForRemove(c *gc.C) {
37
ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "riak")
38
err := runDeploy(c, ch, "riak", "--series", "quantal")
39
c.Assert(err, jc.ErrorIsNil)
40
ch = testcharms.Repo.CharmArchivePath(s.CharmsPath, "logging")
41
err = runDeploy(c, ch, "logging", "--series", "quantal")
42
c.Assert(err, jc.ErrorIsNil)
43
runAddRelation(c, "riak", "logging")
46
func (s *RemoveRelationSuite) TestRemoveRelation(c *gc.C) {
47
s.setupRelationForRemove(c)
49
// Destroy a relation that exists.
50
err := runRemoveRelation(c, "logging", "riak")
51
c.Assert(err, jc.ErrorIsNil)
53
// Destroy a relation that used to exist.
54
err = runRemoveRelation(c, "riak", "logging")
55
c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
56
Message: `relation "logging:info riak:juju-info" not found`,
61
err = runRemoveRelation(c, "ping", "pong")
62
c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
63
Message: `application "ping" not found`,
66
err = runRemoveRelation(c, "riak")
67
c.Assert(err, gc.ErrorMatches, `a relation must involve two applications`)
70
func (s *RemoveRelationSuite) TestBlockRemoveRelation(c *gc.C) {
71
s.setupRelationForRemove(c)
74
s.BlockRemoveObject(c, "TestBlockRemoveRelation")
75
// Destroy a relation that exists.
76
err := runRemoveRelation(c, "logging", "riak")
77
s.AssertBlocked(c, err, ".*TestBlockRemoveRelation.*")
36
func (s *RemoveRelationSuite) TestRemoveRelationWrongNumberOfArguments(c *gc.C) {
38
err := s.runRemoveRelation(c)
39
c.Assert(err, gc.ErrorMatches, "a relation must involve two applications")
42
err = s.runRemoveRelation(c, "application1")
43
c.Assert(err, gc.ErrorMatches, "a relation must involve two applications")
45
// More than 2 arguments
46
err = s.runRemoveRelation(c, "application1", "application2", "application3")
47
c.Assert(err, gc.ErrorMatches, "a relation must involve two applications")
50
func (s *RemoveRelationSuite) TestRemoveRelationSuccess(c *gc.C) {
51
err := s.runRemoveRelation(c, "application1", "application2")
52
c.Assert(err, jc.ErrorIsNil)
53
s.mockAPI.CheckCall(c, 0, "DestroyRelation", []string{"application1", "application2"})
54
s.mockAPI.CheckCall(c, 1, "Close")
57
func (s *RemoveRelationSuite) TestRemoveRelationFail(c *gc.C) {
58
msg := "fail remove-relation at API"
59
s.mockAPI.SetErrors(errors.New(msg))
60
err := s.runRemoveRelation(c, "application1", "application2")
61
c.Assert(err, gc.ErrorMatches, msg)
62
s.mockAPI.CheckCall(c, 0, "DestroyRelation", []string{"application1", "application2"})
63
s.mockAPI.CheckCall(c, 1, "Close")
66
func (s *RemoveRelationSuite) TestRemoveRelationBlocked(c *gc.C) {
67
s.mockAPI.SetErrors(common.OperationBlockedError("TestRemoveRelationBlocked"))
68
err := s.runRemoveRelation(c, "application1", "application2")
69
coretesting.AssertOperationWasBlocked(c, err, ".*TestRemoveRelationBlocked.*")
70
s.mockAPI.CheckCall(c, 0, "DestroyRelation", []string{"application1", "application2"})
71
s.mockAPI.CheckCall(c, 1, "Close")
74
type mockRemoveAPI struct {
76
removeRelationFunc func(endpoints ...string) error
79
func (s mockRemoveAPI) Close() error {
80
s.MethodCall(s, "Close")
84
func (s mockRemoveAPI) DestroyRelation(endpoints ...string) error {
85
s.MethodCall(s, "DestroyRelation", endpoints)
86
return s.removeRelationFunc(endpoints...)