mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-02-05 13:25:19 +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:
parent
a1232799e2
commit
61da9faf98
@ -10,6 +10,11 @@ import org.apache.commons.io.IOUtils
|
||||
import org.jenkinsci.plugins.workflow.libs.LibrariesAction
|
||||
import org.jenkinsci.plugins.workflow.steps.MissingContextVariableException
|
||||
|
||||
@NonCPS
|
||||
def getActiveJenkinsInstance() {
|
||||
return Jenkins.getActiveInstance()
|
||||
}
|
||||
|
||||
@API
|
||||
@NonCPS
|
||||
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) {
|
||||
def reportsFileName = "${stepName}_reports.json"
|
||||
def reportsFileExists = fileExists(file: reportsFileName)
|
||||
|
@ -1,4 +1,5 @@
|
||||
import com.sap.piper.DefaultValueCache
|
||||
import com.sap.piper.JenkinsUtils
|
||||
import com.sap.piper.analytics.InfluxData
|
||||
|
||||
import org.junit.Before
|
||||
@ -35,6 +36,13 @@ class InfluxWriteDataTest extends BasePiperTest {
|
||||
Map fileMap = [:]
|
||||
Map stepMap = [:]
|
||||
String echoLog = ''
|
||||
String influxVersion
|
||||
|
||||
class JenkinsUtilsMock extends JenkinsUtils {
|
||||
String getPluginVersion(name) {
|
||||
return influxVersion
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
void init() throws Exception {
|
||||
@ -47,6 +55,7 @@ class InfluxWriteDataTest extends BasePiperTest {
|
||||
stepMap = [:]
|
||||
//reset fileMap
|
||||
fileMap = [:]
|
||||
influxVersion = '1.15'
|
||||
|
||||
helper.registerAllowedMethod('readYaml', [Map.class], { map ->
|
||||
return [
|
||||
@ -56,6 +65,8 @@ class InfluxWriteDataTest extends BasePiperTest {
|
||||
})
|
||||
helper.registerAllowedMethod('writeFile', [Map.class],{m -> fileMap[m.file] = m.text})
|
||||
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 {
|
||||
|
||||
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'))
|
||||
|
||||
@ -78,6 +92,8 @@ class InfluxWriteDataTest extends BasePiperTest {
|
||||
assertThat(fileMap, hasKey('jenkins_data_tags.json'))
|
||||
assertThat(fileMap, hasKey('influx_data_tags.json'))
|
||||
|
||||
assertThat(stepMap['$class'], is('InfluxDbPublisher'))
|
||||
|
||||
assertJobStatusSuccess()
|
||||
}
|
||||
|
||||
@ -120,7 +136,11 @@ class InfluxWriteDataTest extends BasePiperTest {
|
||||
helper.registerAllowedMethod("deleteDir", [], null)
|
||||
|
||||
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))
|
||||
|
||||
@ -132,6 +152,7 @@ class InfluxWriteDataTest extends BasePiperTest {
|
||||
stepRule.step.influxWriteData(
|
||||
//juStabUtils: utils,
|
||||
script: nullScript,
|
||||
jenkinsUtilsStub: new JenkinsUtilsMock(),
|
||||
influxServer: 'myInstance',
|
||||
customData: [key1: 'test1'],
|
||||
customDataTags: [tag1: 'testTag1'],
|
||||
@ -152,7 +173,7 @@ class InfluxWriteDataTest extends BasePiperTest {
|
||||
InfluxData.addField('test_data', 'key1', 'keyValue1')
|
||||
InfluxData.addTag('test_data', 'tag1', 'tagValue1')
|
||||
stepRule.step.influxWriteData(
|
||||
//juStabUtils: utils,
|
||||
jenkinsUtilsStub: new JenkinsUtilsMock(),
|
||||
script: nullScript,
|
||||
influxServer: 'myInstance'
|
||||
)
|
||||
@ -162,4 +183,22 @@ class InfluxWriteDataTest extends BasePiperTest {
|
||||
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())
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import com.sap.piper.JenkinsUtils
|
||||
|
||||
import static com.sap.piper.Prerequisites.checkScript
|
||||
|
||||
import com.sap.piper.GenerateDocumentation
|
||||
@ -71,6 +73,7 @@ void call(Map parameters = [:]) {
|
||||
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters, allowBuildFailure: true) {
|
||||
|
||||
def script = checkScript(this, parameters)
|
||||
def jenkinsUtils = parameters.jenkinsUtilsStub ?: new JenkinsUtils()
|
||||
if (script == null)
|
||||
script = this
|
||||
|
||||
@ -118,29 +121,39 @@ InfluxDB data map tags: ${config.customDataMapTags}
|
||||
if(config.wrapInNode){
|
||||
node(''){
|
||||
try{
|
||||
writeToInflux(config, script)
|
||||
writeToInflux(config, jenkinsUtils, script)
|
||||
}finally{
|
||||
deleteDir()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
writeToInflux(config, script)
|
||||
writeToInflux(config, jenkinsUtils, script)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void writeToInflux(config, script){
|
||||
private void writeToInflux(config, JenkinsUtils jenkinsUtils, script){
|
||||
if (config.influxServer) {
|
||||
|
||||
def influxPluginVersion = jenkinsUtils.getPluginVersion('influxdb')
|
||||
|
||||
try {
|
||||
step([
|
||||
$class: 'InfluxDbPublisher',
|
||||
def influxParams = [
|
||||
selectedTarget: config.influxServer,
|
||||
customPrefix: config.influxPrefix,
|
||||
customData: config.customData.size()>0 ? config.customData : null,
|
||||
customDataTags: config.customDataTags.size()>0 ? config.customDataTags : null,
|
||||
customDataMap: config.customDataMap.size()>0 ? config.customDataMap : 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){
|
||||
if(!e.getMessage()){
|
||||
//TODO: catch NPEs as long as https://issues.jenkins-ci.org/browse/JENKINS-55594 is not fixed & released
|
||||
|
Loading…
x
Reference in New Issue
Block a user