github.com/kubeshop/testkube@v1.17.23/pkg/api/v1/testkube/model_event_extended_test.go (about) 1 package testkube 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestEmitter_IsValidEvent_ForTest(t *testing.T) { 10 11 t.Run("should pass only events with given selector", func(t *testing.T) { 12 // given 13 execution := NewQueuedExecution() 14 execution.Labels = map[string]string{"test": "1"} 15 e := Event{Type_: EventStartTest, TestExecution: execution} 16 17 // when 18 valid := e.Valid("test=1", AllEventTypes) 19 20 // then 21 assert.True(t, valid) 22 }) 23 24 t.Run("should not pass events with not matching selector", func(t *testing.T) { 25 // given 26 execution := NewQueuedExecution() 27 execution.Labels = map[string]string{"test": "2"} 28 e := Event{Type_: EventStartTest, TestExecution: execution} 29 30 // when 31 valid := e.Valid("test=1", AllEventTypes) 32 33 // then 34 assert.False(t, valid) 35 }) 36 37 t.Run("should pass events without selector", func(t *testing.T) { 38 // given 39 execution := NewQueuedExecution() 40 e := Event{Type_: EventStartTest, TestExecution: execution} 41 42 // when 43 valid := e.Valid("", AllEventTypes) 44 45 // then 46 assert.True(t, valid) 47 }) 48 } 49 50 func TestEmitter_IsValidEvent_ForTestSuite(t *testing.T) { 51 52 t.Run("should pass only events with given selector", func(t *testing.T) { 53 // given 54 execution := NewQueuedTestSuiteExecution("", "") 55 execution.Labels = map[string]string{"test": "1"} 56 e := Event{Type_: EventStartTestSuite, TestSuiteExecution: execution} 57 58 // when 59 valid := e.Valid("test=1", AllEventTypes) 60 61 // then 62 assert.True(t, valid) 63 }) 64 65 t.Run("should not pass events with not matching selector", func(t *testing.T) { 66 // given 67 execution := NewQueuedTestSuiteExecution("", "") 68 execution.Labels = map[string]string{"test": "2"} 69 e := Event{Type_: EventStartTest, TestSuiteExecution: execution} 70 71 // when 72 valid := e.Valid("test=1", AllEventTypes) 73 74 // then 75 assert.False(t, valid) 76 }) 77 78 t.Run("should pass events without selector", func(t *testing.T) { 79 // given 80 execution := NewQueuedTestSuiteExecution("", "") 81 e := Event{Type_: EventStartTest, TestSuiteExecution: execution} 82 83 // when 84 valid := e.Valid("", AllEventTypes) 85 86 // then 87 assert.True(t, valid) 88 }) 89 } 90 91 func TestEvent_IsSuccess(t *testing.T) { 92 93 t.Run("should return true for success events", func(t *testing.T) { 94 events := map[EventType]bool{ 95 START_TEST_EventType: false, 96 START_TESTSUITE_EventType: false, 97 END_TEST_FAILED_EventType: false, 98 END_TEST_SUCCESS_EventType: true, 99 END_TESTSUITE_FAILED_EventType: false, 100 END_TESTSUITE_SUCCESS_EventType: true, 101 } 102 103 for eventType, expected := range events { 104 // given 105 e := Event{Type_: &eventType} 106 107 // when 108 success := e.IsSuccess() 109 110 // then 111 assert.Equal(t, expected, success) 112 } 113 }) 114 115 } 116 117 func TestEvent_Topic(t *testing.T) { 118 119 t.Run("should return events topic if explicitly set", func(t *testing.T) { 120 evt := Event{Type_: EventStartTest, StreamTopic: "topic"} 121 assert.Equal(t, "topic", evt.Topic()) 122 }) 123 124 t.Run("should return events topic if not resource set", func(t *testing.T) { 125 evt := Event{Type_: EventStartTest, Resource: nil} 126 assert.Equal(t, "events.all", evt.Topic()) 127 }) 128 129 t.Run("should return event topic with resource name and id if set", func(t *testing.T) { 130 evt := Event{Type_: EventStartTest, Resource: EventResourceExecutor, ResourceId: "a12"} 131 assert.Equal(t, "events.executor.a12", evt.Topic()) 132 }) 133 134 t.Run("should return event topic with resource name when id not set", func(t *testing.T) { 135 evt := Event{Type_: EventStartTest, Resource: EventResourceExecutor} 136 assert.Equal(t, "events.executor", evt.Topic()) 137 }) 138 }