Skip to content

Commit d2d0bd6

Browse files
committed
Fix test scripts
1 parent 5d8878f commit d2d0bd6

14 files changed

+219
-58
lines changed

cleanup-k8s.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
set -e
44

55
# Source shared utilities
6-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7-
source "$SCRIPT_DIR/scripts/utils.sh"
8-
source "$SCRIPT_DIR/scripts/config.sh"
6+
if [ -z "$SCRIPT_DIR" ]; then
7+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8+
fi
9+
10+
# If SCRIPT_DIR is the root directory, we need to source from scripts subdirectory
11+
if [ "$(basename "$SCRIPT_DIR")" = "scripts" ]; then
12+
source "$SCRIPT_DIR/utils.sh"
13+
source "$SCRIPT_DIR/config.sh"
14+
else
15+
source "$SCRIPT_DIR/scripts/utils.sh"
16+
source "$SCRIPT_DIR/scripts/config.sh"
17+
fi
918

1019
show_script_header "Cleanup" "Cleaning up Apollo Supergraph from minikube"
1120

k8s-status.sh

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
#!/bin/bash
22

33
# Source shared utilities
4-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5-
if [ -f "$SCRIPT_DIR/scripts/utils.sh" ]; then
6-
source "$SCRIPT_DIR/scripts/utils.sh"
4+
if [ -z "$SCRIPT_DIR" ]; then
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
fi
7+
8+
# If SCRIPT_DIR is the root directory, we need to source from scripts subdirectory
9+
if [ "$(basename "$SCRIPT_DIR")" = "scripts" ]; then
10+
source "$SCRIPT_DIR/utils.sh"
711
else
8-
# Fallback if utils.sh not found
9-
RED='\033[0;31m'
10-
GREEN='\033[0;32m'
11-
YELLOW='\033[1;33m'
12-
BLUE='\033[0;34m'
13-
NC='\033[0m'
14-
15-
print_status() { echo -e "${BLUE}[INFO]${NC} $1"; }
16-
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
17-
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
18-
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
12+
source "$SCRIPT_DIR/scripts/utils.sh"
1913
fi
2014

2115
# Function to show usage

kill-minikube.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
set -e
44

55
# Source shared utilities
6-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7-
source "$SCRIPT_DIR/scripts/utils.sh"
6+
if [ -z "$SCRIPT_DIR" ]; then
7+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8+
fi
9+
10+
# If SCRIPT_DIR is the root directory, we need to source from scripts subdirectory
11+
if [ "$(basename "$SCRIPT_DIR")" = "scripts" ]; then
12+
source "$SCRIPT_DIR/utils.sh"
13+
else
14+
source "$SCRIPT_DIR/scripts/utils.sh"
15+
fi
816

917
show_script_header "Minikube Cleanup" "Killing minikube for Apollo Supergraph cleanup"
1018

run-k8s.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
set -e
44

55
# Source shared utilities
6-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7-
source "$SCRIPT_DIR/scripts/utils.sh"
8-
source "$SCRIPT_DIR/scripts/config.sh"
6+
if [ -z "$SCRIPT_DIR" ]; then
7+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8+
fi
9+
10+
# If SCRIPT_DIR is the root directory, we need to source from scripts subdirectory
11+
if [ "$(basename "$SCRIPT_DIR")" = "scripts" ]; then
12+
source "$SCRIPT_DIR/utils.sh"
13+
source "$SCRIPT_DIR/config.sh"
14+
else
15+
source "$SCRIPT_DIR/scripts/utils.sh"
16+
source "$SCRIPT_DIR/scripts/config.sh"
17+
fi
918

