github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/maas/errors_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package maas
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/gomaasapi"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/environs/context"
    15  	"github.com/juju/juju/provider/common"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  type ErrorSuite struct {
    20  	testing.BaseSuite
    21  
    22  	maasError error
    23  }
    24  
    25  var _ = gc.Suite(&ErrorSuite{})
    26  
    27  func (s *ErrorSuite) SetUpTest(c *gc.C) {
    28  	s.BaseSuite.SetUpTest(c)
    29  	s.maasError = gomaasapi.NewPermissionError("denial")
    30  }
    31  
    32  func (s *ErrorSuite) TestNilContext(c *gc.C) {
    33  	denied := common.MaybeHandleCredentialError(IsAuthorisationFailure, s.maasError, nil)
    34  	c.Assert(c.GetTestLog(), jc.DeepEquals, "")
    35  	c.Assert(denied, jc.IsTrue)
    36  }
    37  
    38  func (s *ErrorSuite) TestInvalidationCallbackErrorOnlyLogs(c *gc.C) {
    39  	ctx := context.NewCloudCallContext()
    40  	ctx.InvalidateCredentialFunc = func(msg string) error {
    41  		return errors.New("kaboom")
    42  	}
    43  	denied := common.MaybeHandleCredentialError(IsAuthorisationFailure, s.maasError, ctx)
    44  	c.Assert(c.GetTestLog(), jc.Contains, "could not invalidate stored cloud credential on the controller")
    45  	c.Assert(denied, jc.IsTrue)
    46  }
    47  
    48  func (s *ErrorSuite) TestHandleCredentialErrorPermissionError(c *gc.C) {
    49  	s.checkMaasPermissionHandling(c, true)
    50  
    51  	s.maasError = errors.Trace(s.maasError)
    52  	s.checkMaasPermissionHandling(c, true)
    53  
    54  	s.maasError = errors.Annotatef(s.maasError, "more and more")
    55  	s.checkMaasPermissionHandling(c, true)
    56  }
    57  
    58  func (s *ErrorSuite) TestHandleCredentialErrorAnotherError(c *gc.C) {
    59  	s.maasError = errors.New("fluffy")
    60  	s.checkMaasPermissionHandling(c, false)
    61  }
    62  
    63  func (s *ErrorSuite) TestNilError(c *gc.C) {
    64  	s.maasError = nil
    65  	s.checkMaasPermissionHandling(c, false)
    66  }
    67  
    68  func (s *ErrorSuite) TestGomaasError(c *gc.C) {
    69  	// check accepted status codes
    70  	s.maasError = gomaasapi.ServerError{StatusCode: http.StatusAccepted}
    71  	s.checkMaasPermissionHandling(c, false)
    72  
    73  	for t := range common.AuthorisationFailureStatusCodes {
    74  		s.maasError = gomaasapi.ServerError{StatusCode: t}
    75  		s.checkMaasPermissionHandling(c, true)
    76  	}
    77  }
    78  
    79  func (s *ErrorSuite) checkMaasPermissionHandling(c *gc.C, handled bool) {
    80  	ctx := context.NewCloudCallContext()
    81  	called := false
    82  	ctx.InvalidateCredentialFunc = func(msg string) error {
    83  		c.Assert(msg, gc.DeepEquals, "cloud denied access")
    84  		called = true
    85  		return nil
    86  	}
    87  
    88  	denied := common.MaybeHandleCredentialError(IsAuthorisationFailure, s.maasError, ctx)
    89  	c.Assert(called, gc.Equals, handled)
    90  	c.Assert(denied, gc.Equals, handled)
    91  }