github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/cmd/cmdtesting"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/apiserver/common"
    14  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    15  	coretesting "github.com/juju/juju/testing"
    16  )
    17  
    18  type RemoveRelationSuite struct {
    19  	testing.IsolationSuite
    20  	mockAPI *mockRemoveAPI
    21  }
    22  
    23  func (s *RemoveRelationSuite) SetUpTest(c *gc.C) {
    24  	s.IsolationSuite.SetUpTest(c)
    25  	s.mockAPI = &mockRemoveAPI{Stub: &testing.Stub{}, version: 6}
    26  	s.mockAPI.removeRelationFunc = func(endpoints ...string) error {
    27  		return s.mockAPI.NextErr()
    28  	}
    29  }
    30  
    31  var _ = gc.Suite(&RemoveRelationSuite{})
    32  
    33  func (s *RemoveRelationSuite) runRemoveRelation(c *gc.C, args ...string) error {
    34  	store := jujuclienttesting.MinimalStore()
    35  	_, err := cmdtesting.RunCommand(c, NewRemoveRelationCommandForTest(s.mockAPI, store), args...)
    36  	return err
    37  }
    38  
    39  func (s *RemoveRelationSuite) TestRemoveRelationWrongNumberOfArguments(c *gc.C) {
    40  	// No arguments
    41  	err := s.runRemoveRelation(c)
    42  	c.Assert(err, gc.ErrorMatches, "a relation must involve two applications")
    43  
    44  	// 1 argument not an integer
    45  	err = s.runRemoveRelation(c, "application1")
    46  	c.Assert(err, gc.ErrorMatches, `relation ID "application1" not valid`)
    47  
    48  	// More than 2 arguments
    49  	err = s.runRemoveRelation(c, "application1", "application2", "application3")
    50  	c.Assert(err, gc.ErrorMatches, "a relation must involve two applications")
    51  }
    52  
    53  func (s *RemoveRelationSuite) TestRemoveRelationIdOldServer(c *gc.C) {
    54  	s.mockAPI.version = 4
    55  	err := s.runRemoveRelation(c, "123")
    56  	c.Assert(err, gc.ErrorMatches, "removing a relation using its ID is not supported by this version of Juju")
    57  	s.mockAPI.CheckCall(c, 0, "Close")
    58  }
    59  
    60  func (s *RemoveRelationSuite) TestRemoveRelationSuccess(c *gc.C) {
    61  	err := s.runRemoveRelation(c, "application1", "application2")
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	s.mockAPI.CheckCall(c, 0, "DestroyRelation", []string{"application1", "application2"})
    64  	s.mockAPI.CheckCall(c, 1, "Close")
    65  }
    66  
    67  func (s *RemoveRelationSuite) TestRemoveRelationIdSuccess(c *gc.C) {
    68  	err := s.runRemoveRelation(c, "123")
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	s.mockAPI.CheckCall(c, 0, "DestroyRelationId", 123)
    71  	s.mockAPI.CheckCall(c, 1, "Close")
    72  }
    73  
    74  func (s *RemoveRelationSuite) TestRemoveRelationFail(c *gc.C) {
    75  	msg := "fail remove-relation at API"
    76  	s.mockAPI.SetErrors(errors.New(msg))
    77  	err := s.runRemoveRelation(c, "application1", "application2")
    78  	c.Assert(err, gc.ErrorMatches, msg)
    79  	s.mockAPI.CheckCall(c, 0, "DestroyRelation", []string{"application1", "application2"})
    80  	s.mockAPI.CheckCall(c, 1, "Close")
    81  }
    82  
    83  func (s *RemoveRelationSuite) TestRemoveRelationBlocked(c *gc.C) {
    84  	s.mockAPI.SetErrors(common.OperationBlockedError("TestRemoveRelationBlocked"))
    85  	err := s.runRemoveRelation(c, "application1", "application2")
    86  	coretesting.AssertOperationWasBlocked(c, err, ".*TestRemoveRelationBlocked.*")
    87  	s.mockAPI.CheckCall(c, 0, "DestroyRelation", []string{"application1", "application2"})
    88  	s.mockAPI.CheckCall(c, 1, "Close")
    89  }
    90  
    91  type mockRemoveAPI struct {
    92  	*testing.Stub
    93  	version            int
    94  	removeRelationFunc func(endpoints ...string) error
    95  }
    96  
    97  func (s mockRemoveAPI) Close() error {
    98  	s.MethodCall(s, "Close")
    99  	return s.NextErr()
   100  }
   101  
   102  func (s mockRemoveAPI) DestroyRelation(endpoints ...string) error {
   103  	s.MethodCall(s, "DestroyRelation", endpoints)
   104  	return s.removeRelationFunc(endpoints...)
   105  }
   106  
   107  func (s mockRemoveAPI) DestroyRelationId(relationId int) error {
   108  	s.MethodCall(s, "DestroyRelationId", relationId)
   109  	return nil
   110  }
   111  
   112  func (s mockRemoveAPI) BestAPIVersion() int {
   113  	return s.version
   114  }