From c0c31de0c8fd72b6b7bd69cbcbb9d3051d4ce2d7 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 30 Jun 2020 17:15:52 +0200 Subject: [PATCH] testcase to check service mapping Signed-off-by: Nicolas De Loof --- ecs/pkg/amazon/backend/cloudformation.go | 15 +++++++ ecs/pkg/amazon/backend/cloudformation_test.go | 44 ++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/ecs/pkg/amazon/backend/cloudformation.go b/ecs/pkg/amazon/backend/cloudformation.go index 98b9b00bb..3df5b8ab1 100644 --- a/ecs/pkg/amazon/backend/cloudformation.go +++ b/ecs/pkg/amazon/backend/cloudformation.go @@ -43,6 +43,19 @@ func (c *FargateCompatibilityChecker) CheckPortsPublished(p *types.ServicePortCo } } +func (c *FargateCompatibilityChecker) CheckCapAdd(service *types.ServiceConfig) { + add := []string{} + for _, cap := range service.CapAdd { + switch cap { + case "SYS_PTRACE": + add = append(add, cap) + default: + c.Error("service.cap_add = %s", cap) + } + } + service.CapAdd = add +} + // Convert a compose project into a CloudFormation template func (b Backend) Convert(project *types.Project) (*cloudformation.Template, error) { var checker compatibility.Checker = &FargateCompatibilityChecker{ @@ -50,9 +63,11 @@ func (b Backend) Convert(project *types.Project) (*cloudformation.Template, erro Supported: []string{ "services.command", "services.container_name", + "services.cap_drop", "services.depends_on", "services.entrypoint", "services.environment", + "services.init", "services.healthcheck", "services.healthcheck.interval", "services.healthcheck.start_period", diff --git a/ecs/pkg/amazon/backend/cloudformation_test.go b/ecs/pkg/amazon/backend/cloudformation_test.go index fec167203..2fcd15420 100644 --- a/ecs/pkg/amazon/backend/cloudformation_test.go +++ b/ecs/pkg/amazon/backend/cloudformation_test.go @@ -5,16 +5,16 @@ import ( "reflect" "testing" - "github.com/docker/ecs-plugin/pkg/compose" - "github.com/aws/aws-sdk-go/service/elbv2" "github.com/awslabs/goformation/v4/cloudformation" "github.com/awslabs/goformation/v4/cloudformation/ec2" + "github.com/awslabs/goformation/v4/cloudformation/ecs" "github.com/awslabs/goformation/v4/cloudformation/elasticloadbalancingv2" "github.com/awslabs/goformation/v4/cloudformation/iam" "github.com/compose-spec/compose-go/cli" "github.com/compose-spec/compose-go/loader" "github.com/compose-spec/compose-go/types" + "github.com/docker/ecs-plugin/pkg/compose" "gotest.tools/v3/assert" "gotest.tools/v3/golden" ) @@ -108,6 +108,46 @@ services: assert.Check(t, lb.Type == elbv2.LoadBalancerTypeEnumNetwork) } +func TestServiceMapping(t *testing.T) { + template := convertYaml(t, ` +version: "3" +services: + test: + image: "image" + command: "command" + entrypoint: "entrypoint" + environment: + - "FOO=BAR" + cap_add: + - SYS_PTRACE + cap_drop: + - SYSLOG + init: true + user: "user" + working_dir: "working_dir" +`) + def := template.Resources["TestTaskDefinition"].(*ecs.TaskDefinition) + container := def.ContainerDefinitions[0] + assert.Equal(t, container.Image, "docker.io/library/image") + assert.Equal(t, container.Command[0], "command") + assert.Equal(t, container.EntryPoint[0], "entrypoint") + assert.Equal(t, get(container.Environment, "FOO"), "BAR") + assert.Check(t, container.LinuxParameters.InitProcessEnabled) + assert.Equal(t, container.LinuxParameters.Capabilities.Add[0], "SYS_PTRACE") + assert.Equal(t, container.LinuxParameters.Capabilities.Drop[0], "SYSLOG") + assert.Equal(t, container.User, "user") + assert.Equal(t, container.WorkingDirectory, "working_dir") +} + +func get(l []ecs.TaskDefinition_KeyValuePair, name string) string { + for _, e := range l { + if e.Name == name { + return e.Value + } + } + return "" +} + func TestResourcesHaveProjectTagSet(t *testing.T) { template := convertYaml(t, ` version: "3"