@@ -31,6 +31,10 @@ import (
31
31
"github.com/urfave/cli"
32
32
)
33
33
34
+ const (
35
+ arnPrefix = "arn:aws:ecs:us-west-2:accountId:task-definition/"
36
+ )
37
+
34
38
//////////////////////////
35
39
// Create Service tests //
36
40
/////////////////////////
@@ -483,7 +487,7 @@ func TestCreateWithServiceDiscoveryWithContainerNameAndPort(t *testing.T) {
483
487
484
488
//////////////////////////////////////
485
489
// Helpers for CreateService tests //
486
- //////////////////////////////////////
490
+ /////////////////////////////////////
487
491
type validateCreateServiceInputField func (* ecs.CreateServiceInput )
488
492
489
493
func createServiceTest (t * testing.T ,
@@ -498,6 +502,7 @@ func createServiceTest(t *testing.T,
498
502
taskDefID := "taskDefinitionId"
499
503
taskDefArn , taskDefinition , registerTaskDefResponse := getTestTaskDef (taskDefID )
500
504
505
+ // Mock ECS calls
501
506
mockEcs := mock_ecs .NewMockECSClient (ctrl )
502
507
gomock .InOrder (
503
508
mockEcs .EXPECT ().RegisterTaskDefinitionIfNeeded (
@@ -516,7 +521,6 @@ func createServiceTest(t *testing.T,
516
521
)
517
522
518
523
cliContext := cli .NewContext (nil , flagSet , nil )
519
-
520
524
context := & context.ECSContext {
521
525
ECSClient : mockEcs ,
522
526
CommandConfig : commandConfig ,
@@ -556,7 +560,6 @@ func getCreateServiceWithDelayMockClient(t *testing.T,
556
560
gomock .InOrder (
557
561
mockEcs .EXPECT ().DescribeService (gomock .Any ()).Return (getDescribeServiceTestResponse (nil ), nil ),
558
562
559
-
560
563
mockEcs .EXPECT ().RegisterTaskDefinitionIfNeeded (
561
564
gomock .Any (), // RegisterTaskDefinitionInput
562
565
gomock .Any (), // taskDefinitionCache
@@ -637,7 +640,6 @@ func TestLoadContext(t *testing.T) {
637
640
"DeploymentConfig.MaxPercent should match" )
638
641
assert .Nil (t , observedDeploymentConfig .MinimumHealthyPercent ,
639
642
"DeploymentConfig.MinimumHealthyPercent should be nil" )
640
-
641
643
}
642
644
643
645
func TestLoadContextForIncorrectInput (t * testing.T ) {
@@ -683,7 +685,6 @@ func TestServiceInfo(t *testing.T) {
683
685
}, t , true )
684
686
}
685
687
686
-
687
688
////////////////
688
689
// Run tests //
689
690
///////////////
@@ -715,7 +716,6 @@ func TestUpdateExistingServiceWithForceFlag(t *testing.T) {
715
716
716
717
flagSet := flag .NewFlagSet ("ecs-cli-up" , 0 )
717
718
flagSet .Bool (flags .ForceDeploymentFlag , forceFlagValue , "" )
718
- cliContext := cli .NewContext (nil , flagSet , nil )
719
719
720
720
// define existing service
721
721
serviceName := "test-service"
@@ -732,8 +732,7 @@ func TestUpdateExistingServiceWithForceFlag(t *testing.T) {
732
732
expectedInput .forceDeployment = forceFlagValue
733
733
734
734
// call tests
735
- upServiceWithCurrentTaskDefTest (t , cliContext , & config.CommandConfig {}, & utils.ECSParams {}, expectedInput , existingService )
736
- upServiceWithNewTaskDefTest (t , cliContext , & config.CommandConfig {}, & utils.ECSParams {}, expectedInput , existingService )
735
+ updateServiceTest (t , flagSet , & config.CommandConfig {}, & utils.ECSParams {}, expectedInput , existingService )
737
736
}
738
737
739
738
func TestUpdateExistingServiceWithNewDeploymentConfig (t * testing.T ) {
@@ -744,7 +743,6 @@ func TestUpdateExistingServiceWithNewDeploymentConfig(t *testing.T) {
744
743
flagSet := flag .NewFlagSet ("ecs-cli-up" , 0 )
745
744
flagSet .String (flags .DeploymentMaxPercentFlag , strconv .Itoa (deploymentMaxPercent ), "" )
746
745
flagSet .String (flags .DeploymentMinHealthyPercentFlag , strconv .Itoa (deploymentMinPercent ), "" )
747
- cliContext := cli .NewContext (nil , flagSet , nil )
748
746
749
747
// define existing service
750
748
serviceName := "test-service"
@@ -764,8 +762,7 @@ func TestUpdateExistingServiceWithNewDeploymentConfig(t *testing.T) {
764
762
}
765
763
766
764
// call tests
767
- upServiceWithCurrentTaskDefTest (t , cliContext , & config.CommandConfig {}, & utils.ECSParams {}, expectedInput , existingService )
768
- upServiceWithNewTaskDefTest (t , cliContext , & config.CommandConfig {}, & utils.ECSParams {}, expectedInput , existingService )
765
+ updateServiceTest (t , flagSet , & config.CommandConfig {}, & utils.ECSParams {}, expectedInput , existingService )
769
766
}
770
767
771
768
func TestUpdateExistingServiceWithNewHCGP (t * testing.T ) {
@@ -774,7 +771,6 @@ func TestUpdateExistingServiceWithNewHCGP(t *testing.T) {
774
771
775
772
flagSet := flag .NewFlagSet ("ecs-cli-up" , 0 )
776
773
flagSet .String (flags .HealthCheckGracePeriodFlag , strconv .Itoa (healthCheckGracePeriod ), "" )
777
- cliContext := cli .NewContext (nil , flagSet , nil )
778
774
779
775
// define existing service
780
776
serviceName := "test-service"
@@ -792,16 +788,14 @@ func TestUpdateExistingServiceWithNewHCGP(t *testing.T) {
792
788
expectedInput .healthCheckGracePeriod = aws .Int64 (int64 (healthCheckGracePeriod ))
793
789
794
790
// call tests
795
- upServiceWithCurrentTaskDefTest (t , cliContext , & config.CommandConfig {}, & utils.ECSParams {}, expectedInput , existingService )
796
- upServiceWithNewTaskDefTest (t , cliContext , & config.CommandConfig {}, & utils.ECSParams {}, expectedInput , existingService )
791
+ updateServiceTest (t , flagSet , & config.CommandConfig {}, & utils.ECSParams {}, expectedInput , existingService )
797
792
}
798
793
799
794
func TestUpdateExistingServiceWithDesiredCountOverOne (t * testing.T ) {
800
795
// define test values
801
796
existingDesiredCount := 2
802
797
803
798
flagSet := flag .NewFlagSet ("ecs-cli-up" , 0 )
804
- cliContext := cli .NewContext (nil , flagSet , nil )
805
799
806
800
// define existing service
807
801
serviceName := "test-service"
@@ -818,8 +812,7 @@ func TestUpdateExistingServiceWithDesiredCountOverOne(t *testing.T) {
818
812
expectedInput .count = * aws .Int64 (int64 (existingDesiredCount ))
819
813
820
814
// call tests
821
- upServiceWithCurrentTaskDefTest (t , cliContext , & config.CommandConfig {}, & utils.ECSParams {}, expectedInput , existingService )
822
- upServiceWithNewTaskDefTest (t , cliContext , & config.CommandConfig {}, & utils.ECSParams {}, expectedInput , existingService )
815
+ updateServiceTest (t , flagSet , & config.CommandConfig {}, & utils.ECSParams {}, expectedInput , existingService )
823
816
}
824
817
825
818
///////////////////////////////////////
@@ -833,15 +826,25 @@ func getDefaultUpdateInput() UpdateServiceParams {
833
826
}
834
827
}
835
828
836
- // helper for upServiceWithNewTaskDefTest and upServiceWithCurrentTaskDefTest
837
- func getUpdateServiceMockClient (t * testing.T ,
838
- ctrl * gomock.Controller ,
839
- describeServiceResponse * ecs.DescribeServicesOutput ,
840
- taskDefinition ecs.TaskDefinition ,
841
- registerTaskDefResponse ecs.TaskDefinition ,
842
- expectedInput UpdateServiceParams ) * mock_ecs.MockECSClient {
829
+ // Tests only existing services, e.g. updateService private method rather than
830
+ // the public Up method, which would potentially create a new service if it
831
+ // does not already exist.
832
+ func updateServiceTest (t * testing.T ,
833
+ flagSet * flag.FlagSet ,
834
+ commandConfig * config.CommandConfig ,
835
+ ecsParams * utils.ECSParams ,
836
+ expectedInput UpdateServiceParams ,
837
+ existingService * ecs.Service ) {
838
+
839
+ ctrl := gomock .NewController (t )
840
+ defer ctrl .Finish ()
841
+
842
+ taskDefID := entity .GetIdFromArn (existingService .TaskDefinition )
843
+ taskDefArn , taskDefinition , registerTaskDefResponse := getTestTaskDef (taskDefID )
843
844
845
+ // Mock ECS calls
844
846
mockEcs := mock_ecs .NewMockECSClient (ctrl )
847
+ describeServiceResponse := getDescribeServiceTestResponse (existingService )
845
848
gomock .InOrder (
846
849
mockEcs .EXPECT ().DescribeService (gomock .Any ()).Return (describeServiceResponse , nil ),
847
850
@@ -869,43 +872,16 @@ func getUpdateServiceMockClient(t *testing.T,
869
872
870
873
}).Return (nil ),
871
874
)
872
- return mockEcs
873
- }
874
-
875
- func upServiceWithCurrentTaskDefTest (t * testing.T ,
876
- cliContext * cli.Context ,
877
- commandConfig * config.CommandConfig ,
878
- ecsParams * utils.ECSParams ,
879
- expectedInput UpdateServiceParams ,
880
- existingService * ecs.Service ) {
881
-
882
- ctrl := gomock .NewController (t )
883
- defer ctrl .Finish ()
884
-
885
- // set up expected task def
886
- taskDefID := "taskDefinitionId"
887
- if existingService != nil {
888
- // set to existing if provided
889
- taskDefID = entity .GetIdFromArn (existingService .TaskDefinition )
890
- }
891
- taskDefArn , taskDefinition , registerTaskDefResponse := getTestTaskDef (taskDefID )
892
-
893
- // set up DescribeService() response
894
- describeServiceResponse := getDescribeServiceTestResponse (existingService )
895
-
896
- mockEcs := getUpdateServiceMockClient (t , ctrl , describeServiceResponse ,
897
- taskDefinition , registerTaskDefResponse , expectedInput )
898
875
876
+ cliContext := cli .NewContext (nil , flagSet , nil )
899
877
ecsContext := & context.ECSContext {
900
878
ECSClient : mockEcs ,
901
879
CommandConfig : commandConfig ,
902
880
CLIContext : cliContext ,
903
881
ECSParams : ecsParams ,
904
882
}
905
- // if taskDef is unchanged, serviceName is taken from current context
906
- if existingService != nil {
907
- ecsContext .ProjectName = * existingService .ServiceName
908
- }
883
+
884
+ ecsContext .ProjectName = * existingService .ServiceName
909
885
service := NewService (ecsContext )
910
886
err := service .LoadContext ()
911
887
assert .NoError (t , err , "Unexpected error while loading context in update service with current task def test" )
@@ -918,52 +894,6 @@ func upServiceWithCurrentTaskDefTest(t *testing.T,
918
894
assert .Equal (t , taskDefArn , aws .StringValue (service .TaskDefinition ().TaskDefinitionArn ), "TaskDefArn should match" )
919
895
}
920
896
921
- func upServiceWithNewTaskDefTest (t * testing.T ,
922
- cliContext * cli.Context ,
923
- commandConfig * config.CommandConfig ,
924
- ecsParams * utils.ECSParams ,
925
- expectedInput UpdateServiceParams ,
926
- existingService * ecs.Service ) {
927
-
928
- ctrl := gomock .NewController (t )
929
- defer ctrl .Finish ()
930
-
931
- // set up expected (new) task def
932
- taskDefID := "newTaskDefinitionId"
933
- taskDefArn , taskDefinition , registerTaskDefResponse := getTestTaskDef (taskDefID )
934
-
935
- // expect input to include new task def
936
- expectedInput .taskDefinition = taskDefID
937
-
938
- // set up DescribeService() response
939
- describeServiceResponse := getDescribeServiceTestResponse (existingService )
940
-
941
- mockEcs := getUpdateServiceMockClient (t , ctrl , describeServiceResponse ,
942
- taskDefinition , registerTaskDefResponse , expectedInput )
943
-
944
- ecsContext := & context.ECSContext {
945
- ECSClient : mockEcs ,
946
- CommandConfig : commandConfig ,
947
- CLIContext : cliContext ,
948
- ECSParams : ecsParams ,
949
- }
950
-
951
-
952
-
953
-
954
-
955
- service := NewService (ecsContext )
956
- err := service .LoadContext ()
957
- assert .NoError (t , err , "Unexpected error while loading context in update service with new task def test" )
958
-
959
- service .SetTaskDefinition (& taskDefinition )
960
- err = service .Up ()
961
- assert .NoError (t , err , "Unexpected error on service up with new task def" )
962
-
963
- // task definition should be set
964
- assert .Equal (t , taskDefArn , aws .StringValue (service .TaskDefinition ().TaskDefinitionArn ), "TaskDefArn should match" )
965
- }
966
-
967
897
func getDescribeServiceTestResponse (existingService * ecs.Service ) * ecs.DescribeServicesOutput {
968
898
if existingService != nil {
969
899
return & ecs.DescribeServicesOutput {
@@ -985,16 +915,16 @@ func getDescribeServiceTestResponse(existingService *ecs.Service) *ecs.DescribeS
985
915
}
986
916
987
917
func getTestTaskDef (taskDefID string ) (taskDefArn string , taskDefinition , registerTaskDefResponse ecs.TaskDefinition ) {
988
- taskDefArn = "arn/" + taskDefID
918
+ taskDefArn = arnPrefix + taskDefID
989
919
taskDefinition = ecs.TaskDefinition {
990
- Family : aws .String ("family" ),
920
+ Family : aws .String (taskDefID ),
991
921
ContainerDefinitions : []* ecs.ContainerDefinition {},
992
922
Volumes : []* ecs.Volume {},
993
923
}
994
924
registerTaskDefResponse = taskDefinition
995
925
registerTaskDefResponse .TaskDefinitionArn = aws .String (taskDefArn )
996
926
997
- return taskDefArn , taskDefinition , registerTaskDefResponse
927
+ return
998
928
}
999
929
1000
930
func verifyTaskDefinitionInput (t * testing.T ,
0 commit comments