~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/application/removerelation_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2012, 2013 Canonical Ltd.
 
1
// Copyright 2012 Canonical Ltd.
2
2
// Licensed under the AGPLv3, see LICENCE file for details.
3
3
 
4
4
package application
5
5
 
6
6
import (
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"
10
11
 
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"
15
14
)
16
15
 
17
16
type RemoveRelationSuite struct {
18
 
        jujutesting.RepoSuite
19
 
        testing.CmdBlockHelper
 
17
        testing.IsolationSuite
 
18
        mockAPI *mockRemoveAPI
20
19
}
21
20
 
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()
 
26
        }
27
27
}
28
28
 
29
29
var _ = gc.Suite(&RemoveRelationSuite{})
30
30
 
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...)
33
33
        return err
34
34
}
35
35
 
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")
44
 
}
45
 
 
46
 
func (s *RemoveRelationSuite) TestRemoveRelation(c *gc.C) {
47
 
        s.setupRelationForRemove(c)
48
 
 
49
 
        // Destroy a relation that exists.
50
 
        err := runRemoveRelation(c, "logging", "riak")
51
 
        c.Assert(err, jc.ErrorIsNil)
52
 
 
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`,
57
 
                Code:    "not found",
58
 
        })
59
 
 
60
 
        // Invalid removes.
61
 
        err = runRemoveRelation(c, "ping", "pong")
62
 
        c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
63
 
                Message: `application "ping" not found`,
64
 
                Code:    "not found",
65
 
        })
66
 
        err = runRemoveRelation(c, "riak")
67
 
        c.Assert(err, gc.ErrorMatches, `a relation must involve two applications`)
68
 
}
69
 
 
70
 
func (s *RemoveRelationSuite) TestBlockRemoveRelation(c *gc.C) {
71
 
        s.setupRelationForRemove(c)
72
 
 
73
 
        // block operation
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) {
 
37
        // No arguments
 
38
        err := s.runRemoveRelation(c)
 
39
        c.Assert(err, gc.ErrorMatches, "a relation must involve two applications")
 
40
 
 
41
        // 1 argument
 
42
        err = s.runRemoveRelation(c, "application1")
 
43
        c.Assert(err, gc.ErrorMatches, "a relation must involve two applications")
 
44
 
 
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")
 
48
}
 
49
 
 
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")
 
55
}
 
56
 
 
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")
 
64
}
 
65
 
 
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")
 
72
}
 
73
 
 
74
type mockRemoveAPI struct {
 
75
        *testing.Stub
 
76
        removeRelationFunc func(endpoints ...string) error
 
77
}
 
78
 
 
79
func (s mockRemoveAPI) Close() error {
 
80
        s.MethodCall(s, "Close")
 
81
        return s.NextErr()
 
82
}
 
83
 
 
84
func (s mockRemoveAPI) DestroyRelation(endpoints ...string) error {
 
85
        s.MethodCall(s, "DestroyRelation", endpoints)
 
86
        return s.removeRelationFunc(endpoints...)
78
87
}