87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
// Copyright IBM Corp. 2021, 2026
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package errors
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
|
|
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
|
"github.com/aws/smithy-go"
|
|
smithyhttp "github.com/aws/smithy-go/transport/http"
|
|
pb "github.com/hashicorp/boundary/sdk/pbs/plugin"
|
|
"github.com/stretchr/testify/assert"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
// TestAwsHttpResponseError returns an aws http response error
|
|
func TestAwsHttpResponseError(code int, msg string) error {
|
|
return &awshttp.ResponseError{
|
|
ResponseError: &smithyhttp.ResponseError{
|
|
Response: &smithyhttp.Response{
|
|
Response: &http.Response{
|
|
StatusCode: code,
|
|
Body: io.NopCloser(strings.NewReader(msg)),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// TestAwsError returns an generic api error
|
|
func TestAwsError(code, msg string) error {
|
|
return &smithy.GenericAPIError{
|
|
Code: code,
|
|
Message: msg,
|
|
Fault: smithy.FaultServer,
|
|
}
|
|
}
|
|
|
|
// TestAwsS3NotFoundError returns an S3 NotFound error.
|
|
func TestAwsS3NotFoundError(msg string) error {
|
|
return &types.NotFound{Message: &msg}
|
|
}
|
|
|
|
// TestAwsS3NoSuchKeyError returns an S3 NoSuchKey error.
|
|
func TestAwsS3NoSuchKeyError(msg string) error {
|
|
return &types.NoSuchKey{Message: &msg}
|
|
}
|
|
|
|
// TestAwsS3InvalidObjectStateError returns an S3 InvalidObjectState error.
|
|
func TestAwsS3InvalidObjectStateError(msg string) error {
|
|
return &types.InvalidObjectState{Message: &msg}
|
|
}
|
|
|
|
// TestAwsS3NoSuchBucketError returns an S3 NoSuchBucket error.
|
|
func TestAwsS3NoSuchBucketError(msg string) error {
|
|
return &types.NoSuchBucket{Message: &msg}
|
|
}
|
|
|
|
// TestAwsS3AccessDeniedError returns an S3 AccessDenied error.
|
|
func TestAwsS3AccessDeniedError(msg string) error {
|
|
return &types.AccessDenied{Message: &msg}
|
|
}
|
|
|
|
func CheckSimilarPermission(assert *assert.Assertions, expected, actual *pb.Permission, substituteNilWithDefaultOk bool) {
|
|
if expected == nil && substituteNilWithDefaultOk {
|
|
expected = &pb.Permission{
|
|
State: pb.StateType_STATE_TYPE_OK,
|
|
CheckedAt: timestamppb.Now(),
|
|
}
|
|
}
|
|
|
|
if expected != nil {
|
|
assert.NotNil(actual)
|
|
if actual != nil {
|
|
assert.Equal(expected.State, actual.State, "StateType mismatch")
|
|
assert.Equal(expected.ErrorDetails, actual.ErrorDetails)
|
|
assert.NotNil(actual.CheckedAt)
|
|
}
|
|
} else {
|
|
assert.Equal(expected, actual)
|
|
}
|
|
}
|