1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/test/groovy/SnykExecuteTest.groovy

134 lines
4.7 KiB
Groovy
Raw Normal View History

2018-06-26 15:08:46 +02:00
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
2018-06-26 15:08:46 +02:00
import util.JenkinsShellCallRule
import util.JenkinsStepRule
import util.JenkinsLoggingRule
import util.Rules
class SnykExecuteTest extends BasePiperTest {
private ExpectedException thrown = ExpectedException.none()
private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this)
2019-01-22 10:19:28 +02:00
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
2019-01-22 10:25:42 +02:00
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
2018-06-26 15:08:46 +02:00
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
2018-06-26 15:08:46 +02:00
.around(thrown)
.around(dockerExecuteRule)
2019-01-22 10:19:28 +02:00
.around(shellRule)
.around(loggingRule)
2019-01-22 10:25:42 +02:00
.around(stepRule)
2018-06-26 15:08:46 +02:00
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 ->
2018-06-27 14:56:50 +02:00
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()
2018-06-26 15:08:46 +02:00
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!')
2019-01-22 10:25:42 +02:00
stepRule.step.snykExecute(
2018-06-26 15:08:46 +02:00
script: nullScript,
juStabUtils: utils,
scanType: 'seagul'
)
}
@Test
void testDefaultsSettings() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.snykExecute(
2018-06-26 15:08:46 +02:00
script: nullScript,
juStabUtils: utils
)
assertThat(withCredentialsParameters.credentialsId, is('myPassword'))
assertThat(dockerExecuteRule.dockerParams, hasEntry('dockerImage', 'node:8-stretch'))
assertThat(dockerExecuteRule.dockerParams.stashContent, hasItem('buildDescriptor'))
assertThat(dockerExecuteRule.dockerParams.stashContent, hasItem('opensourceConfiguration'))
2018-06-26 15:08:46 +02:00
}
@Test
void testScanTypeNpm() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.snykExecute(
2018-06-26 15:08:46 +02:00
script: nullScript,
juStabUtils: utils
)
// asserts
2019-01-22 10:19:28 +02:00
assertThat(shellRule.shell, hasItem('npm install snyk --global --quiet'))
assertThat(shellRule.shell, hasItem('cd \'./\' && npm install --quiet'))
assertThat(shellRule.shell, hasItem('cd \'./\' && snyk monitor && snyk test'))
2018-06-26 15:08:46 +02:00
}
@Test
void testScanTypeNpmWithOrgAndJsonReport() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.snykExecute(
2018-06-26 15:08:46 +02:00
script: nullScript,
juStabUtils: utils,
snykOrg: 'myOrg',
toJson: true
)
// asserts
2019-01-22 10:19:28 +02:00
assertThat(shellRule.shell, hasItem("cd './' && snyk monitor --org=myOrg && snyk test --json > snyk.json".toString()))
2018-06-26 15:08:46 +02:00
assertThat(archiveStepPatterns, hasItem('snyk.json'))
}
@Test
void testScanTypeMta() throws Exception {
2019-01-22 10:25:42 +02:00
stepRule.step.snykExecute(
2018-06-26 15:08:46 +02:00
script: nullScript,
juStabUtils: utils,
scanType: 'mta'
)
// asserts
2019-01-22 10:19:28 +02:00
assertThat(shellRule.shell, hasItem("cd 'some-ui${File.separator}' && snyk monitor && snyk test".toString()))
assertThat(shellRule.shell, hasItem("cd 'some-service-broker${File.separator}' && snyk monitor && snyk test".toString()))
2018-06-26 15:08:46 +02:00
}
}