1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/test/groovy/SnykExecuteTest.groovy
Marcus Holl 8a019f5b86 Remove read yaml rule from common rules
read yaml rule is a very frequently used rule. But having the rule in the common rules
means we cannot register text or files to that rule, which makes it less handy to work
with yaml files in the tests.
2018-08-31 10:22:46 +02:00

134 lines
4.6 KiB
Groovy

import static org.hamcrest.Matchers.hasEntry
import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.is
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.rules.ExpectedException
import static org.junit.Assert.assertThat
import util.BasePiperTest
import util.JenkinsDockerExecuteRule
import util.JenkinsReadYamlRule
import util.JenkinsShellCallRule
import util.JenkinsStepRule
import util.JenkinsLoggingRule
import util.Rules
class SnykExecuteTest extends BasePiperTest {
private ExpectedException thrown = ExpectedException.none()
private JenkinsDockerExecuteRule jder = new JenkinsDockerExecuteRule(this)
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
private JenkinsStepRule jsr = new JenkinsStepRule(this)
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(thrown)
.around(jder)
.around(jscr)
.around(jlr)
.around(jsr)
def withCredentialsParameters
List archiveStepPatterns
@Before
void init() {
archiveStepPatterns = []
nullScript.commonPipelineEnvironment.configuration = [
steps: [
snykExecute: [
snykCredentialsId: 'myPassword'
]
]
]
helper.registerAllowedMethod('string', [Map], { m -> withCredentialsParameters = m
return m })
helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
binding.setProperty('token', 'test_snyk')
try {
c()
} finally {
binding.setProperty('token', null)
}
})
helper.registerAllowedMethod("findFiles", [Map.class], { map ->
if (map.glob == "**${File.separator}pom.xml")
return [new File("some-service${File.separator}pom.xml"), new File("some-other-service${File.separator}pom.xml")].toArray()
if (map.glob == "**${File.separator}package.json")
return [new File("some-ui${File.separator}package.json"), new File("some-service-broker${File.separator}package.json")].toArray()
return [].toArray()
})
helper.registerAllowedMethod('archiveArtifacts', [String], {
s -> archiveStepPatterns.push(s.toString())
})
}
@Test
void testUnsupportedScanType() throws Exception {
thrown.expect(hudson.AbortException)
thrown.expectMessage('[ERROR][snykExecute] ScanType \'seagul\' not supported!')
jsr.step.snykExecute(
script: nullScript,
juStabUtils: utils,
scanType: 'seagul'
)
}
@Test
void testDefaultsSettings() throws Exception {
jsr.step.snykExecute(
script: nullScript,
juStabUtils: utils
)
assertThat(withCredentialsParameters.credentialsId, is('myPassword'))
assertThat(jder.dockerParams, hasEntry('dockerImage', 'node:8-stretch'))
assertThat(jder.dockerParams.stashContent, hasItem('buildDescriptor'))
assertThat(jder.dockerParams.stashContent, hasItem('opensourceConfiguration'))
}
@Test
void testScanTypeNpm() throws Exception {
jsr.step.snykExecute(
script: nullScript,
juStabUtils: utils
)
// asserts
assertThat(jscr.shell, hasItem('npm install snyk --global --quiet'))
assertThat(jscr.shell, hasItem('cd \'./\' && npm install --quiet'))
assertThat(jscr.shell, hasItem('cd \'./\' && snyk monitor && snyk test'))
}
@Test
void testScanTypeNpmWithOrgAndJsonReport() throws Exception {
jsr.step.snykExecute(
script: nullScript,
juStabUtils: utils,
snykOrg: 'myOrg',
toJson: true
)
// asserts
assertThat(jscr.shell, hasItem("cd './' && snyk monitor --org=myOrg && snyk test --json > snyk.json".toString()))
assertThat(archiveStepPatterns, hasItem('snyk.json'))
}
@Test
void testScanTypeMta() throws Exception {
jsr.step.snykExecute(
script: nullScript,
juStabUtils: utils,
scanType: 'mta'
)
// asserts
assertThat(jscr.shell, hasItem("cd 'some-ui${File.separator}' && snyk monitor && snyk test".toString()))
assertThat(jscr.shell, hasItem("cd 'some-service-broker${File.separator}' && snyk monitor && snyk test".toString()))
}
}