Skip to content

Commit a906820

Browse files
committed
Added ECRImageActionEvent and test
1 parent 0d45ea2 commit a906820

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

events/ecr_image_action.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package events
2+
3+
type ECRImageActionEvent struct {
4+
Version string `json:"version"`
5+
ID string `json:"id"`
6+
DetailType string `json:"detail-type"`
7+
Source string `json:"source"`
8+
Account string `json:"account"`
9+
Time string `json:"time"`
10+
Region string `json:"region"`
11+
Resources []string `json:"resources"`
12+
Detail ECRImageActionEventDetailType `json:"detail"`
13+
}
14+
15+
type ECRImageActionEventDetailType struct {
16+
Result string `json:"result"`
17+
RepositoryName string `json:"repository-name"`
18+
ImageDigest string `json:"image-digest"`
19+
ActionType string `json:"action-type"`
20+
ImageTag string `json:"image-tag"`
21+
}

events/ecr_image_action_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
package events
3+
4+
import (
5+
"encoding/json"
6+
"testing"
7+
8+
"github.com/aws/aws-lambda-go/events/test"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestECRImageActionEventMarshaling(t *testing.T) {
13+
// 1. read JSON from file
14+
inputJSON := test.ReadJSONFromFile(t, "./testdata/ecr-image-push-event.json")
15+
16+
// 2. de-serialize into Go object
17+
var inputEvent ECRImageActionEvent
18+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
19+
t.Errorf("could not unmarshal event. details: %v", err)
20+
}
21+
22+
// 3. Verify values populated into Go Object, at least one validation per data type
23+
assert.Equal(t, "0", inputEvent.Version)
24+
assert.Equal(t, "13cde686-328b-6117-af20-0e5566167482", inputEvent.ID)
25+
assert.Equal(t, "ECR Image Action", inputEvent.DetailType)
26+
assert.Equal(t, "aws.ecr", inputEvent.Source)
27+
assert.Equal(t, "123456789012", inputEvent.Account)
28+
assert.Equal(t, "2019-11-16T01:54:34Z", inputEvent.Time)
29+
assert.Equal(t, "us-west-2", inputEvent.Region)
30+
assert.Empty(t, inputEvent.Resources)
31+
32+
var detail = inputEvent.Detail
33+
assert.Equal(t, "SUCCESS", detail.Result)
34+
assert.Equal(t, "my-repository-name", detail.RepositoryName)
35+
assert.Equal(t, "sha256:7f5b2640fe6fb4f46592dfd3410c4a79dac4f89e4782432e0378abcd1234", detail.ImageDigest)
36+
assert.Equal(t, "latest", detail.ImageTag)
37+
38+
// 4. serialize to JSON
39+
outputJSON, err := json.Marshal(inputEvent)
40+
if err != nil {
41+
t.Errorf("could not marshal event. details: %v", err)
42+
}
43+
44+
// 5. check result
45+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
46+
}
47+
48+
func TestECRPushMarshalingMalformedJson(t *testing.T) {
49+
test.TestMalformedJson(t, ECRImageActionEvent{})
50+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"version": "0",
3+
"id": "13cde686-328b-6117-af20-0e5566167482",
4+
"detail-type": "ECR Image Action",
5+
"source": "aws.ecr",
6+
"account": "123456789012",
7+
"time": "2019-11-16T01:54:34Z",
8+
"region": "us-west-2",
9+
"resources": [],
10+
"detail": {
11+
"result": "SUCCESS",
12+
"repository-name": "my-repository-name",
13+
"image-digest": "sha256:7f5b2640fe6fb4f46592dfd3410c4a79dac4f89e4782432e0378abcd1234",
14+
"action-type": "PUSH",
15+
"image-tag": "latest"
16+
}
17+
}

0 commit comments

Comments
 (0)