Skip to content

Commit

Permalink
Integration test results
Browse files Browse the repository at this point in the history
Signed-off-by: Prudhvi Godithi <[email protected]>
  • Loading branch information
prudhvigodithi committed Oct 17, 2024
1 parent 4e94ed7 commit e6dc462
Show file tree
Hide file tree
Showing 2 changed files with 520 additions and 7 deletions.
313 changes: 309 additions & 4 deletions tests/jenkins/TestPublishIntegTestResults.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,6 @@ class TestPublishIntegTestResults extends BuildPipelineTest {
"without_security_test_stderr": {
"type": "keyword"
}
},
"aliases": {
"opensearch-integration-test-results": {}
}
}
}'
Expand All @@ -170,6 +167,22 @@ class TestPublishIntegTestResults extends BuildPipelineTest {
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."
echo "Updating alias..."
update_alias_response=\$(curl -s -XPOST "METRICS_HOST_URL/_aliases" --aws-sigv4 "aws:amz:us-east-1:es" --user "null:null" -H "x-amz-security-token:null" -H "Content-Type: application/json" -d '{
"actions": [
{
"add": {
"index": "test-index",
"alias": "opensearch-integration-test-results"
}
}
]
}')
if [[ \$update_alias_response == *'"acknowledged":true'* ]]; then
echo "Alias updated successfully."
else
echo "Failed to update alias. Error message: \$update_alias_response"
fi
else
echo "Failed to create index. Error message: $create_index_response"
exit 1
Expand All @@ -185,6 +198,132 @@ class TestPublishIntegTestResults extends BuildPipelineTest {
assert normalizeString(calledCommands[0]) == normalizeString(expectedCommandBlock)
}

@Test
void testIndexTestFailuresData() {
def indexName = 'opensearch-integration-test-failures-test-index'
def testRecordsFile = 'test-failures.json'

def script = loadScript('vars/publishIntegTestResults.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(indexName)) {
return '{"acknowledged":true}'
} else if (command.contains("curl -XPOST") && command.contains(indexName)) {
return '{"took":10, "errors":false}'
} else {
throw new IllegalArgumentException("Unexpected command: $command")
}
}

script.indexTestFailuresData(indexName, testRecordsFile)

def expectedCommandBlock = '''set +e
set +x
echo "INDEX NAME IS opensearch-integration-test-failures-test-index"
INDEX_MAPPING='{
"mappings": {
"properties": {
"component": {
"type": "keyword"
},
"component_repo": {
"type": "keyword"
},
"component_repo_url": {
"type": "keyword"
},
"version": {
"type": "keyword"
},
"integ_test_build_number": {
"type": "integer"
},
"integ_test_build_url": {
"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"
},
"platform": {
"type": "keyword"
},
"architecture": {
"type": "keyword"
},
"distribution": {
"type": "keyword"
},
"component_category": {
"type": "keyword"
},
"test_type": {
"type": "keyword"
},
"test_class": {
"type": "keyword"
},
"test_name": {
"type": "keyword"
}
}
}
}'
curl -I "METRICS_HOST_URL/opensearch-integration-test-failures-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/opensearch-integration-test-failures-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."
echo "Updating alias..."
update_alias_response=$(curl -s -XPOST "METRICS_HOST_URL/_aliases" --aws-sigv4 "aws:amz:us-east-1:es" --user "null:null" -H "x-amz-security-token:null" -H "Content-Type: application/json" -d '{
"actions": [
{
"add": {
"index": "opensearch-integration-test-failures-test-index",
"alias": "opensearch-integration-test-failures"
}
}
]
}')
if [[ $update_alias_response == *'"acknowledged":true'* ]]; then
echo "Alias updated successfully."
else
echo "Failed to update alias. Error message: $update_alias_response"
fi
else
echo "Failed to create index. Error message: $create_index_response"
exit 1
fi
fi
if [ -s test-failures.json ]; then
echo "File Exists, indexing results."
curl -XPOST "METRICS_HOST_URL/opensearch-integration-test-failures-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-failures.json"
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() {
Expand Down Expand Up @@ -235,12 +374,178 @@ class TestPublishIntegTestResults extends BuildPipelineTest {
assert parsedResult == expectedJson
}

@Test
void testGenerateFailedTestJson() {
def script = loadScript('vars/publishIntegTestResults.groovy')

def result = script.generatefailedTestJson(
'component1', 'componentRepo', 'https://componentRepoUrl', '1.0', 123,
'http://example.com/build/123', 456, 'http://example.com/distribution/456',
System.currentTimeMillis(), 'rc1', 1, 'linux', 'x64', 'tar', 'test_category',
'test_type', 'test_class', 'test_name'
)

def parsedResult = new JsonSlurper().parseText(result)
def expectedJson = [
component: 'component1',
component_repo: 'componentRepo',
component_repo_url: 'https://componentRepoUrl',
version: '1.0',
integ_test_build_number: 123,
integ_test_build_url: 'http://example.com/build/123',
distribution_build_number: 456,
distribution_build_url: 'http://example.com/distribution/456',
rc: 'rc1',
rc_number: 1,
platform: 'linux',
architecture: 'x64',
distribution: 'tar',
component_category: 'test_category',
test_type: 'test_type',
test_class: 'test_class',
test_name: 'test_name'
]

// Remove the dynamic field for comparison
parsedResult.remove('build_start_time')
assert parsedResult == expectedJson
}

@Test
void testProcessFailedTestsWithEmptyList_withInputs() {
def failedTests = []
def componentName = "MyComponent"
def componentRepo = "my-repo"
def componentRepoUrl = "https://example.com/my-repo"
def version = "1.0.0"
def integTestBuildNumber = 123
def integTestBuildUrl = "https://example.com/builds/123"
def distributionBuildNumber = 456
def distributionBuildUrl = "https://example.com/builds/456"
def buildStartTime = "2023-10-17T12:00:00Z"
def rc = "RC1"
def rcNumber = 1
def platform = "Linux"
def architecture = "x86_64"
def distribution = "Ubuntu 22.04"
def componentCategory = "Backend"
def securityType = "Web Application"
def testFailuresindexName = "test-failures-index"
def script = loadScript('vars/publishIntegTestResults.groovy')
def result = script.processFailedTests(failedTests, componentName, componentRepo, componentRepoUrl, version, integTestBuildNumber, integTestBuildUrl, distributionBuildNumber, distributionBuildUrl, buildStartTime, rc, rcNumber, platform, architecture, distribution, componentCategory, securityType, testFailuresindexName)
assert result == ""
}

@Test
void testProcessFailedTestsWithEmptyList() {
def script = loadScript('vars/publishIntegTestResults.groovy')
def result = script.processFailedTests([], 'component1', 'componentRepo', 'https://componentRepoUrl', '1.0',
123, 'http://example.com/build/123', 456, 'http://example.com/distribution/456',
System.currentTimeMillis(), 'rc1', 1, 'linux', 'x64', 'tar', 'test_category',
'test_type', 'test_failures_index')
assert result == ""
}

@Test
void testProcessFailedTestsWithTestResultNotAvailable() {
def script = loadScript('vars/publishIntegTestResults.groovy')
def result = script.processFailedTests(['Test Result Not Available'], 'component1', 'componentRepo',
'https://componentRepoUrl', '1.0', 123, 'http://example.com/build/123', 456,
'http://example.com/distribution/456', System.currentTimeMillis(), 'rc1', 1,
'linux', 'x64', 'tar', 'test_category', 'test_type', 'test_failures_index')
def expectedJson = [
[
index: [
_index: 'test_failures_index'
]
],
[
component: 'component1',
component_repo: 'componentRepo',
component_repo_url: 'https://componentRepoUrl',
version: '1.0',
integ_test_build_number: 123,
integ_test_build_url: 'http://example.com/build/123',
distribution_build_number: 456,
distribution_build_url: 'http://example.com/distribution/456',
rc: 'rc1',
rc_number: 1,
platform: 'linux',
architecture: 'x64',
distribution: 'tar',
component_category: 'test_category',
test_type: 'test_type',
test_class: 'Not Available',
test_name: 'Not Available'
]
]
def parsedResult = result.trim().split('\n').collect { new JsonSlurper().parseText(it) }
parsedResult.each { json ->
json.remove('build_start_time')
}
assert parsedResult == expectedJson
}



@Test
void testProcessFailedTestsWithNoFailedTest() {
def script = loadScript('vars/publishIntegTestResults.groovy')
def result = script.processFailedTests(['No Failed Test'], 'component1', 'componentRepo',
'https://componentRepoUrl', '1.0', 123, 'http://example.com/build/123', 456,
'http://example.com/distribution/456', System.currentTimeMillis(), 'rc1', 1,
'linux', 'x64', 'tar', 'test_category', 'test_type', 'test_failures_index')

assert result == ""
}

@Test
void testProcessFailedTestsWithMultipleFailures() {
def script = loadScript('vars/publishIntegTestResults.groovy')
def result = script.processFailedTests(['Test1#Failure1'], 'component1',
'componentRepo', 'https://componentRepoUrl', '1.0', 123, 'http://example.com/build/123', 456,
'http://example.com/distribution/456', System.currentTimeMillis(), 'rc1', 1,
'linux', 'x64', 'tar', 'test_category', 'test_type', 'test_failures_index')
def expectedJson = [
[
index: [
_index: 'test_failures_index'
]
],
[
component: 'component1',
component_repo: 'componentRepo',
component_repo_url: 'https://componentRepoUrl',
version: '1.0',
integ_test_build_number: 123,
integ_test_build_url: 'http://example.com/build/123',
distribution_build_number: 456,
distribution_build_url: 'http://example.com/distribution/456',
rc: 'rc1',
rc_number: 1,
platform: 'linux',
architecture: 'x64',
distribution: 'tar',
component_category: 'test_category',
test_type: 'test_type',
test_class: 'Test1',
test_name: 'Failure1'
]

]
def parsedResult = result.trim().split('\n').collect { new JsonSlurper().parseText(it) }
parsedResult.each { json ->
json.remove('build_start_time')
}
assert parsedResult == expectedJson
}


@Test
void testComponentResultWithSecurityFail() {
def withSecurity = 'fail'
def withoutSecurity = 'pass'
def componentResult = (withSecurity == 'fail' || withoutSecurity == 'fail' || withSecurity == 'Not Available' || withoutSecurity == 'Not Available') ? 'failed' : 'passed'

assert componentResult == 'failed'
}

Expand Down
Loading

0 comments on commit e6dc462

Please sign in to comment.