Skip to content

Commit dff67f2

Browse files
SoManyHsallisaurus
authored andcommitted
Set default for essential to be true on ContainerDef
Previously, the only field on a service in ecs-params.yml was essential. The desired behavior is that if essential is not specified, the default is true. However, the yaml unmarshaller by default sets any unspecified field in the yaml to the zero value, which in the case of a boolean is false. Since we did not expect customers to specify the 'services' field at all in the past unless they wished to specify essential to be false, this worked. However, with the addition of other fields (CPU, Memory) on container definitions, we need to ensure that the default for essential is still true even when not specified.
1 parent 8166fef commit dff67f2

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

ecs-cli/modules/utils/compose/convert_task_definition_test.go

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package utils
1515

1616
import (
17-
"fmt"
1817
"io/ioutil"
1918
"os"
2019
"reflect"
@@ -205,7 +204,6 @@ func TestConvertToTaskDefinition(t *testing.T) {
205204

206205
// convert
207206
taskDefinition := convertToTaskDefinitionInTest(t, nil, testContainerConfig, taskRoleArn, "")
208-
fmt.Printf("TASK DEF: %+v\n\n", taskDefinition)
209207
containerDef := *taskDefinition.ContainerDefinitions[0]
210208

211209
// verify task def fields
@@ -629,6 +627,77 @@ task_definition:
629627
assert.Error(t, err, "Expected error if no containers are marked essential")
630628
}
631629

630+
func TestConvertToTaskDefinitionWithECSParams_EssentialDefaultsToTrueWhenNoServicesSpecified(t *testing.T) {
631+
// We expect essential to be set to be true in the converter
632+
containerConfigs := testContainerConfigs([]string{"mysql", "wordpress"})
633+
ecsParamsString := `version: 1
634+
task_definition:
635+
ecs_network_mode: host`
636+
637+
content := []byte(ecsParamsString)
638+
639+
tmpfile, err := ioutil.TempFile("", "ecs-params")
640+
assert.NoError(t, err, "Could not create ecs fields tempfile")
641+
642+
defer os.Remove(tmpfile.Name())
643+
644+
_, err = tmpfile.Write(content)
645+
assert.NoError(t, err, "Could not write data to ecs fields tempfile")
646+
647+
err = tmpfile.Close()
648+
assert.NoError(t, err, "Could not close tempfile")
649+
650+
ecsParamsFileName := tmpfile.Name()
651+
ecsParams, err := ReadECSParams(ecsParamsFileName)
652+
assert.NoError(t, err, "Could not read ECS Params file")
653+
654+
taskDefinition, err := convertToTaskDefWithEcsParamsInTest(t, nil, containerConfigs, "", ecsParams)
655+
assert.NoError(t, err, "Unexpected error when no containers are marked essential")
656+
657+
mysql := findContainerByName("mysql", taskDefinition.ContainerDefinitions)
658+
assert.True(t, *mysql.Essential, "Expected mysql to be essential")
659+
wordpress := findContainerByName("wordpress", taskDefinition.ContainerDefinitions)
660+
assert.True(t, *wordpress.Essential, "Expected wordpressto be essential")
661+
}
662+
663+
func TestConvertToTaskDefinitionWithECSParams_EssentialDefaultsToTrueWhenNotSpecified(t *testing.T) {
664+
// We expect essential to be set to be true in the unmarshaller
665+
containerConfigs := testContainerConfigs([]string{"mysql", "wordpress"})
666+
ecsParamsString := `version: 1
667+
task_definition:
668+
ecs_network_mode: host
669+
services:
670+
wordpress:
671+
mem_limit: 1000000
672+
mysql:
673+
mem_limit: 1000000`
674+
675+
content := []byte(ecsParamsString)
676+
677+
tmpfile, err := ioutil.TempFile("", "ecs-params")
678+
assert.NoError(t, err, "Could not create ecs fields tempfile")
679+
680+
defer os.Remove(tmpfile.Name())
681+
682+
_, err = tmpfile.Write(content)
683+
assert.NoError(t, err, "Could not write data to ecs fields tempfile")
684+
685+
err = tmpfile.Close()
686+
assert.NoError(t, err, "Could not close tempfile")
687+
688+
ecsParamsFileName := tmpfile.Name()
689+
ecsParams, err := ReadECSParams(ecsParamsFileName)
690+
assert.NoError(t, err, "Could not read ECS Params file")
691+
692+
taskDefinition, err := convertToTaskDefWithEcsParamsInTest(t, nil, containerConfigs, "", ecsParams)
693+
694+
assert.NoError(t, err, "Unexpected error when no containers are marked essential")
695+
mysql := findContainerByName("mysql", taskDefinition.ContainerDefinitions)
696+
assert.True(t, *mysql.Essential, "Expected mysql to be essential")
697+
wordpress := findContainerByName("wordpress", taskDefinition.ContainerDefinitions)
698+
assert.True(t, *wordpress.Essential, "Expected wordpressto be essential")
699+
}
700+
632701
func TestConvertToTaskDefinitionWithECSParamsAndTaskRoleArnFlag(t *testing.T) {
633702
containerConfigs := testContainerConfigs([]string{"mysql", "wordpress"})
634703
ecsParamsString := `version: 1

ecs-cli/modules/utils/compose/ecs_params_reader.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ const (
8787
Disabled AssignPublicIp = "DISABLED"
8888
)
8989

90+
func (cd *ContainerDef) UnmarshalYAML(unmarshal func(interface{}) error) error {
91+
type rawContainerDef ContainerDef
92+
raw := rawContainerDef{Essential: true} // If essential is not specified, we want it to be true
93+
if err := unmarshal(&raw); err != nil {
94+
return err
95+
}
96+
97+
*cd = ContainerDef(raw)
98+
return nil
99+
}
100+
90101
// ReadECSParams parses the ecs-params.yml file and puts it into an ECSParams struct.
91102
func ReadECSParams(filename string) (*ECSParams, error) {
92103
if filename == "" {

0 commit comments

Comments
 (0)