github.com/chenbh/concourse/v6@v6.4.2/atc/event/parser_test.go (about)

     1  package event_test
     2  
     3  import (
     4  	"github.com/chenbh/concourse/v6/atc"
     5  	"github.com/chenbh/concourse/v6/atc/event"
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/ginkgo/extensions/table"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  type fakeEvent struct {
    12  	Hello      string `json:"hello"`
    13  	AddedField string `json:"added"`
    14  }
    15  
    16  func (fakeEvent) EventType() atc.EventType  { return "fake" }
    17  func (fakeEvent) Version() atc.EventVersion { return "5.1" }
    18  
    19  var _ = Describe("ParseEvent", func() {
    20  	BeforeEach(func() {
    21  		event.RegisterEvent(fakeEvent{})
    22  	})
    23  
    24  	It("can parse an older event type with a compatible major version", func() {
    25  		e, err := event.ParseEvent("5.0", "fake", []byte(`{"hello":"sup"}`))
    26  		Expect(err).ToNot(HaveOccurred())
    27  		Expect(e).To(Equal(fakeEvent{Hello: "sup"}))
    28  	})
    29  
    30  	It("can parse a newer event type with a compatible major version", func() {
    31  		e, err := event.ParseEvent("5.3", "fake", []byte(`{"hello":"sup","future":"field"}`))
    32  		Expect(err).ToNot(HaveOccurred())
    33  		Expect(e).To(Equal(fakeEvent{Hello: "sup"}))
    34  	})
    35  
    36  	It("fails to parse if the type is unknown", func() {
    37  		_, err := event.ParseEvent("4.0", "fake-unknown", []byte(`{"hello":"sup"}`))
    38  		Expect(err).To(Equal(event.UnknownEventTypeError{
    39  			Type: "fake-unknown",
    40  		}))
    41  	})
    42  
    43  	It("fails to parse if the version is incompatible", func() {
    44  		_, err := event.ParseEvent("4.0", "fake", []byte(`{"hello":"sup"}`))
    45  		Expect(err).To(Equal(event.UnknownEventVersionError{
    46  			Type:          "fake",
    47  			Version:       "4.0",
    48  			KnownVersions: []string{"5.1"},
    49  		}))
    50  	})
    51  
    52  	DescribeTable("should register all non-deprecated events successfully",
    53  		func(eventType atc.Event) {
    54  			parsedEvent, err := event.ParseEvent(eventType.Version(), eventType.EventType(), []byte("{}"))
    55  			Expect(err).ToNot(HaveOccurred())
    56  			Expect(parsedEvent).To(BeAssignableToTypeOf(eventType))
    57  		},
    58  
    59  		Entry("InitializeTask", event.InitializeTask{}),
    60  		Entry("StartTask", event.StartTask{}),
    61  		Entry("FinishTask", event.FinishTask{}),
    62  		Entry("InitializeGet", event.InitializeGet{}),
    63  		Entry("StartGet", event.StartGet{}),
    64  		Entry("FinishGet", event.FinishGet{}),
    65  		Entry("InitializePut", event.InitializePut{}),
    66  		Entry("StartPut", event.StartPut{}),
    67  		Entry("FinishPut", event.FinishPut{}),
    68  		Entry("Status", event.Status{}),
    69  		Entry("SelectedWorker", event.SelectedWorker{}),
    70  		Entry("Log", event.Log{}),
    71  		Entry("Error", event.Error{}),
    72  	)
    73  })