github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/azure/internal/errorutils/errors_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package errorutils_test 5 6 import ( 7 "net/http" 8 9 "github.com/Azure/go-autorest/autorest" 10 "github.com/juju/errors" 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/azure/internal/errorutils" 16 "github.com/juju/juju/provider/common" 17 "github.com/juju/juju/testing" 18 ) 19 20 type ErrorSuite struct { 21 testing.BaseSuite 22 23 azureError autorest.DetailedError 24 } 25 26 var _ = gc.Suite(&ErrorSuite{}) 27 28 func (s *ErrorSuite) SetUpTest(c *gc.C) { 29 s.BaseSuite.SetUpTest(c) 30 s.azureError = autorest.DetailedError{ 31 StatusCode: http.StatusUnauthorized, 32 } 33 } 34 35 func (s *ErrorSuite) TestNilContext(c *gc.C) { 36 err := errorutils.HandleCredentialError(s.azureError, nil) 37 c.Assert(err, gc.DeepEquals, s.azureError) 38 39 invalidated := errorutils.MaybeInvalidateCredential(s.azureError, nil) 40 c.Assert(invalidated, jc.IsFalse) 41 42 c.Assert(c.GetTestLog(), jc.DeepEquals, "") 43 } 44 45 func (s *ErrorSuite) TestInvalidationCallbackErrorOnlyLogs(c *gc.C) { 46 ctx := context.NewCloudCallContext() 47 ctx.InvalidateCredentialFunc = func(msg string) error { 48 return errors.New("kaboom") 49 } 50 errorutils.MaybeInvalidateCredential(s.azureError, ctx) 51 c.Assert(c.GetTestLog(), jc.Contains, "could not invalidate stored azure cloud credential on the controller") 52 } 53 54 func (s *ErrorSuite) TestAuthRelatedStatusCodes(c *gc.C) { 55 ctx := context.NewCloudCallContext() 56 called := false 57 ctx.InvalidateCredentialFunc = func(msg string) error { 58 c.Assert(msg, gc.DeepEquals, "azure cloud denied access") 59 called = true 60 return nil 61 } 62 63 // First test another status code. 64 s.azureError.StatusCode = http.StatusAccepted 65 errorutils.HandleCredentialError(s.azureError, ctx) 66 c.Assert(called, jc.IsFalse) 67 68 for t := range common.AuthorisationFailureStatusCodes { 69 called = false 70 s.azureError.StatusCode = t 71 errorutils.HandleCredentialError(s.azureError, ctx) 72 c.Assert(called, jc.IsTrue) 73 } 74 } 75 76 func (*ErrorSuite) TestNilAzureError(c *gc.C) { 77 ctx := context.NewCloudCallContext() 78 called := false 79 ctx.InvalidateCredentialFunc = func(msg string) error { 80 called = true 81 return nil 82 } 83 returnedErr := errorutils.HandleCredentialError(nil, ctx) 84 c.Assert(called, jc.IsFalse) 85 c.Assert(returnedErr, jc.ErrorIsNil) 86 }