1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-02-11 13:53:53 +02:00

Support InfluxDB plugin in version 2.0 (#1130)

* Support InfluxDB plugin in version 2.0
closes #932
superseeds #933
This commit is contained in:
Oliver Nocon 2020-02-06 11:27:31 +01:00 committed by GitHub
parent a1232799e2
commit 61da9faf98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 9 deletions

View File

@ -10,6 +10,11 @@ import org.apache.commons.io.IOUtils
import org.jenkinsci.plugins.workflow.libs.LibrariesAction import org.jenkinsci.plugins.workflow.libs.LibrariesAction
import org.jenkinsci.plugins.workflow.steps.MissingContextVariableException import org.jenkinsci.plugins.workflow.steps.MissingContextVariableException
@NonCPS
def getActiveJenkinsInstance() {
return Jenkins.getActiveInstance()
}
@API @API
@NonCPS @NonCPS
static def isPluginActive(pluginId) { static def isPluginActive(pluginId) {
@ -144,6 +149,21 @@ void addRunSideBarLink(String relativeUrl, String displayName, String relativeIc
} }
} }
@NonCPS
def getPlugin(name){
for (plugin in getActiveJenkinsInstance().pluginManager.plugins) {
if (name == plugin.getShortName()) {
return plugin
}
}
return null
}
@NonCPS
String getPluginVersion(name) {
return getPlugin(name)?.getVersion()
}
void handleStepResults(String stepName, boolean failOnMissingReports, boolean failOnMissingLinks) { void handleStepResults(String stepName, boolean failOnMissingReports, boolean failOnMissingLinks) {
def reportsFileName = "${stepName}_reports.json" def reportsFileName = "${stepName}_reports.json"
def reportsFileExists = fileExists(file: reportsFileName) def reportsFileExists = fileExists(file: reportsFileName)

View File

@ -1,4 +1,5 @@
import com.sap.piper.DefaultValueCache import com.sap.piper.DefaultValueCache
import com.sap.piper.JenkinsUtils
import com.sap.piper.analytics.InfluxData import com.sap.piper.analytics.InfluxData
import org.junit.Before import org.junit.Before
@ -35,6 +36,13 @@ class InfluxWriteDataTest extends BasePiperTest {
Map fileMap = [:] Map fileMap = [:]
Map stepMap = [:] Map stepMap = [:]
String echoLog = '' String echoLog = ''
String influxVersion
class JenkinsUtilsMock extends JenkinsUtils {
String getPluginVersion(name) {
return influxVersion
}
}
@Before @Before
void init() throws Exception { void init() throws Exception {
@ -47,6 +55,7 @@ class InfluxWriteDataTest extends BasePiperTest {
stepMap = [:] stepMap = [:]
//reset fileMap //reset fileMap
fileMap = [:] fileMap = [:]
influxVersion = '1.15'
helper.registerAllowedMethod('readYaml', [Map.class], { map -> helper.registerAllowedMethod('readYaml', [Map.class], { map ->
return [ return [
@ -56,6 +65,8 @@ class InfluxWriteDataTest extends BasePiperTest {
}) })
helper.registerAllowedMethod('writeFile', [Map.class],{m -> fileMap[m.file] = m.text}) helper.registerAllowedMethod('writeFile', [Map.class],{m -> fileMap[m.file] = m.text})
helper.registerAllowedMethod('step', [Map.class],{m -> stepMap = m}) helper.registerAllowedMethod('step', [Map.class],{m -> stepMap = m})
helper.registerAllowedMethod('influxDbPublisher', [Map.class],{m -> stepMap = m})
} }
@ -63,7 +74,10 @@ class InfluxWriteDataTest extends BasePiperTest {
void testInfluxWriteDataWithDefault() throws Exception { void testInfluxWriteDataWithDefault() throws Exception {
nullScript.commonPipelineEnvironment.setArtifactVersion('1.2.3') nullScript.commonPipelineEnvironment.setArtifactVersion('1.2.3')
stepRule.step.influxWriteData(script: nullScript) stepRule.step.influxWriteData(
script: nullScript,
jenkinsUtilsStub: new JenkinsUtilsMock(),
)
assertThat(loggingRule.log, containsString('Artifact version: 1.2.3')) assertThat(loggingRule.log, containsString('Artifact version: 1.2.3'))
@ -78,6 +92,8 @@ class InfluxWriteDataTest extends BasePiperTest {
assertThat(fileMap, hasKey('jenkins_data_tags.json')) assertThat(fileMap, hasKey('jenkins_data_tags.json'))
assertThat(fileMap, hasKey('influx_data_tags.json')) assertThat(fileMap, hasKey('influx_data_tags.json'))
assertThat(stepMap['$class'], is('InfluxDbPublisher'))
assertJobStatusSuccess() assertJobStatusSuccess()
} }
@ -120,7 +136,11 @@ class InfluxWriteDataTest extends BasePiperTest {
helper.registerAllowedMethod("deleteDir", [], null) helper.registerAllowedMethod("deleteDir", [], null)
nullScript.commonPipelineEnvironment.setArtifactVersion('1.2.3') nullScript.commonPipelineEnvironment.setArtifactVersion('1.2.3')
stepRule.step.influxWriteData(script: nullScript, wrapInNode: true) stepRule.step.influxWriteData(
script: nullScript,
jenkinsUtilsStub: new JenkinsUtilsMock(),
wrapInNode: true
)
assertThat(nodeCalled, is(true)) assertThat(nodeCalled, is(true))
@ -132,6 +152,7 @@ class InfluxWriteDataTest extends BasePiperTest {
stepRule.step.influxWriteData( stepRule.step.influxWriteData(
//juStabUtils: utils, //juStabUtils: utils,
script: nullScript, script: nullScript,
jenkinsUtilsStub: new JenkinsUtilsMock(),
influxServer: 'myInstance', influxServer: 'myInstance',
customData: [key1: 'test1'], customData: [key1: 'test1'],
customDataTags: [tag1: 'testTag1'], customDataTags: [tag1: 'testTag1'],
@ -152,7 +173,7 @@ class InfluxWriteDataTest extends BasePiperTest {
InfluxData.addField('test_data', 'key1', 'keyValue1') InfluxData.addField('test_data', 'key1', 'keyValue1')
InfluxData.addTag('test_data', 'tag1', 'tagValue1') InfluxData.addTag('test_data', 'tag1', 'tagValue1')
stepRule.step.influxWriteData( stepRule.step.influxWriteData(
//juStabUtils: utils, jenkinsUtilsStub: new JenkinsUtilsMock(),
script: nullScript, script: nullScript,
influxServer: 'myInstance' influxServer: 'myInstance'
) )
@ -162,4 +183,22 @@ class InfluxWriteDataTest extends BasePiperTest {
assertThat(stepMap.customDataMapTags, hasKey('test_data')) assertThat(stepMap.customDataMapTags, hasKey('test_data'))
} }
@Test
void testInfluxWriteDataPluginVersion2() {
nullScript.commonPipelineEnvironment.setArtifactVersion('1.2.3')
influxVersion = '2.0'
stepRule.step.influxWriteData(
script: nullScript,
jenkinsUtilsStub: new JenkinsUtilsMock(),
)
assertThat(loggingRule.log, containsString('Artifact version: 1.2.3'))
assertThat(stepMap.selectedTarget, is('testInflux'))
assertThat(stepMap.customPrefix, isEmptyOrNullString())
assertThat(stepMap['$class'], isEmptyOrNullString())
}
} }

View File

@ -1,3 +1,5 @@
import com.sap.piper.JenkinsUtils
import static com.sap.piper.Prerequisites.checkScript import static com.sap.piper.Prerequisites.checkScript
import com.sap.piper.GenerateDocumentation import com.sap.piper.GenerateDocumentation
@ -71,6 +73,7 @@ void call(Map parameters = [:]) {
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters, allowBuildFailure: true) { handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters, allowBuildFailure: true) {
def script = checkScript(this, parameters) def script = checkScript(this, parameters)
def jenkinsUtils = parameters.jenkinsUtilsStub ?: new JenkinsUtils()
if (script == null) if (script == null)
script = this script = this
@ -118,29 +121,39 @@ InfluxDB data map tags: ${config.customDataMapTags}
if(config.wrapInNode){ if(config.wrapInNode){
node(''){ node(''){
try{ try{
writeToInflux(config, script) writeToInflux(config, jenkinsUtils, script)
}finally{ }finally{
deleteDir() deleteDir()
} }
} }
} else { } else {
writeToInflux(config, script) writeToInflux(config, jenkinsUtils, script)
} }
} }
} }
private void writeToInflux(config, script){ private void writeToInflux(config, JenkinsUtils jenkinsUtils, script){
if (config.influxServer) { if (config.influxServer) {
def influxPluginVersion = jenkinsUtils.getPluginVersion('influxdb')
try { try {
step([ def influxParams = [
$class: 'InfluxDbPublisher',
selectedTarget: config.influxServer, selectedTarget: config.influxServer,
customPrefix: config.influxPrefix, customPrefix: config.influxPrefix,
customData: config.customData.size()>0 ? config.customData : null, customData: config.customData.size()>0 ? config.customData : null,
customDataTags: config.customDataTags.size()>0 ? config.customDataTags : null, customDataTags: config.customDataTags.size()>0 ? config.customDataTags : null,
customDataMap: config.customDataMap.size()>0 ? config.customDataMap : null, customDataMap: config.customDataMap.size()>0 ? config.customDataMap : null,
customDataMapTags: config.customDataMapTags.size()>0 ? config.customDataMapTags : null customDataMapTags: config.customDataMapTags.size()>0 ? config.customDataMapTags : null
]) ]
if (!influxPluginVersion || influxPluginVersion.startsWith('1.')) {
influxParams['$class'] = 'InfluxDbPublisher'
step(influxParams)
} else {
influxDbPublisher(influxParams)
}
} catch (NullPointerException e){ } catch (NullPointerException e){
if(!e.getMessage()){ if(!e.getMessage()){
//TODO: catch NPEs as long as https://issues.jenkins-ci.org/browse/JENKINS-55594 is not fixed & released //TODO: catch NPEs as long as https://issues.jenkins-ci.org/browse/JENKINS-55594 is not fixed & released