-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package events | ||
|
||
import "time" | ||
|
||
type ECRImageActionEvent struct { | ||
Version string `json:"version"` | ||
ID string `json:"id"` | ||
DetailType string `json:"detail-type"` | ||
Source string `json:"source"` | ||
Account string `json:"account"` | ||
Time time.Time `json:"time"` | ||
Region string `json:"region"` | ||
Resources []string `json:"resources"` | ||
Detail ECRImageActionEventDetailType `json:"detail"` | ||
} | ||
|
||
type ECRImageActionEventDetailType struct { | ||
Result string `json:"result"` | ||
RepositoryName string `json:"repository-name"` | ||
ImageDigest string `json:"image-digest"` | ||
ActionType string `json:"action-type"` | ||
ImageTag string `json:"image-tag"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
package events | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-lambda-go/events/test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestECRImageActionEventMarshaling(t *testing.T) { | ||
// 1. read JSON from file | ||
inputJSON := test.ReadJSONFromFile(t, "./testdata/ecr-image-push-event.json") | ||
|
||
// 2. de-serialize into Go object | ||
var inputEvent ECRImageActionEvent | ||
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { | ||
t.Errorf("could not unmarshal event. details: %v", err) | ||
} | ||
|
||
// 3. Verify values populated into Go Object, at least one validation per data type | ||
assert.Equal(t, "0", inputEvent.Version) | ||
assert.Equal(t, "13cde686-328b-6117-af20-0e5566167482", inputEvent.ID) | ||
assert.Equal(t, "ECR Image Action", inputEvent.DetailType) | ||
assert.Equal(t, "aws.ecr", inputEvent.Source) | ||
assert.Equal(t, "123456789012", inputEvent.Account) | ||
assert.Equal(t, "us-west-2", inputEvent.Region) | ||
assert.Empty(t, inputEvent.Resources) | ||
|
||
testTime, err := time.Parse(time.RFC3339, "2019-11-16T01:54:34Z") | ||
if err != nil { | ||
t.Errorf("Failed to parse time: %v", err) | ||
} | ||
assert.Equal(t, testTime, inputEvent.Time) | ||
|
||
var detail = inputEvent.Detail | ||
assert.Equal(t, "SUCCESS", detail.Result) | ||
assert.Equal(t, "my-repository-name", detail.RepositoryName) | ||
assert.Equal(t, "sha256:7f5b2640fe6fb4f46592dfd3410c4a79dac4f89e4782432e0378abcd1234", detail.ImageDigest) | ||
assert.Equal(t, "latest", detail.ImageTag) | ||
|
||
// 4. serialize to JSON | ||
outputJSON, err := json.Marshal(inputEvent) | ||
if err != nil { | ||
t.Errorf("could not marshal event. details: %v", err) | ||
} | ||
|
||
// 5. check result | ||
assert.JSONEq(t, string(inputJSON), string(outputJSON)) | ||
} | ||
|
||
func TestECRPushMarshalingMalformedJson(t *testing.T) { | ||
test.TestMalformedJson(t, ECRImageActionEvent{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"version": "0", | ||
"id": "13cde686-328b-6117-af20-0e5566167482", | ||
"detail-type": "ECR Image Action", | ||
"source": "aws.ecr", | ||
"account": "123456789012", | ||
"time": "2019-11-16T01:54:34Z", | ||
"region": "us-west-2", | ||
"resources": [], | ||
"detail": { | ||
"result": "SUCCESS", | ||
"repository-name": "my-repository-name", | ||
"image-digest": "sha256:7f5b2640fe6fb4f46592dfd3410c4a79dac4f89e4782432e0378abcd1234", | ||
"action-type": "PUSH", | ||
"image-tag": "latest" | ||
} | ||
} |