github.com/npaton/distribution@v2.3.1-rc.0+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/schema1"
    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+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 Event
   118  	manifestPush = prototype
   119  	manifestPush.ID = "asdf-asdf-asdf-asdf-0"
   120  	manifestPush.Target.Digest = "sha256:0123456789abcdef0"
   121  	manifestPush.Target.Length = 1
   122  	manifestPush.Target.Size = 1
   123  	manifestPush.Target.MediaType = schema1.MediaTypeSignedManifest
   124  	manifestPush.Target.Repository = "library/test"
   125  	manifestPush.Target.URL = "http://example.com/v2/library/test/manifests/latest"
   126  
   127  	var layerPush0 Event
   128  	layerPush0 = prototype
   129  	layerPush0.ID = "asdf-asdf-asdf-asdf-1"
   130  	layerPush0.Target.Digest = "sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5"
   131  	layerPush0.Target.Length = 2
   132  	layerPush0.Target.Size = 2
   133  	layerPush0.Target.MediaType = layerMediaType
   134  	layerPush0.Target.Repository = "library/test"
   135  	layerPush0.Target.URL = "http://example.com/v2/library/test/manifests/latest"
   136  
   137  	var layerPush1 Event
   138  	layerPush1 = prototype
   139  	layerPush1.ID = "asdf-asdf-asdf-asdf-2"
   140  	layerPush1.Target.Digest = "sha256:3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d6"
   141  	layerPush1.Target.Length = 3
   142  	layerPush1.Target.Size = 3
   143  	layerPush1.Target.MediaType = layerMediaType
   144  	layerPush1.Target.Repository = "library/test"
   145  	layerPush1.Target.URL = "http://example.com/v2/library/test/manifests/latest"
   146  
   147  	var envelope Envelope
   148  	envelope.Events = append(envelope.Events, manifestPush, layerPush0, layerPush1)
   149  
   150  	p, err := json.MarshalIndent(envelope, "", "   ")
   151  	if err != nil {
   152  		t.Fatalf("unexpected error marshaling envelope: %v", err)
   153  	}
   154  	if string(p) != expected {
   155  		t.Fatalf("format has changed\n%s\n != \n%s", string(p), expected)
   156  	}
   157  }