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/HadolintExecuteTest.groovy
Sven Merk 111080cbfe Add new step for Dockerfile linting (#723)
* Add new step for Dockerfile linting

* Add documentation template file

* Remove newlines

* Remove internal URL

* Rephrase comment

* Ammend stash

* Fix test

* move dockerImage to general

* use explicit curl options

* small changes

* small changes

* skip GIT blame

* First comments

* Also add remark to URL parameter

* Second set of comments

* Fix return code handling

* Switch type to set

* Revert unrelated changes

* Avoid modification of config

* add quality gate defaults

* Update hadolintExecute.groovy

* fix code climate issue
2019-06-04 08:01:43 +02:00

68 lines
3.0 KiB
Groovy

import hudson.AbortException
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsDockerExecuteRule
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsShellCallRule
import util.JenkinsStepRule
import util.Rules
import static org.junit.Assert.assertThat
import static org.hamcrest.Matchers.*
class HadolintExecuteTest extends BasePiperTest {
private ExpectedException thrown = new ExpectedException().none()
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
private JenkinsDockerExecuteRule dockerExecuteRule = new JenkinsDockerExecuteRule(this)
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
private JenkinsReadYamlRule yamlRule = new JenkinsReadYamlRule(this)
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(thrown)
.around(yamlRule)
.around(dockerExecuteRule)
.around(shellRule)
.around(stepRule)
.around(loggingRule)
@Before
void init() {
helper.registerAllowedMethod 'stash', [String, String], { name, includes -> assertThat(name, is('hadolintConfiguration')); assertThat(includes, is('.hadolint.yaml')) }
helper.registerAllowedMethod 'fileExists', [String], { s -> s == './Dockerfile' }
helper.registerAllowedMethod 'checkStyle', [Map], { m -> assertThat(m.pattern, is('hadolint.xml')); return 'checkstyle' }
helper.registerAllowedMethod 'recordIssues', [Map], { m -> assertThat(m.tools, hasItem('checkstyle')) }
helper.registerAllowedMethod 'archiveArtifacts', [String], { String p -> assertThat('hadolint.xml', is(p)) }
}
@Test
void testHadolintExecute() {
stepRule.step.hadolintExecute(script: nullScript, juStabUtils: utils, dockerImage: 'hadolint/hadolint:latest-debian', configurationUrl: 'https://github.wdf.sap.corp/raw/SGS/Hadolint-Dockerfile/master/.hadolint.yaml')
assertThat(dockerExecuteRule.dockerParams.dockerImage, is('hadolint/hadolint:latest-debian'))
assertThat(loggingRule.log, containsString("Unstash content: buildDescriptor"))
assertThat(shellRule.shell,
hasItems(
"curl --fail --location --output .hadolint.yaml https://github.wdf.sap.corp/raw/SGS/Hadolint-Dockerfile/master/.hadolint.yaml",
"hadolint ./Dockerfile --config .hadolint.yaml --format checkstyle > hadolint.xml"
)
)
}
@Test
void testNoDockerfile() {
helper.registerAllowedMethod 'fileExists', [String], { false }
thrown.expect AbortException
thrown.expectMessage '[hadolintExecute] Dockerfile \'./Dockerfile\' is not found.'
stepRule.step.hadolintExecute(script: nullScript, juStabUtils: utils, dockerImage: 'hadolint/hadolint:latest-debian')
}
}