Skip to content

Commit 850cc60

Browse files
SoManyHsallisaurus
authored andcommitted
Replace ServiceConfig with ContainerConfig in converter
1 parent 60dafde commit 850cc60

File tree

6 files changed

+596
-571
lines changed

6 files changed

+596
-571
lines changed

ecs-cli/modules/cli/compose/project/project.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Project interface {
3737

3838
Context() *context.ECSContext
3939
ServiceConfigs() *config.ServiceConfigs
40-
ContainerConfigs() []containerconfig.ContainerConfig
40+
ContainerConfigs() []containerconfig.ContainerConfig // TODO change this to a pointer to slice?
4141
Entity() entity.ProjectEntity
4242

4343
// commands
@@ -184,7 +184,8 @@ func (p *ecsProject) parseECSParams() error {
184184
return nil
185185
}
186186

187-
// transformTaskDefinition converts the compose yml and ecs-params yml into an ECS task definition
187+
// transformTaskDefinition converts the compose yml and ecs-params yml into an
188+
// ECS task definition and loads it onto the project entity
188189
func (p *ecsProject) transformTaskDefinition() error {
189190
ecsContext := p.ecsContext
190191

@@ -197,7 +198,7 @@ func (p *ecsProject) transformTaskDefinition() error {
197198
taskDefinition, err := utils.ConvertToTaskDefinition(
198199
&ecsContext.Context,
199200
p.VolumeConfigs(),
200-
p.ServiceConfigs(),
201+
p.ContainerConfigs(), // TODO Change to pointer on project?
201202
taskRoleArn,
202203
requiredCompatibilities,
203204
ecsContext.ECSParams,

ecs-cli/modules/cli/compose/project/project_parseV1V2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func convertV1V2ToContainerConfig(context *project.Context, serviceName string,
105105
Entrypoint: service.Entrypoint,
106106
Environment: environment,
107107
ExtraHosts: extraHosts,
108-
Hostname: service.Hostname,
108+
Hostname: service.Hostname, // only set if not blank?
109109
Image: service.Image,
110110
Links: service.Links,
111111
LogConfiguration: logConfiguration,

ecs-cli/modules/cli/compose/project/project_parseV1V2_test.go

Lines changed: 95 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ func TestParseV1V2_Version1_HappyPath(t *testing.T) {
7474
securityOpts := []string{"label:type:test_virt"}
7575
shmSize := int64(128)
7676
user := "user"
77+
ulimits := []*ecs.Ulimit{
78+
{
79+
Name: aws.String("nproc"),
80+
HardLimit: aws.Int64(65535),
81+
SoftLimit: aws.Int64(65535),
82+
},
83+
{
84+
Name: aws.String("nofile"),
85+
HardLimit: aws.Int64(40000),
86+
SoftLimit: aws.Int64(20000),
87+
},
88+
}
7789
volumesFrom := []*ecs.VolumeFrom{
7890
{
7991
ReadOnly: aws.Bool(true),
@@ -115,7 +127,10 @@ func TestParseV1V2_Version1_HappyPath(t *testing.T) {
115127
- label:type:test_virt
116128
shm_size: 128M
117129
ulimits:
118-
nofile: 1024
130+
nproc: 65535
131+
nofile:
132+
soft: 20000
133+
hard: 40000
119134
user: user
120135
volumes:
121136
- ./code
@@ -160,7 +175,7 @@ redis:
160175
assert.Equal(t, cpuShares, web.CPU, "Expected CPU to match")
161176
assert.Equal(t, dnsSearchDomains, web.DNSSearchDomains, "Expected DNSSearchDomains to match")
162177
assert.Equal(t, dnsServers, web.DNSServers, "Expected DNSServers to match")
163-
assert.Equal(t, labels, web.DockerLabels, "Expected docker labels to match")
178+
assert.Equal(t, labels, web.DockerLabels, "Expected DockerLabels to match")
164179
assert.Equal(t, securityOpts, web.DockerSecurityOptions, "Expected DockerSecurityOptions to match")
165180
assert.Equal(t, entryPoint, web.Entrypoint, "Expected EntryPoint to be match")
166181
assert.Equal(t, env, web.Environment, "Expected Environment to match")
@@ -169,13 +184,14 @@ redis:
169184
assert.Equal(t, webImage, web.Image, "Expected Image to match")
170185
assert.Equal(t, links, web.Links, "Expected Links to match")
171186
assert.Equal(t, logging, web.LogConfiguration, "Expected LogConfiguration to match")
172-
assert.Equal(t, memory, web.Memory, "Expected memory to match")
187+
assert.Equal(t, memory, web.Memory, "Expected Memory to match")
173188
assert.Equal(t, mountPoints, web.MountPoints, "Expected MountPoints to match")
174-
assert.Equal(t, ports, web.PortMappings, "Expected PortMappings to match")
189+
assert.ElementsMatch(t, ports, web.PortMappings, "Expected PortMappings to match")
175190
assert.Equal(t, privileged, web.Privileged, "Expected Privileged to match")
176191
assert.Equal(t, readonly, web.ReadOnly, "Expected ReadOnly to match")
177192
assert.Equal(t, shmSize, web.ShmSize, "Expected ShmSize to match")
178-
assert.Equal(t, user, web.User, "Expected user to match")
193+
assert.Equal(t, user, web.User, "Expected User to match")
194+
assert.ElementsMatch(t, ulimits, web.Ulimits, "Expected Ulimits to match")
179195
assert.Equal(t, workingDir, web.WorkingDirectory, "Expected WorkingDirectory to match")
180196
}
181197

@@ -232,8 +248,7 @@ func TestParseV1V2_Version2Files(t *testing.T) {
232248
Size: aws.Int64(64),
233249
},
234250
}
235-
volumesFrom := []*ecs.VolumeFrom{
236-
{
251+
volumesFrom := []*ecs.VolumeFrom{ {
237252
ReadOnly: aws.Bool(false),
238253
SourceContainer: aws.String("mysql"),
239254
},
@@ -367,6 +382,79 @@ func TestParseV1V2_Version1_WithEnvFile(t *testing.T) {
367382
assert.Equal(t, expectedEnv, web.Environment, "Expected Environment to match")
368383
}
369384

385+
func TestParseV1V2_WithEnvFromShell(t *testing.T) {
386+
// If the value for a key in the environment field is blank, it
387+
// resolves to the shell value (good for specifying secrets).
388+
// If a value is specified for a key in the environment field, it
389+
// overrides the shell value for that key.
390+
391+
envKey1 := "RACK_ENV"
392+
envValue1 := "staging"
393+
394+
envKey2 := "SHOW"
395+
envValue2 := "true"
396+
envKey3 := "SESSION_SECRET"
397+
envValue3 := "clydeIsTheGoodestDog"
398+
399+
os.Setenv(envKey1, envValue1)
400+
os.Setenv(envKey2, envValue2)
401+
os.Setenv(envKey3, envValue3)
402+
defer func() {
403+
os.Unsetenv(envKey1)
404+
os.Unsetenv(envKey2)
405+
os.Unsetenv(envKey3)
406+
}()
407+
408+
expectedEnv := []*ecs.KeyValuePair{
409+
{
410+
Name: aws.String(envKey1),
411+
Value: aws.String("development"),
412+
},
413+
{
414+
Name: aws.String(envKey2),
415+
Value: aws.String(envValue2),
416+
},
417+
{
418+
Name: aws.String(envKey3),
419+
Value: aws.String(envValue3),
420+
},
421+
}
422+
// Setup docker-compose file
423+
webImage := "webapp"
424+
composeFileString := `version: '2'
425+
services:
426+
web:
427+
image: webapp
428+
environment:
429+
- RACK_ENV=development
430+
- SHOW=
431+
- SESSION_SECRET`
432+
433+
tmpfile, err := ioutil.TempFile("", "test")
434+
assert.NoError(t, err, "Unexpected error in creating test file")
435+
436+
defer os.Remove(tmpfile.Name())
437+
438+
_, err = tmpfile.Write([]byte(composeFileString))
439+
assert.NoError(t, err, "Unexpected error in writing to test file")
440+
441+
err = tmpfile.Close()
442+
assert.NoError(t, err, "Unexpected error closing file")
443+
444+
// Set up project
445+
project := setupTestProject(t)
446+
project.ecsContext.ComposeFiles = append(project.ecsContext.ComposeFiles, tmpfile.Name())
447+
448+
actualConfigs, err := project.parseV1V2()
449+
assert.NoError(t, err, "Unexpected error parsing file")
450+
451+
// verify wordpress ServiceConfig
452+
web, err := getContainerConfigByName("web", actualConfigs)
453+
assert.NoError(t, err, "Unexpected error retrieving container config")
454+
assert.Equal(t, webImage, web.Image, "Expected Image to match")
455+
assert.Equal(t, expectedEnv, web.Environment, "Expected Environment to match")
456+
}
457+
370458
func getContainerConfigByName(name string, configs *[]containerconfig.ContainerConfig) (*containerconfig.ContainerConfig, error) {
371459
for _, config := range *configs {
372460
if config.Name == name {

0 commit comments

Comments
 (0)