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 support for continue-on-error flag for build and add tests cases #269

Merged
merged 1 commit into from
Aug 15, 2023
Merged
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
44 changes: 39 additions & 5 deletions tests/jenkins/TestbuildManifestVar.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}

4 changes: 3 additions & 1 deletion tests/jenkins/jobs/BuildShManifest_Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -28,6 +29,7 @@ pipeline {
componentName: "common-utils",
inputManifest: "tests/data/opensearch-2.0.0.yml",
distribution: "rpm",
lock: true,
snapshot: true
)
}
Expand Down
8 changes: 4 additions & 4 deletions tests/jenkins/jobs/BuildShManifest_Jenkinsfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
72 changes: 72 additions & 0 deletions tests/jenkins/lib-testers/BuildManifestLibTester.groovy
Original file line number Diff line number Diff line change
@@ -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'
}
}
14 changes: 13 additions & 1 deletion vars/buildManifest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 <required> - Relative path to input manifest containing all the components to build.
@param args.distribution <optional> - Type of distribution to build. Defaults to null.
@param args.componentName <optional> - Name of the single component to build. Defaults to null and builds all the components in the manifest.
@param args.platform <optional> - Platform to build. Defaults to null.
@param args.architecture <optional> - Architecture to build. Defaults to null.
@param args.snapshot <optional> - Boolean value. Defaults to null.
@param args.lock <optional> - Generate a stable reference manifest. Defaults to null.
@param args.continueOnError <optional> - Do not fail the distribution build on any plugin component failure. Defaults to null
*/
void call(Map args = [:]) {
sh(([
'./build.sh',
Expand All @@ -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(' '))
}
Loading