2018-01-24 10:55:38 +02:00
|
|
|
#!groovy
|
2018-01-26 16:39:04 +02:00
|
|
|
import com.sap.piper.DefaultValueCache
|
2019-04-11 11:39:41 +02:00
|
|
|
import com.sap.piper.analytics.InfluxData
|
|
|
|
|
2018-01-24 10:55:38 +02:00
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.RuleChain
|
2018-06-06 11:19:19 +02:00
|
|
|
import util.BasePiperTest
|
2018-01-24 10:55:38 +02:00
|
|
|
import util.JenkinsLoggingRule
|
2018-02-28 14:11:09 +02:00
|
|
|
import util.JenkinsStepRule
|
2018-08-31 10:22:43 +02:00
|
|
|
import util.JenkinsReadYamlRule
|
2018-01-26 15:55:15 +02:00
|
|
|
import util.Rules
|
2018-01-24 10:55:38 +02:00
|
|
|
|
2019-01-17 16:42:03 +02:00
|
|
|
import static org.hamcrest.Matchers.allOf
|
|
|
|
import static org.hamcrest.Matchers.containsString
|
|
|
|
import static org.hamcrest.Matchers.hasKey
|
|
|
|
import static org.hamcrest.Matchers.hasValue
|
2018-12-12 18:33:36 +02:00
|
|
|
import static org.hamcrest.Matchers.is
|
2019-01-17 16:42:03 +02:00
|
|
|
import static org.hamcrest.Matchers.isEmptyOrNullString
|
2018-12-12 18:33:36 +02:00
|
|
|
import static org.junit.Assert.assertThat
|
2018-01-24 10:55:38 +02:00
|
|
|
import static org.junit.Assert.assertTrue
|
|
|
|
import static org.junit.Assert.assertEquals
|
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
class InfluxWriteDataTest extends BasePiperTest {
|
2018-02-28 14:11:09 +02:00
|
|
|
public JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
|
2019-01-22 10:25:42 +02:00
|
|
|
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
|
2018-01-24 10:55:38 +02:00
|
|
|
|
2018-02-28 14:11:09 +02:00
|
|
|
@Rule
|
|
|
|
public RuleChain ruleChain = Rules
|
|
|
|
.getCommonRules(this)
|
2018-08-31 10:22:43 +02:00
|
|
|
.around(new JenkinsReadYamlRule(this))
|
2018-02-28 14:11:09 +02:00
|
|
|
.around(loggingRule)
|
2019-01-22 10:25:42 +02:00
|
|
|
.around(stepRule)
|
2018-01-24 10:55:38 +02:00
|
|
|
|
|
|
|
Map fileMap = [:]
|
|
|
|
Map stepMap = [:]
|
|
|
|
String echoLog = ''
|
|
|
|
|
|
|
|
@Before
|
|
|
|
void init() throws Exception {
|
2018-01-26 16:39:04 +02:00
|
|
|
// Currently we have dependencies between the tests since
|
|
|
|
// DefaultValueCache is a singleton which keeps its status
|
|
|
|
// for all the tests. Depending on the test order we fail.
|
|
|
|
// As long as this status remains we need:
|
|
|
|
DefaultValueCache.reset()
|
2018-01-24 10:55:38 +02:00
|
|
|
//reset stepMap
|
|
|
|
stepMap = [:]
|
|
|
|
//reset fileMap
|
|
|
|
fileMap = [:]
|
|
|
|
|
|
|
|
helper.registerAllowedMethod('readYaml', [Map.class], { map ->
|
|
|
|
return [
|
|
|
|
general: [productiveBranch: 'develop'],
|
|
|
|
steps : [influxWriteData: [influxServer: 'testInflux']]
|
|
|
|
]
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod('writeFile', [Map.class],{m -> fileMap[m.file] = m.text})
|
|
|
|
helper.registerAllowedMethod('step', [Map.class],{m -> stepMap = m})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testInfluxWriteDataWithDefault() throws Exception {
|
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
nullScript.commonPipelineEnvironment.setArtifactVersion('1.2.3')
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.influxWriteData(script: nullScript)
|
2018-01-24 10:55:38 +02:00
|
|
|
|
2019-01-17 16:42:03 +02:00
|
|
|
assertThat(loggingRule.log, containsString('Artifact version: 1.2.3'))
|
2018-01-24 10:55:38 +02:00
|
|
|
|
2019-01-17 16:42:03 +02:00
|
|
|
assertThat(stepMap.selectedTarget, is('testInflux'))
|
|
|
|
assertThat(stepMap.customPrefix, isEmptyOrNullString())
|
2018-01-24 10:55:38 +02:00
|
|
|
|
2019-01-17 16:42:03 +02:00
|
|
|
assertThat(stepMap.customData, isEmptyOrNullString())
|
|
|
|
assertThat(stepMap.customDataMap, is([pipeline_data: [:], step_data: [:]]))
|
|
|
|
|
|
|
|
assertThat(fileMap, hasKey('jenkins_data.json'))
|
|
|
|
assertThat(fileMap, hasKey('influx_data.json'))
|
|
|
|
assertThat(fileMap, hasKey('jenkins_data_tags.json'))
|
|
|
|
assertThat(fileMap, hasKey('influx_data_tags.json'))
|
2018-01-24 10:55:38 +02:00
|
|
|
|
|
|
|
assertJobStatusSuccess()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testInfluxWriteDataNoInflux() throws Exception {
|
|
|
|
|
2018-06-06 11:19:19 +02:00
|
|
|
nullScript.commonPipelineEnvironment.setArtifactVersion('1.2.3')
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.influxWriteData(script: nullScript, influxServer: '')
|
2018-01-24 10:55:38 +02:00
|
|
|
|
|
|
|
assertEquals(0, stepMap.size())
|
|
|
|
|
|
|
|
assertTrue(fileMap.containsKey('jenkins_data.json'))
|
2019-01-17 16:42:03 +02:00
|
|
|
assertTrue(fileMap.containsKey('influx_data.json'))
|
2018-01-24 10:55:38 +02:00
|
|
|
|
|
|
|
assertJobStatusSuccess()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testInfluxWriteDataNoArtifactVersion() throws Exception {
|
|
|
|
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.influxWriteData(script: nullScript)
|
2018-01-24 10:55:38 +02:00
|
|
|
|
|
|
|
assertEquals(0, stepMap.size())
|
|
|
|
assertEquals(0, fileMap.size())
|
|
|
|
|
|
|
|
assertTrue(loggingRule.log.contains('no artifact version available -> exiting writeInflux without writing data'))
|
|
|
|
|
|
|
|
assertJobStatusSuccess()
|
|
|
|
}
|
2018-12-12 18:33:36 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
void testInfluxWriteDataWrapInNode() throws Exception {
|
|
|
|
|
|
|
|
boolean nodeCalled = false
|
|
|
|
helper.registerAllowedMethod('node', [String.class, Closure.class]) {s, body ->
|
|
|
|
nodeCalled = true
|
|
|
|
return body()
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.registerAllowedMethod("deleteDir", [], null)
|
|
|
|
|
|
|
|
nullScript.commonPipelineEnvironment.setArtifactVersion('1.2.3')
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.influxWriteData(script: nullScript, wrapInNode: true)
|
2018-12-12 18:33:36 +02:00
|
|
|
|
|
|
|
assertThat(nodeCalled, is(true))
|
|
|
|
|
|
|
|
}
|
2019-01-17 16:42:03 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
void testInfluxCustomData() {
|
|
|
|
nullScript.commonPipelineEnvironment.setArtifactVersion('1.2.3')
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.influxWriteData(
|
2019-01-17 16:42:03 +02:00
|
|
|
//juStabUtils: utils,
|
|
|
|
script: nullScript,
|
|
|
|
influxServer: 'myInstance',
|
|
|
|
customData: [key1: 'test1'],
|
|
|
|
customDataTags: [tag1: 'testTag1'],
|
|
|
|
customDataMap: [test_data: [key1: 'keyValue1']],
|
|
|
|
customDataMapTags: [test_data: [tag1: 'tagValue1']]
|
|
|
|
)
|
|
|
|
assertThat(stepMap.customData, allOf(hasKey('key1'), hasValue('test1')))
|
|
|
|
assertThat(stepMap.customDataTags, allOf(hasKey('tag1'), hasValue('testTag1')))
|
|
|
|
assertThat(stepMap.customDataMap, hasKey('test_data'))
|
|
|
|
assertThat(stepMap.customDataMapTags, hasKey('test_data'))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testInfluxCustomDataFromCPE() {
|
|
|
|
nullScript.commonPipelineEnvironment.reset()
|
|
|
|
nullScript.commonPipelineEnvironment.setArtifactVersion('1.2.3')
|
2019-04-11 11:39:41 +02:00
|
|
|
InfluxData.addTag('jenkins_custom_data', 'tag1', 'testTag1')
|
|
|
|
InfluxData.addField('test_data', 'key1', 'keyValue1')
|
|
|
|
InfluxData.addTag('test_data', 'tag1', 'tagValue1')
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.influxWriteData(
|
2019-01-17 16:42:03 +02:00
|
|
|
//juStabUtils: utils,
|
|
|
|
script: nullScript,
|
|
|
|
influxServer: 'myInstance'
|
|
|
|
)
|
|
|
|
assertThat(stepMap.customData, isEmptyOrNullString())
|
|
|
|
assertThat(stepMap.customDataTags, allOf(hasKey('tag1'), hasValue('testTag1')))
|
|
|
|
assertThat(stepMap.customDataMap, hasKey('test_data'))
|
|
|
|
assertThat(stepMap.customDataMapTags, hasKey('test_data'))
|
|
|
|
}
|
|
|
|
|
2018-01-24 10:55:38 +02:00
|
|
|
}
|