2020-09-24 13:47:20 +02:00
import org.junit.After
2018-10-17 12:05:11 +02:00
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.*
2020-09-24 13:47:20 +02:00
import com.sap.piper.Utils
2018-10-17 12:05:11 +02:00
import static org . hamcrest . Matchers . *
import static org . junit . Assert . assertThat
class MailSendNotificationTest extends BasePiperTest {
2019-01-22 10:22:15 +02:00
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule ( this )
2019-01-22 10:25:42 +02:00
private JenkinsStepRule stepRule = new JenkinsStepRule ( this )
2019-01-22 10:19:28 +02:00
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule ( this )
2018-10-17 12:05:11 +02:00
@Rule
public RuleChain ruleChain = Rules
. getCommonRules ( this )
. around ( new JenkinsReadYamlRule ( this ) )
2019-01-22 10:22:15 +02:00
. around ( loggingRule )
2019-01-22 10:19:28 +02:00
. around ( shellRule )
2019-01-22 10:25:42 +02:00
. around ( stepRule )
2018-10-17 12:05:11 +02:00
@Before
void init ( ) throws Exception {
// register Jenkins commands with mock values
helper . registerAllowedMethod ( "deleteDir" , [ ] , null )
helper . registerAllowedMethod ( "sshagent" , [ Map . class , Closure . class ] , null )
nullScript . commonPipelineEnvironment . configuration = nullScript . commonPipelineEnvironment . configuration ? : [ : ]
nullScript . commonPipelineEnvironment . configuration [ 'general' ] = nullScript . commonPipelineEnvironment . configuration [ 'general' ] ? : [ : ]
nullScript . commonPipelineEnvironment . configuration [ 'steps' ] = nullScript . commonPipelineEnvironment . configuration [ 'steps' ] ? : [ : ]
nullScript . commonPipelineEnvironment . configuration [ 'steps' ] [ 'mailSendNotification' ] = nullScript . commonPipelineEnvironment . configuration [ 'steps' ] [ 'mailSendNotification' ] ? : [ : ]
helper . registerAllowedMethod ( 'requestor' , [ ] , { - > return [ $class : 'RequesterRecipientProvider' ] } )
2020-09-24 13:47:20 +02:00
Utils . metaClass . echo = { def m - > }
}
@After
public void tearDown ( ) {
Utils . metaClass = null
2018-10-17 12:05:11 +02:00
}
@Test
void testGetDistinctRecipients ( ) throws Exception {
// git log -10 --pretty=format:"%ae %ce"
def input = '' ' user1 @domain.com noreply + github @domain.com
user1 @domain.com noreply + github @domain.com
user3 @domain.com noreply + github @domain.com
user2 @domain.com user1 @domain.com
2020-02-13 16:12:48 +02:00
user1 @noreply.domain.com
2018-10-17 12:05:11 +02:00
user1 @domain.com noreply + github @domain.com
user3 @domain.com noreply + github @domain.com
user3 @domain.com noreply + github @domain.com
user3 @domain.com noreply + github @domain.com
user3 @domain.com noreply + github @domain.com
user3 @domain.com noreply + github @domain.com '' '
2019-01-22 10:25:42 +02:00
def result = stepRule . step . getDistinctRecipients ( input )
2018-10-17 12:05:11 +02:00
// asserts
assertThat ( result . split ( ' ' ) , arrayWithSize ( 3 ) )
assertThat ( result , containsString ( 'user1@domain.com' ) )
assertThat ( result , containsString ( 'user2@domain.com' ) )
assertThat ( result , containsString ( 'user3@domain.com' ) )
}
@Test
void testCulpritsFromGitCommit ( ) throws Exception {
2019-09-16 10:02:21 +02:00
def gitCommand = "git log -2 --first-parent --pretty=format:'%ae %ce'"
2018-10-17 12:05:11 +02:00
def expected = "user2@domain.com user3@domain.com"
2019-09-16 10:02:21 +02:00
shellRule . setReturnValue ( "git log -2 --first-parent --pretty=format:'%ae %ce'" , 'user2@domain.com user3@domain.com' )
2018-10-17 12:05:11 +02:00
2019-01-22 10:25:42 +02:00
def result = stepRule . step . getCulprits (
2018-10-17 12:05:11 +02:00
[
gitSSHCredentialsId: '' ,
2019-11-11 18:40:23 +02:00
gitUrl: 'git@github.domain.com:IndustryCloudFoundation/pipeline-test-node.git' ,
2018-10-17 12:05:11 +02:00
gitCommitId: 'f0973368a35a2b973612acb86f932c61f2635f6e'
] ,
'master' ,
2 )
// asserts
assertThat ( result , containsString ( 'user2@domain.com' ) )
assertThat ( result , containsString ( 'user3@domain.com' ) )
}
@Test
void testCulpritsWithEmptyGitCommit ( ) throws Exception {
2019-01-22 10:19:28 +02:00
shellRule . setReturnValue ( 'git log > /dev/null 2>&1' , 1 )
2018-10-17 12:05:11 +02:00
2019-01-22 10:25:42 +02:00
stepRule . step . getCulprits (
2018-10-17 12:05:11 +02:00
[
gitSSHCredentialsId: '' ,
2019-11-11 18:40:23 +02:00
gitUrl: 'git@github.domain.com:IndustryCloudFoundation/pipeline-test-node.git' ,
2018-10-17 12:05:11 +02:00
gitCommitId: ''
] ,
'master' ,
2 )
// asserts
2019-01-22 10:22:15 +02:00
assertThat ( loggingRule . log , containsString ( '[mailSendNotification] No git context available to retrieve culprits' ) )
2018-10-17 12:05:11 +02:00
}
@Test
void testCulpritsWithoutGitCommit ( ) throws Exception {
2019-01-22 10:19:28 +02:00
shellRule . setReturnValue ( 'git log > /dev/null 2>&1' , 1 )
2018-10-17 12:05:11 +02:00
2019-01-22 10:25:42 +02:00
stepRule . step . getCulprits (
2018-10-17 12:05:11 +02:00
[
gitSSHCredentialsId: '' ,
2019-11-11 18:40:23 +02:00
gitUrl: 'git@github.domain.com:IndustryCloudFoundation/pipeline-test-node.git' ,
2018-10-17 12:05:11 +02:00
gitCommitId: null
] ,
'master' ,
2 )
// asserts
2019-01-22 10:22:15 +02:00
assertThat ( loggingRule . log , containsString ( '[mailSendNotification] No git context available to retrieve culprits' ) )
2018-10-17 12:05:11 +02:00
}
@Test
void testCulpritsWithoutBranch ( ) throws Exception {
2019-01-22 10:19:28 +02:00
shellRule . setReturnValue ( 'git log > /dev/null 2>&1' , 1 )
2018-10-17 12:05:11 +02:00
2019-01-22 10:25:42 +02:00
stepRule . step . getCulprits (
2018-10-17 12:05:11 +02:00
[
gitSSHCredentialsId: '' ,
2019-11-11 18:40:23 +02:00
gitUrl: 'git@github.domain.com:IndustryCloudFoundation/pipeline-test-node.git' ,
2018-10-17 12:05:11 +02:00
gitCommitId: ''
] ,
null ,
2 )
// asserts
2019-01-22 10:22:15 +02:00
assertThat ( loggingRule . log , containsString ( '[mailSendNotification] No git context available to retrieve culprits' ) )
2018-10-17 12:05:11 +02:00
}
@Test
void testSendNotificationMail ( ) throws Exception {
def emailParameters = [ : ]
def buildMock = [
fullProjectName: 'testProjectName' ,
displayName: 'testDisplayName' ,
result: 'FAILURE' ,
rawBuild: [
2019-11-11 18:40:23 +02:00
getLog: { cnt - > return [ 'Setting http proxy: proxy.domain.com:8080' ,
2018-10-17 12:05:11 +02:00
' > git fetch --no-tags --progress https://github.com/SAP/jenkins-library.git +refs/heads/*:refs/remotes/origin/*' ,
'Checking out Revision myUniqueCommitId (master)' ,
' > git config core.sparsecheckout # timeout=10' ,
' > git checkout -f myUniqueCommitId' ,
'Commit message: "Merge pull request #147 from marcusholl/pr/useGitRevParseForInsideGitRepoCheck"' ,
' > git rev-list --no-walk myUniqueCommitId # timeout=10' ,
'[Pipeline] node' ,
'Running on Jenkins in /var/jenkins_home/workspace/Test/UserId/ECHO' ,
'[Pipeline] {' ,
'[Pipeline] stage' ,
'[Pipeline] { (A)' ,
'[Pipeline] script' ,
'[Pipeline] {' ]
}
] ,
getPreviousBuild: {
return null
}
]
nullScript . currentBuild = buildMock
nullScript . commonPipelineEnvironment . configuration [ 'steps' ] [ 'mailSendNotification' ] [ 'notificationRecipients' ] = 'piper@domain.com'
helper . registerAllowedMethod ( 'emailext' , [ Map . class ] , { map - >
emailParameters = map
return ''
} )
2019-01-22 10:25:42 +02:00
stepRule . step . mailSendNotification (
2018-10-17 12:05:11 +02:00
script: nullScript ,
notifyCulprits: false ,
2019-11-11 18:40:23 +02:00
gitUrl: 'git@github.domain.com:IndustryCloudFoundation/pipeline-test-node.git'
2018-10-17 12:05:11 +02:00
)
// asserts
assertThat ( emailParameters . to , is ( 'piper@domain.com' ) )
assertThat ( emailParameters . subject , is ( 'FAILURE: Build testProjectName testDisplayName' ) )
assertThat ( emailParameters . body , startsWith ( '<a href="http://build.url">http://build.url</a>\n<br>\nTo have a detailed look at the different pipeline stages: <a href="null">null</a>\n<br>\n<h3>Last lines of output</h3>' ) )
assertThat ( emailParameters . body , containsString ( ' > git fetch --no-tags --progress https://github.com/SAP/jenkins-library.git +refs/heads/*:refs/remotes/origin/*' ) )
assertJobStatusSuccess ( )
}
@Test
void testSendNotificationMailWithGeneralConfig ( ) throws Exception {
def credentials
nullScript . currentBuild = [
fullProjectName: 'testProjectName' ,
displayName: 'testDisplayName' ,
result: 'FAILURE' ,
rawBuild: [ getLog: { cnt - > return [ 'empty' ] } ] ,
getChangeSets: { return null } ,
getPreviousBuild: { return null }
]
nullScript . commonPipelineEnvironment . configuration [ 'general' ] [ 'gitSshKeyCredentialsId' ] = 'myCredentialsId'
helper . registerAllowedMethod ( 'emailext' , [ Map . class ] , null )
helper . registerAllowedMethod ( "sshagent" , [ Map . class , Closure . class ] , { map , closure - >
credentials = map . credentials
return null
} )
2019-01-22 10:19:28 +02:00
shellRule . setReturnValue ( "git log -0 --pretty=format:'%ae %ce'" , 'user2@domain.com user3@domain.com' )
2018-10-17 12:05:11 +02:00
2019-01-22 10:25:42 +02:00
stepRule . step . mailSendNotification (
2018-10-17 12:05:11 +02:00
script: nullScript ,
gitCommitId: 'abcd1234' ,
//notifyCulprits: true,
2019-11-11 18:40:23 +02:00
gitUrl: 'git@github.domain.com:IndustryCloudFoundation/pipeline-test-node.git'
2018-10-17 12:05:11 +02:00
)
// asserts
assertThat ( credentials , hasItem ( 'myCredentialsId' ) )
assertJobStatusSuccess ( )
}
@Test
void testSendNotificationMailWithEmptySshKey ( ) throws Exception {
def credentials
nullScript . currentBuild = [
fullProjectName: 'testProjectName' ,
displayName: 'testDisplayName' ,
result: 'FAILURE' ,
rawBuild: [ getLog: { cnt - > return [ 'empty' ] } ] ,
getChangeSets: { return null } ,
getPreviousBuild: { return null }
]
helper . registerAllowedMethod ( 'emailext' , [ Map . class ] , null )
helper . registerAllowedMethod ( "sshagent" , [ Map . class , Closure . class ] , { map , closure - >
credentials = map . credentials
return null
} )
2019-01-22 10:19:28 +02:00
shellRule . setReturnValue ( "git log -0 --pretty=format:'%ae %ce'" , 'user2@domain.com user3@domain.com' )
2018-10-17 12:05:11 +02:00
2019-01-22 10:25:42 +02:00
stepRule . step . mailSendNotification (
2018-10-17 12:05:11 +02:00
script: nullScript ,
gitCommitId: 'abcd1234' ,
2019-11-11 18:40:23 +02:00
gitUrl: 'git@github.domain.com:IndustryCloudFoundation/pipeline-test-node.git'
2018-10-17 12:05:11 +02:00
)
// asserts
assertThat ( credentials , hasItem ( '' ) )
assertJobStatusSuccess ( )
}
2022-12-12 11:26:16 +02:00
@Test
void testSendNotificationMailOnFirstBuild ( ) throws Exception {
def emailExtCalls = [ ]
def buildMock = [
fullProjectName: 'testProjectName' ,
displayName: 'testDisplayName' ,
result: 'SUCCESS' ,
getPreviousBuild: {
return null
}
]
nullScript . currentBuild = buildMock
helper . registerAllowedMethod ( 'emailext' , [ Map . class ] , { map - >
emailExtCalls . add ( map )
return ''
} )
stepRule . step . mailSendNotification (
script: nullScript ,
notifyCulprits: false ,
gitUrl: 'git@github.domain.com:IndustryCloudFoundation/pipeline-test-node.git'
)
assertThat ( emailExtCalls , hasSize ( 0 ) )
}
@Test
void testSendNotificationMailOnRecovery ( ) throws Exception {
def emailExtCalls = [ ]
def buildMock = [
fullProjectName: 'testProjectName' ,
displayName: 'testDisplayName' ,
result: 'SUCCESS' ,
getPreviousBuild: {
return [ result: 'FAILURE' ]
}
]
nullScript . currentBuild = buildMock
helper . registerAllowedMethod ( 'emailext' , [ Map . class ] , { map - >
emailExtCalls . add ( map )
return ''
} )
stepRule . step . mailSendNotification (
script: nullScript ,
notifyCulprits: false ,
gitUrl: 'git@github.domain.com:IndustryCloudFoundation/pipeline-test-node.git'
)
assertThat ( emailExtCalls , hasSize ( 1 ) )
assertThat ( emailExtCalls [ 0 ] . subject , is ( "SUCCESS: Build testProjectName testDisplayName is back to normal" ) )
}
2018-10-17 12:05:11 +02:00
}