diff --git a/tests/jenkins/TestbuildManifestVar.groovy b/tests/jenkins/TestbuildManifestVar.groovy index ffba8caab..4ebc5f56f 100644 --- a/tests/jenkins/TestbuildManifestVar.groovy +++ b/tests/jenkins/TestbuildManifestVar.groovy @@ -6,16 +6,50 @@ * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ - package jenkins.tests -import org.junit.* +import org.junit.Test +import jenkins.tests.BuildPipelineTest +import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString +import static org.hamcrest.CoreMatchers.hasItems +import static org.hamcrest.MatcherAssert.assertThat class TestbuildManifestVar extends BuildPipelineTest { - + + @Test + void testbuildManifestWithSnapshotContinueOnError() { + this.registerLibTester(new BuildManifestLibTester('tests/data/opensearch-2.0.0.yml', 'tar', true, true)) + super.testPipeline('tests/jenkins/jobs/BuildShManifest_Jenkinsfile') + def shCommands = getCommands('sh', 'build.sh') + assertThat(shCommands, hasItems('./build.sh tests/data/opensearch-2.0.0.yml -d tar --snapshot --continue-on-error')) + } + @Test - void testbuildManifest() { - super.testPipeline("tests/jenkins/jobs/BuildShManifest_Jenkinsfile") + void testbuildManifestWithSnapshotComponent() { + this.registerLibTester(new BuildManifestLibTester('tests/data/opensearch-2.0.0.yml', 'tar', 'job-scheduler', true)) + super.testPipeline('tests/jenkins/jobs/BuildShManifest_Jenkinsfile') + def shCommands = getCommands('sh', 'build.sh') + assertThat(shCommands, hasItems('./build.sh tests/data/opensearch-2.0.0.yml -d tar --component job-scheduler --snapshot')) } + + @Test + void testbuildManifestWithSnapshotComponentLock() { + this.registerLibTester(new BuildManifestLibTester('tests/data/opensearch-2.0.0.yml', 'rpm', 'common-utils', true, true)) + super.testPipeline('tests/jenkins/jobs/BuildShManifest_Jenkinsfile') + def shCommands = getCommands('sh', 'build.sh') + assertThat(shCommands, hasItems('./build.sh tests/data/opensearch-2.0.0.yml -d rpm --component common-utils --snapshot --lock')) + } + + def getCommands(method, text) { + def shCommands = helper.callStack.findAll { call -> + call.methodName == method + }.collect { call -> + callArgsToString(call) + }.findAll { command -> + command.contains(text) + } + return shCommands + } + } diff --git a/tests/jenkins/jobs/BuildShManifest_Jenkinsfile b/tests/jenkins/jobs/BuildShManifest_Jenkinsfile index 7cc30579a..024835465 100644 --- a/tests/jenkins/jobs/BuildShManifest_Jenkinsfile +++ b/tests/jenkins/jobs/BuildShManifest_Jenkinsfile @@ -16,7 +16,8 @@ pipeline { buildManifest( inputManifest: "tests/data/opensearch-2.0.0.yml", distribution: "tar", - snapshot: true + snapshot: true, + continueOnError: true ) buildManifest( componentName: "job-scheduler", @@ -28,6 +29,7 @@ pipeline { componentName: "common-utils", inputManifest: "tests/data/opensearch-2.0.0.yml", distribution: "rpm", + lock: true, snapshot: true ) } diff --git a/tests/jenkins/jobs/BuildShManifest_Jenkinsfile.txt b/tests/jenkins/jobs/BuildShManifest_Jenkinsfile.txt index 732e9aa00..2cb30afcf 100644 --- a/tests/jenkins/jobs/BuildShManifest_Jenkinsfile.txt +++ b/tests/jenkins/jobs/BuildShManifest_Jenkinsfile.txt @@ -3,9 +3,9 @@ BuildShManifest_Jenkinsfile.echo(Executing on agent [label:none]) BuildShManifest_Jenkinsfile.stage(Test build manifest jenkins var, groovy.lang.Closure) BuildShManifest_Jenkinsfile.script(groovy.lang.Closure) - BuildShManifest_Jenkinsfile.buildManifest({inputManifest=tests/data/opensearch-2.0.0.yml, distribution=tar, snapshot=true}) - buildManifest.sh(./build.sh tests/data/opensearch-2.0.0.yml -d tar --snapshot) + BuildShManifest_Jenkinsfile.buildManifest({inputManifest=tests/data/opensearch-2.0.0.yml, distribution=tar, snapshot=true, continueOnError=true}) + buildManifest.sh(./build.sh tests/data/opensearch-2.0.0.yml -d tar --snapshot --continue-on-error) BuildShManifest_Jenkinsfile.buildManifest({componentName=job-scheduler, inputManifest=tests/data/opensearch-2.0.0.yml, distribution=tar, snapshot=true}) buildManifest.sh(./build.sh tests/data/opensearch-2.0.0.yml -d tar --component job-scheduler --snapshot) - BuildShManifest_Jenkinsfile.buildManifest({componentName=common-utils, inputManifest=tests/data/opensearch-2.0.0.yml, distribution=rpm, snapshot=true}) - buildManifest.sh(./build.sh tests/data/opensearch-2.0.0.yml -d rpm --component common-utils --snapshot) + BuildShManifest_Jenkinsfile.buildManifest({componentName=common-utils, inputManifest=tests/data/opensearch-2.0.0.yml, distribution=rpm, lock=true, snapshot=true}) + buildManifest.sh(./build.sh tests/data/opensearch-2.0.0.yml -d rpm --component common-utils --snapshot --lock) diff --git a/tests/jenkins/lib-testers/BuildManifestLibTester.groovy b/tests/jenkins/lib-testers/BuildManifestLibTester.groovy new file mode 100644 index 000000000..7e9903da1 --- /dev/null +++ b/tests/jenkins/lib-testers/BuildManifestLibTester.groovy @@ -0,0 +1,72 @@ +/* + * Copyright OpenSearch Contributors + * 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. + */ +import static org.hamcrest.CoreMatchers.notNullValue +import static org.hamcrest.CoreMatchers.nullValue +import static org.hamcrest.MatcherAssert.assertThat + +class BuildManifestLibTester extends LibFunctionTester { + private String inputManifestPath + private String distribution = 'tar' + private String componentName + private Boolean snapshot = false + private Boolean lock = false + private Boolean continueOnError = false + + public BuildManifestLibTester(String inputManifestPath){ + this.inputManifestPath = inputManifestPath + } + + public BuildManifestLibTester(String inputManifestPath, String distribution, Boolean snapshot){ + this.inputManifestPath = inputManifestPath + this.distribution = distribution + this.snapshot = snapshot + } + + public BuildManifestLibTester(String inputManifestPath, String distribution, String componentName, Boolean snapshot){ + this.inputManifestPath = inputManifestPath + this.distribution = distribution + this.componentName = componentName + this.snapshot = snapshot + } + + public BuildManifestLibTester(String inputManifestPath, String distribution, Boolean snapshot, Boolean continueOnError){ + this.inputManifestPath = inputManifestPath + this.distribution = distribution + this.snapshot = snapshot + this.continueOnError = continueOnError + } + + public BuildManifestLibTester(String inputManifestPath, String distribution, String componentName, Boolean lock, Boolean continueOnError){ + this.inputManifestPath = inputManifestPath + this.distribution = distribution + this.componentName = componentName + this.lock = lock + this.continueOnError = continueOnError + } + + @Override + void configure(helper, binding) { + helper.registerAllowedMethod("checkout", [Map], {}) + } + + @Override + void parameterInvariantsAssertions(call) { + assertThat(call.args.inputManifest.first(), notNullValue()) + } + + @Override + boolean expectedParametersMatcher(call) { + return call.args.inputManifest.first().equals(this.inputManifestPath) + } + + @Override + String libFunctionName() { + return 'buildManifest' + } +} diff --git a/vars/buildManifest.groovy b/vars/buildManifest.groovy index 17f19f7fc..5c13372d4 100644 --- a/vars/buildManifest.groovy +++ b/vars/buildManifest.groovy @@ -6,6 +6,17 @@ * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ + /** Library to call build.sh script (builds OpenSearch and OpenSearch Dashboards distribution) + @param Map args = [:] args A map of the following parameters + @param args.inputManifest - Relative path to input manifest containing all the components to build. + @param args.distribution - Type of distribution to build. Defaults to null. + @param args.componentName - Name of the single component to build. Defaults to null and builds all the components in the manifest. + @param args.platform - Platform to build. Defaults to null. + @param args.architecture - Architecture to build. Defaults to null. + @param args.snapshot - Boolean value. Defaults to null. + @param args.lock - Generate a stable reference manifest. Defaults to null. + @param args.continueOnError - Do not fail the distribution build on any plugin component failure. Defaults to null + */ void call(Map args = [:]) { sh(([ './build.sh', @@ -15,6 +26,7 @@ void call(Map args = [:]) { args.platform ? "-p ${args.platform}" : null, args.architecture ? "-a ${args.architecture}" : null, args.snapshot ? '--snapshot' : null, - args.lock ? '--lock' : null + args.lock ? '--lock' : null, + args.continueOnError ? '--continue-on-error' : null, ] - null).join(' ')) }