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

Allow config to reverse library precedence #104

Closed
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -17,6 +17,7 @@
package org.boozallen.plugins.jte.binding.injectors

import org.boozallen.plugins.jte.binding.*
import org.boozallen.plugins.jte.config.TemplateGlobalConfig
import org.boozallen.plugins.jte.utils.RunUtils
import org.boozallen.plugins.jte.utils.TemplateScriptEngine
import org.boozallen.plugins.jte.config.TemplateConfigObject
Expand All @@ -38,6 +39,9 @@ import com.cloudbees.groovy.cps.NonCPS
List<GovernanceTier> tiers = GovernanceTier.getHierarchy()
List<LibraryConfiguration> libs = tiers.collect{ it.getLibraries() }.flatten().minus(null)
List<LibraryProvider> providers = libs.collect{ it.getLibraryProvider() }.flatten().minus(null)
if(!TemplateGlobalConfig.get().getAllowLibraryOverride()) {
providers = providers.reverse();
}

ArrayList libConfigErrors = []
config.getConfig().libraries.each{ libName, libConfig ->
Expand All @@ -50,7 +54,7 @@ import com.cloudbees.groovy.cps.NonCPS
}
libConfigErrors = libConfigErrors.flatten().minus(null)

// if library errors were found:
// if library errors were found:
if(libConfigErrors){
TemplateLogger.printError("----------------------------------")
TemplateLogger.printError(" Library Configuration Errors ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import jenkins.model.GlobalConfiguration
public class TemplateGlobalConfig extends GlobalConfiguration {

private GovernanceTier tier
private Boolean allowLibraryOverride = true;

public static TemplateGlobalConfig get() {
return GlobalConfiguration.all().get(TemplateGlobalConfig)
Expand All @@ -44,4 +45,11 @@ public class TemplateGlobalConfig extends GlobalConfiguration {
return tier
}

Boolean getAllowLibraryOverride() {
return allowLibraryOverride
}

void setAllowLibraryOverride(Boolean allowLibraryOverride) {
this.allowLibraryOverride = allowLibraryOverride
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:section title="${%Jenkins Templating Engine}">
<f:entry title="Allow library override">
<f:checkbox field="allowLibraryOverride" checked="${descriptor.libraryOverride}"/>
</f:entry>
<f:entry field="tier" title="">
<f:property/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.boozallen.plugins.jte.binding

import org.boozallen.plugins.jte.binding.injectors.LibraryLoader
import org.boozallen.plugins.jte.config.TemplateGlobalConfig
import org.boozallen.plugins.jte.utils.TemplateScriptEngine
import spock.lang.*
import org.boozallen.plugins.jte.binding.injectors.StepWrapper
Expand Down Expand Up @@ -196,6 +197,49 @@ class LibraryLoaderSpec extends Specification {
0 * s2.loadLibrary(script, "libA", [:])
}

@WithoutJenkins
def "library on higher governance tier gets loaded if library override set to false"(){
setup:
TemplateGlobalConfig.get().setAllowLibraryOverride(false)
MockLibraryProvider s1 = Mock{
hasLibrary("libA") >> true
}
MockLibraryProvider s2 = Mock{
hasLibrary("libA") >> true
}

LibraryConfiguration c1 = Mock{
getLibraryProvider() >> s1
}
LibraryConfiguration c2 = Mock{
getLibraryProvider() >> s2
}

GovernanceTier tier1 = Mock{
getLibraries() >> [ c1 ]
}
GovernanceTier tier2 = GroovyMock(global:true){
getLibraries() >> [ c2 ]
}
GovernanceTier.getHierarchy() >> [ tier1, tier2 ]

// mock libraries to load
TemplateConfigObject config = new TemplateConfigObject(config: [
libraries: [
libA: [:]
]
])

when:
LibraryLoader.doInject(config, script)
then:
0 * s1.loadLibrary(script, "libA", [:])
1 * s2.loadLibrary(script, "libA", [:])

cleanup:
TemplateGlobalConfig.get().setAllowLibraryOverride(true)
}

@WithoutJenkins
def "library loader correctly passes step config"(){
setup:
Expand Down