Skip to content

Commit 6645426

Browse files
authored
Added ECRImageActionEvent and test (#481)
* Added ECRImageActionEvent and test * converted time to time.Time * import time * Fix for time.Time type * fix error handling of time parsing * fix lint issue
1 parent caace58 commit 6645426

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

events/ecr_image_action.go

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

events/ecr_image_action_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}
+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)