2020-01-29 14:28:30 +02:00
|
|
|
package com.sap.piper
|
|
|
|
|
|
|
|
import org.junit.Assert
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
import util.BasePiperTest
|
2020-01-29 16:04:28 +02:00
|
|
|
import util.JenkinsLoggingRule
|
2020-01-29 14:28:30 +02:00
|
|
|
import util.Rules
|
|
|
|
|
|
|
|
class DebugReportTest extends BasePiperTest {
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain ruleChain = Rules.getCommonRules(this)
|
2020-01-29 16:04:28 +02:00
|
|
|
.around(new JenkinsLoggingRule(this))
|
2020-01-29 14:28:30 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
void testInitFromEnvironment() {
|
|
|
|
Map env = createEnv()
|
|
|
|
DebugReport.instance.initFromEnvironment(env)
|
|
|
|
|
|
|
|
Assert.assertTrue(DebugReport.instance.environment.containsKey('build_details'))
|
2020-04-17 10:31:04 +02:00
|
|
|
Assert.assertEquals('Kubernetes', DebugReport.instance.environment.get('environment'))
|
2020-01-29 14:28:30 +02:00
|
|
|
|
|
|
|
Set<String> buildDetails = DebugReport.instance.environment.build_details as Set<String>
|
|
|
|
Assert.assertTrue(buildDetails.size() > 0)
|
|
|
|
|
|
|
|
boolean foundJenkinsVersion = false
|
|
|
|
boolean foundJavaVersion = false
|
|
|
|
|
|
|
|
for (String details in buildDetails) {
|
|
|
|
if (details.contains(env.get('JENKINS_VERSION') as String))
|
|
|
|
foundJenkinsVersion = true
|
|
|
|
if (details.contains(env.get('JAVA_VERSION') as String))
|
|
|
|
foundJavaVersion = true
|
|
|
|
}
|
|
|
|
Assert.assertTrue(foundJenkinsVersion)
|
|
|
|
Assert.assertTrue(foundJavaVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testLogOutput() {
|
|
|
|
DebugReport.instance.initFromEnvironment(createEnv())
|
|
|
|
DebugReport.instance.setGitRepoInfo('GIT_URL' : 'git://url', 'GIT_LOCAL_BRANCH' : 'some-branch')
|
|
|
|
|
2020-02-07 17:30:08 +02:00
|
|
|
String debugReport = DebugReport.instance.generateReport(mockScript(), false)
|
2020-01-29 14:28:30 +02:00
|
|
|
|
|
|
|
Assert.assertTrue(debugReport.contains('## Pipeline Environment'))
|
|
|
|
Assert.assertTrue(debugReport.contains('## Local Extensions'))
|
|
|
|
Assert.assertTrue(debugReport.contains('#### Environment\n' +
|
2020-04-17 10:31:04 +02:00
|
|
|
'`Kubernetes`'))
|
2020-01-29 14:28:30 +02:00
|
|
|
Assert.assertFalse(debugReport.contains('Repository | Branch'))
|
|
|
|
Assert.assertFalse(debugReport.contains('some-branch'))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testLogOutputConfidential() {
|
|
|
|
DebugReport.instance.initFromEnvironment(createEnv())
|
|
|
|
DebugReport.instance.setGitRepoInfo('GIT_URL' : 'git://url', 'GIT_LOCAL_BRANCH' : 'some-branch')
|
|
|
|
|
2020-02-07 17:30:08 +02:00
|
|
|
String debugReport = DebugReport.instance.generateReport(mockScript(), true)
|
2020-01-29 14:28:30 +02:00
|
|
|
|
|
|
|
Assert.assertTrue(debugReport.contains('## Pipeline Environment'))
|
|
|
|
Assert.assertTrue(debugReport.contains('## Local Extensions'))
|
|
|
|
Assert.assertTrue(debugReport.contains('#### Environment\n' +
|
2020-04-17 10:31:04 +02:00
|
|
|
'`Kubernetes`'))
|
2020-01-29 14:28:30 +02:00
|
|
|
Assert.assertTrue(debugReport.contains('Repository | Branch'))
|
|
|
|
Assert.assertTrue(debugReport.contains('some-branch'))
|
|
|
|
}
|
|
|
|
|
|
|
|
private Script mockScript() {
|
|
|
|
helper.registerAllowedMethod("libraryResource", [String.class], { path ->
|
|
|
|
|
|
|
|
File resource = new File(new File('resources'), path)
|
|
|
|
if (resource.exists()) {
|
|
|
|
return resource.getText()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ''
|
|
|
|
})
|
|
|
|
|
|
|
|
return nullScript
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Map createEnv() {
|
|
|
|
Map env = [:]
|
|
|
|
env.put('JENKINS_VERSION', '42')
|
|
|
|
env.put('JAVA_VERSION', '8')
|
|
|
|
env.put('ON_K8S', 'true')
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
}
|