2015-01-28 04:37:16 +01:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2015-03-05 05:57:14 +01:00
|
|
|
|
2015-08-21 06:24:30 +02:00
|
|
|
"github.com/docker/distribution/manifest/schema1"
|
2015-01-28 04:37:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestEventJSONFormat provides silly test to detect if the event format or
|
|
|
|
// envelope has changed. If this code fails, the revision of the protocol may
|
|
|
|
// need to be incremented.
|
|
|
|
func TestEventEnvelopeJSONFormat(t *testing.T) {
|
|
|
|
var expected = strings.TrimSpace(`
|
|
|
|
{
|
|
|
|
"events": [
|
|
|
|
{
|
2015-01-28 08:27:46 +01:00
|
|
|
"id": "asdf-asdf-asdf-asdf-0",
|
2015-01-28 04:37:16 +01:00
|
|
|
"timestamp": "2006-01-02T15:04:05Z",
|
|
|
|
"action": "push",
|
|
|
|
"target": {
|
2016-01-18 19:26:45 +01:00
|
|
|
"mediaType": "application/vnd.docker.distribution.manifest.v1+prettyjws",
|
2015-07-18 02:07:11 +02:00
|
|
|
"size": 1,
|
2015-01-28 04:37:16 +01:00
|
|
|
"digest": "sha256:0123456789abcdef0",
|
2015-07-18 02:07:11 +02:00
|
|
|
"length": 1,
|
2015-03-05 05:57:14 +01:00
|
|
|
"repository": "library/test",
|
2015-01-28 04:37:16 +01:00
|
|
|
"url": "http://example.com/v2/library/test/manifests/latest"
|
|
|
|
},
|
2015-02-03 22:28:10 +01:00
|
|
|
"request": {
|
|
|
|
"id": "asdfasdf",
|
2015-01-28 08:27:46 +01:00
|
|
|
"addr": "client.local",
|
|
|
|
"host": "registrycluster.local",
|
2015-02-03 22:28:10 +01:00
|
|
|
"method": "PUT",
|
|
|
|
"useragent": "test/0.1"
|
|
|
|
},
|
|
|
|
"actor": {
|
|
|
|
"name": "test-actor"
|
2015-01-28 04:37:16 +01:00
|
|
|
},
|
|
|
|
"source": {
|
2015-01-28 08:27:46 +01:00
|
|
|
"addr": "hostname.local:port"
|
2015-01-28 04:37:16 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2015-01-28 08:27:46 +01:00
|
|
|
"id": "asdf-asdf-asdf-asdf-1",
|
2015-01-28 04:37:16 +01:00
|
|
|
"timestamp": "2006-01-02T15:04:05Z",
|
|
|
|
"action": "push",
|
|
|
|
"target": {
|
2015-03-05 05:57:14 +01:00
|
|
|
"mediaType": "application/vnd.docker.container.image.rootfs.diff+x-gtar",
|
2015-07-18 02:07:11 +02:00
|
|
|
"size": 2,
|
2015-12-16 02:18:13 +01:00
|
|
|
"digest": "sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5",
|
2015-07-18 02:07:11 +02:00
|
|
|
"length": 2,
|
2015-03-05 05:57:14 +01:00
|
|
|
"repository": "library/test",
|
2015-01-28 04:37:16 +01:00
|
|
|
"url": "http://example.com/v2/library/test/manifests/latest"
|
|
|
|
},
|
2015-02-03 22:28:10 +01:00
|
|
|
"request": {
|
|
|
|
"id": "asdfasdf",
|
2015-01-28 08:27:46 +01:00
|
|
|
"addr": "client.local",
|
|
|
|
"host": "registrycluster.local",
|
2015-02-03 22:28:10 +01:00
|
|
|
"method": "PUT",
|
|
|
|
"useragent": "test/0.1"
|
|
|
|
},
|
|
|
|
"actor": {
|
|
|
|
"name": "test-actor"
|
2015-01-28 04:37:16 +01:00
|
|
|
},
|
|
|
|
"source": {
|
2015-01-28 08:27:46 +01:00
|
|
|
"addr": "hostname.local:port"
|
2015-01-28 04:37:16 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2015-01-28 08:27:46 +01:00
|
|
|
"id": "asdf-asdf-asdf-asdf-2",
|
2015-01-28 04:37:16 +01:00
|
|
|
"timestamp": "2006-01-02T15:04:05Z",
|
|
|
|
"action": "push",
|
|
|
|
"target": {
|
2015-03-05 05:57:14 +01:00
|
|
|
"mediaType": "application/vnd.docker.container.image.rootfs.diff+x-gtar",
|
2015-07-18 02:07:11 +02:00
|
|
|
"size": 3,
|
2015-12-16 02:18:13 +01:00
|
|
|
"digest": "sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d6",
|
2015-07-18 02:07:11 +02:00
|
|
|
"length": 3,
|
2015-03-05 05:57:14 +01:00
|
|
|
"repository": "library/test",
|
2015-01-28 04:37:16 +01:00
|
|
|
"url": "http://example.com/v2/library/test/manifests/latest"
|
|
|
|
},
|
2015-02-03 22:28:10 +01:00
|
|
|
"request": {
|
|
|
|
"id": "asdfasdf",
|
2015-01-28 08:27:46 +01:00
|
|
|
"addr": "client.local",
|
|
|
|
"host": "registrycluster.local",
|
2015-02-03 22:28:10 +01:00
|
|
|
"method": "PUT",
|
|
|
|
"useragent": "test/0.1"
|
|
|
|
},
|
|
|
|
"actor": {
|
|
|
|
"name": "test-actor"
|
2015-01-28 04:37:16 +01:00
|
|
|
},
|
|
|
|
"source": {
|
2015-01-28 08:27:46 +01:00
|
|
|
"addr": "hostname.local:port"
|
2015-01-28 04:37:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
tm, err := time.Parse(time.RFC3339, time.RFC3339[:len(time.RFC3339)-5])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating time: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var prototype Event
|
2015-03-05 05:57:14 +01:00
|
|
|
prototype.Action = EventActionPush
|
2015-01-28 04:37:16 +01:00
|
|
|
prototype.Timestamp = tm
|
|
|
|
prototype.Actor.Name = "test-actor"
|
2015-02-03 22:28:10 +01:00
|
|
|
prototype.Request.ID = "asdfasdf"
|
|
|
|
prototype.Request.Addr = "client.local"
|
|
|
|
prototype.Request.Host = "registrycluster.local"
|
|
|
|
prototype.Request.Method = "PUT"
|
|
|
|
prototype.Request.UserAgent = "test/0.1"
|
2015-01-28 08:27:46 +01:00
|
|
|
prototype.Source.Addr = "hostname.local:port"
|
2015-01-28 04:37:16 +01:00
|
|
|
|
2019-02-05 01:01:04 +01:00
|
|
|
var manifestPush = prototype
|
2015-01-28 04:37:16 +01:00
|
|
|
manifestPush.ID = "asdf-asdf-asdf-asdf-0"
|
|
|
|
manifestPush.Target.Digest = "sha256:0123456789abcdef0"
|
2015-07-18 02:07:11 +02:00
|
|
|
manifestPush.Target.Length = 1
|
|
|
|
manifestPush.Target.Size = 1
|
2016-01-18 19:26:45 +01:00
|
|
|
manifestPush.Target.MediaType = schema1.MediaTypeSignedManifest
|
2015-03-05 05:57:14 +01:00
|
|
|
manifestPush.Target.Repository = "library/test"
|
2015-01-28 04:37:16 +01:00
|
|
|
manifestPush.Target.URL = "http://example.com/v2/library/test/manifests/latest"
|
|
|
|
|
2019-02-05 01:01:04 +01:00
|
|
|
var layerPush0 = prototype
|
2015-01-28 04:37:16 +01:00
|
|
|
layerPush0.ID = "asdf-asdf-asdf-asdf-1"
|
2015-12-16 02:18:13 +01:00
|
|
|
layerPush0.Target.Digest = "sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5"
|
2015-03-05 05:57:14 +01:00
|
|
|
layerPush0.Target.Length = 2
|
2015-07-18 02:07:11 +02:00
|
|
|
layerPush0.Target.Size = 2
|
2015-03-05 05:57:14 +01:00
|
|
|
layerPush0.Target.MediaType = layerMediaType
|
|
|
|
layerPush0.Target.Repository = "library/test"
|
2015-01-28 04:37:16 +01:00
|
|
|
layerPush0.Target.URL = "http://example.com/v2/library/test/manifests/latest"
|
|
|
|
|
2019-02-05 01:01:04 +01:00
|
|
|
var layerPush1 = prototype
|
2015-01-28 04:37:16 +01:00
|
|
|
layerPush1.ID = "asdf-asdf-asdf-asdf-2"
|
2015-12-16 02:18:13 +01:00
|
|
|
layerPush1.Target.Digest = "sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d6"
|
2015-03-05 05:57:14 +01:00
|
|
|
layerPush1.Target.Length = 3
|
2015-07-18 02:07:11 +02:00
|
|
|
layerPush1.Target.Size = 3
|
2015-03-05 05:57:14 +01:00
|
|
|
layerPush1.Target.MediaType = layerMediaType
|
|
|
|
layerPush1.Target.Repository = "library/test"
|
2015-01-28 04:37:16 +01:00
|
|
|
layerPush1.Target.URL = "http://example.com/v2/library/test/manifests/latest"
|
|
|
|
|
|
|
|
var envelope Envelope
|
|
|
|
envelope.Events = append(envelope.Events, manifestPush, layerPush0, layerPush1)
|
|
|
|
|
|
|
|
p, err := json.MarshalIndent(envelope, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error marshaling envelope: %v", err)
|
|
|
|
}
|
|
|
|
if string(p) != expected {
|
|
|
|
t.Fatalf("format has changed\n%s\n != \n%s", string(p), expected)
|
|
|
|
}
|
|
|
|
}
|