|
| 1 | +# integration test script for aws-lambda-java-log4j2. |
| 2 | +# invokes the deployed lambda function and verifies logs appear in CloudWatch. |
| 3 | + |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +FUNCTION_NAME="${LOG4J2_TEST_FUNCTION:?LOG4J2_TEST_FUNCTION env var is required}" |
| 7 | +REGION="${AWS_REGION:?AWS_REGION env var is required}" |
| 8 | +MARKER="integ-test-$(date +%s)-${RANDOM}" |
| 9 | + |
| 10 | +echo "=== Log4j2 Integration Test ===" |
| 11 | +echo "Function: ${FUNCTION_NAME}" |
| 12 | +echo "Region: ${REGION}" |
| 13 | +echo "Marker: ${MARKER}" |
| 14 | +echo "" |
| 15 | + |
| 16 | +# invoke the lambda function |
| 17 | +echo ">>> Invoking Lambda function..." |
| 18 | +INVOKE_OUTPUT=$(aws lambda invoke \ |
| 19 | + --function-name "${FUNCTION_NAME}" \ |
| 20 | + --region "${REGION}" \ |
| 21 | + --payload "{\"marker\": \"${MARKER}\"}" \ |
| 22 | + --cli-binary-format raw-in-base64-out \ |
| 23 | + --output json \ |
| 24 | + /tmp/integ-test-response.json) || { |
| 25 | + echo "FAIL: aws lambda invoke command failed with exit code $?" |
| 26 | + echo "Output: ${INVOKE_OUTPUT:-<empty>}" |
| 27 | + exit 1 |
| 28 | +} |
| 29 | + |
| 30 | +echo "Invoke output: ${INVOKE_OUTPUT}" |
| 31 | +RESPONSE=$(cat /tmp/integ-test-response.json) |
| 32 | +echo "Response payload: ${RESPONSE}" |
| 33 | + |
| 34 | +# check for lambda execution errors |
| 35 | +FUNCTION_ERROR=$(echo "${INVOKE_OUTPUT}" | jq -r '.FunctionError // empty') |
| 36 | +if [ -n "${FUNCTION_ERROR}" ]; then |
| 37 | + echo "FAIL: Lambda function returned an execution error (FunctionError: ${FUNCTION_ERROR})" |
| 38 | + echo "Error response: ${RESPONSE}" |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +# verify the function executed successfully |
| 43 | +if echo "${RESPONSE}" | grep -q "OK:${MARKER}"; then |
| 44 | + echo ">>> Function invocation successful." |
| 45 | +else |
| 46 | + echo "FAIL: Unexpected response from Lambda function." |
| 47 | + echo "Expected response containing: OK:${MARKER}" |
| 48 | + echo "Got: ${RESPONSE}" |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +# query CloudWatch logs for the marker |
| 53 | +LOG_GROUP="/aws/lambda/${FUNCTION_NAME}" |
| 54 | +echo "" |
| 55 | +echo ">>> Querying CloudWatch Logs group: ${LOG_GROUP}" |
| 56 | + |
| 57 | +MAX_ATTEMPTS=5 |
| 58 | +WAIT_SECONDS=10 |
| 59 | +FOUND=false |
| 60 | + |
| 61 | +for attempt in $(seq 1 $MAX_ATTEMPTS); do |
| 62 | + echo ">>> Attempt ${attempt}/${MAX_ATTEMPTS}: waiting ${WAIT_SECONDS}s for log propagation..." |
| 63 | + sleep "${WAIT_SECONDS}" |
| 64 | + |
| 65 | + LOGS_OUTPUT=$(aws logs filter-log-events \ |
| 66 | + --log-group-name "${LOG_GROUP}" \ |
| 67 | + --region "${REGION}" \ |
| 68 | + --filter-pattern "\"INTEG_TEST_MARKER\" \"${MARKER}\"" \ |
| 69 | + --start-time $(($(date +%s) * 1000 - 120000)) \ |
| 70 | + --output json 2>&1) |
| 71 | + |
| 72 | + if echo "${LOGS_OUTPUT}" | grep -q "INTEG_TEST_MARKER: ${MARKER}"; then |
| 73 | + FOUND=true |
| 74 | + break |
| 75 | + fi |
| 76 | + |
| 77 | + echo " Marker not found yet." |
| 78 | + WAIT_SECONDS=$((WAIT_SECONDS * 2)) |
| 79 | +done |
| 80 | + |
| 81 | +# verify the marker was found |
| 82 | +if [ "${FOUND}" = true ]; then |
| 83 | + echo "" |
| 84 | + echo "=== PASS: Log4j2 integration test succeeded ===" |
| 85 | + echo "The marker '${MARKER}' was found in CloudWatch Logs (attempt ${attempt})." |
| 86 | + echo "This confirms that the LambdaAppender plugin was discovered by Log4j2" |
| 87 | + echo "and logs are being delivered to CloudWatch correctly." |
| 88 | +else |
| 89 | + echo "" |
| 90 | + echo "=== FAIL: Log4j2 integration test failed ===" |
| 91 | + echo "The marker '${MARKER}' was NOT found in CloudWatch Logs after ${MAX_ATTEMPTS} attempts." |
| 92 | + echo "This indicates that the LambdaAppender was not discovered by Log4j2," |
| 93 | + echo "likely due to a missing Log4j2Plugins.dat in the packaged JAR." |
| 94 | + echo "" |
| 95 | + echo "Dumping all recent log events for debugging:" |
| 96 | + aws logs filter-log-events \ |
| 97 | + --log-group-name "${LOG_GROUP}" \ |
| 98 | + --region "${REGION}" \ |
| 99 | + --start-time $(($(date +%s) * 1000 - 120000)) \ |
| 100 | + --limit 50 \ |
| 101 | + --output text 2>&1 || true |
| 102 | + exit 1 |
| 103 | +fi |
0 commit comments