Skip to content

Commit

Permalink
[Backport 8.x] Add argument to switch to non root user for running sm…
Browse files Browse the repository at this point in the history
…oke test script (#573)

Signed-off-by: Zelin Hao <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Zelin Hao <[email protected]>
  • Loading branch information
3 people authored Jan 14, 2025
1 parent aded3ad commit 63c0062
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ jacocoTestReport {
}
}

String version = '8.1.0'
String version = '8.1.1'

task updateVersion {
doLast {
Expand Down
27 changes: 22 additions & 5 deletions tests/jenkins/TestRunSmokeTestScript.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,33 @@ class TestRunSmokeTestScript extends BuildPipelineTest {
@Test
public void TestRunSmokeTestScript() {
this.registerLibTester(new RunSmokeTestScriptLibTester(
'dummy_job',
'tests/data/opensearch-2.18.0-build.yml',
'tests/data/opensearch-2.18.0-test.yml',
'1234'
'dummy_job',
'tests/data/opensearch-2.18.0-build.yml',
'tests/data/opensearch-2.18.0-test.yml',
'1234',
'false'
)
)

binding.setVariable('env', ['BUILD_NUMBER': '9876'])
super.testPipeline("tests/jenkins/jobs/RunSmokeTestScript_Jenkinsfile")
assertThat(getShellCommands('sh', './test.sh'), hasItems('./test.sh smoke-test tests/data/opensearch-2.18.0-test.yml --test-run-id 9876 --paths opensearch=https://ci.opensearch.org/ci/dbc/dummy_job/2.18.0/1234/linux/x64/tar'))
assertThat(getShellCommands('sh', './test.sh'), hasItems(' ./test.sh smoke-test tests/data/opensearch-2.18.0-test.yml --test-run-id 9876 --paths opensearch=https://ci.opensearch.org/ci/dbc/dummy_job/2.18.0/1234/linux/x64/tar '))
}

@Test
public void TestRunSmokeTestScript_Switch_Non_Root() {
this.registerLibTester(new RunSmokeTestScriptLibTester(
'dummy_job',
'tests/data/opensearch-2.18.0-build.yml',
'tests/data/opensearch-2.18.0-test.yml',
'2345',
'true'
)
)

binding.setVariable('env', ['BUILD_NUMBER': '8765'])
super.testPipeline("tests/jenkins/jobs/RunSmokeTestScript_Switch_Non_Root_Jenkinsfile")
assertThat(getShellCommands('sh', './test.sh'), hasItems('su `id -un 1000` -c \" ./test.sh smoke-test tests/data/opensearch-2.18.0-test.yml --test-run-id 8765 --paths opensearch=https://ci.opensearch.org/ci/dbc/dummy_job/2.18.0/2345/linux/x64/tar \"'))
}

def getShellCommands(methodName, searchString) {
Expand Down
3 changes: 2 additions & 1 deletion tests/jenkins/jobs/RunSmokeTestScript_Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pipeline {
jobName: 'dummy_job',
buildManifest: 'tests/data/opensearch-2.18.0-build.yml',
testManifest: 'tests/data/opensearch-2.18.0-test.yml',
buildId: '1234'
buildId: '1234',
switchUserNonRoot: 'false'
)
}
}
Expand Down
5 changes: 3 additions & 2 deletions tests/jenkins/jobs/RunSmokeTestScript_Jenkinsfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
RunSmokeTestScript_Jenkinsfile.echo(Executing on agent [label:none])
RunSmokeTestScript_Jenkinsfile.stage(smoke-test, groovy.lang.Closure)
RunSmokeTestScript_Jenkinsfile.script(groovy.lang.Closure)
RunSmokeTestScript_Jenkinsfile.runSmokeTestScript({jobName=dummy_job, buildManifest=tests/data/opensearch-2.18.0-build.yml, testManifest=tests/data/opensearch-2.18.0-test.yml, buildId=1234})
RunSmokeTestScript_Jenkinsfile.runSmokeTestScript({jobName=dummy_job, buildManifest=tests/data/opensearch-2.18.0-build.yml, testManifest=tests/data/opensearch-2.18.0-test.yml, buildId=1234, switchUserNonRoot=false})
runSmokeTestScript.legacySCM(groovy.lang.Closure)
runSmokeTestScript.library({[email protected], retriever=null})
runSmokeTestScript.readYaml({file=tests/data/opensearch-2.18.0-build.yml})
BuildManifest.asBoolean()
BuildManifest.getArtifactRootUrl(dummy_job, 1234)
runSmokeTestScript.echo(Artifact root URL: https://ci.opensearch.org/ci/dbc/dummy_job/2.18.0/1234/linux/x64/tar)
runSmokeTestScript.echo(Switch User to Non-Root (uid=1000): false)
runSmokeTestScript.echo(Paths: opensearch=https://ci.opensearch.org/ci/dbc/dummy_job/2.18.0/1234/linux/x64/tar)
runSmokeTestScript.sh(./test.sh smoke-test tests/data/opensearch-2.18.0-test.yml --test-run-id 9876 --paths opensearch=https://ci.opensearch.org/ci/dbc/dummy_job/2.18.0/1234/linux/x64/tar)
runSmokeTestScript.sh( ./test.sh smoke-test tests/data/opensearch-2.18.0-test.yml --test-run-id 9876 --paths opensearch=https://ci.opensearch.org/ci/dbc/dummy_job/2.18.0/1234/linux/x64/tar )
27 changes: 27 additions & 0 deletions tests/jenkins/jobs/RunSmokeTestScript_Switch_Non_Root_Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.
*/

pipeline {
agent none
stages {
stage('smoke-test') {
steps {
script {
runSmokeTestScript(
jobName: 'dummy_job',
buildManifest: 'tests/data/opensearch-2.18.0-build.yml',
testManifest: 'tests/data/opensearch-2.18.0-test.yml',
buildId: '2345',
switchUserNonRoot: 'true'
)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
RunSmokeTestScript_Switch_Non_Root_Jenkinsfile.run()
RunSmokeTestScript_Switch_Non_Root_Jenkinsfile.pipeline(groovy.lang.Closure)
RunSmokeTestScript_Switch_Non_Root_Jenkinsfile.echo(Executing on agent [label:none])
RunSmokeTestScript_Switch_Non_Root_Jenkinsfile.stage(smoke-test, groovy.lang.Closure)
RunSmokeTestScript_Switch_Non_Root_Jenkinsfile.script(groovy.lang.Closure)
RunSmokeTestScript_Switch_Non_Root_Jenkinsfile.runSmokeTestScript({jobName=dummy_job, buildManifest=tests/data/opensearch-2.18.0-build.yml, testManifest=tests/data/opensearch-2.18.0-test.yml, buildId=2345, switchUserNonRoot=true})
runSmokeTestScript.legacySCM(groovy.lang.Closure)
runSmokeTestScript.library({[email protected], retriever=null})
runSmokeTestScript.readYaml({file=tests/data/opensearch-2.18.0-build.yml})
BuildManifest.asBoolean()
BuildManifest.getArtifactRootUrl(dummy_job, 2345)
runSmokeTestScript.echo(Artifact root URL: https://ci.opensearch.org/ci/dbc/dummy_job/2.18.0/2345/linux/x64/tar)
runSmokeTestScript.echo(Switch User to Non-Root (uid=1000): true)
runSmokeTestScript.echo(Paths: opensearch=https://ci.opensearch.org/ci/dbc/dummy_job/2.18.0/2345/linux/x64/tar)
runSmokeTestScript.sh(su `id -un 1000` -c " ./test.sh smoke-test tests/data/opensearch-2.18.0-test.yml --test-run-id 8765 --paths opensearch=https://ci.opensearch.org/ci/dbc/dummy_job/2.18.0/2345/linux/x64/tar ")
5 changes: 4 additions & 1 deletion tests/jenkins/lib-testers/RunSmokeTestScriptLibTester.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ class RunSmokeTestScriptLibTester extends LibFunctionTester {
private String buildManifest
private String testManifest
private String buildId
private String switchUserNonRoot

public RunSmokeTestScriptLibTester(jobName, buildManifest, testManifest, buildId){
public RunSmokeTestScriptLibTester(jobName, buildManifest, testManifest, buildId, switchUserNonRoot){
this.jobName = jobName
this.buildManifest = buildManifest
this.testManifest = testManifest
this.buildId = buildId
this.switchUserNonRoot = switchUserNonRoot
}

void configure(helper, binding) {
Expand All @@ -39,6 +41,7 @@ class RunSmokeTestScriptLibTester extends LibFunctionTester {
&& call.args.buildManifest.first().toString().equals(this.buildManifest)
&& call.args.testManifest.first().toString().equals(this.testManifest)
&& call.args.buildId.first().toString().equals(this.buildId)
&& call.args.switchUserNonRoot.first().toString().equals(this.switchUserNonRoot)
}

String libFunctionName() {
Expand Down
23 changes: 18 additions & 5 deletions vars/runSmokeTestScript.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @param args.testManifest <required> Test manifest file location
* @param args.buildManifest <required> Build manifest file location
* @param args.buildId <required> Build ID of the distribution artifacts
* @param args.switchUserNonRoot <Required> Switch to non root user if true.
* @param args.jobName <optional> Job name that triggered the workflow. 'distribution-build-opensearh' by default.
*/
void call(Map args = [:]) {
Expand All @@ -22,14 +23,26 @@ void call(Map args = [:]) {
String artifactRootUrl = buildManifest.getArtifactRootUrl(jobName, args.buildId)
echo "Artifact root URL: ${artifactRootUrl}"

String switchUser = args.switchUserNonRoot ?: 'false'
if (! (switchUser == 'true' || switchUser == 'false')) {
echo "args.switchUserNonRoot is not valid. Exit the program."
System.exit(1)
}
echo "Switch User to Non-Root (uid=1000): ${switchUser}"

String switchCommandStart = switchUser == 'true' ? "su `id -un 1000` -c \"" : ""
String switchCommandEnd = switchUser == 'true' ? '"' : ''

String paths = "opensearch=${artifactRootUrl}"
echo "Paths: ${paths}"

sh([
'./test.sh',
'smoke-test',
"${args.testManifest}",
"--test-run-id ${env.BUILD_NUMBER}",
"--paths ${paths}",
switchCommandStart,
'./test.sh',
'smoke-test',
"${args.testManifest}",
"--test-run-id ${env.BUILD_NUMBER}",
"--paths ${paths}",
switchCommandEnd,
].join(' '))
}

0 comments on commit 63c0062

Please sign in to comment.