Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NR-347440] CFN template validate lambda integration logs #17

Merged
merged 44 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
a32827d
created events for cloudwatch, s3 and validating logs are present in …
ahegdeNR Jan 15, 2025
2248acd
fixes
ahegdeNR Jan 17, 2025
7924022
fixes
ahegdeNR Jan 17, 2025
3df901e
fixes
ahegdeNR Jan 17, 2025
e14079c
fixes
ahegdeNR Jan 17, 2025
fd2eb85
fixes
ahegdeNR Jan 17, 2025
c9d7f6f
fixes
ahegdeNR Jan 17, 2025
b119f50
fixes
ahegdeNR Jan 17, 2025
5fc970f
fixes
ahegdeNR Jan 19, 2025
ce2b778
fixes
ahegdeNR Jan 19, 2025
244d0c1
fixes
ahegdeNR Jan 19, 2025
062ccfe
fixes
ahegdeNR Jan 20, 2025
363b1f2
fixes
ahegdeNR Jan 20, 2025
b9d8401
fixes
ahegdeNR Jan 21, 2025
96d9333
fixes
ahegdeNR Jan 21, 2025
0521e7d
fixes
ahegdeNR Jan 21, 2025
8c7f08f
fixes
ahegdeNR Jan 21, 2025
39d0ee1
fixes
ahegdeNR Jan 21, 2025
108ca4c
Merge branch 'develop' into NR-347440-e2e-event-creation
ahegdeNR Jan 22, 2025
a473916
fixes
ahegdeNR Jan 22, 2025
dfb4583
fixes
ahegdeNR Jan 29, 2025
73b80e6
fixes
ahegdeNR Feb 3, 2025
2a18eae
testing on pull request
ahegdeNR Feb 4, 2025
3511b81
testing on pull request
ahegdeNR Feb 4, 2025
7b99c52
fixes
ahegdeNR Feb 4, 2025
c0b5c07
fixes
ahegdeNR Feb 4, 2025
f070394
fixes
ahegdeNR Feb 4, 2025
7a8244d
fixes
ahegdeNR Feb 4, 2025
cf9ee11
fixes
ahegdeNR Feb 4, 2025
83d8ee0
fixes
ahegdeNR Feb 5, 2025
2820a28
fixes
ahegdeNR Feb 10, 2025
fa17d67
fixes
ahegdeNR Feb 10, 2025
99ab363
parallelised template build and made it run once only for the tests
ahegdeNR Feb 11, 2025
7faddac
fixes
ahegdeNR Feb 11, 2025
b72039f
send slack notification on failure
ahegdeNR Feb 11, 2025
262157c
fixes
ahegdeNR Feb 11, 2025
2150b04
fixes
ahegdeNR Feb 11, 2025
3612720
fixes
ahegdeNR Feb 11, 2025
eefc09c
fixes
ahegdeNR Feb 11, 2025
6ed1c9c
fixes
ahegdeNR Feb 11, 2025
ae7b378
fixes
ahegdeNR Feb 11, 2025
e1ece89
fixes
ahegdeNR Feb 11, 2025
6e4ed72
fixes
ahegdeNR Feb 11, 2025
5f9e08f
added e2e tests to run once every month as cron job
ahegdeNR Feb 11, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 61 additions & 16 deletions .github/workflows/run-e2e-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,55 @@ name: E2E Test Workflow
on:
pull_request:
branches:
- develop
- main
- develop

jobs:
run-e2e-tests:
run-e2e-tests-cloudwatch:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
strategy:
matrix:
test-case: [test_logs_for_filter_pattern, test_logs_for_secret_manager, test_logs_for_invalid_log_group]
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.22'

- name: Install AWS SAM CLI
run: |
pip install aws-sam-cli

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v3
with:
role-to-assume: ${{ secrets.AWS_E2E_ROLE }}
aws-region: us-east-1

- name: Run e2e tests
env:
NEW_RELIC_USER_KEY: ${{ secrets.NEW_RELIC_USER_KEY }}
NEW_RELIC_LICENSE_KEY: ${{ secrets.NEW_RELIC_LICENSE_KEY }}
run: |
cd e2e-tests/common-scripts
./build-templates.sh
cd ..
./lambda-cloudwatch-trigger.sh ${{ matrix.test-case }}

