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

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     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/environs"
    13  	"github.com/juju/juju/environs/context"
    14  	"github.com/juju/juju/provider/common"
    15  )
    16  
    17  type ErrorsSuite struct {
    18  	testing.IsolationSuite
    19  }
    20  
    21  var _ = gc.Suite(&ErrorsSuite{})
    22  
    23  func (*ErrorsSuite) TestWrapZoneIndependentError(c *gc.C) {
    24  	err1 := errors.New("foo")
    25  	err2 := errors.Annotate(err1, "bar")
    26  	wrapped := common.ZoneIndependentError(err2)
    27  	c.Assert(wrapped, jc.Satisfies, environs.IsAvailabilityZoneIndependent)
    28  	c.Assert(wrapped, gc.ErrorMatches, "bar: foo")
    29  
    30  	stack := errors.ErrorStack(wrapped)
    31  	c.Assert(stack, gc.Matches, `
    32  github.com/juju/juju/provider/common/errors_test.go:.*: foo
    33  github.com/juju/juju/provider/common/errors_test.go:.*: bar
    34  github.com/juju/juju/provider/common/errors_test.go:.*: bar: foo`[1:])
    35  }
    36  
    37  func (s *ErrorsSuite) TestInvalidCredentialWrapped(c *gc.C) {
    38  	err1 := errors.New("foo")
    39  	err2 := errors.Annotate(err1, "bar")
    40  	err := common.CredentialNotValid(err2)
    41  
    42  	// This is to confirm that IsCredentialNotValid is correct.
    43  	c.Assert(err2, gc.Not(jc.Satisfies), common.IsCredentialNotValid)
    44  	c.Assert(err, jc.Satisfies, common.IsCredentialNotValid)
    45  	c.Assert(err, gc.ErrorMatches, "bar: foo")
    46  
    47  	stack := errors.ErrorStack(err)
    48  	c.Assert(stack, gc.Matches, `
    49  github.com/juju/juju/provider/common/errors_test.go:.*: foo
    50  github.com/juju/juju/provider/common/errors_test.go:.*: bar
    51  github.com/juju/juju/provider/common/errors_test.go:.*: bar: foo`[1:])
    52  }
    53  
    54  func (s *ErrorsSuite) TestInvalidCredentialNew(c *gc.C) {
    55  	err := common.NewCredentialNotValid("Your account is blocked.")
    56  	c.Assert(err, jc.Satisfies, common.IsCredentialNotValid)
    57  	c.Assert(err, gc.ErrorMatches, "credential not valid: Your account is blocked.")
    58  }
    59  
    60  func (s *ErrorsSuite) TestInvalidCredentialf(c *gc.C) {
    61  	err1 := errors.New("foo")
    62  	err := common.CredentialNotValidf(err1, "bar")
    63  	c.Assert(err, jc.Satisfies, common.IsCredentialNotValid)
    64  	c.Assert(err, gc.ErrorMatches, "bar: foo")
    65  }
    66  
    67  var authFailureError = errors.New("auth failure")
    68  
    69  func (s *ErrorsSuite) TestNilContext(c *gc.C) {
    70  	isAuthF := func(e error) bool {
    71  		return true
    72  	}
    73  	denied := common.MaybeHandleCredentialError(isAuthF, authFailureError, nil)
    74  	c.Assert(c.GetTestLog(), jc.DeepEquals, "")
    75  	c.Assert(denied, jc.IsTrue)
    76  }
    77  
    78  func (s *ErrorsSuite) TestInvalidationCallbackErrorOnlyLogs(c *gc.C) {
    79  	isAuthF := func(e error) bool {
    80  		return true
    81  	}
    82  	ctx := context.NewCloudCallContext()
    83  	ctx.InvalidateCredentialFunc = func(msg string) error {
    84  		return errors.New("kaboom")
    85  	}
    86  	denied := common.MaybeHandleCredentialError(isAuthF, authFailureError, ctx)
    87  	c.Assert(c.GetTestLog(), jc.Contains, "could not invalidate stored cloud credential on the controller")
    88  	c.Assert(denied, jc.IsTrue)
    89  }
    90  
    91  func (s *ErrorsSuite) TestHandleCredentialErrorPermissionError(c *gc.C) {
    92  	s.checkPermissionHandling(c, authFailureError, true)
    93  
    94  	e := errors.Trace(authFailureError)
    95  	s.checkPermissionHandling(c, e, true)
    96  
    97  	e = errors.Annotatef(authFailureError, "more and more")
    98  	s.checkPermissionHandling(c, e, true)
    99  }
   100  
   101  func (s *ErrorsSuite) TestHandleCredentialErrorAnotherError(c *gc.C) {
   102  	s.checkPermissionHandling(c, errors.New("fluffy"), false)
   103  }
   104  
   105  func (s *ErrorsSuite) TestNilError(c *gc.C) {
   106  	s.checkPermissionHandling(c, nil, false)
   107  }
   108  
   109  func (s *ErrorsSuite) checkPermissionHandling(c *gc.C, e error, handled bool) {
   110  	isAuthF := func(e error) bool {
   111  		return handled
   112  	}
   113  	ctx := context.NewCloudCallContext()
   114  	called := false
   115  	ctx.InvalidateCredentialFunc = func(msg string) error {
   116  		c.Assert(msg, gc.DeepEquals, "cloud denied access")
   117  		called = true
   118  		return nil
   119  	}
   120  
   121  	denied := common.MaybeHandleCredentialError(isAuthF, e, ctx)
   122  	c.Assert(called, gc.Equals, handled)
   123  	c.Assert(denied, gc.Equals, handled)
   124  }