@@ -9,10 +9,12 @@ import (
9
9
"testing"
10
10
11
11
"github.com/aws/aws-sdk-go-v2/aws"
12
+ "github.com/aws/aws-sdk-go-v2/feature/s3/manager"
12
13
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
13
14
cftypes "github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
14
15
"github.com/aws/aws-sdk-go-v2/service/ec2"
15
16
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
17
+ "github.com/aws/aws-sdk-go-v2/service/s3"
16
18
log "github.com/sirupsen/logrus"
17
19
"github.com/stretchr/testify/require"
18
20
"github.com/szuecs/kube-static-egress-controller/provider"
@@ -189,6 +191,7 @@ type mockCloudformation struct {
189
191
err error
190
192
stack cftypes.Stack
191
193
templateBody string
194
+ templateURL string
192
195
}
193
196
194
197
func (cf * mockCloudformation ) DescribeStacks (_ context.Context , input * cloudformation.DescribeStacksInput , optFns ... func (* cloudformation.Options )) (* cloudformation.DescribeStacksOutput , error ) {
@@ -219,6 +222,8 @@ func (cf *mockCloudformation) CreateStack(_ context.Context, input *cloudformati
219
222
StackStatus : cftypes .StackStatusCreateComplete ,
220
223
Tags : input .Tags ,
221
224
}
225
+ cf .templateBody = aws .ToString (input .TemplateBody )
226
+ cf .templateURL = aws .ToString (input .TemplateURL )
222
227
return & cloudformation.CreateStackOutput {
223
228
StackId : aws .String ("" ),
224
229
}, cf .err
@@ -231,6 +236,7 @@ func (cf *mockCloudformation) UpdateStack(_ context.Context, input *cloudformati
231
236
Tags : input .Tags ,
232
237
}
233
238
cf .templateBody = aws .ToString (input .TemplateBody )
239
+ cf .templateURL = aws .ToString (input .TemplateURL )
234
240
return & cloudformation.UpdateStackOutput {
235
241
StackId : aws .String ("" ),
236
242
}, cf .err
@@ -263,6 +269,14 @@ func (ec2 *mockEC2) DescribeRouteTables(context.Context, *ec2.DescribeRouteTable
263
269
return ec2 .describeRouteTables , ec2 .err
264
270
}
265
271
272
+ type mockS3UploaderAPI struct {
273
+ err error
274
+ }
275
+
276
+ func (s3 * mockS3UploaderAPI ) Upload (ctx context.Context , input * s3.PutObjectInput , opts ... func (* manager.Uploader )) (* manager.UploadOutput , error ) {
277
+ return & manager.UploadOutput {Location : aws .ToString (input .Bucket ) + "/" + aws .ToString (input .Key )}, s3 .err
278
+ }
279
+
266
280
func TestEnsure (tt * testing.T ) {
267
281
_ , netA , _ := net .ParseCIDR ("213.95.138.235/32" )
268
282
_ , netB , _ := net .ParseCIDR ("213.95.138.236/32" )
@@ -555,6 +569,68 @@ func TestEnsure(tt *testing.T) {
555
569
}
556
570
}
557
571
572
+ func TestCloudformationS3TemplateUpload (t * testing.T ) {
573
+ for _ , tc := range []struct {
574
+ msg string
575
+ cfTemplateBucket string
576
+ s3UploaderErr error
577
+ }{
578
+ {
579
+ msg : "when cfTemplateBucket is set it should upload" ,
580
+ cfTemplateBucket : "bucket" ,
581
+ },
582
+ {
583
+ msg : "Fail on upload error when cfTemplateBucket is set" ,
584
+ cfTemplateBucket : "bucket" ,
585
+ s3UploaderErr : errors .New ("failed" ),
586
+ },
587
+ } {
588
+ t .Run (tc .msg , func (t * testing.T ) {
589
+ cloudformationAPI := & mockCloudformation {}
590
+
591
+ provider := & AWSProvider {
592
+ vpcID : "x" ,
593
+ cloudformation : cloudformationAPI ,
594
+ s3Uploader : & mockS3UploaderAPI {err : tc .s3UploaderErr },
595
+ cfTemplateBucket : tc .cfTemplateBucket ,
596
+ logger : log .WithFields (log.Fields {"provider" : ProviderName }),
597
+ }
598
+
599
+ err := provider .createCFStack (context .Background (), & stackSpec {name : "stack" , template : "<template>" })
600
+ if tc .s3UploaderErr != nil {
601
+ require .Error (t , err )
602
+ return
603
+ } else {
604
+ require .NoError (t , err )
605
+ }
606
+
607
+ if tc .cfTemplateBucket != "" {
608
+ require .NotEqual (t , cloudformationAPI .templateURL , "" )
609
+ require .Equal (t , cloudformationAPI .templateBody , "" )
610
+ } else {
611
+ require .Equal (t , cloudformationAPI .templateURL , "" )
612
+ require .NotEqual (t , cloudformationAPI .templateBody , "" )
613
+ }
614
+
615
+ err = provider .updateCFStack (context .Background (), & stackSpec {name : "stack" , template : "<template>" })
616
+ if tc .s3UploaderErr != nil {
617
+ require .Error (t , err )
618
+ return
619
+ } else {
620
+ require .NoError (t , err )
621
+ }
622
+
623
+ if tc .cfTemplateBucket != "" {
624
+ require .NotEqual (t , cloudformationAPI .templateURL , "" )
625
+ require .Equal (t , cloudformationAPI .templateBody , "" )
626
+ } else {
627
+ require .Equal (t , cloudformationAPI .templateURL , "" )
628
+ require .NotEqual (t , cloudformationAPI .templateBody , "" )
629
+ }
630
+ })
631
+ }
632
+ }
633
+
558
634
func TestCloudformationHasTags (tt * testing.T ) {
559
635
for _ , tc := range []struct {
560
636
msg string
0 commit comments