2018-09-21 16:55:31 +02:00
|
|
|
import static java.util.stream.Collectors.toList
|
|
|
|
import static org.hamcrest.Matchers.empty
|
|
|
|
import static org.hamcrest.Matchers.equalTo
|
|
|
|
import static org.hamcrest.Matchers.is
|
|
|
|
import static org.junit.Assert.assertThat
|
|
|
|
import static org.junit.Assert.fail
|
2019-05-17 12:11:29 +02:00
|
|
|
import static util.StepHelper.getSteps
|
2018-09-21 16:55:31 +02:00
|
|
|
|
2019-05-22 16:56:50 +02:00
|
|
|
import java.io.File
|
2018-09-21 16:55:31 +02:00
|
|
|
import java.util.stream.Collectors
|
2018-10-30 17:49:53 +02:00
|
|
|
import java.lang.reflect.Field
|
2018-09-21 16:55:31 +02:00
|
|
|
|
|
|
|
import org.codehaus.groovy.runtime.metaclass.MethodSelectionException
|
|
|
|
import org.hamcrest.Matchers
|
|
|
|
import org.junit.Assert
|
|
|
|
import org.junit.Rule
|
2018-10-09 17:09:55 +02:00
|
|
|
import org.junit.Test
|
2018-09-21 16:55:31 +02:00
|
|
|
import org.junit.rules.ExpectedException
|
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
|
|
|
|
import groovy.io.FileType
|
|
|
|
import hudson.AbortException
|
|
|
|
import util.BasePiperTest
|
2019-03-20 12:21:06 +02:00
|
|
|
import util.JenkinsReadYamlRule
|
2018-09-21 16:55:31 +02:00
|
|
|
import util.JenkinsStepRule
|
|
|
|
import util.Rules
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Intended for collecting generic checks applied to all steps.
|
|
|
|
*/
|
|
|
|
public class CommonStepsTest extends BasePiperTest{
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain ruleChain = Rules.getCommonRules(this)
|
2019-03-20 12:21:06 +02:00
|
|
|
.around(new JenkinsReadYamlRule(this))
|
2018-09-21 16:55:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* With that test we ensure the very first action inside a method body of a call method
|
|
|
|
* for a not white listed step is the check for the script handed over properly.
|
|
|
|
* Actually we assert for the exception type (AbortException) and for the exception message.
|
|
|
|
* In case a new step is added this step will fail. It is the duty of the author of the
|
|
|
|
* step to either follow the pattern of checking the script first or to add the step
|
|
|
|
* to the white list.
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void scriptReferenceNotHandedOverTest() {
|
|
|
|
// all steps not adopting the usual pattern of working with the script.
|
|
|
|
def whitelistScriptReference = [
|
2020-06-23 15:56:56 +02:00
|
|
|
'abapEnvironmentPipeline',
|
|
|
|
'buildSetResult',
|
2019-04-15 14:18:08 +02:00
|
|
|
'commonPipelineEnvironment',
|
|
|
|
'handlePipelineStepErrors',
|
|
|
|
'pipelineExecute',
|
2020-03-17 10:19:09 +02:00
|
|
|
'piperExecuteBin',
|
2020-06-23 15:56:56 +02:00
|
|
|
'piperPipeline',
|
2019-04-15 14:18:08 +02:00
|
|
|
'prepareDefaultValues',
|
2020-07-13 15:10:12 +02:00
|
|
|
'runClosures',
|
2020-06-23 15:56:56 +02:00
|
|
|
'setupCommonPipelineEnvironment'
|
2019-04-15 14:18:08 +02:00
|
|
|
]
|
2018-09-21 16:55:31 +02:00
|
|
|
|
|
|
|
List steps = getSteps().stream()
|
|
|
|
.filter {! whitelistScriptReference.contains(it)}
|
|
|
|
.forEach {checkReference(it)}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkReference(step) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
def script = loadScript("${step}.groovy")
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
System.setProperty('com.sap.piper.featureFlag.failOnMissingScript', 'true')
|
|
|
|
|
|
|
|
try {
|
|
|
|
script.call([:])
|
|
|
|
} catch(AbortException | MissingMethodException e) {
|
|
|
|
throw e
|
|
|
|
} catch(Exception e) {
|
|
|
|
fail "Unexpected exception ${e.getClass().getName()} caught from step '${step}': ${e.getMessage()}"
|
|
|
|
}
|
|
|
|
fail("Expected AbortException not raised by step '${step}'")
|
|
|
|
|
|
|
|
} catch(MissingMethodException e) {
|
|
|
|
|
|
|
|
// can be improved: exception handling as some kind of control flow.
|
|
|
|
// we can also check for the methods and call the appropriate one.
|
|
|
|
|
|
|
|
try {
|
|
|
|
script.call([:]) {}
|
|
|
|
} catch(AbortException e1) {
|
|
|
|
throw e1
|
|
|
|
} catch(Exception e1) {
|
|
|
|
fail "Unexpected exception ${e1.getClass().getName()} caught from step '${step}': ${e1.getMessage()}"
|
|
|
|
}
|
|
|
|
fail("Expected AbortException not raised by step '${step}'")
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch(AbortException e) {
|
|
|
|
assertThat("Step ''${step} does not fail with expected error message in case mandatory parameter 'script' is not provided.",
|
|
|
|
e.getMessage() ==~ /.*\[ERROR\]\[.*\] No reference to surrounding script provided with key 'script', e.g. 'script: this'./,
|
|
|
|
is(equalTo(true)))
|
|
|
|
} finally {
|
|
|
|
System.clearProperty('com.sap.piper.featureFlag.failOnMissingScript')
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 17:49:53 +02:00
|
|
|
|
2018-10-25 15:44:51 +02:00
|
|
|
private static fieldRelatedWhitelist = [
|
2020-09-17 11:01:19 +02:00
|
|
|
'abapAddonAssemblyKitCheckCVs', //implementing new golang pattern without fields
|
2020-09-17 15:56:14 +02:00
|
|
|
'abapAddonAssemblyKitCheckPV', //implementing new golang pattern without fields
|
2020-09-18 11:18:51 +02:00
|
|
|
'abapAddonAssemblyKitCreateTargetVector', //implementing new golang pattern without fields
|
2020-09-18 10:24:46 +02:00
|
|
|
'abapAddonAssemblyKitPublishTargetVector', //implementing new golang pattern without fields
|
2020-09-18 14:07:42 +02:00
|
|
|
'abapAddonAssemblyKitRegisterPackages', //implementing new golang pattern without fields
|
2020-09-18 16:40:49 +02:00
|
|
|
'abapAddonAssemblyKitReleasePackages', //implementing new golang pattern without fields
|
2020-09-18 17:27:04 +02:00
|
|
|
'abapAddonAssemblyKitReserveNextPackages', //implementing new golang pattern without fields
|
2020-08-27 07:54:03 +02:00
|
|
|
'abapEnvironmentAssemblePackages', //implementing new golang pattern without fields
|
2020-08-26 09:52:50 +02:00
|
|
|
'abapEnvironmentCheckoutBranch', //implementing new golang pattern without fields
|
|
|
|
'abapEnvironmentCloneGitRepo', //implementing new golang pattern without fields
|
|
|
|
'abapEnvironmentPullGitRepo', //implementing new golang pattern without fields
|
2020-09-24 07:41:06 +02:00
|
|
|
'abapEnvironmentPipeline', // special step (infrastructure)
|
2020-08-26 09:52:50 +02:00
|
|
|
'abapEnvironmentRunATCCheck', //implementing new golang pattern without fields
|
2020-04-03 16:34:40 +02:00
|
|
|
'artifactPrepareVersion',
|
2020-08-26 09:52:50 +02:00
|
|
|
'cloudFoundryCreateService', //implementing new golang pattern without fields
|
|
|
|
'cloudFoundryCreateServiceKey', //implementing new golang pattern without fields
|
2020-09-24 11:30:25 +02:00
|
|
|
'cloudFoundryCreateSpace', //implementing new golang pattern without fields
|
2020-08-26 09:52:50 +02:00
|
|
|
'cloudFoundryDeleteService', //implementing new golang pattern without fields
|
2020-09-24 11:30:25 +02:00
|
|
|
'cloudFoundryDeleteSpace', //implementing new golang pattern without fields
|
2019-04-15 14:18:08 +02:00
|
|
|
'durationMeasure', // only expects parameters via signature
|
|
|
|
'prepareDefaultValues', // special step (infrastructure)
|
|
|
|
'piperPipeline', // special step (infrastructure)
|
|
|
|
'pipelineStashFilesAfterBuild', // intended to be called from pipelineStashFiles
|
|
|
|
'pipelineStashFilesBeforeBuild', // intended to be called from pipelineStashFiles
|
|
|
|
'pipelineStashFiles', // only forwards to before/after step
|
|
|
|
'pipelineExecute', // special step (infrastructure)
|
|
|
|
'commonPipelineEnvironment', // special step (infrastructure)
|
|
|
|
'handlePipelineStepErrors', // special step (infrastructure)
|
|
|
|
'piperStageWrapper', //intended to be called from within stages
|
2019-11-21 17:34:22 +02:00
|
|
|
'buildSetResult',
|
2020-07-13 15:10:12 +02:00
|
|
|
'runClosures',
|
2020-01-28 00:40:53 +02:00
|
|
|
'checkmarxExecuteScan', //implementing new golang pattern without fields
|
2019-12-13 17:05:55 +02:00
|
|
|
'githubPublishRelease', //implementing new golang pattern without fields
|
2020-09-14 12:05:12 +02:00
|
|
|
'githubCheckBranchProtection', //implementing new golang pattern without fields
|
2020-09-14 18:08:24 +02:00
|
|
|
'githubSetCommitStatus', //implementing new golang pattern without fields
|
2020-01-24 15:30:27 +02:00
|
|
|
'kubernetesDeploy', //implementing new golang pattern without fields
|
2020-03-17 10:19:09 +02:00
|
|
|
'piperExecuteBin', //implementing new golang pattern without fields
|
2020-02-06 17:16:34 +02:00
|
|
|
'protecodeExecuteScan', //implementing new golang pattern without fields
|
2019-12-13 17:05:55 +02:00
|
|
|
'xsDeploy', //implementing new golang pattern without fields
|
2020-04-24 18:29:30 +02:00
|
|
|
'npmExecuteScripts', //implementing new golang pattern without fields
|
2020-06-22 10:12:28 +02:00
|
|
|
'npmExecuteLint', //implementing new golang pattern without fields
|
2020-04-30 11:14:14 +02:00
|
|
|
'malwareExecuteScan', //implementing new golang pattern without fields
|
2020-03-23 10:58:54 +02:00
|
|
|
'mavenBuild', //implementing new golang pattern without fields
|
2020-04-24 10:41:49 +02:00
|
|
|
'mavenExecute', //implementing new golang pattern without fields
|
2020-07-29 19:51:27 +02:00
|
|
|
'mavenExecuteIntegration', //implementing new golang pattern without fields
|
2020-03-23 10:58:54 +02:00
|
|
|
'mavenExecuteStaticCodeChecks', //implementing new golang pattern without fields
|
2020-05-19 08:51:18 +02:00
|
|
|
'mtaBuild', //implementing new golang pattern without fields
|
2020-04-24 10:41:49 +02:00
|
|
|
'nexusUpload', //implementing new golang pattern without fields
|
2020-03-31 15:16:18 +02:00
|
|
|
'piperPipelineStageArtifactDeployment', //stage without step flags
|
2020-04-08 12:55:46 +02:00
|
|
|
'sonarExecuteScan', //implementing new golang pattern without fields
|
2020-04-24 15:31:41 +02:00
|
|
|
'gctsCreateRepository', //implementing new golang pattern without fields
|
2020-07-23 20:20:07 +02:00
|
|
|
'gctsRollback', //implementing new golang pattern without fields
|
2020-07-14 10:58:57 +02:00
|
|
|
'gctsExecuteABAPUnitTests', //implementing new golang pattern without fields
|
2020-06-18 07:45:22 +02:00
|
|
|
'gctsCloneRepository', //implementing new golang pattern without fields
|
2020-05-25 19:48:59 +02:00
|
|
|
'fortifyExecuteScan', //implementing new golang pattern without fields
|
2020-05-18 21:39:35 +02:00
|
|
|
'gctsDeploy', //implementing new golang pattern without fields
|
2020-06-15 16:44:49 +02:00
|
|
|
'containerSaveImage', //implementing new golang pattern without fields
|
2020-07-10 08:07:59 +02:00
|
|
|
'detectExecuteScan', //implementing new golang pattern without fields
|
2020-08-26 09:52:50 +02:00
|
|
|
'kanikoExecute' //implementing new golang pattern without fields
|
2019-04-15 14:18:08 +02:00
|
|
|
]
|
2018-10-25 15:44:51 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void generalConfigKeysSetPresentTest() {
|
|
|
|
|
|
|
|
def fieldName = 'GENERAL_CONFIG_KEYS'
|
2018-10-25 16:07:44 +02:00
|
|
|
// the steps added to the fieldRelatedWhitelist do not take the general config at all
|
|
|
|
def stepsWithoutGeneralConfigKeySet = fieldCheck(fieldName, fieldRelatedWhitelist.plus(['gaugeExecuteTests',
|
|
|
|
'pipelineRestartSteps']))
|
2018-10-25 15:44:51 +02:00
|
|
|
|
|
|
|
assertThat("Steps without ${fieldName} field (or that field is not a Set): ${stepsWithoutGeneralConfigKeySet}",
|
|
|
|
stepsWithoutGeneralConfigKeySet, is(empty()))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void stepConfigKeysSetPresentTest() {
|
|
|
|
|
|
|
|
def fieldName = 'STEP_CONFIG_KEYS'
|
|
|
|
def stepsWithoutStepConfigKeySet = fieldCheck(fieldName, fieldRelatedWhitelist.plus('setupCommonPipelineEnvironment'))
|
|
|
|
|
|
|
|
assertThat("Steps without ${fieldName} field (or that field is not a Set): ${stepsWithoutStepConfigKeySet}",
|
|
|
|
stepsWithoutStepConfigKeySet, is(empty()))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void parametersKeysSetPresentTest() {
|
|
|
|
|
|
|
|
def fieldName = 'PARAMETER_KEYS'
|
|
|
|
def stepsWithoutParametersKeySet = fieldCheck(fieldName, fieldRelatedWhitelist.plus('setupCommonPipelineEnvironment'))
|
|
|
|
|
|
|
|
assertThat("Steps without ${fieldName} field (or that field is not a Set): ${stepsWithoutParametersKeySet}",
|
|
|
|
stepsWithoutParametersKeySet, is(empty()))
|
|
|
|
}
|
|
|
|
|
|
|
|
private fieldCheck(fieldName, whitelist) {
|
|
|
|
|
|
|
|
def stepsWithoutGeneralConfigKeySet = []
|
|
|
|
|
|
|
|
for(def step in getSteps()) {
|
|
|
|
if(whitelist.contains(step)) continue
|
|
|
|
|
|
|
|
def fields = loadScript("${step}.groovy").getClass().getDeclaredFields() as Set
|
|
|
|
Field generalConfigKeyField = fields.find{ it.getName() == fieldName}
|
|
|
|
if(! generalConfigKeyField ||
|
|
|
|
! generalConfigKeyField
|
|
|
|
.getType()
|
|
|
|
.isAssignableFrom(Set.class)) {
|
|
|
|
stepsWithoutGeneralConfigKeySet.add(step)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stepsWithoutGeneralConfigKeySet
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:09:55 +02:00
|
|
|
@Test
|
|
|
|
public void stepsWithWrongFieldNameTest() {
|
|
|
|
|
2019-02-05 16:37:59 +02:00
|
|
|
def whitelist = [
|
2020-06-23 15:56:56 +02:00
|
|
|
'abapEnvironmentPipeline',
|
2019-02-05 16:37:59 +02:00
|
|
|
'commonPipelineEnvironment',
|
2019-04-15 14:18:08 +02:00
|
|
|
'piperPipeline',
|
2020-03-17 10:19:09 +02:00
|
|
|
'piperExecuteBin',
|
2020-07-13 15:10:12 +02:00
|
|
|
'buildSetResult',
|
|
|
|
'runClosures'
|
2019-02-05 16:37:59 +02:00
|
|
|
]
|
2018-10-09 17:09:55 +02:00
|
|
|
|
|
|
|
def stepsWithWrongStepName = []
|
|
|
|
|
|
|
|
for(def step in getSteps()) {
|
|
|
|
|
|
|
|
if(whitelist.contains(step)) continue
|
|
|
|
|
|
|
|
def script = loadScript("${step}.groovy")
|
|
|
|
|
|
|
|
def fields = script.getClass().getDeclaredFields() as Set
|
|
|
|
Field stepNameField = fields.find { it.getName() == 'STEP_NAME'}
|
|
|
|
|
|
|
|
if(! stepNameField) {
|
|
|
|
stepsWithWrongStepName.add(step)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-22 16:56:50 +02:00
|
|
|
boolean notAccessible = false
|
2018-10-09 17:09:55 +02:00
|
|
|
def fieldName
|
|
|
|
|
|
|
|
if(!stepNameField.isAccessible()) {
|
|
|
|
stepNameField.setAccessible(true)
|
|
|
|
notAccessible = true
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
fieldName = stepNameField.get(script)
|
|
|
|
} finally {
|
|
|
|
if(notAccessible) stepNameField.setAccessible(false)
|
|
|
|
}
|
|
|
|
if(fieldName != step) {
|
|
|
|
stepsWithWrongStepName.add(step)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertThat("Steps with wrong step name or without STEP_NAME field.: ${stepsWithWrongStepName}",
|
|
|
|
stepsWithWrongStepName, is(empty()))
|
|
|
|
}
|
2018-10-30 17:49:53 +02:00
|
|
|
|
2018-09-25 13:58:38 +02:00
|
|
|
/*
|
|
|
|
* With that test we ensure that all return types of the call methods of all the steps
|
|
|
|
* are void. Return types other than void are not possible when running inside declarative
|
|
|
|
* pipelines. Parameters shared between several steps needs to be shared via the commonPipelineEnvironment.
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void returnTypeForCallMethodsIsVoidTest() {
|
|
|
|
|
|
|
|
def stepsWithCallMethodsOtherThanVoid = []
|
|
|
|
|
|
|
|
def whitelist = [
|
|
|
|
'durationMeasure',
|
2020-01-23 10:31:01 +02:00
|
|
|
'mavenExecute'
|
2018-09-25 13:58:38 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
for(def step in getSteps()) {
|
|
|
|
def methods = loadScript("${step}.groovy").getClass().getDeclaredMethods() as List
|
|
|
|
Collection callMethodsWithReturnTypeOtherThanVoid =
|
|
|
|
methods.stream()
|
|
|
|
.filter { ! whitelist.contains(step) }
|
|
|
|
.filter { it.getName() == 'call' &&
|
|
|
|
it.getReturnType() != Void.TYPE }
|
|
|
|
.collect(toList())
|
|
|
|
if(!callMethodsWithReturnTypeOtherThanVoid.isEmpty()) stepsWithCallMethodsOtherThanVoid << step
|
|
|
|
}
|
|
|
|
|
|
|
|
assertThat("Steps with call methods with return types other than void: ${stepsWithCallMethodsOtherThanVoid}",
|
|
|
|
stepsWithCallMethodsOtherThanVoid, is(empty()))
|
|
|
|
}
|
2018-09-21 16:55:31 +02:00
|
|
|
}
|