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.*
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' ] } )
}
@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
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: '' ,
gitUrl: 'git@github.wdf.domain.com:IndustryCloudFoundation/pipeline-test-node.git' ,
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: '' ,
gitUrl: 'git@github.wdf.domain.com:IndustryCloudFoundation/pipeline-test-node.git' ,
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: '' ,
gitUrl: 'git@github.wdf.domain.com:IndustryCloudFoundation/pipeline-test-node.git' ,
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: '' ,
gitUrl: 'git@github.wdf.domain.com:IndustryCloudFoundation/pipeline-test-node.git' ,
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: [
getLog: { cnt - > return [ 'Setting http proxy: proxy.wdf.domain.com:8080' ,
' > 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 ,
gitUrl: 'git@github.wdf.domain.com:IndustryCloudFoundation/pipeline-test-node.git'
)
// 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,
gitUrl: 'git@github.wdf.domain.com:IndustryCloudFoundation/pipeline-test-node.git'
)
// 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' ,
gitUrl: 'git@github.wdf.domain.com:IndustryCloudFoundation/pipeline-test-node.git'
)
// asserts
assertThat ( credentials , hasItem ( '' ) )
assertJobStatusSuccess ( )
}
}