2018-01-16 16:42:11 +02:00
|
|
|
package util
|
|
|
|
|
|
|
|
import org.junit.rules.TestRule
|
|
|
|
import org.junit.runner.Description
|
|
|
|
import org.junit.runners.model.Statement
|
|
|
|
import org.yaml.snakeyaml.Yaml
|
|
|
|
|
2018-08-31 09:15:44 +02:00
|
|
|
import com.lesfurets.jenkins.unit.BasePipelineTest
|
|
|
|
|
2018-01-26 14:35:49 +02:00
|
|
|
class JenkinsReadYamlRule implements TestRule {
|
2018-01-16 16:42:11 +02:00
|
|
|
final BasePipelineTest testInstance
|
|
|
|
|
2018-08-31 14:17:39 +02:00
|
|
|
// Empty project configuration file registered by default
|
|
|
|
// since almost every test needs it.
|
|
|
|
def ymls = ['.pipeline/config.yml': {''}]
|
2018-08-31 09:15:44 +02:00
|
|
|
|
|
|
|
JenkinsReadYamlRule(BasePipelineTest testInstance) {
|
2018-01-16 16:42:11 +02:00
|
|
|
this.testInstance = testInstance
|
|
|
|
}
|
|
|
|
|
2018-09-04 09:44:34 +02:00
|
|
|
JenkinsReadYamlRule registerYaml(fileName, yaml) {
|
|
|
|
ymls.put(fileName, yaml)
|
2018-08-31 09:15:44 +02:00
|
|
|
return this
|
|
|
|
}
|
2018-01-16 16:42:11 +02:00
|
|
|
@Override
|
|
|
|
Statement apply(Statement base, Description description) {
|
|
|
|
return statement(base)
|
|
|
|
}
|
|
|
|
|
|
|
|
private Statement statement(final Statement base) {
|
|
|
|
return new Statement() {
|
|
|
|
@Override
|
|
|
|
void evaluate() throws Throwable {
|
2018-01-26 14:35:49 +02:00
|
|
|
testInstance.helper.registerAllowedMethod("readYaml", [Map], { Map m ->
|
2018-09-04 09:44:34 +02:00
|
|
|
def yml
|
2018-01-26 14:35:49 +02:00
|
|
|
if(m.text) {
|
2018-09-04 09:44:34 +02:00
|
|
|
yml = m.text
|
2018-01-26 14:35:49 +02:00
|
|
|
} else if(m.file) {
|
2018-09-04 09:44:34 +02:00
|
|
|
yml = ymls.get(m.file)
|
|
|
|
if(yml == null) throw new NullPointerException("yaml file '${m.file}' not registered.")
|
|
|
|
if(yml instanceof Closure) yml = yml()
|
2018-01-26 14:35:49 +02:00
|
|
|
} else {
|
2018-09-04 09:44:34 +02:00
|
|
|
throw new IllegalArgumentException("Key 'text' and 'file' are both missing in map ${m}.")
|
2018-01-26 14:35:49 +02:00
|
|
|
}
|
Variable Substitution in YAML Files (#852)
* Changes:
- New YamlSubstituteVariables step to substitute variables in YAML files with values from another YAML
- New Tests, that check the different substitution patterns.
- Added test resources, including various manifest and variables files.
- Improved usage of JenkinsLoggingRule
- Improved JenkinsReadYamlRule to properly reflect the mocked library's behaviour.
- Added a new JenkinsWriteYamlRule.
* Changes:
- added a Logger that checks a config.verbose flag before it logs debug messages.
- changed error handling to rethrow Yaml parsing exception in case of wrongly-formatted Yaml files.
- changed JenkinsWriteYamlRule to capture Yaml file details of every invocation of writeYaml. This allows sanity checks at end of tests, even if there were multiple invocations.
- adjusted tests.
* Changes:
- Removed javadoc-code blocks from API documentation since they are not supported.
- Removed skipDeletion boolean.
- Added a new deleteFile script which deletes a file if present.
- Added a new JenkinsDeleteFileRule to mock deleteFile script and optionally skip deletion for tests.
- Adjusted yamlSubstituteVariables script.
- Adjusted tests to include new JenkinsDeleteFileRule.
- Changed code that deletes an already existing output file to produce better logs.
* Changes:
- Turned yamlSubstituteVariables into a script that works purely based on Yaml data (not files).
- Added a new cfManifestSubstituteVariables that uses yamlSubstituteVariables under the hood but works based on files.
- Adjusted tests, and added new ones.
* Adjusted documentation and a few log statements.
* Changed documentation to no longer include javadoc code statements.
* Made mocking of deletion of a file a default. Adjusted tests.
* Changed signature of yamlSubstituteVariables' call method to return void.
* Changes:
- Fixed naming issues in deleteFile.
- Renamed Logger to DebugHelper.
- Fixed some documentation.
* Changed implementation of deleteFile not to use java.io.File - which is evil when using it for file operations.
* PROPERLY Changed implementation of deleteFile not to use java.io.File - which is evil when using it for file operations.
* Changes:
- Added tests for deleteFile script
- Changed JenkinsFileExistsRule to also keep track of which files have been queried for existence.
* Changes:
- Removed java.io.File usage from cfManifestSubstituteVariables and using fileExists instead now.
- Adjusted tests.
* Wrapped file path inside ticks to allow spaces in file path when calling deleteFile.
* Removed null checks of mandatory parameters, and resorted to ConfigurationHelper.withMandatoryProperty
* Fixed a NullPointer due to weird Jenkins / Groovy behaviour.
* Changes:
- Turned yamlSubstituteVariables step into a utils class.
- Added tests
- Adjusted cfManifestSubstituteVariables to use utils class instead of step.
- Adjusted tests
- Adjusted APIs of DebugHelper.
* Re-introduced log statement that shows what variables are being replaced and with what.
* Changing API of YamlUtils to take the script and config as input.
* Test
* Test
* Test
* Test
* Test
* Fixing issue.
* Fixing issue.
* Changes:
- Refactored DebugHelper and YamlUtils to make usage nicer and rely on dependency injection.
- Removed Field for DebugHelper and turned it into local variable.
- Adjusted classes using the above.
- Adjusted tests where necessary.
* Added link to CF standards to YamlUtils also.
* Add docu for step cfManifestSubstituteVariables.md
* Added documentation.
* Added missing script parameter to documentation. Some steps document it, some don't. Right now you need it, so we document it.
* Fixed some layouting and typos
* Beautified exception listing.
* Removed trailing whitespaces to make code climate checks pass.
* Trying to get documentation generated, with all the exceptions to markup one should not use.
* cosmetics.
* cosmetics, part 2
* Code climate changes...
* Inlined deleteFile step.
* Added two more tests to properly check file deletion and output handling.
* Changes:
- adjusted API to take a list of variables files, as does 'cf push --vars-file'
- adjusted API to allow for an optional list of variable key-value-maps as does 'cf push --vars'
- reproduced conflict resolution and overriding behavior of variables files and vars lists
- adjusted tests and documentation
* Added missing paramter to doc comment.
* Re-checked docs for missing paramters or params that have no counterpart in the method signature.
* Adjusted documentation.
* Removed absolute path usage from documentation.
* corrected documentation.
* Changed javadoc comment to plain comment.
* Turned all comments to plain comments.
2019-09-06 10:20:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
return readYaml(yml)
|
2018-01-16 16:42:11 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
base.evaluate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
Variable Substitution in YAML Files (#852)
* Changes:
- New YamlSubstituteVariables step to substitute variables in YAML files with values from another YAML
- New Tests, that check the different substitution patterns.
- Added test resources, including various manifest and variables files.
- Improved usage of JenkinsLoggingRule
- Improved JenkinsReadYamlRule to properly reflect the mocked library's behaviour.
- Added a new JenkinsWriteYamlRule.
* Changes:
- added a Logger that checks a config.verbose flag before it logs debug messages.
- changed error handling to rethrow Yaml parsing exception in case of wrongly-formatted Yaml files.
- changed JenkinsWriteYamlRule to capture Yaml file details of every invocation of writeYaml. This allows sanity checks at end of tests, even if there were multiple invocations.
- adjusted tests.
* Changes:
- Removed javadoc-code blocks from API documentation since they are not supported.
- Removed skipDeletion boolean.
- Added a new deleteFile script which deletes a file if present.
- Added a new JenkinsDeleteFileRule to mock deleteFile script and optionally skip deletion for tests.
- Adjusted yamlSubstituteVariables script.
- Adjusted tests to include new JenkinsDeleteFileRule.
- Changed code that deletes an already existing output file to produce better logs.
* Changes:
- Turned yamlSubstituteVariables into a script that works purely based on Yaml data (not files).
- Added a new cfManifestSubstituteVariables that uses yamlSubstituteVariables under the hood but works based on files.
- Adjusted tests, and added new ones.
* Adjusted documentation and a few log statements.
* Changed documentation to no longer include javadoc code statements.
* Made mocking of deletion of a file a default. Adjusted tests.
* Changed signature of yamlSubstituteVariables' call method to return void.
* Changes:
- Fixed naming issues in deleteFile.
- Renamed Logger to DebugHelper.
- Fixed some documentation.
* Changed implementation of deleteFile not to use java.io.File - which is evil when using it for file operations.
* PROPERLY Changed implementation of deleteFile not to use java.io.File - which is evil when using it for file operations.
* Changes:
- Added tests for deleteFile script
- Changed JenkinsFileExistsRule to also keep track of which files have been queried for existence.
* Changes:
- Removed java.io.File usage from cfManifestSubstituteVariables and using fileExists instead now.
- Adjusted tests.
* Wrapped file path inside ticks to allow spaces in file path when calling deleteFile.
* Removed null checks of mandatory parameters, and resorted to ConfigurationHelper.withMandatoryProperty
* Fixed a NullPointer due to weird Jenkins / Groovy behaviour.
* Changes:
- Turned yamlSubstituteVariables step into a utils class.
- Added tests
- Adjusted cfManifestSubstituteVariables to use utils class instead of step.
- Adjusted tests
- Adjusted APIs of DebugHelper.
* Re-introduced log statement that shows what variables are being replaced and with what.
* Changing API of YamlUtils to take the script and config as input.
* Test
* Test
* Test
* Test
* Test
* Fixing issue.
* Fixing issue.
* Changes:
- Refactored DebugHelper and YamlUtils to make usage nicer and rely on dependency injection.
- Removed Field for DebugHelper and turned it into local variable.
- Adjusted classes using the above.
- Adjusted tests where necessary.
* Added link to CF standards to YamlUtils also.
* Add docu for step cfManifestSubstituteVariables.md
* Added documentation.
* Added missing script parameter to documentation. Some steps document it, some don't. Right now you need it, so we document it.
* Fixed some layouting and typos
* Beautified exception listing.
* Removed trailing whitespaces to make code climate checks pass.
* Trying to get documentation generated, with all the exceptions to markup one should not use.
* cosmetics.
* cosmetics, part 2
* Code climate changes...
* Inlined deleteFile step.
* Added two more tests to properly check file deletion and output handling.
* Changes:
- adjusted API to take a list of variables files, as does 'cf push --vars-file'
- adjusted API to allow for an optional list of variable key-value-maps as does 'cf push --vars'
- reproduced conflict resolution and overriding behavior of variables files and vars lists
- adjusted tests and documentation
* Added missing paramter to doc comment.
* Re-checked docs for missing paramters or params that have no counterpart in the method signature.
* Adjusted documentation.
* Removed absolute path usage from documentation.
* corrected documentation.
* Changed javadoc comment to plain comment.
* Turned all comments to plain comments.
2019-09-06 10:20:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mimicking code of the original library (link below).
|
|
|
|
* <p>
|
|
|
|
* Yaml files may contain several YAML sections, separated by ---.
|
|
|
|
* This loads them all and returns a {@code List} of entries in case multiple sections were found or just
|
|
|
|
* a single {@code Object}, if only one section was read.
|
|
|
|
* @see https://github.com/jenkinsci/pipeline-utility-steps-plugin/blob/master/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/ReadYamlStep.java
|
|
|
|
*/
|
|
|
|
private def readYaml(def yml) {
|
|
|
|
Iterable<Object> yaml = new Yaml().loadAll(yml)
|
|
|
|
|
|
|
|
List<Object> result = new LinkedList<Object>()
|
|
|
|
for (Object data : yaml) {
|
|
|
|
result.add(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If only one YAML document, return it directly
|
|
|
|
if (result.size() == 1) {
|
|
|
|
return result.get(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2018-01-16 16:42:11 +02:00
|
|
|
}
|