talk2map-test / .github /workflows /api-testing.yml
Noel-Teens's picture
testing new workflow
bfa95ad
Raw History Blame Contribute Delete
9.88 kB
name: API Endpoint Testing
on:
workflow_run:
workflows: ["Staging Branch CI"]
types:
- completed
branches:
- staging
workflow_dispatch:
inputs:
base_url:
description: 'Base URL for API testing'
required: false
default: 'https://neurostack-in-talk2map-test.hf.space'
wait_time:
description: 'Wait time in seconds before testing (for HF deployment)'
required: false
default: '120'
environment:
description: 'Environment to test (test/production)'
required: false
default: 'test'
type: choice
options:
- test
- production
permissions:
contents: read
issues: write
pull-requests: write
jobs:
api-testing:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set environment variables
run: |
if [ "${{ github.event.inputs.environment }}" = "production" ]; then
echo "BASE_URL=https://neurostack-in-talk2map-prod.hf.space" >> $GITHUB_ENV
echo "ENVIRONMENT=production" >> $GITHUB_ENV
echo "WAIT_TIME=60" >> $GITHUB_ENV
else
echo "BASE_URL=${{ github.event.inputs.base_url || 'https://neurostack-in-talk2map-test.hf.space' }}" >> $GITHUB_ENV
echo "ENVIRONMENT=test" >> $GITHUB_ENV
echo "WAIT_TIME=${{ github.event.inputs.wait_time || '120' }}" >> $GITHUB_ENV
fi
echo "Testing environment: $ENVIRONMENT"
echo "Target URL: $BASE_URL"
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Install Newman (Postman CLI)
run: npm install -g newman newman-reporter-htmlextra
- name: Validate Postman Collection
run: |
# Install jq for JSON parsing
sudo apt-get update && sudo apt-get install -y jq
# Make validation script executable and run it
chmod +x .github/scripts/validate-postman.sh
./.github/scripts/validate-postman.sh
- name: Wait for HuggingFace deployment
run: |
echo "Waiting $WAIT_TIME seconds for HuggingFace deployment to be ready..."
sleep $WAIT_TIME
echo "Checking if API is accessible at $BASE_URL..."
# Try to reach the health endpoint with retries
for i in {1..10}; do
if curl -f -s "$BASE_URL/health" > /dev/null; then
echo "βœ… API is accessible at $BASE_URL"
break
else
echo "⏳ Attempt $i: API not ready yet, waiting 30 seconds..."
sleep 30
fi
if [ $i -eq 10 ]; then
echo "❌ API is not accessible after 10 attempts"
exit 1
fi
done
- name: Prepare Postman environment
run: |
# Create Newman environment file with dynamic base URL
cat > newman-environment.json << EOF
{
"id": "talk2map-$ENVIRONMENT-env",
"name": "Talk2Map $ENVIRONMENT Environment",
"values": [
{
"key": "baseUrl",
"value": "$BASE_URL",
"enabled": true
}
]
}
EOF
echo "Created environment file with baseUrl: $BASE_URL"
- name: Run API Tests with Newman
id: run-tests
env:
GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
run: |
echo "πŸš€ Starting API endpoint testing for $ENVIRONMENT environment..."
# Run Newman with HTML reporter and better error handling
newman run Talk2Map.postman_collection.json \
--environment newman-environment.json \
--reporters cli,htmlextra \
--reporter-htmlextra-export api-test-report-$ENVIRONMENT.html \
--reporter-htmlextra-title "Talk2Map API Test Report - $ENVIRONMENT" \
--timeout-request 30000 \
--timeout-script 10000 \
--delay-request 3000 \
--verbose \
--bail \
&& echo "test_status=success" >> $GITHUB_OUTPUT \
|| echo "test_status=failure" >> $GITHUB_OUTPUT
- name: Upload Test Report
uses: actions/upload-artifact@v4
if: always()
with:
name: api-test-report-${{ env.ENVIRONMENT }}
path: api-test-report-${{ env.ENVIRONMENT }}.html
retention-days: 30
- name: Create Test Summary
if: always()
run: |
echo "## πŸ§ͺ API Test Results - $ENVIRONMENT Environment" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Test Environment:** $ENVIRONMENT" >> $GITHUB_STEP_SUMMARY
echo "**Target URL:** $BASE_URL" >> $GITHUB_STEP_SUMMARY
echo "**Test Status:** ${{ steps.run-tests.outputs.test_status }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ -f "api-test-report-$ENVIRONMENT.html" ]; then
echo "βœ… **Test report generated successfully**" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "πŸ“ˆ **Download the detailed HTML report from the Artifacts section above**" >> $GITHUB_STEP_SUMMARY
else
echo "❌ **Test report generation failed**" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Test Endpoints Covered:" >> $GITHUB_STEP_SUMMARY
echo "- πŸ₯ Health Check" >> $GITHUB_STEP_SUMMARY
echo "- 🏨 Chat with Places (Hotels in Chennai)" >> $GITHUB_STEP_SUMMARY
echo "- πŸ’¬ Chat without Places (General Knowledge)" >> $GITHUB_STEP_SUMMARY
echo "- πŸ“ Contextual Lat/Lng Query" >> $GITHUB_STEP_SUMMARY
echo "- πŸ—ΊοΈ Bounding Box Only Query" >> $GITHUB_STEP_SUMMARY
- name: Health Check Verification
run: |
echo "πŸ” Final health check verification..."
response=$(curl -s -w "HTTP_STATUS:%{http_code}" "$BASE_URL/health")
http_status=$(echo $response | grep -o "HTTP_STATUS:[0-9]*" | cut -d: -f2)
body=$(echo $response | sed 's/HTTP_STATUS:[0-9]*$//')
if [ "$http_status" = "200" ]; then
echo "βœ… Health check passed - API is healthy"
echo "Response: $body"
else
echo "❌ Health check failed - HTTP Status: $http_status"
echo "Response: $body"
exit 1
fi
- name: Create test success notification
if: steps.run-tests.outputs.test_status == 'success'
uses: actions/github-script@v7
with:
script: |
const title = `βœ… API Tests Passed - ${{ env.ENVIRONMENT }} Environment`;
const body = `## API Testing Successful
**Environment:** \`${{ env.ENVIRONMENT }}\`
**Target URL:** ${{ env.BASE_URL }}
**Commit:** ${context.sha}
### Test Results:
- βœ… All API endpoints responded correctly
- βœ… Health check passed
- βœ… Chat functionality verified
- βœ… Bounding box queries working
### Test Coverage:
- Health Check endpoint
- Chat with Places (Hotels in Chennai)
- Chat without Places (General Knowledge)
- Contextual Lat/Lng Query
- Bounding Box Only Query
**πŸ“ˆ Detailed test report available in workflow artifacts**`;
github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: title,
body: body,
labels: ['api-test-success', '${{ env.ENVIRONMENT }}']
});
- name: Create test failure alert
if: steps.run-tests.outputs.test_status == 'failure'
uses: actions/github-script@v7
with:
script: |
const title = `❌ API Tests Failed - ${{ env.ENVIRONMENT }} Environment`;
const body = `## API Testing Failed
**Environment:** \`${{ env.ENVIRONMENT }}\`
**Target URL:** ${{ env.BASE_URL }}
**Commit:** ${context.sha}
**Workflow Run:** [View Logs](${context.payload.repository.html_url}/actions/runs/${context.runId})
### Issue:
API endpoint tests failed in the ${{ env.ENVIRONMENT }} environment.
### Possible Causes:
- API endpoints not responding correctly
- Authentication issues (GROQ_API_KEY)
- Network connectivity problems
- Application errors in deployed code
- Test data or configuration issues
### Action Required:
1. Check the [workflow logs](${context.payload.repository.html_url}/actions/runs/${context.runId})
2. Review the test report in artifacts
3. Verify the API is accessible at ${{ env.BASE_URL }}
4. Check for application errors or configuration issues
**⚠️ ${{ env.ENVIRONMENT }} environment may have issues - investigation required!**`;
github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: title,
body: body,
labels: ['api-test-failure', '${{ env.ENVIRONMENT }}', 'urgent']
});