github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/gce/google/errors_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package google_test 5 6 import ( 7 "fmt" 8 "net/http" 9 "net/url" 10 11 "github.com/juju/errors" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/environs/context" 16 "github.com/juju/juju/provider/gce/google" 17 "github.com/juju/juju/testing" 18 ) 19 20 type ErrorSuite struct { 21 testing.BaseSuite 22 23 googleError *url.Error 24 internalError *googlyError 25 } 26 27 var _ = gc.Suite(&ErrorSuite{}) 28 29 func (s *ErrorSuite) SetUpTest(c *gc.C) { 30 s.BaseSuite.SetUpTest(c) 31 s.internalError = &googlyError{"400 Bad Request"} 32 s.googleError = &url.Error{"Get", "http://notforreal.com/", s.internalError} 33 } 34 35 func (s *ErrorSuite) TestNilContext(c *gc.C) { 36 err := google.HandleCredentialError(s.googleError, nil) 37 c.Assert(err, gc.DeepEquals, s.googleError) 38 c.Assert(c.GetTestLog(), jc.DeepEquals, "") 39 } 40 41 func (s *ErrorSuite) TestInvalidationCallbackErrorOnlyLogs(c *gc.C) { 42 ctx := context.NewCloudCallContext() 43 ctx.InvalidateCredentialFunc = func(msg string) error { 44 return errors.New("kaboom") 45 } 46 google.HandleCredentialError(s.googleError, ctx) 47 c.Assert(c.GetTestLog(), jc.Contains, "could not invalidate stored google cloud credential on the controller") 48 } 49 50 func (s *ErrorSuite) TestAuthRelatedStatusCodes(c *gc.C) { 51 ctx := context.NewCloudCallContext() 52 called := false 53 ctx.InvalidateCredentialFunc = func(msg string) error { 54 c.Assert(msg, gc.DeepEquals, "google cloud denied access") 55 called = true 56 return nil 57 } 58 59 // First test another status code. 60 s.internalError.SetMessage(http.StatusAccepted, "Accepted") 61 google.HandleCredentialError(s.googleError, ctx) 62 c.Assert(called, jc.IsFalse) 63 64 for code, desc := range google.AuthorisationFailureStatusCodes { 65 called = false 66 s.internalError.SetMessage(code, desc) 67 google.HandleCredentialError(s.googleError, ctx) 68 c.Assert(called, jc.IsTrue) 69 } 70 } 71 72 func (*ErrorSuite) TestNilGoogleError(c *gc.C) { 73 ctx := context.NewCloudCallContext() 74 called := false 75 ctx.InvalidateCredentialFunc = func(msg string) error { 76 called = true 77 return nil 78 } 79 returnedErr := google.HandleCredentialError(nil, ctx) 80 c.Assert(called, jc.IsFalse) 81 c.Assert(returnedErr, jc.ErrorIsNil) 82 } 83 84 func (*ErrorSuite) TestAnyOtherError(c *gc.C) { 85 ctx := context.NewCloudCallContext() 86 called := false 87 ctx.InvalidateCredentialFunc = func(msg string) error { 88 called = true 89 return nil 90 } 91 92 notinterestingErr := errors.New("not kaboom") 93 returnedErr := google.HandleCredentialError(notinterestingErr, ctx) 94 c.Assert(called, jc.IsFalse) 95 c.Assert(returnedErr, gc.DeepEquals, notinterestingErr) 96 } 97 98 type googlyError struct { 99 msg string 100 } 101 102 func (e *googlyError) Error() string { return e.msg } 103 104 func (e *googlyError) SetMessage(code int, desc string) { 105 e.msg = fmt.Sprintf("%v %v", code, desc) 106 }