Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Retriever for testing pipeline steps of a project itself. #64

Merged
merged 5 commits into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.lesfurets.jenkins.unit.global.lib

import com.lesfurets.jenkins.unit.global.lib.SourceRetriever

import groovy.transform.CompileStatic
import groovy.transform.Immutable

/**
* Retrieves the shared lib sources of the current project which are expected to be
* at the default location ("./vars"), "./src", "./resources").
* When working with this retriever the LibraryConfiguration which is provided
* with this SourceRetrievers should be configured with <code>allowOverride=false<code>
* since the ProjectSource retriever does not support loading of alternate versions. As
* already outlined above this source retriever always loads the version as it is
* contained at the default location.
*/

@Immutable
@CompileStatic
class ProjectSource implements SourceRetriever {

String sourceURL

/*
* None of the parameters provided in the signature are used in the use-case of that retriever.
*/
@Override
List<URL> retrieve(String repository, String branch, String targetPath) {
def sourceDir = new File(sourceURL)
if (sourceDir.exists()) {
return [sourceDir.getAbsoluteFile().toURI().toURL()]
}
throw new IllegalStateException("Directory $sourceDir.path does not exists")
}

static ProjectSource projectSource(String sourceDir = '.') {
new ProjectSource(sourceDir)
}

@Override
String toString() {
return "${getClass().getSimpleName()}{" +
"sourceURL='" + sourceURL + '\'' +
'}'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.lesfurets.jenkins

import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static com.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSource
import static org.assertj.core.api.Assertions.assertThat

import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameter
import org.junit.runners.Parameterized.Parameters

import com.lesfurets.jenkins.unit.BasePipelineTest

@RunWith(Parameterized.class)
class TestSharedLibraryWithProjectSourceRetriever extends BasePipelineTest {

// For simplicity we use the common@master here. In this case 'commons@master'
// does not denote the master branch of the commons lib. In this case it is
// just a folder containing the lib. ProjectSourceRetriever is by design
// agnostic to branches and agnostic to library names.
// By default ProjectSourceRetriever works upon '.', which is the project
// root directory. Here for testing the retriever is used in a way so that
// it refers to the folder mentioned below.
String sharedLibs = this.class.getResource('/libs/commons@master').getFile()

@Parameter(0)
public String script
@Parameter(1)
public boolean allowOverride
@Parameter(2)
public boolean implicit
@Parameter(3)
public boolean expected

@Override
@Before
void setUp() throws Exception {
scriptRoots += 'src/test/jenkins'
super.setUp()
}

@Parameters(name = "Test {0} allowOverride:{1} implicit:{2} expected:{3}")
static Collection<Object[]> data() {
return [['libraryJob', false, false, false],
stchar marked this conversation as resolved.
Show resolved Hide resolved
['libraryJob', false, false, false],
['libraryJob_implicit', false, false, true],
['libraryJob_implicit', false, true, false],

// Utils2, denoted in the job below, cannot be resolved
// since we do not have the lib from the feature branch
// despite this is requested in the pipeline.
// ProjectSourceRetriever does by design
// not take branches into account.
['libraryJob_feature', true, true, true],

].collect { it as Object[] }
}

@Test
void library_annotation() throws Exception {
boolean exception = false
def library = library().name('commons')
.defaultVersion('<notNeeded>')
.allowOverride(allowOverride)
.implicit(implicit)
.targetPath('<notNeeded>')
.retriever(projectSource(sharedLibs))
.build()
helper.registerSharedLibrary(library)
try {
def script = runScript("job/library/${script}.jenkins")
script.execute()
printCallStack()
} catch (e) {
e.printStackTrace()
exception = true
}
assertThat(expected).isEqualTo(exception)

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.lesfurets.jenkins

import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static com.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSource
import static org.assertj.core.api.Assertions.assertThat

import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameter
import org.junit.runners.Parameterized.Parameters

import com.lesfurets.jenkins.unit.cps.BasePipelineTestCPS

@RunWith(Parameterized.class)
class TestSharedLibraryWithProjectSourceRetrieverCPS extends BasePipelineTestCPS {

// For simplicity we use the common@master here. In this case 'commons@master'
// does not denote the master branch of the commons lib. In this case it is
// just a folder containing the lib. ProjectSourceRetriever is by design
// agnostic to branches and agnostic to library names.
// By default ProjectSourceRetriever works upon '.', which is the project
// root directory. Here for testing the retriever is used in a way so that
// it refers to the folder mentioned below.
String sharedLibs = this.class.getResource('/libs/commons@master').getFile()

@Parameter(0)
public String script
@Parameter(1)
public boolean allowOverride
@Parameter(2)
public boolean implicit
@Parameter(3)
public boolean expected

@Override
@Before
void setUp() throws Exception {
scriptRoots += 'src/test/jenkins'
super.setUp()
}

@Parameters(name = "Test {0} allowOverride:{1} implicit:{2} expected:{3}")
static Collection<Object[]> data() {
return [['libraryJob', false, false, false],
stchar marked this conversation as resolved.
Show resolved Hide resolved
['libraryJob', false, false, false],
['libraryJob_implicit', false, false, true],
['libraryJob_implicit', false, true, false],

// Utils2, denoted in the job below, cannot be resolved
// since we do not have the lib from the feature branch
// despite this is requested in the pipeline.
// ProjectSourceRetriever does by design
// not take branches into account.
['libraryJob_feature', true, true, true],

].collect { it as Object[] }
}

@Test
void library_annotation() throws Exception {
boolean exception = false
def library = library().name('commons')
.defaultVersion('<notNeeded>')
.allowOverride(allowOverride)
.implicit(implicit)
.targetPath('<notNeeded>')
.retriever(projectSource(sharedLibs))
.build()
helper.registerSharedLibrary(library)
try {
def script = runScript("job/library/${script}.jenkins")
script.execute()
printCallStack()
} catch (e) {
e.printStackTrace()
exception = true
}
assertThat(expected).isEqualTo(exception)

}
}