Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
Signed-off-by: milinddethe15 <[email protected]>
  • Loading branch information
milinddethe15 committed Oct 24, 2024
1 parent 11940f6 commit ce65fb9
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 24 deletions.
10 changes: 7 additions & 3 deletions errutil/rt_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import (
"github.com/pkg/errors"
)

var Rt_err = errors.New("RoundTripper error")
var rtErr = errors.New("RoundTripper error")

func IsMockedError(err error) bool {
return errors.Is(err, rtErr)
}

// ErrorRoundTripper is a custom RoundTripper that always returns an error.
type ErrorRoundTripper struct {
Expand All @@ -17,6 +21,6 @@ func (ert *ErrorRoundTripper) RoundTrip(*http.Request) (*http.Response, error) {
return nil, ert.Err
}

func WrapRoundtripper(rt http.RoundTripper) http.RoundTripper {
return &ErrorRoundTripper{Err: Rt_err}
func WrapWithErrRoundtripper(rt http.RoundTripper) http.RoundTripper {
return &ErrorRoundTripper{Err: rtErr}
}
5 changes: 2 additions & 3 deletions providers/azure/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/efficientgo/core/testutil"
"github.com/go-kit/log"
"github.com/pkg/errors"

"github.com/thanos-io/objstore/errutil"
"github.com/thanos-io/objstore/exthttp"
Expand Down Expand Up @@ -230,9 +229,9 @@ func TestNewBucketWithErrorRoundTripper(t *testing.T) {
cfg, err := parseConfig(validConfig)
testutil.Ok(t, err)

_, err = NewBucketWithConfig(log.NewNopLogger(), cfg, "test", errutil.WrapRoundtripper)
_, err = NewBucketWithConfig(log.NewNopLogger(), cfg, "test", errutil.WrapWithErrRoundtripper)

// We expect an error from the RoundTripper
testutil.NotOk(t, err)
testutil.Assert(t, errors.Is(err, errutil.Rt_err), "Expected RoundTripper error, got: %v", err)
testutil.Assert(t, errutil.IsMockedError(err), "Expected RoundTripper error, got: %v", err)
}
5 changes: 2 additions & 3 deletions providers/cos/cos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/efficientgo/core/testutil"
"github.com/go-kit/log"
"github.com/pkg/errors"
"github.com/prometheus/common/model"

"github.com/thanos-io/objstore/errutil"
Expand Down Expand Up @@ -151,10 +150,10 @@ func TestNewBucketWithErrorRoundTripper(t *testing.T) {
SecretKey: "skey",
}

bkt, err := NewBucketWithConfig(log.NewNopLogger(), config, "test", errutil.WrapRoundtripper)
bkt, err := NewBucketWithConfig(log.NewNopLogger(), config, "test", errutil.WrapWithErrRoundtripper)
testutil.Ok(t, err)
_, err = bkt.Get(context.Background(), "Test")
// We expect an error from the RoundTripper
testutil.NotOk(t, err)
testutil.Assert(t, errors.Is(err, errutil.Rt_err), "Expected RoundTripper error, got: %v", err)
testutil.Assert(t, errutil.IsMockedError(err), "Expected RoundTripper error, got: %v", err)
}
5 changes: 2 additions & 3 deletions providers/gcs/gcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/efficientgo/core/testutil"
"github.com/fullstorydev/emulators/storage/gcsemu"
"github.com/go-kit/log"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/thanos-io/objstore/errutil"
"google.golang.org/api/option"
Expand Down Expand Up @@ -173,9 +172,9 @@ func TestNewBucketWithErrorRoundTripper(t *testing.T) {
err = os.Setenv("STORAGE_EMULATOR_HOST", svr.Addr)
testutil.Ok(t, err)

bkt, err := NewBucketWithConfig(context.Background(), log.NewNopLogger(), cfg, "test-bucket", errutil.WrapRoundtripper)
bkt, err := NewBucketWithConfig(context.Background(), log.NewNopLogger(), cfg, "test-bucket", errutil.WrapWithErrRoundtripper)
testutil.Ok(t, err)
_, err = bkt.Get(context.Background(), "test-bucket")
testutil.NotOk(t, err)
testutil.Assert(t, errors.Is(err, errutil.Rt_err), "Expected RoundTripper error, got: %v", err)
testutil.Assert(t, errutil.IsMockedError(err), "Expected RoundTripper error, got: %v", err)
}
5 changes: 2 additions & 3 deletions providers/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/efficientgo/core/testutil"
"github.com/go-kit/log"
"github.com/pkg/errors"
"github.com/thanos-io/objstore/errutil"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -38,8 +37,8 @@ G6aFKaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI
ociConfig, err := yaml.Marshal(config)
testutil.Ok(t, err)

_, err = NewBucket(log.NewNopLogger(), ociConfig, errutil.WrapRoundtripper)
_, err = NewBucket(log.NewNopLogger(), ociConfig, errutil.WrapWithErrRoundtripper)
// We expect an error from the RoundTripper
testutil.NotOk(t, err)
testutil.Assert(t, errors.Is(err, errutil.Rt_err), "Expected RoundTripper error, got: %v", err)
testutil.Assert(t, errutil.IsMockedError(err), "Expected RoundTripper error, got: %v", err)
}
5 changes: 2 additions & 3 deletions providers/oss/oss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/efficientgo/core/testutil"
"github.com/go-kit/log"
"github.com/pkg/errors"
"github.com/thanos-io/objstore/errutil"
)

