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 library to create test report manifest #270

Merged
merged 4 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 46 additions & 0 deletions tests/jenkins/TestCreateUploadTestReportManifest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.
*/

package jenkins.tests

import org.junit.Test
import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString
import static org.hamcrest.CoreMatchers.hasItems
import static org.hamcrest.MatcherAssert.assertThat


class TestCreateUploadTestReportManifest extends BuildPipelineTest {

@Test
public void TestCreateUploadTestReportManifest() {
this.registerLibTester(new CreateUploadTestReportManifestLibTester(
'tests/data/opensearch-1.3.0-test.yml',
'tests/data/opensearch-1.3.0-build.yml',
'tests/data/opensearch-dashboards-build-1.3.0.yml',
'1234',
'integ-test',
'',
)
)
super.setUp()
super.testPipeline("tests/jenkins/jobs/CreateUploadTestReportManifest_Jenkinsfile")
assertThat(getShellCommands('sh', 'report.sh'), hasItems('./report.sh tests/data/opensearch-1.3.0-test.yml --artifact-paths opensearch=https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/1.3.0/c3ff7a232d25403fa8cc14c97799c323/linux/x64/tar --test-run-id 1234 --test-type integ-test --base-path null/null/1.3.0/c3ff7a232d25403fa8cc14c97799c323/linux/x64/tar --component null'))
}

def getShellCommands(methodName, searchString) {
gaiksaya marked this conversation as resolved.
Show resolved Hide resolved
def shCommands = helper.callStack.findAll { call ->
call.methodName == methodName
}.collect { call ->
callArgsToString(call)
}.findAll { command ->
command.contains(searchString)
}
return shCommands
}
}
26 changes: 26 additions & 0 deletions tests/jenkins/jobs/CreateUploadTestReportManifest_Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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('create-test-run-manifest') {
steps {
script {
createUploadTestReportManifest(
testManifest: "tests/data/opensearch-1.3.0-test.yml",
buildManifest: "tests/data/opensearch-1.3.0-build.yml",
testRunID: "1234",
testType: "integ-test",
)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CreateUploadTestReportManifest_Jenkinsfile.run()
CreateUploadTestReportManifest_Jenkinsfile.pipeline(groovy.lang.Closure)
CreateUploadTestReportManifest_Jenkinsfile.echo(Executing on agent [label:none])
CreateUploadTestReportManifest_Jenkinsfile.stage(create-test-run-manifest, groovy.lang.Closure)
CreateUploadTestReportManifest_Jenkinsfile.script(groovy.lang.Closure)
CreateUploadTestReportManifest_Jenkinsfile.createUploadTestReportManifest({testManifest=tests/data/opensearch-1.3.0-test.yml, buildManifest=tests/data/opensearch-1.3.0-build.yml, testRunID=1234, testType=integ-test})
createUploadTestReportManifest.legacySCM(groovy.lang.Closure)
createUploadTestReportManifest.library({identifier=jenkins@main, retriever=null})
createUploadTestReportManifest.readYaml({file=tests/data/opensearch-1.3.0-test.yml})
TestManifest.asBoolean()
createUploadTestReportManifest.readYaml({file=tests/data/opensearch-1.3.0-build.yml})
BuildManifest.asBoolean()
BuildManifest.getDistribution()
createUploadTestReportManifest.echo(Start Reporting workflow for test type: tar)
BuildManifest.getArtifactRootUrl(distribution-build-opensearch, c3ff7a232d25403fa8cc14c97799c323)
createUploadTestReportManifest.echo(Artifact root URL: https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/1.3.0/c3ff7a232d25403fa8cc14c97799c323/linux/x64/tar)
createUploadTestReportManifest.echo(Paths: opensearch=https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/1.3.0/c3ff7a232d25403fa8cc14c97799c323/linux/x64/tar)
createUploadTestReportManifest.echo(Base Path null/null/1.3.0/c3ff7a232d25403fa8cc14c97799c323/linux/x64/tar)
createUploadTestReportManifest.echo(Component: null)
createUploadTestReportManifest.echo(Run command: ./report.sh tests/data/opensearch-1.3.0-test.yml --artifact-paths opensearch=https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/1.3.0/c3ff7a232d25403fa8cc14c97799c323/linux/x64/tar --test-run-id 1234 --test-type integ-test --base-path null/null/1.3.0/c3ff7a232d25403fa8cc14c97799c323/linux/x64/tar --component null)
createUploadTestReportManifest.sh(./report.sh tests/data/opensearch-1.3.0-test.yml --artifact-paths opensearch=https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/1.3.0/c3ff7a232d25403fa8cc14c97799c323/linux/x64/tar --test-run-id 1234 --test-type integ-test --base-path null/null/1.3.0/c3ff7a232d25403fa8cc14c97799c323/linux/x64/tar --component null)
createUploadTestReportManifest.echo(Build Id: c3ff7a232d25403fa8cc14c97799c323)
BuildManifest.getArtifactRoot(null, c3ff7a232d25403fa8cc14c97799c323)
createUploadTestReportManifest.string({credentialsId=jenkins-artifact-bucket-name, variable=ARTIFACT_BUCKET_NAME})
createUploadTestReportManifest.string({credentialsId=jenkins-aws-account-public, variable=AWS_ACCOUNT_PUBLIC})
createUploadTestReportManifest.withCredentials([ARTIFACT_BUCKET_NAME, AWS_ACCOUNT_PUBLIC], groovy.lang.Closure)
createUploadTestReportManifest.echo(Uploading to s3://null/1.3.0/c3ff7a232d25403fa8cc14c97799c323/linux/x64/tar/test-results/1234/integ-test/test-report.yml)
createUploadTestReportManifest.withAWS({role=opensearch-test, roleAccount=AWS_ACCOUNT_PUBLIC, duration=900, roleSessionName=jenkins-session}, groovy.lang.Closure)
createUploadTestReportManifest.s3Upload({file=/tmp/workspace/test-report.yml, bucket=ARTIFACT_BUCKET_NAME, path=null/1.3.0/c3ff7a232d25403fa8cc14c97799c323/linux/x64/tar/test-results/1234/integ-test/test-report.yml})
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.MatcherAssert.assertThat


class CreateUploadTestReportManifestLibTester extends LibFunctionTester {

private String testType
private String componentName
private String buildManifest
private String testManifest
private String buildManifestDashboards
private String testRunID

public CreateUploadTestReportManifestLibTester(testManifest, buildManifest, buildManifestDashboards, testRunID, testType, componentName){
this.testManifest = testManifest
this.buildManifest = buildManifest
this.buildManifestDashboards = buildManifestDashboards
this.testRunID = testRunID
this.testType = testType
this.componentName = componentName
}

void configure(helper, binding) {
binding.setVariable('env', ['JOB_NAME': 'dummy_integ_test'])
binding.setVariable('ARTIFACT_BUCKET_NAME', 'DUMMY_BUCKET_NAME')
binding.setVariable('AWS_ACCOUNT_PUBLIC', 'DUMMY_AWS_ACCOUNT_PUBLIC')
binding.setVariable('ARTIFACT_BUCKET_NAME', 'DUMMY_ARTIFACT_BUCKET_NAME')
binding.setVariable('PUBLIC_ARTIFACT_URL', 'DUMMY_PUBLIC_ARTIFACT_URL')
binding.setVariable('env', ['BUILD_NUMBER': '487'])
binding.setVariable('STAGE_NAME', 'DUMMY_STAGE_NAME')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of these variables are used in this library. Can you check and remove the unsed ones?
Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you are right! and also explains those null in the base-path. Updated in the latest commit.

helper.registerAllowedMethod("withCredentials", [Map, Closure], { args, closure ->
closure.delegate = delegate
return helper.callClosure(closure)
})
helper.registerAllowedMethod("withAWS", [Map, Closure], { args, closure ->
closure.delegate = delegate
return helper.callClosure(closure)
})
helper.registerAllowedMethod("s3Upload", [Map])
}

void parameterInvariantsAssertions(call) {
assertThat(call.args.testManifest.first(), notNullValue())
assertThat(call.args.buildManifest.first(), notNullValue())
assertThat(call.args.testRunID.first(), notNullValue())
assertThat(call.args.testType.first(), notNullValue())
}

boolean expectedParametersMatcher(call) {
return call.args.testManifest.first().toString().equals(this.testManifest)
&& call.args.buildManifest.first().toString().equals(this.buildManifest)
&& call.args.testRunID.first().toString().equals(this.testRunID)
&& call.args.testType.first().toString().equals(this.testType)
}

String libFunctionName() {
return 'createUploadTestReportManifest'
}
}
98 changes: 98 additions & 0 deletions vars/createUploadTestReportManifest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
gaiksaya marked this conversation as resolved.
Show resolved Hide resolved
* 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.
*/

/** Library to close GitHub issue across opensearch-project repositories.
zelinh marked this conversation as resolved.
Show resolved Hide resolved
@param Map args = [:] args A map of the following parameters
@param args.testManifest <required> - The path of the test manifest used
@param args.buildManifest <required> - The path of the build manifest of OpenSearch artifacts.
@param args.buildManifestDashboards <optional> - The path of the build manifest of OpenSearch Dashboards
@param args.testRunID <required> - Test run id of the test workflow being reported.
@param args.testType <required> - Type of the test workflow being reported.
@param args.componentName <Optional> - Components that workflow runs on.
*/

def call(Map args = [:]) {
lib = library(identifier: 'jenkins@main', retriever: legacySCM(scm))

def testID = args.testRunID;
zelinh marked this conversation as resolved.
Show resolved Hide resolved
def testType = args.testType;

def testManifest = lib.jenkins.TestManifest.new(readYaml(file: args.testManifest))
def buildManifest = lib.jenkins.BuildManifest.new(readYaml(file: args.buildManifest))
def buildManifestDashboards = args.buildManifestDashboards ? lib.jenkins.BuildManifest.new(readYaml(file: args.buildManifestDashboards)) : null;
zelinh marked this conversation as resolved.
Show resolved Hide resolved

echo "Start Reporting workflow for test type: " + buildManifest.getDistribution()

String paths = generatePaths(testManifest, buildManifest, buildManifestDashboards)
echo "Paths: ${paths}"

String basePath = generateBasePaths(buildManifest)
echo "Base Path ${basePath}"

String component = args.componentName
echo "Component: ${component}"
zelinh marked this conversation as resolved.
Show resolved Hide resolved

String reportCommand =
[
'./report.sh',
"${args.testManifest}",
"--artifact-paths ${paths}",
"--test-run-id ${testID}",
"--test-type ${testType}",
"--base-path ${basePath}",
"--component ${component}",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the component is an optional parameter, you can have a null check like here and then change it to
isNullOrEmpty(args.componentName.toString()) ? "" : "--component ${args.componentName}",

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion. Updating here. Thanks!

].join(' ')

echo "Run command: " + reportCommand
sh(reportCommand)

String finalUploadPath = generateUploadPath(testManifest, buildManifest, buildManifestDashboards, testID)
withCredentials([
string(credentialsId: 'jenkins-artifact-bucket-name', variable: 'ARTIFACT_BUCKET_NAME'),
string(credentialsId: 'jenkins-aws-account-public', variable: 'AWS_ACCOUNT_PUBLIC')]) {
echo "Uploading to s3://${finalUploadPath}"

withAWS(role: 'opensearch-test', roleAccount: "${AWS_ACCOUNT_PUBLIC}", duration: 900, roleSessionName: 'jenkins-session') {
s3Upload(file: "$WORKSPACE/test-report.yml", bucket: "${ARTIFACT_BUCKET_NAME}", path: finalUploadPath)
}
}
}

String generatePaths(testManifest, buildManifest, buildManifestDashboards) {
String buildId = buildManifest.build.id
String artifactRootUrl = buildManifest.getArtifactRootUrl('distribution-build-opensearch', buildId)

String artifactRootUrlDashboards
if (buildManifestDashboards != null) {
String buildIdDashboards = buildManifestDashboards.build.id
artifactRootUrlDashboards = buildManifestDashboards.getArtifactRootUrl('distribution-build-opensearch-dashboards', buildIdDashboards)
}

echo "Artifact root URL: ${artifactRootUrl}"

return !artifactRootUrlDashboards ?
"opensearch=${artifactRootUrl}" :
"opensearch=${artifactRootUrl} opensearch-dashboards=${artifactRootUrlDashboards}"

}

String generateBasePaths(buildManifest) {
return ["${env.PUBLIC_ARTIFACT_URL}", "${env.JOB_NAME}", buildManifest.build.version, buildManifest.build.id, buildManifest.build.platform, buildManifest.build.architecture, buildManifest.build.distribution].join("/")
}

String generateUploadPath(testManifest, buildManifest, buildManifestDashboards, testID) {
def product = testManifest.name
def productBuildManifest = (product.equals("OpenSearch")) ? buildManifest : buildManifestDashboards

String buildId = productBuildManifest.build.id
echo "Build Id: ${buildId}"
zelinh marked this conversation as resolved.
Show resolved Hide resolved

def artifactPath = productBuildManifest.getArtifactRoot("${env.JOB_NAME}", buildId)
return [artifactPath, "test-results", testID, "integ-test", "test-report.yml"].join("/")
}