|
| 1 | +// Copyright 2020 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 TestECRImageActionEventMarshaling(t *testing.T) { |
| 14 | + // 1. read JSON from file |
| 15 | + inputJSON := test.ReadJSONFromFile(t, "./testdata/ecr-image-push-event.json") |
| 16 | + |
| 17 | + // 2. de-serialize into Go object |
| 18 | + var inputEvent ECRImageActionEvent |
| 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, "13cde686-328b-6117-af20-0e5566167482", inputEvent.ID) |
| 26 | + assert.Equal(t, "ECR Image Action", inputEvent.DetailType) |
| 27 | + assert.Equal(t, "aws.ecr", inputEvent.Source) |
| 28 | + assert.Equal(t, "123456789012", inputEvent.Account) |
| 29 | + assert.Equal(t, "us-west-2", inputEvent.Region) |
| 30 | + assert.Empty(t, inputEvent.Resources) |
| 31 | + |
| 32 | + testTime, err := time.Parse(time.RFC3339, "2019-11-16T01:54:34Z") |
| 33 | + if err != nil { |
| 34 | + t.Errorf("Failed to parse time: %v", err) |
| 35 | + } |
| 36 | + assert.Equal(t, testTime, inputEvent.Time) |
| 37 | + |
| 38 | + var detail = inputEvent.Detail |
| 39 | + assert.Equal(t, "SUCCESS", detail.Result) |
| 40 | + assert.Equal(t, "my-repository-name", detail.RepositoryName) |
| 41 | + assert.Equal(t, "sha256:7f5b2640fe6fb4f46592dfd3410c4a79dac4f89e4782432e0378abcd1234", detail.ImageDigest) |
| 42 | + assert.Equal(t, "latest", detail.ImageTag) |
| 43 | + |
| 44 | + // 4. serialize to JSON |
| 45 | + outputJSON, err := json.Marshal(inputEvent) |
| 46 | + if err != nil { |
| 47 | + t.Errorf("could not marshal event. details: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + // 5. check result |
| 51 | + assert.JSONEq(t, string(inputJSON), string(outputJSON)) |
| 52 | +} |
| 53 | + |
| 54 | +func TestECRPushMarshalingMalformedJson(t *testing.T) { |
| 55 | + test.TestMalformedJson(t, ECRImageActionEvent{}) |
| 56 | +} |
0 commit comments