2015-01-28 04:37:16 +01:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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": {
|
|
|
|
"type": "manifest",
|
|
|
|
"name": "library/test",
|
|
|
|
"digest": "sha256:0123456789abcdef0",
|
|
|
|
"tag": "latest",
|
|
|
|
"url": "http://example.com/v2/library/test/manifests/latest"
|
|
|
|
},
|
|
|
|
"actor": {
|
|
|
|
"name": "test-actor",
|
2015-01-28 08:27:46 +01:00
|
|
|
"addr": "client.local",
|
|
|
|
"host": "registrycluster.local",
|
|
|
|
"requestID": "asdfasdf"
|
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": {
|
|
|
|
"type": "blob",
|
|
|
|
"name": "library/test",
|
|
|
|
"digest": "tarsum.v2+sha256:0123456789abcdef1",
|
|
|
|
"url": "http://example.com/v2/library/test/manifests/latest"
|
|
|
|
},
|
|
|
|
"actor": {
|
|
|
|
"name": "test-actor",
|
2015-01-28 08:27:46 +01:00
|
|
|
"addr": "client.local",
|
|
|
|
"host": "registrycluster.local",
|
|
|
|
"requestID": "asdfasdf"
|
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": {
|
|
|
|
"type": "blob",
|
|
|
|
"name": "library/test",
|
|
|
|
"digest": "tarsum.v2+sha256:0123456789abcdef2",
|
|
|
|
"url": "http://example.com/v2/library/test/manifests/latest"
|
|
|
|
},
|
|
|
|
"actor": {
|
|
|
|
"name": "test-actor",
|
2015-01-28 08:27:46 +01:00
|
|
|
"addr": "client.local",
|
|
|
|
"host": "registrycluster.local",
|
|
|
|
"requestID": "asdfasdf"
|
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
|
|
|
|
prototype.Action = "push"
|
|
|
|
prototype.Timestamp = tm
|
2015-01-28 08:27:46 +01:00
|
|
|
prototype.Actor.Addr = "client.local"
|
2015-01-28 04:37:16 +01:00
|
|
|
prototype.Actor.Name = "test-actor"
|
2015-01-28 08:27:46 +01:00
|
|
|
prototype.Actor.RequestID = "asdfasdf"
|
|
|
|
prototype.Actor.Host = "registrycluster.local"
|
|
|
|
prototype.Source.Addr = "hostname.local:port"
|
2015-01-28 04:37:16 +01:00
|
|
|
|
|
|
|
var manifestPush Event
|
|
|
|
manifestPush = prototype
|
|
|
|
manifestPush.ID = "asdf-asdf-asdf-asdf-0"
|
|
|
|
manifestPush.Target.Digest = "sha256:0123456789abcdef0"
|
2015-01-29 00:55:18 +01:00
|
|
|
manifestPush.Target.Type = EventTargetTypeManifest
|
2015-01-28 04:37:16 +01:00
|
|
|
manifestPush.Target.Name = "library/test"
|
|
|
|
manifestPush.Target.Tag = "latest"
|
|
|
|
manifestPush.Target.URL = "http://example.com/v2/library/test/manifests/latest"
|
|
|
|
|
|
|
|
var layerPush0 Event
|
|
|
|
layerPush0 = prototype
|
|
|
|
layerPush0.ID = "asdf-asdf-asdf-asdf-1"
|
|
|
|
layerPush0.Target.Digest = "tarsum.v2+sha256:0123456789abcdef1"
|
2015-01-29 00:55:18 +01:00
|
|
|
layerPush0.Target.Type = EventTargetTypeBlob
|
2015-01-28 04:37:16 +01:00
|
|
|
layerPush0.Target.Name = "library/test"
|
|
|
|
layerPush0.Target.URL = "http://example.com/v2/library/test/manifests/latest"
|
|
|
|
|
|
|
|
var layerPush1 Event
|
|
|
|
layerPush1 = prototype
|
|
|
|
layerPush1.ID = "asdf-asdf-asdf-asdf-2"
|
|
|
|
layerPush1.Target.Digest = "tarsum.v2+sha256:0123456789abcdef2"
|
2015-01-29 00:55:18 +01:00
|
|
|
layerPush1.Target.Type = EventTargetTypeBlob
|
2015-01-28 04:37:16 +01:00
|
|
|
layerPush1.Target.Name = "library/test"
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|