|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# Parseable Server (C) 2023 Cloudnatively Pvt. Ltd. |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU Affero General Public License as |
| 7 | +# published by the Free Software Foundation, either version 3 of the |
| 8 | +# License, or (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU Affero General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Affero General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +# |
| 18 | + |
| 19 | +parseable_url=$1 |
| 20 | +stream_name=$2 |
| 21 | +username=$3 |
| 22 | +password=$4 |
| 23 | + |
| 24 | +events=50 |
| 25 | +input_file=$PWD/input.json |
| 26 | + |
| 27 | +curl_std_opts=( -sS --header 'Content-Type: application/json' -w '\n\n%{http_code}' -u "$username":"$password" ) |
| 28 | + |
| 29 | +alert_body='{"alerts":[{"name":"server-fail-alert1","message":"server reported error status","rule":{"field":"http_status","contains":"500","repeats":5,"within":"10m"},"target":[{"name":"slack-target","server_url":"http://mailgun.com","api_key":"xxxx"}]}]}' |
| 30 | + |
| 31 | +schema_body='{"fields":[{"name":"host","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false},{"name":"user-identifier","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false},{"name":"datetime","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false},{"name":"method","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false},{"name":"request","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false},{"name":"protocol","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false},{"name":"status","data_type":"Int64","nullable":true,"dict_id":0,"dict_is_ordered":false},{"name":"bytes","data_type":"Int64","nullable":true,"dict_id":0,"dict_is_ordered":false},{"name":"referer","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false},{"name":"labels","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false}]}' |
| 32 | + |
| 33 | +# Generate events using flog (https://github.com/mingrammer/flog) and store it in input.json file |
| 34 | +create_input_file () { |
| 35 | + flog -f json -n "$events" -t log -o "$input_file" |
| 36 | + sleep 2 |
| 37 | + sed -i '1s/^/[/;$!s/$/,/;$s/$/]/' "$input_file" |
| 38 | + return $? |
| 39 | +} |
| 40 | + |
| 41 | +# Create stream |
| 42 | +create_stream () { |
| 43 | + response=$(curl "${curl_std_opts[@]}" --request PUT "$parseable_url"/api/v1/logstream/"$stream_name") |
| 44 | + |
| 45 | + if [ $? -ne 0 ]; then |
| 46 | + printf "Failed to create log stream %s with exit code: %s\n" "$stream_name" "$?" |
| 47 | + printf "Test create_stream: failed\n" |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | + |
| 51 | + http_code=$(tail -n1 <<< "$response") |
| 52 | + if [ "$http_code" -ne 200 ]; then |
| 53 | + printf "Failed to create log stream %s with http code: %s and response: %s\n" "$stream_name" "$http_code" "$content" |
| 54 | + printf "Test create_stream: failed\n" |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | + |
| 58 | + content=$(sed '$ d' <<< "$response") |
| 59 | + if [ "$content" != "Created log stream $stream_name" ]; then |
| 60 | + printf "Failed to create log stream $stream_name with response: %s\n" "$content" |
| 61 | + printf "Test create_stream: failed\n" |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + |
| 65 | + printf "Test create_stream: successful\n" |
| 66 | + return 0 |
| 67 | +} |
| 68 | + |
| 69 | +# Post log data to the stream |
| 70 | +post_event_data () { |
| 71 | + create_input_file |
| 72 | + if [ $? -ne 0 ]; then |
| 73 | + printf "Failed to create log data to be posted to %s with exit code: %s\n" "$stream_name" "$?" |
| 74 | + printf "Test post_event_data: failed\n" |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | + |
| 78 | + content=$(cat "$input_file") |
| 79 | + response=$(curl "${curl_std_opts[@]}" --request POST "$parseable_url"/api/v1/logstream/"$stream_name" --data-raw "$content") |
| 80 | + if [ $? -ne 0 ]; then |
| 81 | + printf "Failed to post log data to %s with exit code: %s\n" "$stream_name" "$?" |
| 82 | + printf "Test post_event_data: failed\n" |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | + |
| 86 | + http_code=$(tail -n1 <<< "$response") |
| 87 | + if [ "$http_code" -ne 200 ]; then |
| 88 | + printf "Failed to create log stream %s with http code: %s and response: %s\n" "$stream_name" "$http_code" "$content" |
| 89 | + printf "Test post_event_data: failed\n" |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | + |
| 93 | + content=$(sed '$ d' <<< "$response") |
| 94 | + if [ "$content" != "Successfully posted $events events" ]; then |
| 95 | + printf "Failed to post log data to %s with response: %s\n" "$stream_name" "$content" |
| 96 | + printf "Test post_event_data: failed\n" |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + |
| 100 | + printf "Test post_event_data: successful\n" |
| 101 | + return 0 |
| 102 | +} |
| 103 | + |
| 104 | +# List all log stream and [TODO] verify if the stream is created |
| 105 | +list_log_streams () { |
| 106 | + response=$(curl "${curl_std_opts[@]}" --request GET "$parseable_url"/api/v1/logstream) |
| 107 | + if [ $? -ne 0 ]; then |
| 108 | + printf "Failed to list log streams with exit code: %s\n" "$?" |
| 109 | + printf "Test list_log_streams: failed\n" |
| 110 | + exit 1 |
| 111 | + fi |
| 112 | + |
| 113 | + http_code=$(tail -n1 <<< "$response") |
| 114 | + if [ "$http_code" -ne 200 ]; then |
| 115 | + printf "Failed to list all log streams with http code: %s and response: %s" "$http_code" "$content" |
| 116 | + printf "Test list_log_streams: failed\n" |
| 117 | + exit 1 |
| 118 | + fi |
| 119 | + |
| 120 | + content=$(sed '$ d' <<< "$response") |
| 121 | + echo "$content" > "$PWD/log_streams.json" |
| 122 | + |
| 123 | + if [ "$(jq < $PWD/log_streams.json '[.[].name | select(. == "'"$stream_name"'")] | length')" -ne 1 ]; then |
| 124 | + printf "Failed to find new log stream %s in list stream result: %s\n" "$stream_name" "$content" |
| 125 | + printf "Test list_log_streams: failed\n" |
| 126 | + exit 1 |
| 127 | + fi |
| 128 | + |
| 129 | + printf "Test list_log_streams: successful\n" |
| 130 | + return 0 |
| 131 | +} |
| 132 | + |
| 133 | +# Get Stream's schema and [TODO] validate its schema |
| 134 | +get_streams_schema () { |
| 135 | + response=$(curl "${curl_std_opts[@]}" --request GET "$parseable_url"/api/v1/logstream/"$stream_name"/schema) |
| 136 | + if [ $? -ne 0 ]; then |
| 137 | + printf "Failed to fetch stream schema with exit code: %s\n" "$?" |
| 138 | + printf "Test get_streams_schema: failed\n" |
| 139 | + exit 1 |
| 140 | + fi |
| 141 | + |
| 142 | + http_code=$(tail -n1 <<< "$response") |
| 143 | + if [ "$http_code" -ne 200 ]; then |
| 144 | + printf "Failed to get schema for stream %s with http code: %s and response: %s" "$stream_name" "$http_code" "$content" |
| 145 | + printf "Test get_streams_schema: failed\n" |
| 146 | + exit 1 |
| 147 | + fi |
| 148 | + |
| 149 | + content=$(sed '$ d' <<< "$response") |
| 150 | + if [ "$content" != "$schema_body" ]; then |
| 151 | + printf "Get schema response doesn't match with expected schema.\n" |
| 152 | + printf "Schema expected: %s\n" "$schema_body" |
| 153 | + printf "Schema returned: %s\n" "$content" |
| 154 | + printf "Test get_streams_schema: failed\n" |
| 155 | + exit 1 |
| 156 | + fi |
| 157 | + |
| 158 | + printf "Test get_streams_schema: successful\n" |
| 159 | + return 0 |
| 160 | +} |
| 161 | + |
| 162 | +# Query the log stream and verify if count of events is equal to the number of events posted |
| 163 | +query_log_stream() { |
| 164 | + # Query last two minutes of data only |
| 165 | + end_time=$(date "+%Y-%m-%dT%H:%M:%S%:z") |
| 166 | + start_time=$(date --date="@$(($(date +%s)-120))" "+%Y-%m-%dT%H:%M:%S%:z") |
| 167 | + |
| 168 | + response=$(curl "${curl_std_opts[@]}" --request POST "$parseable_url"/api/v1/query --data-raw '{ |
| 169 | + "query": "select count(*) from '$stream_name'", |
| 170 | + "startTime": "'$start_time'", |
| 171 | + "endTime": "'$end_time'" |
| 172 | + }') |
| 173 | + if [ $? -ne 0 ]; then |
| 174 | + printf "Failed to query log data from %s with exit code: %s\n" "$stream_name" "$?" |
| 175 | + printf "Test query_log_stream: failed\n" |
| 176 | + exit 1 |
| 177 | + fi |
| 178 | + |
| 179 | + http_code=$(tail -n1 <<< "$response") |
| 180 | + if [ "$http_code" -ne 200 ]; then |
| 181 | + printf "Failed to query stream %s with http code: %s and response: %s" "$stream_name" "$http_code" "$content" |
| 182 | + printf "Test query_log_stream: failed\n" |
| 183 | + exit 1 |
| 184 | + fi |
| 185 | + |
| 186 | + content=$(sed '$ d' <<< "$response") |
| 187 | + queryResult=$(echo "$content" | cut -d ':' -f2 | cut -d '}' -f1) |
| 188 | + if [ "$queryResult" != $events ]; then |
| 189 | + printf "Validation failed. Count of events returned from query does not match with the ones posted.\n" |
| 190 | + printf "Test query_log_stream: failed\n" |
| 191 | + exit 1 |
| 192 | + fi |
| 193 | + printf "Test query_log_stream: successful\n" |
| 194 | + return 0 |
| 195 | +} |
| 196 | + |
| 197 | +# Set Alert |
| 198 | +set_alert () { |
| 199 | + response=$(curl "${curl_std_opts[@]}" --request PUT "$parseable_url"/api/v1/logstream/"$stream_name"/alert --data-raw "$alert_body") |
| 200 | + if [ $? -ne 0 ]; then |
| 201 | + printf "Failed to set alert for %s with exit code: %s\n" "$stream_name" "$?" |
| 202 | + printf "Test set_alert: failed\n" |
| 203 | + exit 1 |
| 204 | + fi |
| 205 | + |
| 206 | + http_code=$(tail -n1 <<< "$response") |
| 207 | + if [ "$http_code" -ne 200 ]; then |
| 208 | + printf "Failed to set alert for %s with http code: %s and response: %s\n" "$stream_name" "$http_code" "$content" |
| 209 | + printf "Test set_alert: failed\n" |
| 210 | + exit 1 |
| 211 | + fi |
| 212 | + |
| 213 | + content=$(sed '$ d' <<< "$response") |
| 214 | + if [ "$content" != "Set alert configuration for log stream $stream_name" ]; then |
| 215 | + printf "Failed to set alert on log stream %s with response: %s\n" "$stream_name" "$content" |
| 216 | + printf "Test set_alert: failed\n" |
| 217 | + exit 1 |
| 218 | + fi |
| 219 | + |
| 220 | + printf "Test set_alert: successful\n" |
| 221 | + return 0 |
| 222 | +} |
| 223 | + |
| 224 | +# Get Alert |
| 225 | +get_alert () { |
| 226 | + response=$(curl "${curl_std_opts[@]}" --request GET "$parseable_url"/api/v1/logstream/"$stream_name"/alert) |
| 227 | + if [ $? -ne 0 ]; then |
| 228 | + printf "Failed to get alert for %s with exit code: %s\n" "$stream_name" "$?" |
| 229 | + printf "Test get_alert: failed\n" |
| 230 | + exit 1 |
| 231 | + fi |
| 232 | + |
| 233 | + http_code=$(tail -n1 <<< "$response") |
| 234 | + if [ "$http_code" -ne 200 ]; then |
| 235 | + printf "Failed to get alert for %s with http code: %s and response: %s" "$stream_name" "$http_code" "$content" |
| 236 | + printf "Test get_alert: failed\n" |
| 237 | + exit 1 |
| 238 | + fi |
| 239 | + |
| 240 | + content=$(sed '$ d' <<< "$response") |
| 241 | + if [ "$content" != "$alert_body" ]; then |
| 242 | + printf "Get alert response doesn't match with Alert config returned.\n" |
| 243 | + printf "Alert set: %s\n" "$alert_body" |
| 244 | + printf "Alert returned: %s\n" "$content" |
| 245 | + printf "Test get_alert: failed\n" |
| 246 | + exit 1 |
| 247 | + fi |
| 248 | + |
| 249 | + printf "Test get_alert: successful\n" |
| 250 | + return 0 |
| 251 | +} |
| 252 | + |
| 253 | +# Delete stream |
| 254 | +delete_stream () { |
| 255 | + response=$(curl "${curl_std_opts[@]}" --request DELETE "$parseable_url"/api/v1/logstream/"$stream_name") |
| 256 | + |
| 257 | + if [ $? -ne 0 ]; then |
| 258 | + printf "Failed to delete stream for %s with exit code: %s\n" "$stream_name" "$?" |
| 259 | + printf "Test delete_stream: failed\n" |
| 260 | + exit 1 |
| 261 | + fi |
| 262 | + |
| 263 | + http_code=$(tail -n1 <<< "$response") |
| 264 | + if [ "$http_code" -ne 200 ]; then |
| 265 | + printf "Failed to delete log stream %s with http code: %s and response: %s\n" "$stream_name" "$http_code" "$content" |
| 266 | + printf "Test delete_stream: failed\n" |
| 267 | + exit 1 |
| 268 | + fi |
| 269 | + |
| 270 | + content=$(sed '$ d' <<< "$response") |
| 271 | + if [ "$content" != "log stream $stream_name deleted" ]; then |
| 272 | + printf "Failed to delete log stream %s with response: %s" "$stream_name" "$content" |
| 273 | + printf "Test delete_stream: failed\n" |
| 274 | + exit 1 |
| 275 | + fi |
| 276 | + |
| 277 | + printf "Test delete_stream: successful\n" |
| 278 | + return 0 |
| 279 | +} |
| 280 | + |
| 281 | +cleanup () { |
| 282 | + rm -rf "$input_file" |
| 283 | + rm -rf "$PWD/logstream_test.json" |
| 284 | + return $? |
| 285 | +} |
| 286 | + |
| 287 | +printf "======= Starting smoke tests =======\n" |
| 288 | +printf "** Log stream name: %s **\n" "$stream_name" |
| 289 | +printf "** Event count: %s **\n" "$events" |
| 290 | +printf "====================================\n" |
| 291 | +create_stream |
| 292 | +post_event_data |
| 293 | +list_log_streams |
| 294 | +get_streams_schema |
| 295 | +query_log_stream |
| 296 | +set_alert |
| 297 | +get_alert |
| 298 | +delete_stream |
| 299 | +cleanup |
| 300 | +printf "======= Smoke tests completed ======\n" |
0 commit comments