-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Prudhvi Godithi <[email protected]>
- Loading branch information
1 parent
9338046
commit 955cafa
Showing
6 changed files
with
819 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
tests/jenkins/TestPublishDistributionBuildResults.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
/* | ||
* 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.Before | ||
import static org.mockito.Mockito.* | ||
import groovy.json.JsonSlurper | ||
import org.junit.Test | ||
import groovy.json.JsonOutput | ||
|
||
class TestPublishDistributionBuildResults extends BuildPipelineTest { | ||
|
||
@Override | ||
@Before | ||
void setUp() { | ||
super.setUp() | ||
binding.setVariable('currentBuild', [ | ||
number: 123, | ||
startTimeInMillis: System.currentTimeMillis(), | ||
]) | ||
binding.setVariable('env', [ | ||
RUN_DISPLAY_URL: 'http://example.com/build/123' | ||
]) | ||
binding.setVariable('sh', { cmd -> println cmd }) | ||
binding.setVariable('readFile', { filePath -> 'components:\n- name: component1\n repository: repo1\n ref: ref1' }) | ||
binding.setVariable('writeFile', { params -> println params.text }) | ||
binding.setVariable('withCredentials', { creds, closure -> closure() }) | ||
helper.registerAllowedMethod("withAWS", [Map, Closure], { args, closure -> | ||
closure.delegate = delegate | ||
return helper.callClosure(closure) | ||
}) | ||
binding.setVariable('curl', { params -> println params }) | ||
} | ||
|
||
@Test | ||
void testIndexBuildData() { | ||
def indexName = 'test-index' | ||
def testRecordsFile = 'test-records.ndjson' | ||
|
||
def script = loadScript('vars/publishDistributionBuildResults.groovy') | ||
|
||
def calledCommands = new ArrayList() | ||
script.metaClass.sh = { String command -> | ||
calledCommands << command | ||
if (command.contains("curl -I")) { | ||
return "HTTP/1.1 200 OK" | ||
} else if (command.contains("curl -s -XPUT") && command.contains("test-index")) { | ||
return '{"acknowledged":true}' | ||
} else if (command.contains("curl -XPOST") && command.contains("test-index")) { | ||
return '{"took":10, "errors":false}' | ||
} else { | ||
throw new IllegalArgumentException("Unexpected command: $command") | ||
} | ||
} | ||
|
||
script.indexFailedTestData(indexName, testRecordsFile) | ||
|
||
def expectedCommandBlock = '''set +e | ||
set +x | ||
echo "INDEX NAME IS test-index" | ||
INDEX_MAPPING='{ | ||
"mappings": { | ||
"properties": { | ||
"component": { | ||
"type": "keyword" | ||
}, | ||
"component_repo": { | ||
"type": "keyword" | ||
}, | ||
"component_ref": { | ||
"type": "keyword" | ||
}, | ||
"version": { | ||
"type": "keyword" | ||
}, | ||
"distribution_build_number": { | ||
"type": "integer" | ||
}, | ||
"distribution_build_url": { | ||
"type": "keyword" | ||
}, | ||
"build_start_time": { | ||
"type": "date", | ||
"format": "epoch_millis" | ||
}, | ||
"rc": { | ||
"type": "keyword" | ||
}, | ||
"rc_number": { | ||
"type": "integer" | ||
}, | ||
"component_category": { | ||
"type": "keyword" | ||
}, | ||
"component_build_result": { | ||
"type": "keyword" | ||
} | ||
} | ||
} | ||
}' | ||
curl -I "METRICS_HOST_URL/test-index" --aws-sigv4 "aws:amz:us-east-1:es" --user "null:null" -H "x-amz-security-token:null" | grep -E "HTTP\\/[0-9]+(\\.[0-9]+)? 200" | ||
if [ $? -eq 0 ]; then | ||
echo "Index already exists. Indexing Results" | ||
else | ||
echo "Index does not exist. Creating..." | ||
create_index_response=$(curl -s -XPUT "METRICS_HOST_URL/test-index" --aws-sigv4 "aws:amz:us-east-1:es" --user "null:null" -H "x-amz-security-token:null" -H 'Content-Type: application/json' -d "${INDEX_MAPPING}") | ||
if [[ $create_index_response == *'"acknowledged":true'* ]]; then | ||
echo "Index created successfully." | ||
else | ||
echo "Failed to create index. Error message: $create_index_response" | ||
exit 1 | ||
fi | ||
fi | ||
if [ -s test-records.ndjson ]; then | ||
echo "File Exists, indexing results." | ||
curl -XPOST "METRICS_HOST_URL/test-index/_bulk" --aws-sigv4 "aws:amz:us-east-1:es" --user "null:null" -H "x-amz-security-token:null" -H "Content-Type: application/x-ndjson" --data-binary "@test-records.ndjson" | ||
else | ||
echo "File Does not exist. No tests records to process." | ||
fi''' | ||
assert calledCommands.size() == 1 | ||
assert normalizeString(calledCommands[0]) == normalizeString(expectedCommandBlock) | ||
} | ||
|
||
@Test | ||
void testGenerateJson() { | ||
def script = loadScript('vars/publishDistributionBuildResults.groovy') | ||
def result = script.generateJson( | ||
'component1', 'repo1', 'ref1', '1.0', 123, | ||
'http://example.com/build/123', System.currentTimeMillis(), 'rc1', 1, 'test-category', 'failed' | ||
) | ||
|
||
def parsedResult = new JsonSlurper().parseText(result) | ||
def expectedJson = [ | ||
component: 'component1', | ||
component_repo: 'repo1', | ||
component_ref: 'ref1', | ||
version: '1.0', | ||
distribution_build_number: 123, | ||
distribution_build_url: 'http://example.com/build/123', | ||
// Ignore build_start_time for comparison | ||
rc: 'rc1', | ||
rc_number: 1, | ||
component_category: 'test-category', | ||
component_build_result: 'failed' | ||
] | ||
|
||
// Remove the dynamic field for comparison | ||
parsedResult.remove('build_start_time') | ||
assert parsedResult == expectedJson | ||
} | ||
|
||
def normalizeString(String str) { | ||
return str.replaceAll(/\s+/, " ").trim() | ||
} | ||
|
||
@Test | ||
void testGenerateAndAppendJson() { | ||
def script = loadScript('vars/publishDistributionBuildResults.groovy') | ||
// Test valid parameters | ||
def indexName = "test-index" | ||
def component = "componentA" | ||
def componentRepo = "repoA" | ||
def componentRef = "refA" | ||
def version = "1.0.0" | ||
def distributionBuildNumber = "123" | ||
def distributionBuildUrl = "http://example.com/build/123" | ||
def buildStartTime = "2024-07-19T00:00:00Z" | ||
def rc = true | ||
def rcNumber = "RC1" | ||
def componentCategory = "categoryA" | ||
def status = "success" | ||
|
||
def result = script.generateAndAppendJson(indexName, component, componentRepo, componentRef, version, distributionBuildNumber, distributionBuildUrl, buildStartTime, rc, rcNumber, componentCategory, status) | ||
def expectedJson = JsonOutput.toJson([ | ||
component: component, | ||
component_repo: componentRepo, | ||
component_ref: componentRef, | ||
version: version, | ||
distribution_build_number: distributionBuildNumber, | ||
distribution_build_url: distributionBuildUrl, | ||
build_start_time: buildStartTime, | ||
rc: rc, | ||
rc_number: rcNumber, | ||
component_category: componentCategory, | ||
component_build_result: status | ||
]) | ||
assert result == expectedJson | ||
|
||
result = script.generateAndAppendJson(indexName, null, null, null, null, null, null, null, null, null, null, null) | ||
expectedJson = JsonOutput.toJson([ | ||
component: null, | ||
component_repo: null, | ||
component_ref: null, | ||
version: null, | ||
distribution_build_number: null, | ||
distribution_build_url: null, | ||
build_start_time: null, | ||
rc: null, | ||
rc_number: null, | ||
component_category: null, | ||
component_build_result: null | ||
]) | ||
assert result == expectedJson | ||
} | ||
} |
Oops, something went wrong.