1package events 2 3import ( 4 "encoding/json" 5 "github.com/stretchr/testify/require" 6 "io/ioutil" 7 "testing" 8 "time" 9) 10 11func TestUnmarshalCodeDeployEvent(t *testing.T) { 12 tests := []struct { 13 input string 14 expect CodeDeployEvent 15 }{ 16 { 17 input: "testdata/codedeploy-deployment-event.json", 18 expect: CodeDeployEvent{ 19 AccountID: "123456789012", 20 Region: "us-east-1", 21 DetailType: CodeDeployDeploymentEventDetailType, 22 Source: CodeDeployEventSource, 23 Version: "0", 24 Time: time.Date(2016, 6, 30, 22, 6, 31, 0, time.UTC), 25 ID: "c071bfbf-83c4-49ca-a6ff-3df053957145", 26 Resources: []string{ 27 "arn:aws:codedeploy:us-east-1:123456789012:application:myApplication", 28 "arn:aws:codedeploy:us-east-1:123456789012:deploymentgroup:myApplication/myDeploymentGroup", 29 }, 30 Detail: CodeDeployEventDetail{ 31 InstanceGroupID: "9fd2fbef-2157-40d8-91e7-6845af69e2d2", 32 InstanceID: "", 33 Region: "us-east-1", 34 Application: "myApplication", 35 DeploymentID: "d-123456789", 36 State: CodeDeployDeploymentStateSuccess, 37 DeploymentGroup: "myDeploymentGroup", 38 }, 39 }, 40 }, 41 { 42 input: "testdata/codedeploy-instance-event.json", 43 expect: CodeDeployEvent{ 44 AccountID: "123456789012", 45 Region: "us-east-1", 46 DetailType: CodeDeployInstanceEventDetailType, 47 Source: CodeDeployEventSource, 48 Version: "0", 49 Time: time.Date(2016, 6, 30, 23, 18, 50, 0, time.UTC), 50 ID: "fb1d3015-c091-4bf9-95e2-d98521ab2ecb", 51 Resources: []string{ 52 "arn:aws:ec2:us-east-1:123456789012:instance/i-0000000aaaaaaaaaa", 53 "arn:aws:codedeploy:us-east-1:123456789012:deploymentgroup:myApplication/myDeploymentGroup", 54 "arn:aws:codedeploy:us-east-1:123456789012:application:myApplication", 55 }, 56 Detail: CodeDeployEventDetail{ 57 InstanceGroupID: "8cd3bfa8-9e72-4cbe-a1e5-da4efc7efd49", 58 InstanceID: "i-0000000aaaaaaaaaa", 59 Region: "us-east-1", 60 Application: "myApplication", 61 DeploymentID: "d-123456789", 62 State: CodeDeployDeploymentStateSuccess, 63 DeploymentGroup: "myDeploymentGroup", 64 }, 65 }, 66 }, 67 } 68 69 for _, testcase := range tests { 70 data, err := ioutil.ReadFile(testcase.input) 71 require.NoError(t, err) 72 73 var actual CodeDeployEvent 74 require.NoError(t, json.Unmarshal(data, &actual)) 75 76 require.Equal(t, testcase.expect, actual) 77 } 78} 79