|  | 
| 10 | 10 | 
 | 
| 11 | 11 | # These values need to be in sync with serverless.yml, where there needs to be a function | 
| 12 | 12 | # defined for every handler_runtime combination | 
| 13 |  | -LAMBDA_HANDLERS=("async-metrics" "sync-metrics" "http-requests") | 
|  | 13 | +LAMBDA_HANDLERS=("async-metrics" "sync-metrics" "http-requests" "http-error") | 
| 14 | 14 | RUNTIMES=("python27" "python36" "python37" "python38") | 
|  | 15 | +CONFIGS=("with-plugin" "without-plugin") | 
| 15 | 16 | 
 | 
| 16 | 17 | LOGS_WAIT_SECONDS=20 | 
| 17 | 18 | 
 | 
| @@ -45,115 +46,139 @@ input_event_files=$(ls ./input_events) | 
| 45 | 46 | # Sort event files by name so that snapshots stay consistent | 
| 46 | 47 | input_event_files=($(for file_name in ${input_event_files[@]}; do echo $file_name; done | sort)) | 
| 47 | 48 | 
 | 
| 48 |  | -echo "Deploying functions" | 
|  | 49 | +echo "Deploying functions with plugin" | 
|  | 50 | +serverless deploy -c "./serverless-plugin.yml" | 
|  | 51 | +echo "Deploying functions without plugin" | 
| 49 | 52 | serverless deploy | 
| 50 | 53 | 
 | 
| 51 | 54 | echo "Invoking functions" | 
| 52 | 55 | 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 | 
|  | 56 | +for _sls_type in "${CONFIGS[@]}"; do | 
|  | 57 | + for handler_name in "${LAMBDA_HANDLERS[@]}"; do | 
|  | 58 | + for runtime in "${RUNTIMES[@]}"; do | 
|  | 59 | + if [ "$_sls_type" = "with-plugin" ]; then | 
|  | 60 | + function_name="${handler_name}_${runtime}_with_plugin" | 
| 73 | 61 |  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 | 
|  | 62 | + function_name="${handler_name}_${runtime}" | 
|  | 63 | + fi | 
|  | 64 | + | 
|  | 65 | + # Invoke function once for each input event | 
|  | 66 | + for input_event_file in "${input_event_files[@]}"; do | 
|  | 67 | + # Get event name without trailing ".json" so we can build the snapshot file name | 
|  | 68 | + input_event_name=$(echo "$input_event_file" | sed "s/.json//") | 
|  | 69 | + # Return value snapshot file format is snapshots/return_values/{handler}_{runtime}_{input-event} | 
|  | 70 | + snapshot_path="./snapshots/return_values/${handler_name}_${runtime}_${input_event_name}.json" | 
|  | 71 | + | 
|  | 72 | + if [ "$_sls_type" = "with-plugin" ]; then | 
|  | 73 | + return_value=$(serverless invoke -f $function_name --path "./input_events/$input_event_file" -c "serverless-plugin.yml") | 
| 80 | 74 |  else | 
| 81 |  | - echo "Ok: Return value for $function_name with $input_event_name event matches snapshot" | 
|  | 75 | + return_value=$(serverless invoke -f $function_name --path "./input_events/$input_event_file") | 
| 82 | 76 |  fi | 
| 83 |  | - fi | 
| 84 |  | - done | 
| 85 | 77 | 
 | 
|  | 78 | + if [ ! -f $snapshot_path ]; then | 
|  | 79 | + # If the snapshot file doesn't exist yet, we create it | 
|  | 80 | + echo "Writing return value to $snapshot_path because no snapshot exists yet" | 
|  | 81 | + echo "$return_value" >$snapshot_path | 
|  | 82 | + elif [ -n "$UPDATE_SNAPSHOTS" ]; then | 
|  | 83 | + # If $UPDATE_SNAPSHOTS is set to true, write the new logs over the current snapshot | 
|  | 84 | + echo "Overwriting return value snapshot for $snapshot_path" | 
|  | 85 | + echo "$return_value" >$snapshot_path | 
|  | 86 | + else | 
|  | 87 | + # Compare new return value to snapshot | 
|  | 88 | + diff_output=$(echo "$return_value" | diff - $snapshot_path) | 
|  | 89 | + if [ $? -eq 1 ]; then | 
|  | 90 | + echo "Failed: Return value for $function_name does not match snapshot:" | 
|  | 91 | + echo "$diff_output" | 
|  | 92 | + mismatch_found=true | 
|  | 93 | + else | 
|  | 94 | + echo "Ok: Return value for $function_name with $input_event_name event matches snapshot" | 
|  | 95 | + fi | 
|  | 96 | + fi | 
|  | 97 | + done | 
|  | 98 | + done | 
| 86 | 99 |  done | 
| 87 |  | - | 
| 88 | 100 | done | 
| 89 | 101 | set -e | 
| 90 | 102 | 
 | 
