Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
Signed-off-by: Mikkel Oscar Lyderik Larsen <[email protected]>
  • Loading branch information
mikkeloscar committed Oct 10, 2024
1 parent f9140d4 commit 2c0b6bb
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions provider/aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
cftypes "github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"github.com/szuecs/kube-static-egress-controller/provider"
Expand Down Expand Up @@ -189,6 +191,7 @@ type mockCloudformation struct {
err error
stack cftypes.Stack
templateBody string
templateURL string
}

func (cf *mockCloudformation) DescribeStacks(_ context.Context, input *cloudformation.DescribeStacksInput, optFns ...func(*cloudformation.Options)) (*cloudformation.DescribeStacksOutput, error) {
Expand Down Expand Up @@ -219,6 +222,8 @@ func (cf *mockCloudformation) CreateStack(_ context.Context, input *cloudformati
StackStatus: cftypes.StackStatusCreateComplete,
Tags: input.Tags,
}
cf.templateBody = aws.ToString(input.TemplateBody)
cf.templateURL = aws.ToString(input.TemplateURL)
return &cloudformation.CreateStackOutput{
StackId: aws.String(""),
}, cf.err
Expand All @@ -231,6 +236,7 @@ func (cf *mockCloudformation) UpdateStack(_ context.Context, input *cloudformati
Tags: input.Tags,
}
cf.templateBody = aws.ToString(input.TemplateBody)
cf.templateURL = aws.ToString(input.TemplateURL)
return &cloudformation.UpdateStackOutput{
StackId: aws.String(""),
}, cf.err
Expand Down Expand Up @@ -263,6 +269,14 @@ func (ec2 *mockEC2) DescribeRouteTables(context.Context, *ec2.DescribeRouteTable
return ec2.describeRouteTables, ec2.err
}

type mockS3UploaderAPI struct {
err error
}

func (s3 *mockS3UploaderAPI) Upload(ctx context.Context, input *s3.PutObjectInput, opts ...func(*manager.Uploader)) (*manager.UploadOutput, error) {
return &manager.UploadOutput{Location: aws.ToString(input.Bucket) + "/" + aws.ToString(input.Key)}, s3.err
}

func TestEnsure(tt *testing.T) {
_, netA, _ := net.ParseCIDR("213.95.138.235/32")
_, netB, _ := net.ParseCIDR("213.95.138.236/32")
Expand Down Expand Up @@ -555,6 +569,68 @@ func TestEnsure(tt *testing.T) {
}
}

func TestCloudformationS3TemplateUpload(t *testing.T) {
for _, tc := range []struct {
msg string
cfTemplateBucket string
s3UploaderErr error
}{
{
msg: "when cfTemplateBucket is set it should upload",
cfTemplateBucket: "bucket",
},
{
msg: "Fail on upload error when cfTemplateBucket is set",
cfTemplateBucket: "bucket",
s3UploaderErr: errors.New("failed"),
},
} {
t.Run(tc.msg, func(t *testing.T) {
cloudformationAPI := &mockCloudformation{}

provider := &AWSProvider{
vpcID: "x",
cloudformation: cloudformationAPI,
s3Uploader: &mockS3UploaderAPI{err: tc.s3UploaderErr},
cfTemplateBucket: tc.cfTemplateBucket,
logger: log.WithFields(log.Fields{"provider": ProviderName}),
}

err := provider.createCFStack(context.Background(), &stackSpec{name: "stack", template: "<template>"})
if tc.s3UploaderErr != nil {
require.Error(t, err)
return
} else {
require.NoError(t, err)
}

if tc.cfTemplateBucket != "" {
require.NotEqual(t, cloudformationAPI.templateURL, "")
require.Equal(t, cloudformationAPI.templateBody, "")
} else {
require.Equal(t, cloudformationAPI.templateURL, "")
require.NotEqual(t, cloudformationAPI.templateBody, "")
}

err = provider.updateCFStack(context.Background(), &stackSpec{name: "stack", template: "<template>"})
if tc.s3UploaderErr != nil {
require.Error(t, err)
return
} else {
require.NoError(t, err)
}

if tc.cfTemplateBucket != "" {
require.NotEqual(t, cloudformationAPI.templateURL, "")
require.Equal(t, cloudformationAPI.templateBody, "")
} else {
require.Equal(t, cloudformationAPI.templateURL, "")
require.NotEqual(t, cloudformationAPI.templateBody, "")
}
})
}
}

func TestCloudformationHasTags(tt *testing.T) {
for _, tc := range []struct {
msg string
Expand Down

0 comments on commit 2c0b6bb

Please sign in to comment.