github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/application/addrelation_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  	"strings"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/testing"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/apiserver/common"
    15  	"github.com/juju/juju/apiserver/params"
    16  	coretesting "github.com/juju/juju/testing"
    17  )
    18  
    19  type AddRelationSuite struct {
    20  	testing.IsolationSuite
    21  	mockAPI *mockAddAPI
    22  }
    23  
    24  func (s *AddRelationSuite) SetUpTest(c *gc.C) {
    25  	s.IsolationSuite.SetUpTest(c)
    26  	s.mockAPI = &mockAddAPI{Stub: &testing.Stub{}}
    27  	s.mockAPI.addRelationFunc = func(endpoints ...string) (*params.AddRelationResults, error) {
    28  		// At the moment, cmd implementation ignores the return values,
    29  		// so nil is an acceptable return for testing purposes.
    30  		return nil, s.mockAPI.NextErr()
    31  	}
    32  }
    33  
    34  var _ = gc.Suite(&AddRelationSuite{})
    35  
    36  func (s *AddRelationSuite) runAddRelation(c *gc.C, args ...string) error {
    37  	_, err := coretesting.RunCommand(c, NewAddRelationCommandForTest(s.mockAPI), args...)
    38  	return err
    39  }
    40  
    41  func (s *AddRelationSuite) TestAddRelationWrongNumberOfArguments(c *gc.C) {
    42  	// No arguments
    43  	err := s.runAddRelation(c)
    44  	c.Assert(err, gc.ErrorMatches, "a relation must involve two applications")
    45  
    46  	// 1 argument
    47  	err = s.runAddRelation(c, "application1")
    48  	c.Assert(err, gc.ErrorMatches, "a relation must involve two applications")
    49  
    50  	// more than 2 arguments
    51  	err = s.runAddRelation(c, "application1", "application2", "application3")
    52  	c.Assert(err, gc.ErrorMatches, "a relation must involve two applications")
    53  }
    54  
    55  func (s *AddRelationSuite) TestAddRelationSuccess(c *gc.C) {
    56  	err := s.runAddRelation(c, "application1", "application2")
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	s.mockAPI.CheckCall(c, 0, "AddRelation", []string{"application1", "application2"})
    59  	s.mockAPI.CheckCall(c, 1, "Close")
    60  }
    61  
    62  func (s *AddRelationSuite) TestAddRelationFail(c *gc.C) {
    63  	msg := "fail add-relation call at API"
    64  	s.mockAPI.SetErrors(errors.New(msg))
    65  	err := s.runAddRelation(c, "application1", "application2")
    66  	c.Assert(err, gc.ErrorMatches, msg)
    67  	s.mockAPI.CheckCall(c, 0, "AddRelation", []string{"application1", "application2"})
    68  	s.mockAPI.CheckCall(c, 1, "Close")
    69  }
    70  
    71  func (s *AddRelationSuite) TestAddRelationBlocked(c *gc.C) {
    72  	s.mockAPI.SetErrors(common.OperationBlockedError("TestBlockAddRelation"))
    73  	err := s.runAddRelation(c, "application1", "application2")
    74  	coretesting.AssertOperationWasBlocked(c, err, ".*TestBlockAddRelation.*")
    75  	s.mockAPI.CheckCall(c, 0, "AddRelation", []string{"application1", "application2"})
    76  	s.mockAPI.CheckCall(c, 1, "Close")
    77  }
    78  
    79  func (s *AddRelationSuite) TestAddRelationUnauthorizedMentionsJujuGrant(c *gc.C) {
    80  	s.mockAPI.SetErrors(&params.Error{
    81  		Message: "permission denied",
    82  		Code:    params.CodeUnauthorized,
    83  	})
    84  	ctx, _ := coretesting.RunCommand(c, NewAddRelationCommandForTest(s.mockAPI), "application1", "application2")
    85  	errString := strings.Replace(coretesting.Stderr(ctx), "\n", " ", -1)
    86  	c.Assert(errString, gc.Matches, `.*juju grant.*`)
    87  }
    88  
    89  type mockAddAPI struct {
    90  	*testing.Stub
    91  	addRelationFunc func(endpoints ...string) (*params.AddRelationResults, error)
    92  }
    93  
    94  func (s mockAddAPI) Close() error {
    95  	s.MethodCall(s, "Close")
    96  	return s.NextErr()
    97  }
    98  
    99  func (s mockAddAPI) AddRelation(endpoints ...string) (*params.AddRelationResults, error) {
   100  	s.MethodCall(s, "AddRelation", endpoints)
   101  	return s.addRelationFunc(endpoints...)
   102  }