| 91 | 103 | echo "Sleeping $LOGS_WAIT_SECONDS seconds to wait for logs to appear in CloudWatch..." | 
| 92 | 104 | sleep $LOGS_WAIT_SECONDS | 
| 93 | 105 | 
 | 
| 94 | 106 | 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 |  | - # Strip out trace/span/parent/timestamps | 
| 121 |  | - sed -E "s/(\"trace_id\"\: \")[A-Z0-9\.\-]+/\1XXXX/g" | | 
| 122 |  | - sed -E "s/(\"span_id\"\: \")[A-Z0-9\.\-]+/\1XXXX/g" | | 
| 123 |  | - sed -E "s/(\"parent_id\"\: \")[A-Z0-9\.\-]+/\1XXXX/g" | | 
| 124 |  | - sed -E "s/(\"request_id\"\: \")[a-z0-9\.\-]+/\1XXXX/g" | | 
| 125 |  | - sed -E "s/(\"duration\"\: )[0-9\.\-]+/\1XXXX/g" | | 
| 126 |  | - sed -E "s/(\"start\"\: )[0-9\.\-]+/\1XXXX/g" | | 
| 127 |  | - sed -E "s/(\"system\.pid\"\: )[0-9\.\-]+/\1XXXX/g" | 
| 128 |  | - ) | 
| 129 |  | - | 
| 130 |  | - if [ ! -f $function_snapshot_path ]; then | 
| 131 |  | - # If no snapshot file exists yet, we create one | 
| 132 |  | - echo "Writing logs to $function_snapshot_path because no snapshot exists yet" | 
| 133 |  | - echo "$logs" >$function_snapshot_path | 
| 134 |  | - elif [ -n "$UPDATE_SNAPSHOTS" ]; then | 
| 135 |  | - # If $UPDATE_SNAPSHOTS is set to true write the new logs over the current snapshot | 
| 136 |  | - echo "Overwriting log snapshot for $function_snapshot_path" | 
| 137 |  | - echo "$logs" >$function_snapshot_path | 
| 138 |  | - else | 
| 139 |  | - # Compare new logs to snapshots | 
| 140 |  | - set +e # Don't exit this script if there is a diff | 
| 141 |  | - diff_output=$(echo "$logs" | diff - $function_snapshot_path) | 
| 142 |  | - if [ $? -eq 1 ]; then | 
| 143 |  | - echo "Failed: Mismatch found between new $function_name logs (first) and snapshot (second):" | 
| 144 |  | - echo "$diff_output" | 
| 145 |  | - mismatch_found=true | 
|  | 107 | +for _sls_type in "${CONFIGS[@]}"; do | 
|  | 108 | + for handler_name in "${LAMBDA_HANDLERS[@]}"; do | 
|  | 109 | + for runtime in "${RUNTIMES[@]}"; do | 
|  | 110 | + if [ "$_sls_type" = "with-plugin" ]; then | 
|  | 111 | + function_name="${handler_name}_${runtime}_with_plugin" | 
| 146 | 112 |  else | 
| 147 |  | - echo "Ok: New logs for $function_name match snapshot" | 
|  | 113 | + function_name="${handler_name}_${runtime}" | 
| 148 | 114 |  fi | 
| 149 |  | - set -e | 
| 150 |  | - fi | 
|  | 115 | + | 
|  | 116 | + function_snapshot_path="./snapshots/logs/$function_name.log" | 
|  | 117 | + | 
|  | 118 | + # Fetch logs with serverless cli | 
|  | 119 | + if [ "$_sls_type" = "with-plugin" ]; then | 
|  | 120 | + raw_logs=$(serverless logs -f $function_name --startTime $script_start_time -c "serverless-plugin.yml") | 
|  | 121 | + else | 
|  | 122 | + raw_logs=$(serverless logs -f $function_name --startTime $script_start_time) | 
|  | 123 | + fi | 
|  | 124 | + | 
|  | 125 | + # Replace invocation-specific data like timestamps and IDs with XXXX to normalize logs across executions | 
|  | 126 | + logs=$( | 
|  | 127 | + echo "$raw_logs" | | 
|  | 128 | + # Filter serverless cli errors | 
|  | 129 | + sed '/Serverless: Recoverable error occurred/d' | | 
|  | 130 | + # Remove blank lines | 
|  | 131 | + sed '/^$/d' | | 
|  | 132 | + # Normalize Lambda runtime report logs | 
|  | 133 | + sed -E 's/(RequestId|TraceId|SegmentId|Duration|Memory Used|"e"): [a-z0-9\.\-]+/\1: XXXX/g' | | 
|  | 134 | + # Normalize DD APM headers and AWS account ID | 
|  | 135 | + sed -E "s/(x-datadog-parent-id:|x-datadog-trace-id:|account_id:)[0-9]+/\1XXXX/g" | | 
|  | 136 | + # Normalize timestamps in datapoints POSTed to DD | 
|  | 137 | + sed -E 's/"points": \[\[[0-9\.]+,/"points": \[\[XXXX,/g' | | 
|  | 138 | + # Strip API key from logged requests | 
|  | 139 | + sed -E "s/(api_key=|'api_key': ')[a-z0-9\.\-]+/\1XXXX/g" | | 
|  | 140 | + # Normalize minor package version so that these snapshots aren't broken on version bumps | 
|  | 141 | + sed -E "s/(dd_lambda_layer:datadog-python[0-9]+_2\.)[0-9]+\.0/\1XX\.0/g" | | 
|  | 142 | + # Strip out trace/span/parent/timestamps | 
|  | 143 | + sed -E "s/(\"trace_id\"\: \")[A-Z0-9\.\-]+/\1XXXX/g" | | 
|  | 144 | + sed -E "s/(\"span_id\"\: \")[A-Z0-9\.\-]+/\1XXXX/g" | | 
|  | 145 | + sed -E "s/(\"parent_id\"\: \")[A-Z0-9\.\-]+/\1XXXX/g" | | 
|  | 146 | + sed -E "s/(\"request_id\"\: \")[a-z0-9\.\-]+/\1XXXX/g" | | 
|  | 147 | + sed -E "s/(\"duration\"\: )[0-9\.\-]+/\1XXXX/g" | | 
|  | 148 | + sed -E "s/(\"start\"\: )[0-9\.\-]+/\1XXXX/g" | | 
|  | 149 | + sed -E "s/(\"system\.pid\"\: )[0-9\.\-]+/\1XXXX/g" | | 
|  | 150 | + sed -E "s/(\"runtime-id\"\: \")[a-z0-9\.\-]+/\1XXXX/g" | 
|  | 151 | + ) | 
|  | 152 | + | 
|  | 153 | + if [ ! -f $function_snapshot_path ]; then | 
|  | 154 | + # If no snapshot file exists yet, we create one | 
|  | 155 | + echo "Writing logs to $function_snapshot_path because no snapshot exists yet" | 
|  | 156 | + echo "$logs" >$function_snapshot_path | 
|  | 157 | + elif [ -n "$UPDATE_SNAPSHOTS" ]; then | 
|  | 158 | + # If $UPDATE_SNAPSHOTS is set to true write the new logs over the current snapshot | 
|  | 159 | + echo "Overwriting log snapshot for $function_snapshot_path" | 
|  | 160 | + echo "$logs" >$function_snapshot_path | 
|  | 161 | + else | 
|  | 162 | + # Compare new logs to snapshots | 
|  | 163 | + set +e # Don't exit this script if there is a diff | 
|  | 164 | + diff_output=$(echo "$logs" | diff - $function_snapshot_path) | 
|  | 165 | + if [ $? -eq 1 ]; then | 
|  | 166 | + echo "Failed: Mismatch found between new $function_name logs (first) and snapshot (second):" | 
|  | 167 | + echo "$diff_output" | 
|  | 168 | + mismatch_found=true | 
|  | 169 | + else | 
|  | 170 | + echo "Ok: New logs for $function_name match snapshot" | 
|  | 171 | + fi | 
|  | 172 | + set -e | 
|  | 173 | + fi | 
|  | 174 | + done | 
| 151 | 175 |  done | 
| 152 | 176 | done | 
| 153 | 177 | 
 | 
| 154 | 178 | if [ "$mismatch_found" = true ]; then | 
| 155 | 179 |  echo "FAILURE: A mismatch between new data and a snapshot was found and printed above." | 
| 156 | 180 |  echo "If the change is expected, generate new snapshots by running 'UPDATE_SNAPSHOTS=true DD_API_KEY=XXXX ./scripts/run_integration_tests.sh'" | 
|  | 181 | + echo "Make sure https://httpstat.us/400/ is UP for `http_error` test case" | 
| 157 | 182 |  exit 1 | 
| 158 | 183 | fi | 
| 159 | 184 | 
 | 
|  | 
0 commit comments