github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/application/removerelation_test.go (about) 1 // Copyright 2012 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package application 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/apiserver/common" 13 coretesting "github.com/juju/juju/testing" 14 ) 15 16 type RemoveRelationSuite struct { 17 testing.IsolationSuite 18 mockAPI *mockRemoveAPI 19 } 20 21 func (s *RemoveRelationSuite) SetUpTest(c *gc.C) { 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 } 28 29 var _ = gc.Suite(&RemoveRelationSuite{}) 30 31 func (s *RemoveRelationSuite) runRemoveRelation(c *gc.C, args ...string) error { 32 _, err := coretesting.RunCommand(c, NewRemoveRelationCommandForTest(s.mockAPI), args...) 33 return err 34 } 35 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...) 87 }