run-e2e-tests-s3:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
strategy:
matrix:
test-case: [test_logs_for_prefix, test_logs_for_secret_manager, test_logs_for_invalid_bucket_name]
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -36,22 +75,28 @@ jobs:
env:
NEW_RELIC_USER_KEY: ${{ secrets.NEW_RELIC_USER_KEY }}
NEW_RELIC_LICENSE_KEY: ${{ secrets.NEW_RELIC_LICENSE_KEY }}
S3_BUCKET: unified-lambda-e2e-test-templates
run: |
cd e2e-tests/
cd e2e-tests/common-scripts
./build-templates.sh

echo "Running s3 and cloudwatch trigger tests parallely"
./lambda-cloudwatch-trigger.sh &
pid1=$!
echo "Testing setting up cloudwatch trigger with PID: $pid1"

./lambda-s3-trigger.sh &
pid2=$!
echo "Testing setting up s3 trigger with PID: $pid2"

wait $pid1
wait $pid2
cd ..
./lambda-s3-trigger.sh ${{ matrix.test-case }}

clean-up:
needs: [run-e2e-tests-cloudwatch, run-e2e-tests-s3]
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
steps:
- name: Install AWS SAM CLI
run: |
pip install aws-sam-cli

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v3
with:
role-to-assume: ${{ secrets.AWS_E2E_ROLE }}
aws-region: us-east-1

- name: Delete Resources
env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ for TEMPLATE_FILE in "${TEMPLATES[@]}"; do
BASE_NAME=$(basename "$TEMPLATE_FILE" .yaml)
BUILD_DIR="$BUILD_DIR_BASE/$BASE_NAME"

sam build -u --template-file "../$TEMPLATE_FILE" --build-dir "$BUILD_DIR"
sam build -u --template-file "../../$TEMPLATE_FILE" --build-dir "$BUILD_DIR"
sam package --s3-bucket "$S3_BUCKET" --template-file "$BUILD_DIR/template.yaml" --output-template-file "$BUILD_DIR/$TEMPLATE_FILE"

done
48 changes: 48 additions & 0 deletions e2e-tests/common-scripts/config-file.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# test resources
NEW_RELIC_REGION=US
NEW_RELIC_ACCOUNT_ID=2813435
S3_BUCKET=aws-unified-lambda-e2e-test-templates

# test case constants
CLOUDWATCH_FILTER_PATTERN_CASE=e2e-cloudwatch-filter-pattern-test
CLOUDWATCH_SECRET_MANAGER_CASE=e2e-cloudwatch-secret-manager-test
CLOUDWATCH_INVALID_LOG_GROUP_CASE=e2e-cloudwatch-invalid-log-group-test
S3_PREFIX_CASE=e2e-s3-prefix-test
S3_SECRET_MANAGER_CASE=e2e-s3-secret-manager-test
S3_INVALID_BUCKET_CASE=e2e-s3-invalid-bucket-test

# build constants
BUILD_DIR_BASE=.aws-sam/build

# template constants
LAMBDA_TEMPLATE=lambda-template.yaml
LAMBDA_TEMPLATE_BUILD_DIR=common-scripts/.aws-sam/build/lambda-template
LAMBDA_LOGICAL_RESOURCE_ID=NewRelicLogsServerlessLogForwarder

# deployment constants for s3 bucket cases
S3_BUCKET_NAME=aws-unified-lambda-e2e-test-bucket
S3_BUCKET_NAME_SECRET_MANAGER=aws-unified-lambda-e2e-test-bucket-secret-manager-case
S3_BUCKET_NAME_INVALID=aws-unified-lambda-e2e-test-bucket-non-existent
S3_BUCKET_PREFIX=LogFolder
S3_BUCKET_PREFIX_INVALID=InvalidFolder
S3_BUCKET_OBJECT_NAME=TestTextFile.txt
S3_BUCKET_OBJECT_NAME_FOR_INVALID_CASE=TestTextFileInvalid.txt

