From c65c9b550372dc116d223ef2d6b5936ed05928cd Mon Sep 17 00:00:00 2001 From: Nick Downs Date: Wed, 29 Mar 2023 10:06:07 -0700 Subject: [PATCH 1/6] Added ECRImageActionEvent and test --- events/ecr_image_action.go | 21 ++++++++++ events/ecr_image_action_test.go | 50 +++++++++++++++++++++++ events/testdata/ecr-image-push-event.json | 17 ++++++++ 3 files changed, 88 insertions(+) create mode 100644 events/ecr_image_action.go create mode 100644 events/ecr_image_action_test.go create mode 100644 events/testdata/ecr-image-push-event.json diff --git a/events/ecr_image_action.go b/events/ecr_image_action.go new file mode 100644 index 00000000..d3c523dc --- /dev/null +++ b/events/ecr_image_action.go @@ -0,0 +1,21 @@ +package events + +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 string `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"` +} diff --git a/events/ecr_image_action_test.go b/events/ecr_image_action_test.go new file mode 100644 index 00000000..07e0df10 --- /dev/null +++ b/events/ecr_image_action_test.go @@ -0,0 +1,50 @@ +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. +package events + +import ( + "encoding/json" + "testing" + + "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, "2019-11-16T01:54:34Z", inputEvent.Time) + assert.Equal(t, "us-west-2", inputEvent.Region) + assert.Empty(t, inputEvent.Resources) + + 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{}) +} diff --git a/events/testdata/ecr-image-push-event.json b/events/testdata/ecr-image-push-event.json new file mode 100644 index 00000000..39a2d842 --- /dev/null +++ b/events/testdata/ecr-image-push-event.json @@ -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" + } +} From f2e35eb23ca6a549bc136f81a991c8a74f4ed264 Mon Sep 17 00:00:00 2001 From: Nick Downs Date: Wed, 29 Mar 2023 10:07:18 -0700 Subject: [PATCH 2/6] converted time to time.Time --- events/ecr_image_action.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/events/ecr_image_action.go b/events/ecr_image_action.go index d3c523dc..dada08bd 100644 --- a/events/ecr_image_action.go +++ b/events/ecr_image_action.go @@ -6,7 +6,7 @@ type ECRImageActionEvent struct { DetailType string `json:"detail-type"` Source string `json:"source"` Account string `json:"account"` - Time string `json:"time"` + Time time.Time `json:"time"` Region string `json:"region"` Resources []string `json:"resources"` Detail ECRImageActionEventDetailType `json:"detail"` From 933f06b6dbeb34fe7199ce116e9338e7d42fb9cb Mon Sep 17 00:00:00 2001 From: Nick Downs Date: Wed, 29 Mar 2023 10:11:06 -0700 Subject: [PATCH 3/6] import time --- events/ecr_image_action.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/events/ecr_image_action.go b/events/ecr_image_action.go index dada08bd..d2d404cf 100644 --- a/events/ecr_image_action.go +++ b/events/ecr_image_action.go @@ -1,5 +1,9 @@ package events +import ( + "time" +) + type ECRImageActionEvent struct { Version string `json:"version"` ID string `json:"id"` From 856754978a99e07d6c8acefb44df023c6b92eeb5 Mon Sep 17 00:00:00 2001 From: Nick Downs Date: Wed, 29 Mar 2023 11:35:42 -0700 Subject: [PATCH 4/6] Fix for time.Time type --- events/ecr_image_action_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/events/ecr_image_action_test.go b/events/ecr_image_action_test.go index 07e0df10..e6f87239 100644 --- a/events/ecr_image_action_test.go +++ b/events/ecr_image_action_test.go @@ -4,6 +4,7 @@ package events import ( "encoding/json" "testing" + "time" "github.com/aws/aws-lambda-go/events/test" "github.com/stretchr/testify/assert" @@ -25,10 +26,12 @@ func TestECRImageActionEventMarshaling(t *testing.T) { assert.Equal(t, "ECR Image Action", inputEvent.DetailType) assert.Equal(t, "aws.ecr", inputEvent.Source) assert.Equal(t, "123456789012", inputEvent.Account) - assert.Equal(t, "2019-11-16T01:54:34Z", inputEvent.Time) assert.Equal(t, "us-west-2", inputEvent.Region) assert.Empty(t, inputEvent.Resources) + testTime, err := time.Parse(time.RFC3339, "2019-11-16T01:54:34Z") + assert.Equal(t, testTime, inputEvent.Time) + var detail = inputEvent.Detail assert.Equal(t, "SUCCESS", detail.Result) assert.Equal(t, "my-repository-name", detail.RepositoryName) From 9b054486fb63577db75b07cc87d7888058b239a7 Mon Sep 17 00:00:00 2001 From: Nick Downs Date: Wed, 29 Mar 2023 11:48:46 -0700 Subject: [PATCH 5/6] fix error handling of time parsing --- events/ecr_image_action_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/events/ecr_image_action_test.go b/events/ecr_image_action_test.go index e6f87239..180e8030 100644 --- a/events/ecr_image_action_test.go +++ b/events/ecr_image_action_test.go @@ -30,6 +30,9 @@ func TestECRImageActionEventMarshaling(t *testing.T) { 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 From b2b586caa8212d50a5470210ee4e808e5ebd73eb Mon Sep 17 00:00:00 2001 From: Nick Downs Date: Wed, 29 Mar 2023 12:01:03 -0700 Subject: [PATCH 6/6] fix lint issue --- events/ecr_image_action.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/events/ecr_image_action.go b/events/ecr_image_action.go index d2d404cf..3d93676a 100644 --- a/events/ecr_image_action.go +++ b/events/ecr_image_action.go @@ -1,8 +1,6 @@ package events -import ( - "time" -) +import "time" type ECRImageActionEvent struct { Version string `json:"version"`