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

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	ocicommon "github.com/oracle/oci-go-sdk/common"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/errors"
    13  	"github.com/juju/juju/provider/oci/common"
    14  	"github.com/juju/juju/testing"
    15  	jc "github.com/juju/testing/checkers"
    16  )
    17  
    18  type errorsSuite struct {
    19  	testing.BaseSuite
    20  }
    21  
    22  var _ = gc.Suite(&errorsSuite{})
    23  
    24  type MockServiceError struct {
    25  	ocicommon.ServiceError
    26  
    27  	code string
    28  }
    29  
    30  func (a MockServiceError) Error() string {
    31  	return fmt.Sprintf("Mocked error %s", a.GetCode())
    32  }
    33  
    34  func (a MockServiceError) GetCode() string { return a.code }
    35  
    36  func (s *errorsSuite) TestServiceErrorsCanTriggerIsAuthorisationFailure(c *gc.C) {
    37  	err := MockServiceError{code: "NotAuthenticated"}
    38  	result := common.IsAuthorisationFailure(err)
    39  	c.Assert(result, jc.IsTrue)
    40  
    41  	err = MockServiceError{code: "InternalServerError"}
    42  	result = common.IsAuthorisationFailure(err)
    43  	c.Assert(result, jc.IsFalse)
    44  }
    45  
    46  func (s *errorsSuite) TestUnknownErrorsDoNotTriggerIsAuthorisationFailure(c *gc.C) {
    47  	err1 := errors.New("unknown")
    48  	for _, err := range []error{
    49  		err1,
    50  		errors.Trace(err1),
    51  		errors.Annotate(err1, "really unknown"),
    52  	} {
    53  		c.Assert(common.IsAuthorisationFailure(err), jc.IsFalse)
    54  	}
    55  }
    56  
    57  func (s *errorsSuite) TestNilDoesNotTriggerIsAuthorisationFailure(c *gc.C) {
    58  	result := common.IsAuthorisationFailure(nil)
    59  	c.Assert(result, jc.IsFalse)
    60  }