@@ -59,4 +59,67 @@ class SlackNotificationsTest extends KibanaBasePipelineTest {
5959 args. blocks[2 ]. text. text. toString()
6060 )
6161 }
62+
63+ @Test
64+ void ' sendFailedBuild() should call slackSend() with a backup message when first attempt fails' () {
65+ mockFailureBuild()
66+ def counter = 0
67+ helper. registerAllowedMethod(' slackSend' , [Map . class], { ++ counter > 1 })
68+ slackNotifications. sendFailedBuild()
69+
70+ def args = fnMocks(' slackSend' )[1 ]. args[0 ]
71+
72+ def expected = [
73+ channel : ' #kibana-operations-alerts' ,
74+ username : ' Kibana Operations' ,
75+ iconEmoji : ' :jenkins:' ,
76+ color : ' danger' ,
77+ message : ' :broken_heart: elastic / kibana # master #1' ,
78+ ]
79+
80+ expected. each {
81+ assertEquals (it. value. toString(), args[it. key]. toString())
82+ }
83+
84+ assertEquals (
85+ " :broken_heart: *<http://jenkins.localhost:8080/job/elastic+kibana+master/1/|elastic / kibana # master #1>*" +
86+ " \n\n First attempt at sending this notification failed. Please check the build." ,
87+ args. blocks[0 ]. text. text. toString()
88+ )
89+ }
90+
91+ @Test
92+ void ' getTestFailures() should truncate list of failures to 10' () {
93+ prop(' testUtils' , [
94+ getFailures : {
95+ return (1 .. 12 ). collect {
96+ return [
97+ url : Mocks . TEST_FAILURE_URL ,
98+ fullDisplayName : " Failure #${ it} " ,
99+ ]
100+ }
101+ },
102+ ])
103+
104+ def message = (String ) slackNotifications. getTestFailures()
105+
106+ assertTrue (" Message ends with truncated indicator" , message. endsWith(" ...and 2 more" ))
107+ assertTrue (" Message contains Failure #10" , message. contains(" Failure #10" ))
108+ assertTrue (" Message does not contain Failure #11" , ! message. contains(" Failure #11" ))
109+ }
110+
111+ @Test
112+ void ' shortenMessage() should truncate a long message, but leave parts that fit' () {
113+ assertEquals (' Hello\n Hello\n [...truncated...]' , slackNotifications. shortenMessage(' Hello\n Hello\n this is a long string' , 29 ))
114+ }
115+
116+ @Test
117+ void ' shortenMessage() should not modify a short message' () {
118+ assertEquals (' Hello world' , slackNotifications. shortenMessage(' Hello world' , 11 ))
119+ }
120+
121+ @Test
122+ void ' shortenMessage() should truncate an entire message with only one part' () {
123+ assertEquals (' [...truncated...]' , slackNotifications. shortenMessage(' Hello world this is a really long message' , 40 ))
124+ }
62125}
0 commit comments