GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

event_test.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package event
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/fluxcd/flux/update"
  6. )
  7. var (
  8. spec = update.ReleaseImageSpec{
  9. ImageSpec: update.ImageSpecLatest,
  10. }
  11. cause = update.Cause{
  12. User: "test user",
  13. Message: "test message",
  14. }
  15. )
  16. func TestEvent_ParseReleaseMetaData(t *testing.T) {
  17. origEvent := Event{
  18. Type: EventRelease,
  19. Metadata: &ReleaseEventMetadata{
  20. Cause: cause,
  21. Spec: ReleaseSpec{
  22. Type: ReleaseImageSpecType,
  23. ReleaseImageSpec: &spec,
  24. },
  25. },
  26. }
  27. bytes, _ := json.Marshal(origEvent)
  28. e := Event{}
  29. err := e.UnmarshalJSON(bytes)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. switch r := e.Metadata.(type) {
  34. case *ReleaseEventMetadata:
  35. if r.Spec.ReleaseImageSpec.ImageSpec != spec.ImageSpec ||
  36. r.Cause != cause {
  37. t.Fatal("Release event wasn't marshalled/unmarshalled")
  38. }
  39. default:
  40. t.Fatal("Wrong event type unmarshalled")
  41. }
  42. }
  43. func TestEvent_ParseNoMetadata(t *testing.T) {
  44. origEvent := Event{
  45. Type: EventLock,
  46. }
  47. bytes, _ := json.Marshal(origEvent)
  48. e := Event{}
  49. err := e.UnmarshalJSON(bytes)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. if e.Metadata != nil {
  54. t.Fatal("Hasn't been unmarshalled properly")
  55. }
  56. }
  57. // TestEvent_ParseOldReleaseMetaData makes sure the parsing code can
  58. // handle the older format events recorded against commits.
  59. func TestEvent_ParseOldReleaseMetaData(t *testing.T) {
  60. // A minimal example of an old-style ReleaseEventMetadata. NB it
  61. // must have at least an entry for "spec", since otherwise the
  62. // JSON unmarshaller will not attempt to unparse the spec and
  63. // thereby invoke the specialised UnmarshalJSON.
  64. oldData := `
  65. {
  66. "spec": {
  67. "serviceSpecs": ["<all>"]
  68. }
  69. }
  70. `
  71. var eventData ReleaseEventMetadata
  72. if err := json.Unmarshal([]byte(oldData), &eventData); err != nil {
  73. t.Fatal(err)
  74. }
  75. if eventData.Spec.Type != ReleaseImageSpecType {
  76. t.Error("did not set spec type to ReleaseImageSpecType")
  77. }
  78. if eventData.Spec.ReleaseImageSpec == nil {
  79. t.Error("did not set .ReleaseImageSpec as expected")
  80. }
  81. if eventData.Spec.ReleaseContainersSpec != nil {
  82. t.Error("unexpectedly set .ReleaseContainersSpec")
  83. }
  84. if len(eventData.Spec.ReleaseImageSpec.ServiceSpecs) != 1 {
  85. t.Error("expected service specs of len 1")
  86. }
  87. }