Skip to content

Commit

Permalink
Merge pull request #120 from bilalcaliskan/devel #major
Browse files Browse the repository at this point in the history
Sdk v2 (#119)
  • Loading branch information
bilalcaliskan authored Aug 6, 2023
2 parents 04221c3 + 3d7fb68 commit f5c5760
Show file tree
Hide file tree
Showing 48 changed files with 2,302 additions and 2,059 deletions.
8 changes: 5 additions & 3 deletions cmd/bucketpolicy/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"io"
"os"

internalawstypes "github.com/bilalcaliskan/s3-manager/internal/aws/types"

"github.com/bilalcaliskan/s3-manager/internal/aws"

rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options"

"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/bilalcaliskan/s3-manager/cmd/bucketpolicy/options"
"github.com/bilalcaliskan/s3-manager/internal/aws"
"github.com/bilalcaliskan/s3-manager/internal/prompt"
"github.com/bilalcaliskan/s3-manager/internal/utils"
"github.com/rs/zerolog"
Expand All @@ -21,7 +23,7 @@ func init() {
}

var (
svc s3iface.S3API
svc internalawstypes.S3ClientAPI
logger zerolog.Logger
confirmRunner prompt.PromptRunner
bucketPolicyOpts *options.BucketPolicyOptions
Expand Down
49 changes: 20 additions & 29 deletions cmd/bucketpolicy/add/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,29 @@ package add

import (
"context"
"errors"
"testing"

"github.com/aws/aws-sdk-go-v2/service/s3"
internalawstypes "github.com/bilalcaliskan/s3-manager/internal/aws/types"
"github.com/bilalcaliskan/s3-manager/internal/constants"

"github.com/bilalcaliskan/s3-manager/internal/prompt"

"github.com/aws/aws-sdk-go/service/s3"
"github.com/bilalcaliskan/s3-manager/cmd/root/options"
internalaws "github.com/bilalcaliskan/s3-manager/internal/aws"
"github.com/stretchr/testify/assert"

"github.com/stretchr/testify/mock"
)

func TestExecuteAddCmd(t *testing.T) {
ctx := context.Background()
AddCmd.SetContext(ctx)

rootOpts := options.GetMockedRootOptions()
svc, err := internalaws.CreateAwsService(rootOpts)
assert.NotNil(t, svc)
assert.Nil(t, err)

confirmRunner := prompt.GetConfirmRunner()
assert.NotNil(t, confirmRunner)
ctx := context.Background()
AddCmd.SetContext(ctx)

cases := []struct {
caseName string
args []string
shouldPass bool
putBucketPolicyErr error
putBucketPolicyOutput *s3.PutBucketPolicyOutput
caseName string
args []string
shouldPass bool
putBucketPolicyFunc func(ctx context.Context, params *s3.PutBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.PutBucketPolicyOutput, error)
prompt.PromptRunner
dryRun bool
autoApprove bool
Expand All @@ -43,8 +35,9 @@ func TestExecuteAddCmd(t *testing.T) {
"Success",
[]string{"../../../testdata/bucketpolicy.json"},
true,
nil,
&s3.PutBucketPolicyOutput{},
func(ctx context.Context, params *s3.PutBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.PutBucketPolicyOutput, error) {
return &s3.PutBucketPolicyOutput{}, nil
},
prompt.PromptMock{
Msg: "y",
Err: nil,
Expand All @@ -56,8 +49,9 @@ func TestExecuteAddCmd(t *testing.T) {
"Failure",
[]string{"../../../testdata/bucketpolicy.json"},
false,
errors.New("dummy error"),
&s3.PutBucketPolicyOutput{},
func(ctx context.Context, params *s3.PutBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.PutBucketPolicyOutput, error) {
return nil, constants.ErrInjected
},
prompt.PromptMock{
Msg: "y",
Err: nil,
Expand All @@ -70,7 +64,6 @@ func TestExecuteAddCmd(t *testing.T) {
[]string{"../../../testdata/bucketpolicy.jsonnnn"},
false,
nil,
&s3.PutBucketPolicyOutput{},
prompt.PromptMock{
Msg: "y",
Err: nil,
Expand All @@ -83,7 +76,6 @@ func TestExecuteAddCmd(t *testing.T) {
[]string{"enabled", "foo"},
false,
nil,
&s3.PutBucketPolicyOutput{},
nil,
false,
false,
Expand All @@ -93,7 +85,6 @@ func TestExecuteAddCmd(t *testing.T) {
[]string{},
false,
nil,
&s3.PutBucketPolicyOutput{},
nil,
false,
false,
Expand All @@ -106,16 +97,16 @@ func TestExecuteAddCmd(t *testing.T) {
rootOpts.DryRun = tc.dryRun
rootOpts.AutoApprove = tc.autoApprove

mockS3 := new(internalaws.MockS3Client)
mockS3.On("PutBucketPolicy", mock.AnythingOfType("*s3.PutBucketPolicyInput")).Return(tc.putBucketPolicyOutput, tc.putBucketPolicyErr)
mockS3 := new(internalawstypes.MockS3Client)
mockS3.PutBucketPolicyAPI = tc.putBucketPolicyFunc

AddCmd.SetContext(context.WithValue(AddCmd.Context(), options.S3SvcKey{}, mockS3))
AddCmd.SetContext(context.WithValue(AddCmd.Context(), options.S3ClientKey{}, mockS3))
AddCmd.SetContext(context.WithValue(AddCmd.Context(), options.OptsKey{}, rootOpts))
AddCmd.SetContext(context.WithValue(AddCmd.Context(), options.ConfirmRunnerKey{}, tc.PromptRunner))

AddCmd.SetArgs(tc.args)

err = AddCmd.Execute()
err := AddCmd.Execute()
if tc.shouldPass {
assert.Nil(t, err)
} else {
Expand Down
5 changes: 3 additions & 2 deletions cmd/bucketpolicy/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package remove
import (
"fmt"

internalawstypes "github.com/bilalcaliskan/s3-manager/internal/aws/types"

rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options"

"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/bilalcaliskan/s3-manager/cmd/bucketpolicy/options"
"github.com/bilalcaliskan/s3-manager/internal/utils"

Expand All @@ -20,7 +21,7 @@ func init() {
}

var (
svc s3iface.S3API
svc internalawstypes.S3ClientAPI
logger zerolog.Logger
confirmRunner prompt.PromptRunner
bucketPolicyOpts *options.BucketPolicyOptions
Expand Down
103 changes: 51 additions & 52 deletions cmd/bucketpolicy/remove/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import (
"context"
"testing"

"github.com/bilalcaliskan/s3-manager/internal/prompt"
"github.com/aws/aws-sdk-go-v2/service/s3"
internalawstypes "github.com/bilalcaliskan/s3-manager/internal/aws/types"

"github.com/stretchr/testify/mock"
"github.com/bilalcaliskan/s3-manager/internal/prompt"

"github.com/bilalcaliskan/s3-manager/internal/constants"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go-v2/aws"

"github.com/aws/aws-sdk-go/service/s3"
"github.com/bilalcaliskan/s3-manager/cmd/root/options"
internalaws "github.com/bilalcaliskan/s3-manager/internal/aws"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -49,13 +48,11 @@ func TestExecuteRemoveCmd(t *testing.T) {
ctx := context.Background()
RemoveCmd.SetContext(ctx)
cases := []struct {
caseName string
args []string
shouldPass bool
getBucketPolicyOutput *s3.GetBucketPolicyOutput
getBucketPolicyErr error
deleteBucketPolicyErr error
deleteBucketPolicyOutput *s3.DeleteBucketPolicyOutput
caseName string
args []string
shouldPass bool
getBucketPolicyFunc func(ctx context.Context, params *s3.GetBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.GetBucketPolicyOutput, error)
deleteBucketPolicyFunc func(ctx context.Context, params *s3.DeleteBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.DeleteBucketPolicyOutput, error)
prompt.PromptRunner
dryRun bool
autoApprove bool
Expand All @@ -64,10 +61,8 @@ func TestExecuteRemoveCmd(t *testing.T) {
"Too many arguments",
[]string{"enabled", "foo"},
false,
&s3.GetBucketPolicyOutput{},
nil,
nil,
&s3.DeleteBucketPolicyOutput{},
nil,
false,
false,
Expand All @@ -76,12 +71,14 @@ func TestExecuteRemoveCmd(t *testing.T) {
"Success",
[]string{},
true,
&s3.GetBucketPolicyOutput{
Policy: aws.String(policyStr),
func(ctx context.Context, params *s3.GetBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.GetBucketPolicyOutput, error) {
return &s3.GetBucketPolicyOutput{
Policy: aws.String(policyStr),
}, nil
},
func(ctx context.Context, params *s3.DeleteBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.DeleteBucketPolicyOutput, error) {
return &s3.DeleteBucketPolicyOutput{}, nil
},
nil,
nil,
&s3.DeleteBucketPolicyOutput{},
prompt.PromptMock{
Msg: "y",
Err: nil,
Expand All @@ -93,12 +90,14 @@ func TestExecuteRemoveCmd(t *testing.T) {
"Success with dry run",
[]string{},
true,
&s3.GetBucketPolicyOutput{
Policy: aws.String(policyStr),
func(ctx context.Context, params *s3.GetBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.GetBucketPolicyOutput, error) {
return &s3.GetBucketPolicyOutput{
Policy: aws.String(policyStr),
}, nil
},
func(ctx context.Context, params *s3.DeleteBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.DeleteBucketPolicyOutput, error) {
return &s3.DeleteBucketPolicyOutput{}, nil
},
nil,
nil,
&s3.DeleteBucketPolicyOutput{},
prompt.PromptMock{
Msg: "y",
Err: nil,
Expand All @@ -110,26 +109,30 @@ func TestExecuteRemoveCmd(t *testing.T) {
"Success with auto approve",
[]string{},
true,
&s3.GetBucketPolicyOutput{
Policy: aws.String(policyStr),
func(ctx context.Context, params *s3.GetBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.GetBucketPolicyOutput, error) {
return &s3.GetBucketPolicyOutput{
Policy: aws.String(policyStr),
}, nil
},
func(ctx context.Context, params *s3.DeleteBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.DeleteBucketPolicyOutput, error) {
return &s3.DeleteBucketPolicyOutput{}, nil
},
nil,
nil,
&s3.DeleteBucketPolicyOutput{},
nil,
false,
true,
},
{
"Failure",
"Failure caused by delete error",
[]string{},
false,
&s3.GetBucketPolicyOutput{
Policy: aws.String(policyStr),
func(ctx context.Context, params *s3.GetBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.GetBucketPolicyOutput, error) {
return &s3.GetBucketPolicyOutput{
Policy: aws.String(policyStr),
}, nil
},
func(ctx context.Context, params *s3.DeleteBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.DeleteBucketPolicyOutput, error) {
return nil, constants.ErrInjected
},
nil,
constants.ErrInjected,
&s3.DeleteBucketPolicyOutput{},
prompt.PromptMock{
Msg: "y",
Err: nil,
Expand All @@ -141,12 +144,12 @@ func TestExecuteRemoveCmd(t *testing.T) {
"Failure caused by get bucket policy error",
[]string{},
false,
&s3.GetBucketPolicyOutput{
Policy: nil,
func(ctx context.Context, params *s3.GetBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.GetBucketPolicyOutput, error) {
return nil, constants.ErrInjected
},
func(ctx context.Context, params *s3.DeleteBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.DeleteBucketPolicyOutput, error) {
return nil, constants.ErrInjected
},
constants.ErrInjected,
nil,
&s3.DeleteBucketPolicyOutput{},
prompt.PromptMock{
Msg: "y",
Err: nil,
Expand All @@ -158,12 +161,10 @@ func TestExecuteRemoveCmd(t *testing.T) {
"Failure caused by user terminated process",
[]string{},
false,
&s3.GetBucketPolicyOutput{
Policy: aws.String(policyStr),
func(ctx context.Context, params *s3.GetBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.GetBucketPolicyOutput, error) {
return nil, constants.ErrInjected
},
nil,
nil,
&s3.DeleteBucketPolicyOutput{},
prompt.PromptMock{
Msg: "n",
Err: constants.ErrInjected,
Expand All @@ -175,12 +176,10 @@ func TestExecuteRemoveCmd(t *testing.T) {
"Failure caused by prompt error",
[]string{},
false,
&s3.GetBucketPolicyOutput{
Policy: aws.String(policyStr),
func(ctx context.Context, params *s3.GetBucketPolicyInput, optFns ...func(*s3.Options)) (*s3.GetBucketPolicyOutput, error) {
return nil, constants.ErrInjected
},
nil,
nil,
&s3.DeleteBucketPolicyOutput{},
prompt.PromptMock{
Msg: "nasdfadf",
Err: constants.ErrInjected,
Expand All @@ -193,14 +192,14 @@ func TestExecuteRemoveCmd(t *testing.T) {
for _, tc := range cases {
t.Logf("starting case %s", tc.caseName)

mockS3 := new(internalaws.MockS3Client)
mockS3.On("GetBucketPolicy", mock.AnythingOfType("*s3.GetBucketPolicyInput")).Return(tc.getBucketPolicyOutput, tc.getBucketPolicyErr)
mockS3.On("DeleteBucketPolicy", mock.AnythingOfType("*s3.DeleteBucketPolicyInput")).Return(tc.deleteBucketPolicyOutput, tc.deleteBucketPolicyErr)
mockS3 := new(internalawstypes.MockS3Client)
mockS3.GetBucketPolicyAPI = tc.getBucketPolicyFunc
mockS3.DeleteBucketPolicyAPI = tc.deleteBucketPolicyFunc

rootOpts.DryRun = tc.dryRun
rootOpts.AutoApprove = tc.autoApprove

RemoveCmd.SetContext(context.WithValue(RemoveCmd.Context(), options.S3SvcKey{}, mockS3))
RemoveCmd.SetContext(context.WithValue(RemoveCmd.Context(), options.S3ClientKey{}, mockS3))
RemoveCmd.SetContext(context.WithValue(RemoveCmd.Context(), options.OptsKey{}, rootOpts))
RemoveCmd.SetContext(context.WithValue(RemoveCmd.Context(), options.ConfirmRunnerKey{}, tc.PromptRunner))
RemoveCmd.SetArgs(tc.args)
Expand Down
5 changes: 3 additions & 2 deletions cmd/bucketpolicy/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package show
import (
"fmt"

internalawstypes "github.com/bilalcaliskan/s3-manager/internal/aws/types"

rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options"

"github.com/bilalcaliskan/s3-manager/internal/utils"

options2 "github.com/bilalcaliskan/s3-manager/cmd/bucketpolicy/options"
"github.com/bilalcaliskan/s3-manager/internal/aws"

"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)
Expand All @@ -20,7 +21,7 @@ func init() {
}

var (
svc s3iface.S3API
svc internalawstypes.S3ClientAPI
logger zerolog.Logger
bucketPolicyOpts *options2.BucketPolicyOptions
ShowCmd = &cobra.Command{
Expand Down
Loading

0 comments on commit f5c5760

Please sign in to comment.