1 package notifications
2
3 import (
4 "encoding/json"
5 "strings"
6 "testing"
7 "time"
8
9 "github.com/docker/distribution/manifest/schema1"
10 )
11
12
13
14
15 func TestEventEnvelopeJSONFormat(t *testing.T) {
16 var expected = strings.TrimSpace(`
17 {
18 "events": [
19 {
20 "id": "asdf-asdf-asdf-asdf-0",
21 "timestamp": "2006-01-02T15:04:05Z",
22 "action": "push",
23 "target": {
24 "mediaType": "application/vnd.docker.distribution.manifest.v1+prettyjws",
25 "size": 1,
26 "digest": "sha256:0123456789abcdef0",
27 "length": 1,
28 "repository": "library/test",
29 "url": "http://example.com/v2/library/test/manifests/latest"
30 },
31 "request": {
32 "id": "asdfasdf",
33 "addr": "client.local",
34 "host": "registrycluster.local",
35 "method": "PUT",
36 "useragent": "test/0.1"
37 },
38 "actor": {
39 "name": "test-actor"
40 },
41 "source": {
42 "addr": "hostname.local:port"
43 }
44 },
45 {
46 "id": "asdf-asdf-asdf-asdf-1",
47 "timestamp": "2006-01-02T15:04:05Z",
48 "action": "push",
49 "target": {
50 "mediaType": "application/vnd.docker.container.image.rootfs.diff+x-gtar",
51 "size": 2,
52 "digest": "sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5",
53 "length": 2,
54 "repository": "library/test",
55 "url": "http://example.com/v2/library/test/manifests/latest"
56 },
57 "request": {
58 "id": "asdfasdf",
59 "addr": "client.local",
60 "host": "registrycluster.local",
61 "method": "PUT",
62 "useragent": "test/0.1"
63 },
64 "actor": {
65 "name": "test-actor"
66 },
67 "source": {
68 "addr": "hostname.local:port"
69 }
70 },
71 {
72 "id": "asdf-asdf-asdf-asdf-2",
73 "timestamp": "2006-01-02T15:04:05Z",
74 "action": "push",
75 "target": {
76 "mediaType": "application/vnd.docker.container.image.rootfs.diff+x-gtar",
77 "size": 3,
78 "digest": "sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d6",
79 "length": 3,
80 "repository": "library/test",
81 "url": "http://example.com/v2/library/test/manifests/latest"
82 },
83 "request": {
84 "id": "asdfasdf",
85 "addr": "client.local",
86 "host": "registrycluster.local",
87 "method": "PUT",
88 "useragent": "test/0.1"
89 },
90 "actor": {
91 "name": "test-actor"
92 },
93 "source": {
94 "addr": "hostname.local:port"
95 }
96 }
97 ]
98 }
99 `)
100
101 tm, err := time.Parse(time.RFC3339, time.RFC3339[:len(time.RFC3339)-5])
102 if err != nil {
103 t.Fatalf("error creating time: %v", err)
104 }
105
106 var prototype Event
107 prototype.Action = EventActionPush
108 prototype.Timestamp = tm
109 prototype.Actor.Name = "test-actor"
110 prototype.Request.ID = "asdfasdf"
111 prototype.Request.Addr = "client.local"
112 prototype.Request.Host = "registrycluster.local"
113 prototype.Request.Method = "PUT"
114 prototype.Request.UserAgent = "test/0.1"
115 prototype.Source.Addr = "hostname.local:port"
116
117 var manifestPush = prototype
118 manifestPush.ID = "asdf-asdf-asdf-asdf-0"
119 manifestPush.Target.Digest = "sha256:0123456789abcdef0"
120 manifestPush.Target.Length = 1
121 manifestPush.Target.Size = 1
122 manifestPush.Target.MediaType = schema1.MediaTypeSignedManifest
123 manifestPush.Target.Repository = "library/test"
124 manifestPush.Target.URL = "http://example.com/v2/library/test/manifests/latest"
125
126 var layerPush0 = prototype
127 layerPush0.ID = "asdf-asdf-asdf-asdf-1"
128 layerPush0.Target.Digest = "sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5"
129 layerPush0.Target.Length = 2
130 layerPush0.Target.Size = 2
131 layerPush0.Target.MediaType = layerMediaType
132 layerPush0.Target.Repository = "library/test"
133 layerPush0.Target.URL = "http://example.com/v2/library/test/manifests/latest"
134
135 var layerPush1 = prototype
136 layerPush1.ID = "asdf-asdf-asdf-asdf-2"
137 layerPush1.Target.Digest = "sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d6"
138 layerPush1.Target.Length = 3
139 layerPush1.Target.Size = 3
140 layerPush1.Target.MediaType = layerMediaType
141 layerPush1.Target.Repository = "library/test"
142 layerPush1.Target.URL = "http://example.com/v2/library/test/manifests/latest"
143
144 var envelope Envelope
145 envelope.Events = append(envelope.Events, manifestPush, layerPush0, layerPush1)
146
147 p, err := json.MarshalIndent(envelope, "", " ")
148 if err != nil {
149 t.Fatalf("unexpected error marshaling envelope: %v", err)
150 }
151 if string(p) != expected {
152 t.Fatalf("format has changed\n%s\n != \n%s", string(p), expected)
153 }
154 }
155
View as plain text