@@ -731,6 +731,160 @@ task_definition:
731
731
}
732
732
}
733
733
734
+ func TestConvertToTaskDefinitionWithECSParams_ContainerResourcesPresent (t * testing.T ) {
735
+ containerConfigs := testContainerConfigs ([]string {"mysql" , "wordpress" })
736
+
737
+ mysqlCPU := int64 (100 )
738
+ mysqlMem := int64 (15 )
739
+ mysqlMemRes := int64 (10 )
740
+
741
+ wordpressCPU := int64 (4 )
742
+ wordpressMem := int64 (8 )
743
+ wordpressMemRes := int64 (5 )
744
+
745
+ ecsParamsString := `version: 1
746
+ task_definition:
747
+ services:
748
+ mysql:
749
+ cpu_shares: 100
750
+ mem_limit: 15m
751
+ mem_reservation: 10m
752
+ wordpress:
753
+ cpu_shares: 4
754
+ mem_limit: 8m
755
+ mem_reservation: 5m`
756
+
757
+ content := []byte (ecsParamsString )
758
+
759
+ tmpfile , err := ioutil .TempFile ("" , "ecs-params" )
760
+ assert .NoError (t , err , "Could not create ecs fields tempfile" )
761
+
762
+ defer os .Remove (tmpfile .Name ())
763
+
764
+ _ , err = tmpfile .Write (content )
765
+ assert .NoError (t , err , "Could not write data to ecs fields tempfile" )
766
+
767
+ err = tmpfile .Close ()
768
+ assert .NoError (t , err , "Could not close tempfile" )
769
+
770
+ ecsParamsFileName := tmpfile .Name ()
771
+ ecsParams , err := ReadECSParams (ecsParamsFileName )
772
+ assert .NoError (t , err , "Could not read ECS Params file" )
773
+
774
+ taskDefinition , err := convertToTaskDefWithEcsParamsInTest (t , containerConfigs , "" , ecsParams )
775
+
776
+ containerDefs := taskDefinition .ContainerDefinitions
777
+ mysql := findContainerByName ("mysql" , containerDefs )
778
+ wordpress := findContainerByName ("wordpress" , containerDefs )
779
+
780
+ if assert .NoError (t , err ) {
781
+ assert .Equal (t , mysqlCPU , aws .Int64Value (mysql .Cpu ), "Expected CPU to match" )
782
+ assert .Equal (t , mysqlMem , aws .Int64Value (mysql .Memory ), "Expected Memory to match" )
783
+ assert .Equal (t , mysqlMemRes , aws .Int64Value (mysql .MemoryReservation ), "Expected MemoryReservation to match" )
784
+
785
+ assert .Equal (t , wordpressCPU , aws .Int64Value (wordpress .Cpu ), "Expected CPU to match" )
786
+ assert .Equal (t , wordpressMem , aws .Int64Value (wordpress .Memory ), "Expected Memory to match" )
787
+ assert .Equal (t , wordpressMemRes , aws .Int64Value (wordpress .MemoryReservation ), "Expected MemoryReservation to match" )
788
+ }
789
+ }
790
+
791
+ func TestConvertToTaskDefinitionWithECSParams_ContainerResourcesOverrideProvidedVals (t * testing.T ) {
792
+ containerConfig := & adapter.ContainerConfig {
793
+ Name : "web" ,
794
+ Image : "httpd" ,
795
+ CPU : int64 (2 ),
796
+ Memory : int64 (3 ),
797
+ MemoryReservation : int64 (3 ),
798
+ }
799
+
800
+ // define ecs-params values we expect to override containerConfig vals
801
+ webCPU := int64 (5 )
802
+ webMem := int64 (15 )
803
+ webMemRes := int64 (10 )
804
+
805
+ ecsParamsString := `version: 1
806
+ task_definition:
807
+ services:
808
+ web:
809
+ cpu_shares: 5
810
+ mem_limit: 15m
811
+ mem_reservation: 10m`
812
+
813
+ content := []byte (ecsParamsString )
814
+
815
+ tmpfile , err := ioutil .TempFile ("" , "ecs-params" )
816
+ assert .NoError (t , err , "Could not create ecs fields tempfile" )
817
+
818
+ defer os .Remove (tmpfile .Name ())
819
+
820
+ _ , err = tmpfile .Write (content )
821
+ assert .NoError (t , err , "Could not write data to ecs fields tempfile" )
822
+
823
+ err = tmpfile .Close ()
824
+ assert .NoError (t , err , "Could not close tempfile" )
825
+
826
+ ecsParamsFileName := tmpfile .Name ()
827
+ ecsParams , err := ReadECSParams (ecsParamsFileName )
828
+ assert .NoError (t , err , "Could not read ECS Params file" )
829
+
830
+ containerConfigs := []adapter.ContainerConfig {* containerConfig }
831
+ taskDefinition , err := convertToTaskDefWithEcsParamsInTest (t , containerConfigs , "" , ecsParams )
832
+
833
+ containerDefs := taskDefinition .ContainerDefinitions
834
+ web := findContainerByName ("web" , containerDefs )
835
+
836
+ if assert .NoError (t , err ) {
837
+ assert .Equal (t , webCPU , aws .Int64Value (web .Cpu ), "Expected CPU to match" )
838
+ assert .Equal (t , webMem , aws .Int64Value (web .Memory ), "Expected Memory to match" )
839
+ assert .Equal (t , webMemRes , aws .Int64Value (web .MemoryReservation ), "Expected MemoryReservation to match" )
840
+ }
841
+ }
842
+
843
+ func TestConvertToTaskDefinitionWithECSParams_NoMemoryProvided (t * testing.T ) {
844
+ containerConfig := & adapter.ContainerConfig {
845
+ Name : "web" ,
846
+ Image : "httpd" ,
847
+ }
848
+
849
+ // define ecs-params values we expect to override containerConfig vals
850
+ webCPU := int64 (5 )
851
+
852
+ ecsParamsString := `version: 1
853
+ task_definition:
854
+ services:
855
+ web:
856
+ cpu_shares: 5`
857
+
858
+ content := []byte (ecsParamsString )
859
+
860
+ tmpfile , err := ioutil .TempFile ("" , "ecs-params" )
861
+ assert .NoError (t , err , "Could not create ecs fields tempfile" )
862
+
863
+ defer os .Remove (tmpfile .Name ())
864
+
865
+ _ , err = tmpfile .Write (content )
866
+ assert .NoError (t , err , "Could not write data to ecs fields tempfile" )
867
+
868
+ err = tmpfile .Close ()
869
+ assert .NoError (t , err , "Could not close tempfile" )
870
+
871
+ ecsParamsFileName := tmpfile .Name ()
872
+ ecsParams , err := ReadECSParams (ecsParamsFileName )
873
+ assert .NoError (t , err , "Could not read ECS Params file" )
874
+
875
+ containerConfigs := []adapter.ContainerConfig {* containerConfig }
876
+ taskDefinition , err := convertToTaskDefWithEcsParamsInTest (t , containerConfigs , "" , ecsParams )
877
+
878
+ containerDefs := taskDefinition .ContainerDefinitions
879
+ web := findContainerByName ("web" , containerDefs )
880
+
881
+ if assert .NoError (t , err ) {
882
+ assert .Equal (t , webCPU , aws .Int64Value (web .Cpu ), "Expected CPU to match" )
883
+ assert .Equal (t , int64 (defaultMemLimit ), aws .Int64Value (web .Memory ), "Expected Memory to match default" )
884
+ assert .Empty (t , aws .Int64Value (web .MemoryReservation ), "Expected MemoryReservation to be empty" )
885
+ }
886
+ }
887
+
734
888
func TestConvertToTaskDefinition_WithTaskSize (t * testing.T ) {
735
889
containerConfigs := testContainerConfigs ([]string {"mysql" , "wordpress" })
736
890
ecsParamsString := `version: 1
0 commit comments