Expand All @@ -18,10 +17,10 @@ func TestNewBucketWithErrorRoundTripper(t *testing.T) {
Bucket: "test",
}

bkt, err := NewBucketWithConfig(log.NewNopLogger(), config, "test", errutil.WrapRoundtripper)
bkt, err := NewBucketWithConfig(log.NewNopLogger(), config, "test", errutil.WrapWithErrRoundtripper)
// We expect an error from the RoundTripper
testutil.Ok(t, err)
_, err = bkt.Get(context.Background(), "test")
testutil.NotOk(t, err)
testutil.Assert(t, errors.Is(err, errutil.Rt_err), "Expected RoundTripper error, got: %v", err)
testutil.Assert(t, errutil.IsMockedError(err), "Expected RoundTripper error, got: %v", err)
}
5 changes: 2 additions & 3 deletions providers/s3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/efficientgo/core/testutil"
"github.com/go-kit/log"
"github.com/minio/minio-go/v7/pkg/encrypt"
"github.com/pkg/errors"

"github.com/thanos-io/objstore/errutil"
"github.com/thanos-io/objstore/exthttp"
Expand Down Expand Up @@ -469,10 +468,10 @@ func TestNewBucketWithErrorRoundTripper(t *testing.T) {
cfg := DefaultConfig
cfg.Endpoint = endpoint
cfg.Bucket = "test"
bkt, err := NewBucketWithConfig(log.NewNopLogger(), cfg, "test", errutil.WrapRoundtripper)
bkt, err := NewBucketWithConfig(log.NewNopLogger(), cfg, "test", errutil.WrapWithErrRoundtripper)
testutil.Ok(t, err)
_, err = bkt.Get(context.Background(), "test")
// We expect an error from the RoundTripper
testutil.NotOk(t, err)
testutil.Assert(t, errors.Is(err, errutil.Rt_err), "Expected RoundTripper error, got: %v", err)
testutil.Assert(t, errutil.IsMockedError(err), "Expected RoundTripper error, got: %v", err)
}
5 changes: 2 additions & 3 deletions providers/swift/swift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/efficientgo/core/testutil"
"github.com/go-kit/log"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/thanos-io/objstore/errutil"
)
Expand Down Expand Up @@ -71,9 +70,9 @@ http_config:
func TestNewBucketWithErrorRoundTripper(t *testing.T) {
config := DefaultConfig
config.AuthUrl = "http://identity.something.com/v3"
_, err := NewContainerFromConfig(log.NewNopLogger(), &config, false, errutil.WrapRoundtripper)
_, err := NewContainerFromConfig(log.NewNopLogger(), &config, false, errutil.WrapWithErrRoundtripper)

// We expect an error from the RoundTripper
testutil.NotOk(t, err)
testutil.Assert(t, errors.Is(err, errutil.Rt_err), "Expected RoundTripper error, got: %v", err)
testutil.Assert(t, errutil.IsMockedError(err), "Expected RoundTripper error, got: %v", err)
}

0 comments on commit ce65fb9

Please sign in to comment.