|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Usage - run commands from repo root: |
| 4 | +# To check if new changes to the layer cause changes to any snapshots: |
| 5 | +# BUILD_LAYERS=true DD_API_KEY=XXXX aws-vault exec sandbox-account-admin -- ./scripts/run_integration_tests |
| 6 | +# To regenerate snapshots: |
| 7 | +# UPDATE_SNAPSHOTS=true DD_API_KEY=XXXX aws-vault exec sandbox-account-admin -- ./scripts/run_integration_tests |
| 8 | + |
| 9 | +set -e |
| 10 | + |
| 11 | +# These values need to be in sync with serverless.yml, where there needs to be a function |
| 12 | +# defined for every handler_runtime combination |
| 13 | +LAMBDA_HANDLERS=("async-metrics" "sync-metrics" "http-requests") |
| 14 | +RUNTIMES=("python27" "python36" "python37" "python38") |
| 15 | + |
| 16 | +LOGS_WAIT_SECONDS=20 |
| 17 | + |
| 18 | +script_path=${BASH_SOURCE[0]} |
| 19 | +scripts_dir=$(dirname $script_path) |
| 20 | +repo_dir=$(dirname $scripts_dir) |
| 21 | +integration_tests_dir="$repo_dir/tests/integration" |
| 22 | + |
| 23 | +script_start_time=$(date --iso-8601=seconds) |
| 24 | + |
| 25 | +mismatch_found=false |
| 26 | + |
| 27 | +if [ -z "$DD_API_KEY" ]; then |
| 28 | + echo "No DD_API_KEY env var set, exiting" |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +if [ -n "$UPDATE_SNAPSHOTS" ]; then |
| 33 | + echo "Overwriting snapshots in this execution" |
| 34 | +fi |
| 35 | + |
| 36 | +if [ -n "$BUILD_LAYERS" ]; then |
| 37 | + echo "Building layers that will be deployed with our test functions" |
| 38 | + source $scripts_dir/build_layers.sh |
| 39 | +else |
| 40 | + echo "Not building layers, ensure they've already been built or re-run with 'BUILD_LAYERS=true DD_API_KEY=XXXX ./scripts/run_integration_tests.sh'" |
| 41 | +fi |
| 42 | + |
| 43 | +cd $integration_tests_dir |
| 44 | +input_event_files=$(ls ./input_events) |
| 45 | +# Sort event files by name so that snapshots stay consistent |
| 46 | +input_event_files=($(for file_name in ${input_event_files[@]}; do echo $file_name; done | sort)) |
| 47 | + |
| 48 | +echo "Deploying functions" |
| 49 | +serverless deploy |
| 50 | + |
| 51 | +echo "Invoking functions" |
| 52 | +set +e # Don't exit this script if an invocation fails or there's a diff |
| 53 | +for handler_name in "${LAMBDA_HANDLERS[@]}"; do |
| 54 | + for runtime in "${RUNTIMES[@]}"; do |
| 55 | + function_name="${handler_name}_${runtime}" |
| 56 | + # Invoke function once for each input event |
| 57 | + for input_event_file in "${input_event_files[@]}"; do |
| 58 | + # Get event name without trailing ".json" so we can build the snapshot file name |
| 59 | + input_event_name=$(echo "$input_event_file" | sed "s/.json//") |
| 60 | + # Return value snapshot file format is snapshots/return_values/{handler}_{runtime}_{input-event} |
| 61 | + snapshot_path="./snapshots/return_values/${function_name}_${input_event_name}.json" |
| 62 | + |
| 63 | + return_value=$(serverless invoke -f $function_name --path "./input_events/$input_event_file") |
| 64 | + |
| 65 | + if [ ! -f $snapshot_path ]; then |
| 66 | + # If the snapshot file doesn't exist yet, we create it |
| 67 | + echo "Writing return value to $snapshot_path because no snapshot exists yet" |
| 68 | + echo "$return_value" >$snapshot_path |
| 69 | + elif [ -n "$UPDATE_SNAPSHOTS" ]; then |
| 70 | + # If $UPDATE_SNAPSHOTS is set to true, write the new logs over the current snapshot |
| 71 | + echo "Overwriting return value snapshot for $snapshot_path" |
| 72 | + echo "$return_value" >$snapshot_path |
| 73 | + else |
| 74 | + # Compare new return value to snapshot |
| 75 | + diff_output=$(echo "$return_value" | diff - $snapshot_path) |
| 76 | + if [ $? -eq 1 ]; then |
| 77 | + echo "Failed: Return value for $function_name does not match snapshot:" |
| 78 | + echo "$diff_output" |
| 79 | + mismatch_found=true |
| 80 | + else |
| 81 | + echo "Ok: Return value for $function_name with $input_event_name event matches snapshot" |
| 82 | + fi |
| 83 | + fi |
| 84 | + done |
| 85 | + |
| 86 | + done |
| 87 | + |
| 88 | +done |
| 89 | +set -e |
| 90 | + |
| 91 | +echo "Sleeping $LOGS_WAIT_SECONDS seconds to wait for logs to appear in CloudWatch..." |
| 92 | +sleep $LOGS_WAIT_SECONDS |
| 93 | + |
| 94 | +echo "Fetching logs for invocations and comparing to snapshots" |
| 95 | +for handler_name in "${LAMBDA_HANDLERS[@]}"; do |
| 96 | + for runtime in "${RUNTIMES[@]}"; do |
| 97 | + function_name="${handler_name}_${runtime}" |
| 98 | + function_snapshot_path="./snapshots/logs/$function_name.log" |
| 99 | + |
| 100 | + # Fetch logs with serverless cli |
| 101 | + raw_logs=$(serverless logs -f $function_name --startTime $script_start_time) |
| 102 | + |
| 103 | + # Replace invocation-specific data like timestamps and IDs with XXXX to normalize logs across executions |
| 104 | + logs=$( |
| 105 | + echo "$raw_logs" | |
| 106 | + # Filter serverless cli errors |
| 107 | + sed '/Serverless: Recoverable error occurred/d' | |
| 108 | + # Remove blank lines |
| 109 | + sed '/^$/d' | |
| 110 | + # Normalize Lambda runtime report logs |
| 111 | + sed -E 's/(RequestId|TraceId|SegmentId|Duration|Memory Used|"e"): [a-z0-9\.\-]+/\1: XXXX/g' | |
| 112 | + # Normalize DD APM headers and AWS account ID |
| 113 | + sed -E "s/(x-datadog-parent-id:|x-datadog-trace-id:|account_id:)[0-9]+/\1XXXX/g" | |
| 114 | + # Normalize timestamps in datapoints POSTed to DD |
| 115 | + sed -E 's/"points": \[\[[0-9\.]+,/"points": \[\[XXXX,/g' | |
| 116 | + # Strip API key from logged requests |
| 117 | + sed -E "s/(api_key=|'api_key': ')[a-z0-9\.\-]+/\1XXXX/g" | |
| 118 | + # Normalize minor package version so that these snapshots aren't broken on version bumps |
| 119 | + sed -E "s/(dd_lambda_layer:datadog-python[0-9]+_2\.)[0-9]+\.0/\1XX\.0/g" |
| 120 | + ) |
| 121 | + |
| 122 | + if [ ! -f $function_snapshot_path ]; then |
| 123 | + # If no snapshot file exists yet, we create one |
| 124 | + echo "Writing logs to $function_snapshot_path because no snapshot exists yet" |
| 125 | + echo "$logs" >$function_snapshot_path |
| 126 | + elif [ -n "$UPDATE_SNAPSHOTS" ]; then |
| 127 | + # If $UPDATE_SNAPSHOTS is set to true write the new logs over the current snapshot |
| 128 | + echo "Overwriting log snapshot for $function_snapshot_path" |
| 129 | + echo "$logs" >$function_snapshot_path |
| 130 | + else |
| 131 | + # Compare new logs to snapshots |
| 132 | + set +e # Don't exit this script if there is a diff |
| 133 | + diff_output=$(echo "$logs" | diff - $function_snapshot_path) |
| 134 | + if [ $? -eq 1 ]; then |
| 135 | + echo "Failed: Mismatch found between new $function_name logs (first) and snapshot (second):" |
| 136 | + echo "$diff_output" |
| 137 | + mismatch_found=true |
| 138 | + else |
| 139 | + echo "Ok: New logs for $function_name match snapshot" |
| 140 | + fi |
| 141 | + set -e |
| 142 | + fi |
| 143 | + done |
| 144 | +done |
| 145 | + |
| 146 | +if [ "$mismatch_found" = true ]; then |
| 147 | + echo "FAILURE: A mismatch between new data and a snapshot was found and printed above." |
| 148 | + echo "If the change is expected, generate new snapshots by running 'UPDATE_SNAPSHOTS=true DD_API_KEY=XXXX ./scripts/run_integration_tests.sh'" |
| 149 | + exit 1 |
| 150 | +fi |
| 151 | + |
| 152 | +if [ -n "$UPDATE_SNAPSHOTS" ]; then |
| 153 | + echo "SUCCESS: Wrote new snapshots for all functions" |
| 154 | + exit 0 |
| 155 | +fi |
| 156 | + |
| 157 | +echo "SUCCESS: No difference found between snapshots and new return values or logs" |
0 commit comments