Skip to content

Commit

Permalink
Extracted common test code into a BuildPipelineTest base class. (#979)
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <dblock@amazon.com>
  • Loading branch information
dblock authored Nov 17, 2021
1 parent 62207e8 commit a17f679
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 156 deletions.
56 changes: 56 additions & 0 deletions tests/jenkins/BuildPipelineTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package jenkins.tests

import org.junit.*
import java.util.*
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static com.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSource
import com.lesfurets.jenkins.unit.*
import com.lesfurets.jenkins.unit.declarative.*
import static org.junit.Assert.*
import org.yaml.snakeyaml.Yaml

abstract class BuildPipelineTest extends DeclarativePipelineTest {
@Override
@Before
void setUp() {
super.setUp()

helper.registerSharedLibrary(
library().name('jenkins')
.defaultVersion('<notNeeded>')
.allowOverride(true)
.implicit(true)
.targetPath('<notNeeded>')
.retriever(projectSource())
.build()
)

helper.registerAllowedMethod("readYaml", [Map.class], { args ->
return new Yaml().load((args.file as File).text)
})

binding.setVariable('scm', {})

helper.registerAllowedMethod("legacySCM", [Closure.class], null)

helper.registerAllowedMethod("library", [Map.class], { Map args ->
helper.getLibLoader().loadLibrary(args["identifier"])
return new LibClassLoader(helper, null)
})
}

void testPipeline(String jenkinsScript) {
runScript(jenkinsScript)
RegressionTestHelper.testNonRegression(helper, jenkinsScript)
assertJobStatusSuccess()
printCallStack()
}
}
17 changes: 5 additions & 12 deletions tests/jenkins/TestBuild.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,11 @@
package jenkins.tests

import org.junit.*
import com.lesfurets.jenkins.unit.BasePipelineTest
import com.lesfurets.jenkins.unit.MethodCall
import static org.assertj.core.api.Assertions.assertThat

class TestBuild extends BasePipelineTest {
def jenkinsScript = "tests/jenkins/jobs/build.groovy"

@Override
@Before
void setUp() throws Exception {
super.setUp()
class TestBuild extends BuildPipelineTest {
@Test
@Ignore // TODO: needs fix for https://github.com/jenkinsci/JenkinsPipelineUnit/issues/419 and docker declarations
void testBuild() {
super.testPipeline("tests/jenkins/jobs/Build_Jenkinsfile")
}

// TODO: needs fix for https://github.com/jenkinsci/JenkinsPipelineUnit/issues/419
}
38 changes: 3 additions & 35 deletions tests/jenkins/TestBuildManifest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,10 @@
package jenkins.tests

import org.junit.*
import java.util.*
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static com.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSource
import com.lesfurets.jenkins.unit.*
import com.lesfurets.jenkins.unit.declarative.*
import static org.junit.Assert.*
import org.yaml.snakeyaml.Yaml

class TestBuildManifest extends DeclarativePipelineTest {
def jenkinsScript = "tests/jenkins/jobs/BuildManifest_Jenkinsfile"

@Override
@Before
void setUp() throws Exception {
super.setUp()

helper.registerSharedLibrary(
library().name('jenkins')
.defaultVersion('<notNeeded>')
.allowOverride(true)
.implicit(true)
.targetPath('<notNeeded>')
.retriever(projectSource())
.build()
)

helper.registerAllowedMethod("readYaml", [Map.class], { args ->
return new Yaml().load((args.file as File).text)
})
}

class TestBuildManifest extends BuildPipelineTest {
@Test
void testBuildManifest() throws Exception {
runScript(jenkinsScript)
RegressionTestHelper.testNonRegression(helper, jenkinsScript)
assertJobStatusSuccess()
printCallStack()
void testBuildManifest() {
super.testPipeline("tests/jenkins/jobs/BuildManifest_Jenkinsfile")
}
}
17 changes: 2 additions & 15 deletions tests/jenkins/TestHello.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,10 @@
package jenkins.tests

import org.junit.*
import com.lesfurets.jenkins.unit.*
import static org.assertj.core.api.Assertions.*

class TestHello extends BasePipelineTest {
def jenkinsScript = "tests/jenkins/jobs/Hello_Jenkinsfile"

@Override
@Before
void setUp() throws Exception {
super.setUp()
}

class TestHello extends BuildPipelineTest {
@Test
void testHello() {
runScript(jenkinsScript)
RegressionTestHelper.testNonRegression(helper, jenkinsScript)
assertJobStatusSuccess()
printCallStack()
super.testPipeline("tests/jenkins/jobs/Hello_Jenkinsfile")
}
}
38 changes: 3 additions & 35 deletions tests/jenkins/TestInputManifest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,10 @@
package jenkins.tests

import org.junit.*
import java.util.*
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static com.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSource
import com.lesfurets.jenkins.unit.*
import com.lesfurets.jenkins.unit.declarative.*
import static org.junit.Assert.*
import org.yaml.snakeyaml.Yaml

class TestInputManifest extends DeclarativePipelineTest {
def jenkinsScript = "tests/jenkins/jobs/InputManifest_Jenkinsfile"

@Override
@Before
void setUp() throws Exception {
super.setUp()

helper.registerSharedLibrary(
library().name('jenkins')
.defaultVersion('<notNeeded>')
.allowOverride(true)
.implicit(true)
.targetPath('<notNeeded>')
.retriever(projectSource())
.build()
)

helper.registerAllowedMethod("readYaml", [Map.class], { args ->
return new Yaml().load((args.file as File).text)
})
}

class TestInputManifest extends BuildPipelineTest {
@Test
void testInputManifest() throws Exception {
runScript(jenkinsScript)
RegressionTestHelper.testNonRegression(helper, jenkinsScript)
assertJobStatusSuccess()
printCallStack()
void testInputManifest() {
super.testPipeline("tests/jenkins/jobs/InputManifest_Jenkinsfile")
}
}
30 changes: 5 additions & 25 deletions tests/jenkins/TestMessages.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,18 @@ package jenkins.tests

import org.junit.*
import java.util.*
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static com.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSource
import com.lesfurets.jenkins.unit.*
import com.lesfurets.jenkins.unit.declarative.*
import static org.junit.Assert.*

class TestMessages extends DeclarativePipelineTest {
def jenkinsScript = "tests/jenkins/jobs/Messages_Jenkinsfile"

class TestMessages extends BuildPipelineTest {
@Override
@Before
void setUp() throws Exception {
void setUp() {
super.setUp()

helper.registerAllowedMethod('findFiles', [Map.class], null)

helper.registerSharedLibrary(
library().name('jenkins')
.defaultVersion('<notNeeded>')
.allowOverride(true)
.implicit(true)
.targetPath('<notNeeded>')
.retriever(projectSource())
.build()
)
helper.registerAllowedMethod('findFiles', [Map.class], null)
}

@Test
void testMessages() throws Exception {
runScript(jenkinsScript)
RegressionTestHelper.testNonRegression(helper, jenkinsScript)
assertJobStatusSuccess()
printCallStack()
public void testMessages() {
super.testPipeline("tests/jenkins/jobs/Messages_Jenkinsfile")
}
}
38 changes: 4 additions & 34 deletions tests/jenkins/TestParallelMessages.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,20 @@
package jenkins.tests

import org.junit.*
import static org.junit.Assert.*
import java.util.*
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static com.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSource
import com.lesfurets.jenkins.unit.LibClassLoader
import com.lesfurets.jenkins.unit.declarative.*
import com.lesfurets.jenkins.unit.MethodCall
import static org.assertj.core.api.Assertions.assertThat

class TestParallelMessages extends DeclarativePipelineTest {
def jenkinsScript = "tests/jenkins/jobs/ParallelMessages_Jenkinsfile"

class TestParallelMessages extends BuildPipelineTest {
@Override
@Before
void setUp() throws Exception {
void setUp() {
super.setUp()

helper.registerAllowedMethod('findFiles', [Map.class], null)

helper.registerSharedLibrary(
library().name('jenkins')
.defaultVersion('<notNeeded>')
.allowOverride(true)
.implicit(true)
.targetPath('<notNeeded>')
.retriever(projectSource())
.build()
)
}

@Test
@Ignore // raises MissingMethodException on docker agent declaration, need to upgrade jenkins-pipeline-unit which has new problems
void testParallelMessages() throws Exception {
binding.setVariable('scm', {})
helper.registerAllowedMethod("legacySCM", [Closure.class], null)

helper.registerAllowedMethod("library", [Map.class], { Map args ->
helper.getLibLoader().loadLibrary(args["identifier"])
return new LibClassLoader(helper, null)
})

runScript(jenkinsScript)

assertJobStatusSuccess()
printCallStack()
void testParallelMessages() {
super.testPipeline("tests/jenkins/jobs/ParallelMessages_Jenkinsfile")
}
}

0 comments on commit a17f679

Please sign in to comment.