# deployment constants for cloudwatch log group cases
LOG_GROUP_NAME=aws-unified-lambda-e2e-test-log-group
LOG_GROUP_NAME_SECRET_MANAGER=aws-unified-lambda-e2e-test-log-group-secret-manager
LOG_GROUP_NAME_INVALID=aws-unified-lambda-e2e-test-log-group-invalid
LOG_GROUP_FILTER_PATTERN=ERROR
LOG_STREAM_NAME=aws-unified-lambda-e2e-test-stream

# fetch nr logs constants
LOG_MESSAGE_CLOUDWATCH="This is cloudwatch logs"
LOG_MESSAGE_S3="This is s3 logs"
ATTRIBUTE_KEY_CLOUDWATCH=logStream
ATTRIBUTE_KEY_S3=logObjectKey
TIME_RANGE="5 minutes"
MAX_RETRIES=5
SLEEP_TIME=30
MAX_SLEEP_TIME=180
COMMON_ATTRIBUTE_KEY=testKey
COMMON_ATTRIBUTE_VALUE=testValue
100 changes: 100 additions & 0 deletions e2e-tests/common-scripts/logs-scripts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash

source config-file.cfg
source stack-scripts.sh

validate_logs_in_new_relic() {
user_key=$1
account_id=$2
attribute_key=$3
attribute_value=$4
log_message=$5

sleep_time=$SLEEP_TIME
attempt=1

while [[ $attempt -lt $MAX_RETRIES ]]; do
echo "Fetching logs from new relic for $attribute_key: $attribute_value"
sleep "$sleep_time"
response=$(fetch_new_relic_logs_api "$user_key" "$account_id" "$attribute_key" "$attribute_value")

if echo "$response" | grep -q "$log_message"; then
echo "Log event successfully found in New Relic."
validate_logs_meta_data "$response"
return 0
fi

if (( sleep_time < MAX_SLEEP_TIME )); then
sleep_time=$(( sleep_time * 2 ))
fi
echo "Log event not found in New Relic. Retrying in $sleep_time seconds..."
attempt=$((attempt + 1))
done

exit_with_error "Log event with $attribute_key: $attribute_value not found in New Relic. Error Received: $response"
}

validate_logs_not_present() {
user_key=$1
account_id=$2
attribute_key=$3
attribute_value=$4
log_message=$5

max_retries=3
sleep_time=30
attempt=1

while [[ $attempt -lt $max_retries ]]; do
echo "Fetching logs from new relic for $attribute_key: $attribute_value"
sleep "$sleep_time"
response=$(fetch_new_relic_logs_api "$user_key" "$account_id" "$attribute_key" "$attribute_value")

if echo "$response" | grep -q "$log_message"; then
exit_with_error "Log event found in New Relic. Validation failed"
fi

echo "Log event not found in New Relic. Retrying in $sleep_time seconds..."
attempt=$((attempt + 1))
done

echo "Log event with $attribute_key: $attribute_value not found in New Relic. Validation succeeded"
}

fetch_new_relic_logs_api() {
user_key=$1
account_id=$2
attribute_key=$3
attribute_value=$4

nrql_query="SELECT * FROM Log WHERE $attribute_key LIKE '%$attribute_value%' SINCE $TIME_RANGE ago"
query='{"query":"query($id: Int!, $nrql: Nrql!) { actor { account(id: $id) { nrql(query: $nrql) { results } } } }","variables":{"id":'$account_id',"nrql":"'$nrql_query'"}}'

response=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "API-Key: $user_key" \
-d "$query" \
https://api.newrelic.com/graphql)

echo "$response"
}

create_log_message() {
log_message=$1
filter_pattern=$2

UUID=$(uuidgen)
echo "RequestId: $UUID, message: $log_message, filter: $filter_pattern"
}

validate_logs_meta_data (){
response=$1

# Validate common attributes
if ! echo "$response" | grep -q "\"$COMMON_ATTRIBUTE_KEY\":\"$COMMON_ATTRIBUTE_VALUE\""; then
exit_with_error "Common attribute $COMMON_ATTRIBUTE_KEY with value $COMMON_ATTRIBUTE_VALUE not found in New Relic logs."
fi
echo "Common attributes validated successfully."

# toDo: add entity synthesis validation post confirmation on this
}
Loading
Loading