github.com/kubeshop/testkube@v1.17.23/pkg/slack/config_test.go (about) 1 package slack 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 9 "github.com/kubeshop/testkube/pkg/api/v1/testkube" 10 ) 11 12 const notificationConfigString = `[ 13 { 14 "ChannelID": "", 15 "selector": {}, 16 "testName": [], 17 "testSuiteName": [], 18 "events": [ 19 "start-test", 20 "end-test-success", 21 "end-test-failed", 22 "end-test-aborted", 23 "end-test-timeout", 24 "start-testsuite", 25 "end-testsuite-success", 26 "end-testsuite-failed", 27 "end-testsuite-aborted", 28 "end-testsuite-timeout" 29 ] 30 } 31 ]` 32 33 const multipleConfigurationString = `[ 34 { 35 "ChannelID": "ChannelID1", 36 "selector": {}, 37 "testName": [], 38 "testSuiteName": [], 39 "events": [ 40 "start-test" 41 ] 42 }, 43 { 44 "ChannelID": "ChannelID2", 45 "selector": {}, 46 "testName": [], 47 "testSuiteName": [], 48 "events": [ 49 "end-test-failed" 50 ] 51 } 52 ]` 53 54 func TestParamsNilAssign(t *testing.T) { 55 var notificationConfig []NotificationsConfig 56 err := json.Unmarshal([]byte(notificationConfigString), ¬ificationConfig) 57 58 assert.Nil(t, err) 59 60 t.Run("allow all when selector, testname, testsuite is not specified", func(t *testing.T) { 61 config := NewConfig(notificationConfig) 62 assert.False(t, config.HasChannelsDefined()) 63 channels, needs := config.NeedsSending(&testkube.Event{Type_: testkube.EventStartTest}) 64 assert.True(t, needs) 65 assert.Equal(t, 0, len(channels)) 66 }) 67 68 t.Run("allow only one event when only one event is specified", func(t *testing.T) { 69 oldEvents := notificationConfig[0].Events 70 notificationConfig[0].Events = []testkube.EventType{*testkube.EventStartTest} 71 config := NewConfig(notificationConfig) 72 channels, needs := config.NeedsSending(&testkube.Event{Type_: testkube.EventStartTest}) 73 assert.True(t, needs) 74 assert.Equal(t, 0, len(channels)) 75 76 channels, needs = config.NeedsSending(&testkube.Event{Type_: testkube.EventEndTestSuccess}) 77 assert.False(t, needs) 78 assert.Equal(t, 0, len(channels)) 79 notificationConfig[0].Events = oldEvents 80 }) 81 82 t.Run("allow only one test when only one test is specified", func(t *testing.T) { 83 oldTestNames := notificationConfig[0].TestNames 84 notificationConfig[0].TestNames = []string{"test1"} 85 config := NewConfig(notificationConfig) 86 channels, needs := config.NeedsSending(&testkube.Event{TestExecution: &testkube.Execution{TestName: "test1"}, Type_: testkube.EventStartTest}) 87 assert.True(t, needs) 88 assert.Equal(t, 0, len(channels)) 89 90 channels, needs = config.NeedsSending(&testkube.Event{TestExecution: &testkube.Execution{TestName: "test2"}, Type_: testkube.EventStartTest}) 91 assert.False(t, needs) 92 assert.Equal(t, 0, len(channels)) 93 notificationConfig[0].TestNames = oldTestNames 94 }) 95 96 t.Run("allow only one testsuite when only one testsuite is specified", func(t *testing.T) { 97 oldTestSuiteNames := notificationConfig[0].TestSuiteNames 98 notificationConfig[0].TestSuiteNames = []string{"testsuite1"} 99 config := NewConfig(notificationConfig) 100 testSuite := testkube.ObjectRef{Name: "testsuite1"} 101 channels, needs := config.NeedsSending(&testkube.Event{TestSuiteExecution: &testkube.TestSuiteExecution{TestSuite: &testSuite}, Type_: testkube.EventStartTestSuite}) 102 assert.True(t, needs) 103 assert.Equal(t, 0, len(channels)) 104 testSuite.Name = "testsuite2" 105 channels, needs = config.NeedsSending(&testkube.Event{TestSuiteExecution: &testkube.TestSuiteExecution{TestSuite: &testSuite}, Type_: testkube.EventStartTestSuite}) 106 assert.False(t, needs) 107 assert.Equal(t, 0, len(channels)) 108 notificationConfig[0].TestSuiteNames = oldTestSuiteNames 109 }) 110 111 t.Run("allow only on one channel when only one channel is specified", func(t *testing.T) { 112 oldChannelID := notificationConfig[0].ChannelID 113 notificationConfig[0].ChannelID = "channel1" 114 config := NewConfig(notificationConfig) 115 channels, needs := config.NeedsSending(&testkube.Event{Type_: testkube.EventStartTest}) 116 assert.True(t, needs) 117 assert.Equal(t, 1, len(channels)) 118 assert.Equal(t, "channel1", channels[0]) 119 notificationConfig[0].ChannelID = oldChannelID 120 }) 121 122 t.Run("allow only labels when selector is specified", func(t *testing.T) { 123 oldSelector := notificationConfig[0].Selector 124 notificationConfig[0].Selector = map[string]string{"label1": "value1"} 125 config := NewConfig(notificationConfig) 126 channels, needs := config.NeedsSending(&testkube.Event{TestExecution: &testkube.Execution{Labels: map[string]string{"label1": "value1"}}, Type_: testkube.EventStartTest}) 127 assert.True(t, needs) 128 assert.Equal(t, 0, len(channels)) 129 channels, needs = config.NeedsSending(&testkube.Event{TestExecution: &testkube.Execution{Labels: map[string]string{"label1": "value2"}}, Type_: testkube.EventStartTest}) 130 assert.False(t, needs) 131 assert.Equal(t, 0, len(channels)) 132 notificationConfig[0].Selector = oldSelector 133 }) 134 135 t.Run("don't allow events when events do not match", func(t *testing.T) { 136 oldEvents := notificationConfig[0].Events 137 notificationConfig[0].Events = []testkube.EventType{*testkube.EventStartTest} 138 config := NewConfig(notificationConfig) 139 channels, needs := config.NeedsSending(&testkube.Event{Type_: testkube.EventEndTestSuccess}) 140 assert.False(t, needs) 141 assert.Equal(t, 0, len(channels)) 142 notificationConfig[0].Events = oldEvents 143 }) 144 145 t.Run("don't allow anything when everything is empty", func(t *testing.T) { 146 config := NewConfig([]NotificationsConfig{}) 147 channels, needs := config.NeedsSending(&testkube.Event{Type_: testkube.EventStartTest}) 148 assert.False(t, needs) 149 assert.Equal(t, 0, len(channels)) 150 }) 151 152 t.Run("allow notification when multiple configs, channel is specified and event is end-test-failed", func(t *testing.T) { 153 var notificationConfigMultiple []NotificationsConfig 154 err := json.Unmarshal([]byte(multipleConfigurationString), ¬ificationConfigMultiple) 155 assert.Nil(t, err) 156 config := NewConfig(notificationConfigMultiple) 157 channels, needs := config.NeedsSending(&testkube.Event{Type_: testkube.EventEndTestFailed}) 158 assert.True(t, needs) 159 assert.Equal(t, 1, len(channels)) 160 assert.Equal(t, "ChannelID2", channels[0]) 161 }) 162 }