Skip to content

Commit

Permalink
Merge pull request #294 from TykTechnologies/streams-checks
Browse files Browse the repository at this point in the history
Streams checks
  • Loading branch information
davegarvey authored Feb 6, 2025
2 parents a133d07 + 4bd3e25 commit 016381a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 24 deletions.
48 changes: 28 additions & 20 deletions deployments/tyk/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ for data_group_path in deployments/tyk/data/tyk-dashboard/*; do
log_message "Creating APIs"
for file in $data_group_path/apis/*; do
if [[ -f $file ]]; then
if api_has_section "$file" "x-tyk-streaming" && ! licence_has_scope "DASHBOARD_LICENCE" "streams"; then
log_message " Warning: API $file has Tyk Streaming enabled, but the licence does not have the 'streams' scope. Skipping import."
continue
fi
create_api "$file" "$dashboard_user_api_key"
bootstrap_progress
fi
Expand Down Expand Up @@ -499,27 +503,31 @@ done
log_ok

log_message "Checking Gateway 2 - Anonymous API access"
result=""
reload_attempt=0
while [ "$result" != "0" ]
do
wait_for_response "$gateway2_base_url/basic-open-api/get" "200" "" 3
result="$?"
if [ "$result" != "0" ]
then
reload_attempt=$((reload_attempt+1))
if [ "$reload_attempt" -lt "3" ]; then
log_message " Gateway 2 not returning desired response, attempting hot reload"
hot_reload "$gateway2_base_url" "$gateway2_api_credentials"
sleep 2
else
log_message " Maximum reload attempt reached"
exit 1
if [ "$(licence_allowed_nodes "DASHBOARD_LICENCE")" -lt 2 ]; then
log_message " Skipping Gateway 2 check as licence does not allow 2 or more nodes, so gateway 2 cannot register with the Dashboard"
else
result=""
reload_attempt=0
while [ "$result" != "0" ]
do
wait_for_response "$gateway2_base_url/basic-open-api/get" "200" "" 3
result="$?"
if [ "$result" != "0" ]
then
reload_attempt=$((reload_attempt+1))
if [ "$reload_attempt" -lt "3" ]; then
log_message " Gateway 2 not returning desired response, attempting hot reload"
hot_reload "$gateway2_base_url" "$gateway2_api_credentials"
sleep 2
else
log_message " Maximum reload attempt reached"
exit 1
fi
fi
fi
bootstrap_progress
done
log_ok
bootstrap_progress
done
log_ok
fi

log_message "Sending API requests to generate analytics data"
# global analytics off
Expand Down
31 changes: 31 additions & 0 deletions scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1015,4 +1015,35 @@ check_for_wscat () {
echo "npm install -g wscat"
exit 1
fi
}

api_has_section () {
local json_file="$1"
local section="$2"
if jq -e 'has("'"$section"'")' "$json_file" >/dev/null 2>&1; then
return 0
else
return 1
fi
}

licence_has_scope () {
local licence_payload scope scopes search_term

licence_name="$1"
search_term="$2"
licence_payload=$(get_licence_payload "$licence_name") || return 2

# Check if jq command succeeds
scopes=$(echo "$licence_payload" | jq -r '.scope') || return 2

# Direct return of grep status
echo "$scopes" | tr ',' '\n' | grep -Fx "$search_term" > /dev/null
}

licence_allowed_nodes () {
local licence_name="$1"
local licence_payload=$(get_licence_payload $licence_name)
# Extract the 'allowed_nodes' value and split by commas to count the elements
echo "$licence_payload" | jq -r '.allowed_nodes' | tr ',' '\n' | wc -l
}
14 changes: 10 additions & 4 deletions scripts/test-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ run_postman_test() {
done < "$dynamic_env_var_path"
fi

# Run the Postman test command
"${test_cmd[@]}" | tee -a "logs/postman.log"
return $?
# Run command and tee output, capturing exit status
{ "${test_cmd[@]}" 2>&1 | tee -a "logs/postman.log"; }
local exit_status=${PIPESTATUS[0]}

return $exit_status
}

# Run custom test scripts
Expand All @@ -76,7 +78,11 @@ run_test_scripts() {
for test_script in "${test_scripts[@]}"; do
TEST_SCRIPT_COUNT=$((TEST_SCRIPT_COUNT+1))
echo "Running test script: $test_script" | tee -a "logs/custom_scripts.log"
if bash "$test_script" | tee -a "logs/custom_scripts.log"; then

{ bash "$test_script" 2>&1 | tee -a "logs/custom_scripts.log"; }
local exit_status=${PIPESTATUS[0]}

if [[ $exit_status -eq 0 ]]; then
TEST_SCRIPT_PASSES=$((TEST_SCRIPT_PASSES+1))
echo "✓ Test script passed" | tee -a "logs/custom_scripts.log"
else
Expand Down
3 changes: 3 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ readonly GREEN='\033[0;32m'
readonly BLUE='\033[0;34m'
readonly NOCOLOUR='\033[0m'

declare -a postman_results script_results statuses deployments

log() {
echo -e "$1" | tee -a "$BASE_DIR/logs/test.log"
}
Expand Down Expand Up @@ -48,6 +50,7 @@ run_tests_for_deployment() {
postman_result="Failed"
deployment_status=1
fi
postman_results+=("$postman_result")
else
log "${BLUE}No Postman tests found${NOCOLOUR}"
fi
Expand Down

0 comments on commit 016381a

Please sign in to comment.