From f7697604e89d177e108739e222fbd4c1978c4765 Mon Sep 17 00:00:00 2001 From: David Venable Date: Wed, 12 Jan 2022 09:56:14 -0600 Subject: [PATCH] Data Prepper Unit Test & Test Refactoring (#1451) * Added a unit test for the Data Prepper build. Signed-off-by: David Venable --- tests/jenkins/BaseJobPipelineTest.groovy | 25 +++++++++++++ tests/jenkins/BuildPipelineTest.groovy | 14 +++---- tests/jenkins/CommonPipelineTest.groovy | 31 ++++++++++++++++ ...estDataPrepperDistributionArtifacts.groovy | 37 +++++++++++++++++++ .../distribution-artifacts.jenkinsfile.txt | 19 ++++++++++ 5 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 tests/jenkins/BaseJobPipelineTest.groovy create mode 100644 tests/jenkins/CommonPipelineTest.groovy create mode 100644 tests/jenkins/TestDataPrepperDistributionArtifacts.groovy create mode 100644 tests/jenkins/jobs/data-prepper/distribution-artifacts.jenkinsfile.txt diff --git a/tests/jenkins/BaseJobPipelineTest.groovy b/tests/jenkins/BaseJobPipelineTest.groovy new file mode 100644 index 0000000000..9c17b5d2b2 --- /dev/null +++ b/tests/jenkins/BaseJobPipelineTest.groovy @@ -0,0 +1,25 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package jenkins.tests + +import jenkins.tests.CommonPipelineTest +import org.junit.Before + +/** + * Base class for testing jobs which do not need the implementation + * details of the Jenkins library exposed. + */ +abstract class BaseJobPipelineTest extends CommonPipelineTest { + @Override + @Before + void setUp() { + super.setUp() + + binding.setVariable('scm', {}) + helper.registerAllowedMethod('legacySCM', [Closure.class], null) + helper.registerAllowedMethod("library", [Map.class], {}) + } +} diff --git a/tests/jenkins/BuildPipelineTest.groovy b/tests/jenkins/BuildPipelineTest.groovy index 0f1b9f1b25..78f6dbe0cb 100644 --- a/tests/jenkins/BuildPipelineTest.groovy +++ b/tests/jenkins/BuildPipelineTest.groovy @@ -17,7 +17,12 @@ import com.lesfurets.jenkins.unit.declarative.* import static org.junit.Assert.* import org.yaml.snakeyaml.Yaml -abstract class BuildPipelineTest extends DeclarativePipelineTest { +/** + * Base test class for testing Jenkins library code. This base test + * sets up the project directory as the library and will execute + * library code. + */ +abstract class BuildPipelineTest extends CommonPipelineTest { @Override @Before void setUp() { @@ -46,11 +51,4 @@ abstract class BuildPipelineTest extends DeclarativePipelineTest { return new LibClassLoader(helper, null) }) } - - void testPipeline(String jenkinsScript, String regressionFilename = null) { - runScript(jenkinsScript) - RegressionTestHelper.testNonRegression(helper, regressionFilename ?: jenkinsScript) - assertJobStatusSuccess() - printCallStack() - } } diff --git a/tests/jenkins/CommonPipelineTest.groovy b/tests/jenkins/CommonPipelineTest.groovy new file mode 100644 index 0000000000..7076f2662b --- /dev/null +++ b/tests/jenkins/CommonPipelineTest.groovy @@ -0,0 +1,31 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package jenkins.tests + +import com.lesfurets.jenkins.unit.RegressionTestHelper +import com.lesfurets.jenkins.unit.declarative.DeclarativePipelineTest +import org.junit.Before + + +/** + * This base test class holds common functions, but does not perform + * any additional setup. Test authors can extend from this class if + * they don't need common setup. Otherwise, please see the sub-classes. + */ +abstract class CommonPipelineTest extends DeclarativePipelineTest { + @Override + @Before + void setUp() { + super.setUp() + } + + void testPipeline(String jenkinsScript, String regressionFilename = null) { + runScript(jenkinsScript) + RegressionTestHelper.testNonRegression(helper, regressionFilename ?: jenkinsScript) + assertJobStatusSuccess() + printCallStack() + } +} diff --git a/tests/jenkins/TestDataPrepperDistributionArtifacts.groovy b/tests/jenkins/TestDataPrepperDistributionArtifacts.groovy new file mode 100644 index 0000000000..d37889379c --- /dev/null +++ b/tests/jenkins/TestDataPrepperDistributionArtifacts.groovy @@ -0,0 +1,37 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import jenkins.tests.BaseJobPipelineTest +import org.junit.Before +import org.junit.Test + +class TestDataPrepperDistributionArtifacts extends BaseJobPipelineTest { + + @Before + void setUp() { + super.setUp() + + binding.setVariable('VERSION', '0.22.1') + binding.setVariable('BRANCH', 'refs/tags/0.22.1') + + binding.setVariable('JOB_BASE_NAME', 'data-prepper-distribution-artifacts') + binding.setVariable('BUILD_NUMBER', '7654321') + binding.setVariable('WORKSPACE', '/tmp/workspace') + + binding.setVariable('ARTIFACT_BUCKET_NAME', 'job-s3-bucket-name') + + helper.registerAllowedMethod('checkout', [Map], {}) + helper.registerAllowedMethod('signArtifacts', [Map], {}) + helper.registerAllowedMethod('uploadToS3', [Map], {}) + helper.registerAllowedMethod('printArtifactDownloadUrlsForStaging', [Map], {}) + helper.registerAllowedMethod('postCleanup', [], {}) + } + + @Test + void dataPrepperDistributionArtifacts_builds_consistently() { + testPipeline('jenkins/data-prepper/distribution-artifacts.jenkinsfile', + 'tests/jenkins/jobs/data-prepper/distribution-artifacts.jenkinsfile') + } +} diff --git a/tests/jenkins/jobs/data-prepper/distribution-artifacts.jenkinsfile.txt b/tests/jenkins/jobs/data-prepper/distribution-artifacts.jenkinsfile.txt new file mode 100644 index 0000000000..c63254f7d4 --- /dev/null +++ b/tests/jenkins/jobs/data-prepper/distribution-artifacts.jenkinsfile.txt @@ -0,0 +1,19 @@ + distribution-artifacts.run() + distribution-artifacts.legacySCM(groovy.lang.Closure) + distribution-artifacts.library({identifier=jenkins@20211123, retriever=null}) + distribution-artifacts.pipeline(groovy.lang.Closure) + distribution-artifacts.echo(Executing on agent [docker:[image:opensearchstaging/ci-runner:ci-runner-centos7-v1, reuseNode:false, stages:[:], args:, alwaysPull:true, containerPerStageRoot:false, label:Jenkins-Agent-al2-x64-c54xlarge-Docker-Host]]) + distribution-artifacts.stage(Build Data Prepper, groovy.lang.Closure) + distribution-artifacts.script(groovy.lang.Closure) + distribution-artifacts.checkout({changelog=false, poll=false, scm={$class=GitSCM, branches=[{name=refs/tags/0.22.1}], userRemoteConfigs=[{url=https://github.com/opensearch-project/data-prepper.git}]}}) + distribution-artifacts.sh(./gradlew clean :release:archives:linux:linuxTar -Prelease) + distribution-artifacts.sh({script=find /tmp/workspace/release/archives/linux/build/distributions | sed -n "s|^/tmp/workspace/release/archives/linux/build/distributions/||p", returnStdout=true}) + distribution-artifacts.stage(Sign Artifacts, groovy.lang.Closure) + distribution-artifacts.script(groovy.lang.Closure) + distribution-artifacts.signArtifacts({artifactPath=/tmp/workspace/release/archives/linux/build/distributions, signatureType=.sig, distributionPlatform=linux}) + distribution-artifacts.stage(Upload Artifacts to Staging Distribution Bucket, groovy.lang.Closure) + distribution-artifacts.script(groovy.lang.Closure) + distribution-artifacts.uploadToS3({sourcePath=/tmp/workspace/release/archives/linux/build/distributions, bucket=job-s3-bucket-name, path=data-prepper-distribution-artifacts/0.22.1/7654321/builds/signed}) + distribution-artifacts.printArtifactDownloadUrlsForStaging({artifactFileNames=[bbb, bbb.sig, ccc, ccc.sig], uploadPath=data-prepper-distribution-artifacts/0.22.1/7654321/builds/signed}) + distribution-artifacts.script(groovy.lang.Closure) + distribution-artifacts.postCleanup()