1019
# Function to show usage
1120
show_usage() {

run-local.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
#!/bin/bash
22

33
# Source shared utilities
4-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5-
source "$SCRIPT_DIR/scripts/utils.sh"
6-
source "$SCRIPT_DIR/scripts/config.sh"
4+
if [ -z "$SCRIPT_DIR" ]; then
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
fi
7+
8+
# If SCRIPT_DIR is the root directory, we need to source from scripts subdirectory
9+
if [ "$(basename "$SCRIPT_DIR")" = "scripts" ]; then
10+
source "$SCRIPT_DIR/utils.sh"
11+
source "$SCRIPT_DIR/config.sh"
12+
else
13+
source "$SCRIPT_DIR/scripts/utils.sh"
14+
source "$SCRIPT_DIR/scripts/config.sh"
15+
fi
716

817
# =============================================================================
918
# Apollo Supergraph - Local Development Script (No Kubernetes)

scripts/build-validate.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212
set -e
1313

1414
# Source shared utilities
15-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16-
source "$SCRIPT_DIR/utils.sh"
15+
if [ -z "$SCRIPT_DIR" ]; then
16+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17+
fi
18+
19+
# If SCRIPT_DIR is the root directory, we need to source from scripts subdirectory
20+
if [ "$(basename "$SCRIPT_DIR")" = "scripts" ]; then
21+
source "$SCRIPT_DIR/utils.sh"
22+
else
23+
source "$SCRIPT_DIR/scripts/utils.sh"
24+
fi
1725

1826
show_script_header "Build Validation" "Validating Apollo Supergraph build components"
1927

scripts/port-forward-utils.sh

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313
if [ -z "$SCRIPT_DIR" ]; then
1414
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1515
fi
16-
source "$SCRIPT_DIR/scripts/utils.sh"
17-
source "$SCRIPT_DIR/scripts/config.sh"
16+
17+
# If SCRIPT_DIR is the root directory, we need to source from scripts subdirectory
18+
if [ "$(basename "$SCRIPT_DIR")" = "scripts" ]; then
19+
source "$SCRIPT_DIR/utils.sh"
20+
source "$SCRIPT_DIR/config.sh"
21+
else
22+
source "$SCRIPT_DIR/scripts/utils.sh"
23+
source "$SCRIPT_DIR/scripts/config.sh"
24+
fi
1825

1926
# Configuration (now from config)
2027
NAMESPACE=$(get_k8s_namespace)
@@ -153,9 +160,56 @@ wait_for_port_forward() {
153160

154161
# Start router port forwarding
155162
start_router_port_forward() {
156-
start_port_forward "apollo-router" "$ROUTER_GRAPHQL_PORT"
157-
if [ $? -eq 0 ]; then
163+
# Use a single port-forward command to forward both GraphQL and health ports
164+
local pid_file=$(get_pid_file "apollo-router")
165+
166+
# Check if already running
167+
if is_port_forward_running "apollo-router"; then
168+
local pid=$(cat "$pid_file")
169+
print_warning "Port forwarding for apollo-router is already running (PID: $pid)"
170+
return 0
171+
fi
172+
173+
# Check if service exists
174+
if ! kubectl get svc "apollo-router-service" -n "$NAMESPACE" > /dev/null 2>&1; then
175+
print_error "Service apollo-router-service not found in namespace $NAMESPACE"
176+
return 1
177+
fi
178+
179+
# Start port forwarding for both ports
180+
print_status "Starting port forwarding for apollo-router (GraphQL: $ROUTER_GRAPHQL_PORT, Health: $ROUTER_HEALTH_PORT)..."
181+
182+
# Create a temporary log file for debugging
183+
local temp_log=$(mktemp)
184+
185+
# Forward both ports in a single command
186+
kubectl port-forward "svc/apollo-router-service" "$ROUTER_GRAPHQL_PORT:$ROUTER_GRAPHQL_PORT" "$ROUTER_HEALTH_PORT:$ROUTER_HEALTH_PORT" -n "$NAMESPACE" > "$temp_log" 2>&1 &
187+
local pid=$!
188+
189+
# Save PID
190+
echo "$pid" > "$pid_file"
191+
192+
# Wait a moment for port forward to establish
193+
sleep 3
194+
195+
# Verify it's working
196+
if kill -0 "$pid" 2>/dev/null; then
197+
print_success "Port forwarding for apollo-router started (PID: $pid)"
198+
rm -f "$temp_log"
199+
200+
# Wait for both ports to be listening
158201
wait_for_port_forward "apollo-router" "$ROUTER_GRAPHQL_PORT"
202+
wait_for_port_forward "apollo-router" "$ROUTER_HEALTH_PORT"
203+
else
204+
print_error "Failed to start port forwarding for apollo-router"
205+
print_error "Process died immediately. Checking logs..."
206+
if [ -f "$temp_log" ]; then
207+
print_error "Port forwarding logs:"
208+
cat "$temp_log" | head -10
209+
rm -f "$temp_log"
210+
fi
211+
rm -f "$pid_file"
212+
return 1
159213
fi
160214
}
161215

@@ -180,10 +234,12 @@ show_port_forward_status() {
180234
print_status "Port Forwarding Status:"
181235
echo ""
182236

183-
# Check router
237+
# Check router (both GraphQL and health)
184238
if is_port_forward_running "apollo-router"; then
185239
local pid=$(cat $(get_pid_file "apollo-router"))
186-
print_success "Router: Running (PID: $pid) - http://localhost:$ROUTER_GRAPHQL_PORT"
240+
print_success "Router: Running (PID: $pid)"
241+
print_success " - GraphQL: http://localhost:$ROUTER_GRAPHQL_PORT"
242+
print_success " - Health: http://localhost:$ROUTER_HEALTH_PORT"
187243
else
188244
print_error "Router: Not running"
189245
fi
@@ -203,16 +259,15 @@ show_port_forward_status() {
203259
echo " - Status: source scripts/port-forward-utils.sh && show_port_forward_status"
204260
}
205261

206-
# Cleanup function for script exit
262+
# Cleanup function for script exit (only when explicitly stopping)
207263
cleanup_on_exit() {
208-
# Only cleanup if this script is being run directly
209-
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
264+
# Only cleanup if this script is being run directly and explicitly stopping
265+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]] && [[ "$1" == "stop" ]]; then
210266
stop_all_port_forward
211267
fi
212268
}
213269

214-
# Set trap for cleanup
215-
trap cleanup_on_exit EXIT
270+
# Note: Removed automatic trap to prevent port forwarding from stopping immediately
216271

217272
# If script is run directly, show status
218273
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then

scripts/utils.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
# Apollo Supergraph - Shared Utilities
55
# =============================================================================
66
#
7-
# This script contains common utilities used across multiple scripts:
8-
# - Color definitions for output formatting
9-
# - Common helper functions
10-
# - Shared constants and variables
7+
# This file contains shared utility functions used across the Apollo Supergraph
8+
# deployment scripts.
119
#
1210
# =============================================================================
1311

12+
13+
1414
# Colors for output
1515
RED='\033[0;31m'
1616
GREEN='\033[0;32m'

scripts/validate-external-access.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@
1010
# =============================================================================
1111

1212
# Source shared utilities
13-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
14-
source "$SCRIPT_DIR/utils.sh"
15-
source "$SCRIPT_DIR/config.sh"
13+
if [ -z "$SCRIPT_DIR" ]; then
14+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
15+
fi
16+
17+
# If SCRIPT_DIR is the root directory, we need to source from scripts subdirectory
18+
if [ "$(basename "$SCRIPT_DIR")" = "scripts" ]; then
19+
source "$SCRIPT_DIR/utils.sh"
20+
source "$SCRIPT_DIR/config.sh"
21+
else
22+
source "$SCRIPT_DIR/scripts/utils.sh"
23+
source "$SCRIPT_DIR/scripts/config.sh"
24+
fi
1625

1726
show_script_header "External Access Validation" "Validating external access to subgraphs deployment"
1827

setup-env.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@
1313
set -e
1414

1515
# Source shared utilities
16-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17-
source "$SCRIPT_DIR/scripts/utils.sh"
16+
if [ -z "$SCRIPT_DIR" ]; then
17+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
18+
fi
19+
20+
# If SCRIPT_DIR is the root directory, we need to source from scripts subdirectory
21+
if [ "$(basename "$SCRIPT_DIR")" = "scripts" ]; then
22+
source "$SCRIPT_DIR/utils.sh"
23+
else
24+
source "$SCRIPT_DIR/scripts/utils.sh"
25+
fi
1826

1927
# Help function
2028
show_help() {

0 commit comments

Comments
 (0)