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