| new file mode 100644 |
| index 000000000..6b998f53a |
| --- /dev/null |
| +++ b/internal/media/gate_test.go |
| @@ -0,0 +1,306 @@ |
| +package media |
| + |
| +import ( |
| + "errors" |
| + "testing" |
| +) |
| + |
| +const ( |
| + tdImage = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/carlos-media@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" |
| + tdOtherImage = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/carlos-media@sha256:fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210" |
| + tdARN = "arn:aws:ecs:eu-west-1:123456789012:task-definition/media-video-720p-short:7" |
| +) |
| + |
| +// intendedTaskDefinition is the media-engine module's task definition |
| +// (spec §5.5, plan 3's Global Constraints): built fresh on every call so |
| +// a mutation never leaks into the next case. |
| +func intendedTaskDefinition() TaskDefinition { |
| + thirty := func() *int { v := 30; return &v } |
| + return TaskDefinition{ |
| + ARN: tdARN, |
| + CPUArchitecture: "ARM64", |
| + Containers: []ContainerDef{ |
| + { |
| + Name: ContainerDeadline, Image: tdImage, User: "65534:65534", |
| + Essential: true, ReadOnlyRoot: true, DropsAll: true, |
| + StopTimeout: thirty(), |
| + }, |
| + { |
| + Name: ContainerCodec, Image: tdImage, User: "65534:65534", |
| + Essential: true, ReadOnlyRoot: true, DropsAll: true, |
| + StopTimeout: thirty(), |
| + DependsOn: []Dependency{{Container: ContainerDeadline, Condition: "START"}}, |
| + Volumes: []string{"scratch"}, |
| + }, |
| + }, |
| + } |
| +} |
| + |
| +// container answers the named container of td for a mutation to edit. |
| +func container(t *testing.T, td *TaskDefinition, name string) *ContainerDef { |
| + t.Helper() |
| + for i := range td.Containers { |
| + if td.Containers[i].Name == name { |
| + return &td.Containers[i] |
| + } |
| + } |
| + t.Fatalf("the intended definition has no %s container", name) |
| + return nil |
| +} |
| + |
| +func intPtr(v int) *int { return &v } |
| + |
| +func TestCheckTaskDefinition(t *testing.T) { |
| + if err := CheckTaskDefinition(intendedTaskDefinition()); err != nil { |
| + t.Fatalf("the module's intended definition is refused: %v", err) |
| + } |
| + // The intended definition also passes with the containers listed the |
| + // other way round, and with deadline mounting a volume of its own. |
| + swapped := intendedTaskDefinition() |
| + swapped.Containers[0], swapped.Containers[1] = swapped.Containers[1], swapped.Containers[0] |
| + if err := CheckTaskDefinition(swapped); err != nil { |
| + t.Fatalf("the intended definition in the other order is refused: %v", err) |
| + } |
| + ownVolume := intendedTaskDefinition() |
| + container(t, &ownVolume, ContainerDeadline).Volumes = []string{"timer"} |
| + if err := CheckTaskDefinition(ownVolume); err != nil { |
| + t.Fatalf("a volume deadline alone mounts is refused: %v", err) |
| + } |
| + |
| + type mutation struct { |
| + name string |
| + mutate func(t *testing.T, td *TaskDefinition) |
| + check string |
| + } |
| + cases := []mutation{ |
| + // containers (pre-flight F21) |
| + {"a third container", func(t *testing.T, td *TaskDefinition) { |
| + td.Containers = append(td.Containers, ContainerDef{Name: "sidecar", Image: tdImage, User: "65534", Essential: true, ReadOnlyRoot: true, DropsAll: true, StopTimeout: intPtr(30)}) |
| + }, "containers"}, |
| + {"codec renamed", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).Name = "codex" |
| + }, "containers"}, |
| + {"deadline renamed", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).Name = "timer" |
| + }, "containers"}, |
| + {"codec only", func(t *testing.T, td *TaskDefinition) { |
| + td.Containers = []ContainerDef{*container(t, td, ContainerCodec)} |
| + }, "containers"}, |
| + {"two codecs", func(t *testing.T, td *TaskDefinition) { |
| + c := *container(t, td, ContainerCodec) |
| + td.Containers = []ContainerDef{c, c} |
| + }, "containers"}, |
| + {"a second deadline beside the pair", func(t *testing.T, td *TaskDefinition) { |
| + td.Containers = append(td.Containers, *container(t, td, ContainerDeadline)) |
| + }, "containers"}, |
| + {"a second codec beside the pair", func(t *testing.T, td *TaskDefinition) { |
| + td.Containers = append(td.Containers, *container(t, td, ContainerCodec)) |
| + }, "containers"}, |
| + {"no containers", func(t *testing.T, td *TaskDefinition) { |
| + td.Containers = nil |
| + }, "containers"}, |
| + |
| + // essential (pre-flight F21) |
| + {"deadline not essential", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).Essential = false |
| + }, "essential"}, |
| + {"codec not essential", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).Essential = false |
| + }, "essential"}, |
| + |
| + // depends_on |
| + {"no dependsOn", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).DependsOn = nil |
| + }, "depends_on"}, |
| + {"condition COMPLETE", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).DependsOn[0].Condition = "COMPLETE" |
| + }, "depends_on"}, |
| + {"condition start in lower case", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).DependsOn[0].Condition = "start" |
| + }, "depends_on"}, |
| + {"codec depends on itself", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).DependsOn[0].Container = ContainerCodec |
| + }, "depends_on"}, |
| + {"codec depends on deadline twice", func(t *testing.T, td *TaskDefinition) { |
| + c := container(t, td, ContainerCodec) |
| + c.DependsOn = append(c.DependsOn, Dependency{Container: ContainerDeadline, Condition: "SUCCESS"}) |
| + }, "depends_on"}, |
| + {"deadline also depending on codec", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).DependsOn = []Dependency{{Container: ContainerCodec, Condition: "START"}} |
| + }, "depends_on"}, |
| + |
| + // stop_timeout |
| + {"codec stop timeout 29", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).StopTimeout = intPtr(29) |
| + }, "stop_timeout"}, |
| + {"codec stop timeout 120", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).StopTimeout = intPtr(120) |
| + }, "stop_timeout"}, |
| + {"codec stop timeout nil", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).StopTimeout = nil |
| + }, "stop_timeout"}, |
| + {"deadline stop timeout 29", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).StopTimeout = intPtr(29) |
| + }, "stop_timeout"}, |
| + {"deadline stop timeout nil", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).StopTimeout = nil |
| + }, "stop_timeout"}, |
| + |
| + // task_role |
| + {"a task role", func(t *testing.T, td *TaskDefinition) { |
| + td.TaskRoleARN = "arn:aws:iam::123456789012:role/media-task" |
| + }, "task_role"}, |
| + |
| + // pid_mode |
| + {"pidMode task", func(t *testing.T, td *TaskDefinition) { |
| + td.PIDMode = "task" |
| + }, "pid_mode"}, |
| + |
| + // shared_volume |
| + {"a volume in both", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).Volumes = []string{"timer", "scratch"} |
| + }, "shared_volume"}, |
| + |
| + // image |
| + {":latest instead of a digest", func(t *testing.T, td *TaskDefinition) { |
| + const latest = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/carlos-media:latest" |
| + container(t, td, ContainerDeadline).Image = latest |
| + container(t, td, ContainerCodec).Image = latest |
| + }, "image"}, |
| + {"two different digests", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).Image = tdOtherImage |
| + }, "image"}, |
| + {"a truncated digest", func(t *testing.T, td *TaskDefinition) { |
| + const short = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/carlos-media@sha256:0123" |
| + container(t, td, ContainerDeadline).Image = short |
| + container(t, td, ContainerCodec).Image = short |
| + }, "image"}, |
| + {"an empty digest", func(t *testing.T, td *TaskDefinition) { |
| + const empty = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/carlos-media@sha256:" |
| + container(t, td, ContainerDeadline).Image = empty |
| + container(t, td, ContainerCodec).Image = empty |
| + }, "image"}, |
| + {"a digest with characters that are not hex", func(t *testing.T, td *TaskDefinition) { |
| + const bad = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/carlos-media@sha256:0123456789ABCDEF0123456789abcdef0123456789abcdef0123456789abcdef" |
| + container(t, td, ContainerDeadline).Image = bad |
| + container(t, td, ContainerCodec).Image = bad |
| + }, "image"}, |
| + {"a digest with no repository", func(t *testing.T, td *TaskDefinition) { |
| + const bare = "@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" |
| + container(t, td, ContainerDeadline).Image = bare |
| + container(t, td, ContainerCodec).Image = bare |
| + }, "image"}, |
| + {"no image", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).Image = "" |
| + container(t, td, ContainerCodec).Image = "" |
| + }, "image"}, |
| + |
| + // user |
| + {"codec user 0", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "0" |
| + }, "user"}, |
| + {"deadline user 0", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).User = "0" |
| + }, "user"}, |
| + {"codec user root", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "root" |
| + }, "user"}, |
| + {"codec user 0:65534", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "0:65534" |
| + }, "user"}, |
| + {"codec user root:root", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "root:root" |
| + }, "user"}, |
| + {"codec user 00", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "00" |
| + }, "user"}, |
| + {"codec user +0", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "+0:65534" |
| + }, "user"}, |
| + {"codec user 2^32, which a 32-bit truncation makes 0", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "4294967296" |
| + }, "user"}, |
| + {"codec user beyond int64", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "99999999999999999999" |
| + }, "user"}, |
| + {"codec user unset", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "" |
| + }, "user"}, |
| + {"codec user :65534", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = ":65534" |
| + }, "user"}, |
| + |
| + // read_only |
| + {"codec writable root", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).ReadOnlyRoot = false |
| + }, "read_only"}, |
| + {"deadline writable root", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).ReadOnlyRoot = false |
| + }, "read_only"}, |
| + |
| + // capabilities |
| + {"codec capabilities not dropped", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).DropsAll = false |
| + }, "capabilities"}, |
| + {"deadline capabilities not dropped", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).DropsAll = false |
| + }, "capabilities"}, |
| + |
| + // arm64 |
| + {"X86_64", func(t *testing.T, td *TaskDefinition) { |
| + td.CPUArchitecture = "X86_64" |
| + }, "arm64"}, |
| + {"no architecture", func(t *testing.T, td *TaskDefinition) { |
| + td.CPUArchitecture = "" |
| + }, "arm64"}, |
| + } |
| + for _, c := range cases { |
| + t.Run(c.name, func(t *testing.T) { |
| + td := intendedTaskDefinition() |
| + c.mutate(t, &td) |
| + err := CheckTaskDefinition(td) |
| + var mis *Misconfigured |
| + if !errors.As(err, &mis) { |
| + t.Fatalf("CheckTaskDefinition = %v, want *Misconfigured{Check: %q}", err, c.check) |
| + } |
| + if mis.Check != c.check || mis.Revision != tdARN { |
| + t.Fatalf("CheckTaskDefinition = %+v, want Revision %q, Check %q", *mis, tdARN, c.check) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// TestCheckTaskDefinitionOrder: a definition failing several checks is |
| +// refused for the first of them in the brief's order. Each step applies |
| +// one more check's mutation on top of the later ones'. |
| +func TestCheckTaskDefinitionOrder(t *testing.T) { |
| + steps := []struct { |
| + check string |
| + mutate func(t *testing.T, td *TaskDefinition) |
| + }{ |
| + {"containers", func(t *testing.T, td *TaskDefinition) { |
| + td.Containers = append(td.Containers, ContainerDef{Name: "sidecar"}) |
| + }}, |
| + {"essential", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerDeadline).Essential = false }}, |
| + {"depends_on", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerCodec).DependsOn = nil }}, |
| + {"stop_timeout", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerCodec).StopTimeout = nil }}, |
| + {"task_role", func(t *testing.T, td *TaskDefinition) { td.TaskRoleARN = "arn:aws:iam::123456789012:role/x" }}, |
| + {"pid_mode", func(t *testing.T, td *TaskDefinition) { td.PIDMode = "task" }}, |
| + {"shared_volume", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).Volumes = []string{"scratch"} |
| + }}, |
| + {"image", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerCodec).Image = tdOtherImage }}, |
| + {"user", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerCodec).User = "0" }}, |
| + {"read_only", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerCodec).ReadOnlyRoot = false }}, |
| + {"capabilities", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerCodec).DropsAll = false }}, |
| + {"arm64", func(t *testing.T, td *TaskDefinition) { td.CPUArchitecture = "X86_64" }}, |
| + } |
| + td := intendedTaskDefinition() |
| + for i := len(steps) - 1; i >= 0; i-- { |
| + steps[i].mutate(t, &td) |
| + var mis *Misconfigured |
| + if err := CheckTaskDefinition(td); !errors.As(err, &mis) || mis.Check != steps[i].check { |
| + t.Fatalf("with checks %s onward failing, CheckTaskDefinition = %v, want %q", steps[i].check, err, steps[i].check) |
| + } |
| + } |
| +} |