Files
pw/config/jobs/scripts/qa-api-contract-tests.groovy

130 lines
4.4 KiB
Groovy

pipeline {
agent { label 'maven' }
options {
timestamps()
ansiColor('xterm')
}
stages {
stage('Prepare Metadata') {
steps {
script {
wrap([$class: 'BuildUser']) {
env.RUN_TRIGGER_USER = env.BUILD_USER_ID ?: env.BUILD_USER ?: 'system'
env.RUN_TRIGGER_NAME = env.BUILD_USER ?: env.BUILD_USER_ID ?: 'system'
}
currentBuild.displayName = "#${env.BUILD_NUMBER} contract-api"
currentBuild.description = "by=${env.RUN_TRIGGER_NAME}; scope=users-contract"
}
}
}
stage('Run Contract API Tests') {
steps {
sh '''
set -eux
PROJECT_ROOT_PATH="${OTUS_WORKSPACE_ROOT:-/workspace/projectwork}"
WIREMOCK_DIR="${PROJECT_ROOT_PATH}/config/wiremock"
test -d "${WIREMOCK_DIR}"
test -f "${PROJECT_ROOT_PATH}/contracts-tests/pom.xml"
rm -rf ./artifacts
mkdir -p ./artifacts
WM_NAME="wiremock-contract-${BUILD_NUMBER}"
WM_CID="$(docker run -d --rm --name "${WM_NAME}" -p 18080:8080 wiremock/wiremock:3.9.1)"
WM_IP="$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "${WM_CID}")"
test -n "${WM_IP}"
docker exec "${WM_CID}" mkdir -p /home/wiremock/mappings
tar -C "${WIREMOCK_DIR}" -cf - mappings | docker cp - "${WM_CID}:/home/wiremock"
docker restart "${WM_CID}" >/dev/null
cleanup_all() {
docker rm -f "${WM_CID}" >/dev/null 2>&1 || true
docker rm -f "${CID:-}" >/dev/null 2>&1 || true
}
trap cleanup_all EXIT INT TERM
for _ in $(seq 1 30); do
if curl -fsS "http://${WM_IP}:8080/__admin/health" >/dev/null; then
break
fi
sleep 2
done
curl -fsS "http://${WM_IP}:8080/__admin/health" >/dev/null
CID="$(docker create \
--add-host=host.docker.internal:host-gateway \
localhost:5005/otus/test-api:1.0.0 \
bash -lc "set -e; cd /workspace; mvn -f contracts-tests/pom.xml -Dallure.results.directory=target/allure-results -DbaseUrl=http://${WM_IP}:8080 test")"
tar -C "${PROJECT_ROOT_PATH}" -cf - contracts-tests | docker cp - "${CID}:/workspace"
set +e
docker start -a "${CID}"
TEST_RC=$?
docker cp "${CID}:/workspace/contracts-tests/target" "./artifacts" || true
mkdir -p ./artifacts/target/allure-results
if ! ls ./artifacts/target/allure-results/* >/dev/null 2>&1; then
EXTRA_UUID="$(cat /proc/sys/kernel/random/uuid)"
TS_MS="$(( $(date +%s) * 1000 ))"
STATUS="passed"
if [ "${TEST_RC}" -ne 0 ]; then
STATUS="failed"
fi
cat > "./artifacts/target/allure-results/${EXTRA_UUID}-result.json" <<EOF
{
"uuid": "${EXTRA_UUID}",
"historyId": "api-contract-summary-${BUILD_NUMBER}",
"name": "API contract pipeline summary",
"fullName": "pipeline.ApiContractSummary",
"status": "${STATUS}",
"stage": "finished",
"start": ${TS_MS},
"stop": ${TS_MS},
"labels": [
{"name": "suite", "value": "Pipeline"},
{"name": "package", "value": "pipeline"},
{"name": "testClass", "value": "ApiContractSummary"},
{"name": "testMethod", "value": "run"}
],
"steps": [],
"parameters": []
}
EOF
fi
{
echo "job=${JOB_NAME}"
echo "build=${BUILD_NUMBER}"
echo "scope=users-contract"
echo "wiremock.url=http://${WM_IP}:8080"
} > ./artifacts/target/allure-results/environment.properties
cat > ./artifacts/target/allure-results/executor.json <<EOF
{
"name": "Jenkins",
"type": "jenkins",
"url": "${JENKINS_URL}",
"buildName": "${JOB_NAME} #${BUILD_NUMBER}",
"buildUrl": "${BUILD_URL}",
"reportUrl": "${BUILD_URL}allure",
"buildOrder": ${BUILD_NUMBER}
}
EOF
set -e
exit "${TEST_RC}"
'''
}
}
}
post {
always {
script {
try {
allure commandline: 'allure', includeProperties: false, jdk: '', results: [[path: 'artifacts/target/allure-results']]
} catch (Exception ex) {
echo "Allure publisher unavailable: ${ex.message}"
}
}
junit allowEmptyResults: true, testResults: 'artifacts/target/surefire-reports/*.xml'
archiveArtifacts allowEmptyArchive: true, artifacts: 'artifacts/target/**'
}
}
}