|
| 1 | +// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +package events |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/json" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/aws/aws-lambda-go/events/test" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestECSContainerInstanceEventMarshaling(t *testing.T) { |
| 14 | + // 1. read JSON from file |
| 15 | + inputJSON := test.ReadJSONFromFile(t, "./testdata/ecs-container-instance-state-change.json") |
| 16 | + |
| 17 | + // 2. de-serialize into Go object |
| 18 | + var inputEvent ECSContainerInstanceEvent |
| 19 | + if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { |
| 20 | + t.Errorf("could not unmarshal event. details: %v", err) |
| 21 | + } |
| 22 | + |
| 23 | + // 3. Verify values populated into Go Object, at least one validation per data type |
| 24 | + assert.Equal(t, "0", inputEvent.Version) |
| 25 | + assert.Equal(t, "8952ba83-7be2-4ab5-9c32-6687532d15a2", inputEvent.ID) |
| 26 | + assert.Equal(t, "ECS Container Instance State Change", inputEvent.DetailType) |
| 27 | + assert.Equal(t, "aws.ecs", inputEvent.Source) |
| 28 | + assert.Equal(t, "111122223333", inputEvent.Account) |
| 29 | + assert.Equal(t, "us-east-1", inputEvent.Region) |
| 30 | + assert.Equal(t, "arn:aws:ecs:us-east-1:111122223333:container-instance/b54a2a04-046f-4331-9d74-3f6d7f6ca315", inputEvent.Resources[0]) |
| 31 | + testTime, err := time.Parse(time.RFC3339, "2016-12-06T16:41:06Z") |
| 32 | + if err != nil { |
| 33 | + t.Errorf("Failed to parse time: %v", err) |
| 34 | + } |
| 35 | + assert.Equal(t, testTime, inputEvent.Time) |
| 36 | + |
| 37 | + var detail = inputEvent.Detail |
| 38 | + assert.True(t, detail.AgentConnected) |
| 39 | + assert.Equal(t, "com.amazonaws.ecs.capability.logging-driver.syslog", detail.Attributes[0].Name) |
| 40 | + assert.Equal(t, "arn:aws:ecs:us-east-1:111122223333:cluster/default", detail.ClusterARN) |
| 41 | + assert.Equal(t, "arn:aws:ecs:us-east-1:111122223333:container-instance/b54a2a04-046f-4331-9d74-3f6d7f6ca315", detail.ContainerInstanceARN) |
| 42 | + assert.Equal(t, "i-f3a8506b", detail.EC2InstanceID) |
| 43 | + assert.Equal(t, "CPU", detail.RegisteredResources[0].Name) |
| 44 | + assert.Equal(t, "INTEGER", detail.RegisteredResources[0].Type) |
| 45 | + assert.Equal(t, 2048, detail.RegisteredResources[0].IntegerValue) |
| 46 | + assert.Equal(t, "MEMORY", detail.RegisteredResources[1].Name) |
| 47 | + assert.Equal(t, "INTEGER", detail.RegisteredResources[1].Type) |
| 48 | + assert.Equal(t, 3767, detail.RegisteredResources[1].IntegerValue) |
| 49 | + assert.Equal(t, "PORTS", detail.RegisteredResources[2].Name) |
| 50 | + assert.Equal(t, "STRINGSET", detail.RegisteredResources[2].Type) |
| 51 | + assert.Equal(t, []*string{ptr("22"), ptr("2376"), ptr("2375"), ptr("51678"), ptr("51679")}, detail.RegisteredResources[2].StringSetValue) |
| 52 | + assert.Equal(t, "PORTS_UDP", detail.RegisteredResources[3].Name) |
| 53 | + assert.Equal(t, "STRINGSET", detail.RegisteredResources[3].Type) |
| 54 | + assert.Equal(t, []*string{}, detail.RegisteredResources[3].StringSetValue) |
| 55 | + assert.Equal(t, "CPU", detail.RemainingResources[0].Name) |
| 56 | + assert.Equal(t, "INTEGER", detail.RemainingResources[0].Type) |
| 57 | + assert.Equal(t, 1988, detail.RemainingResources[0].IntegerValue) |
| 58 | + assert.Equal(t, "MEMORY", detail.RemainingResources[1].Name) |
| 59 | + assert.Equal(t, "INTEGER", detail.RemainingResources[1].Type) |
| 60 | + assert.Equal(t, 767, detail.RemainingResources[1].IntegerValue) |
| 61 | + assert.Equal(t, "PORTS", detail.RemainingResources[2].Name) |
| 62 | + assert.Equal(t, "STRINGSET", detail.RemainingResources[2].Type) |
| 63 | + assert.Equal(t, []*string{ptr("22"), ptr("2376"), ptr("2375"), ptr("51678"), ptr("51679")}, detail.RemainingResources[2].StringSetValue) |
| 64 | + assert.Equal(t, "PORTS_UDP", detail.RemainingResources[3].Name) |
| 65 | + assert.Equal(t, "STRINGSET", detail.RemainingResources[3].Type) |
| 66 | + assert.Equal(t, []*string{}, detail.RemainingResources[3].StringSetValue) |
| 67 | + assert.Equal(t, "ACTIVE", detail.Status) |
| 68 | + assert.Equal(t, 14801, detail.Version) |
| 69 | + assert.Equal(t, "aebcbca", detail.VersionInfo.AgentHash) |
| 70 | + assert.Equal(t, "1.13.0", detail.VersionInfo.AgentVersion) |
| 71 | + assert.Equal(t, "DockerVersion: 1.11.2", detail.VersionInfo.DockerVersion) |
| 72 | + testUpdateTime, err := time.Parse(time.RFC3339, "2016-12-06T16:41:06Z") |
| 73 | + if err != nil { |
| 74 | + t.Errorf("Failed to parse time: %v", err) |
| 75 | + } |
| 76 | + assert.Equal(t, testUpdateTime, inputEvent.Time) |
| 77 | + |
| 78 | + // 4. serialize to JSON |
| 79 | + outputJSON, err := json.Marshal(inputEvent) |
| 80 | + if err != nil { |
| 81 | + t.Errorf("could not marshal event. details: %v", err) |
| 82 | + } |
| 83 | + |
| 84 | + // 5. check result |
| 85 | + assert.JSONEq(t, string(inputJSON), string(outputJSON)) |
| 86 | +} |
| 87 | + |
| 88 | +func ptr(s string) *string { |
| 89 | + return &s |
| 90 | +} |
| 91 | + |
| 92 | +func TestECSContainerInstanceMarshalingMalformedJson(t *testing.T) { |
| 93 | + test.TestMalformedJson(t, ECSContainerInstanceEvent{}) |
| 94 | +} |
0 commit comments