event_test.go 4.9 KB

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