1+ #! /bin/bash
2+ set -e
3+
4+ # ANSI color codes for better output readability
5+ GREEN=' \033[0;32m'
6+ BLUE=' \033[0;34m'
7+ YELLOW=' \033[1;33m'
8+ RED=' \033[0;31m'
9+ NC=' \033[0m' # No Color
10+
11+ # Function to print section headers
12+ print_header () {
13+ echo -e " \n${BLUE} ===================================================${NC} "
14+ echo -e " ${BLUE} $1 ${NC} "
15+ echo -e " ${BLUE} ===================================================${NC} \n"
16+ }
17+
18+ # Function to print success messages
19+ print_success () {
20+ echo -e " ${GREEN} ✓ $1 ${NC} "
21+ }
22+
23+ # Function to print info messages
24+ print_info () {
25+ echo -e " ${YELLOW} ℹ $1 ${NC} "
26+ }
27+
28+ # Function to print error messages
29+ print_error () {
30+ echo -e " ${RED} ✗ $1 ${NC} "
31+ }
32+
33+ # Function to make API calls and format the output with jq
34+ call_api () {
35+ local method=$1
36+ local endpoint=$2
37+ local data=$3
38+ local description=$4
39+
40+ echo -e " ${YELLOW} $description ...${NC} "
41+
42+ if [ -z " $data " ]; then
43+ # GET or DELETE request without body
44+ response=$( curl -s -X $method " http://localhost:8000$endpoint " -H " accept: application/json" )
45+ else
46+ # POST, PUT or PATCH request with JSON body
47+ response=$( curl -s -X $method " http://localhost:8000$endpoint " \
48+ -H " accept: application/json" \
49+ -H " Content-Type: application/json" \
50+ -d " $data " )
51+ fi
52+
53+ # Check if response is valid JSON
54+ if echo " $response " | jq -e . > /dev/null 2>&1 ; then
55+ echo " $response " | jq .
56+ print_success " API call completed successfully"
57+ else
58+ print_error " Failed to parse response as JSON"
59+ echo " $response "
60+ return 1
61+ fi
62+
63+ echo " "
64+ }
65+
66+ # Step 1: Build the Docker container
67+ print_header " BUILDING DOCKER CONTAINER"
68+ docker build -t cloudproxy:test .
69+ print_success " Docker image built successfully"
70+
71+ # Step 2: Check for and clean up any existing containers
72+ print_header " CLEANING UP EXISTING CONTAINERS"
73+ if docker ps -a | grep -q cloudproxy-test; then
74+ print_info " Found existing cloudproxy-test container, removing..."
75+ docker rm -f cloudproxy-test
76+ print_success " Removed existing container"
77+ else
78+ print_info " No existing cloudproxy-test container found"
79+ fi
80+
81+ # Step 3: Run the Docker container
82+ print_header " STARTING CLOUDPROXY CONTAINER"
83+ docker run -d --name cloudproxy-test -p 8000:8000 --env-file .env cloudproxy:test
84+ print_success " Container started"
85+
86+ # Wait for the container to initialize
87+ print_info " Waiting for container to initialize..."
88+ sleep 5
89+
90+ # Check if container is still running
91+ if ! docker ps | grep -q cloudproxy-test; then
92+ print_error " Container failed to start or crashed. Showing logs:"
93+ docker logs cloudproxy-test
94+ exit 1
95+ fi
96+
97+ # Step 4: Show container logs
98+ print_header " CONTAINER LOGS"
99+ docker logs cloudproxy-test
100+
101+ # Step 5: Begin API testing
102+ print_header " TESTING CLOUDPROXY API"
103+
104+ # Test 1: List initial proxies
105+ call_api " GET" " /" " " " Listing initial proxies"
106+
107+ # Test 2: Check providers
108+ call_api " GET" " /providers" " " " Checking all providers"
109+
110+ # Test 3: Get auth configuration
111+ call_api " GET" " /auth" " " " Checking authentication configuration"
112+
113+ # Test 4: Try to get a random proxy (may not have proxies yet)
114+ call_api " GET" " /random" " " " Getting a random proxy"
115+
116+ # Test 5: Update DigitalOcean scaling
117+ call_api " PATCH" " /providers/digitalocean" ' {"min_scaling": 3, "max_scaling": 5}' " Updating DigitalOcean scaling"
118+
119+ # Test 6: Update AWS scaling
120+ call_api " PATCH" " /providers/aws" ' {"min_scaling": 3, "max_scaling": 4}' " Updating AWS scaling"
121+
122+ # Test 7: Update Hetzner scaling
123+ call_api " PATCH" " /providers/hetzner" ' {"min_scaling": 3, "max_scaling": 3}' " Updating Hetzner scaling"
124+
125+ # Wait for proxies to be created
126+ print_info " Waiting for proxies to be created (this might take a moment)..."
127+ sleep 10
128+
129+ # Test 8: List proxies after scaling up
130+ call_api " GET" " /" " " " Listing proxies after scaling up"
131+
132+ # Test 9: Check providers again
133+ call_api " GET" " /providers" " " " Checking all providers after scaling"
134+
135+ # Test 10: Get a random proxy again
136+ call_api " GET" " /random" " " " Getting a random proxy after scaling"
137+
138+ # Get the IP of a proxy to delete - using jq to extract the first proxy IP
139+ first_proxy_ip=$( curl -s -X GET " http://localhost:8000/" -H " accept: application/json" |
140+ jq -r ' .proxies[0].ip' )
141+
142+ if [ -n " $first_proxy_ip " ] && [ " $first_proxy_ip " != " null" ]; then
143+ # Test 11: Delete a specific proxy
144+ call_api " DELETE" " /destroy?ip_address=$first_proxy_ip " " " " Deleting proxy with IP $first_proxy_ip "
145+
146+ # Wait for the proxy to be deleted
147+ print_info " Waiting for proxy to be deleted..."
148+ sleep 5
149+
150+ # Test 12: Check if proxy is in destroy queue
151+ call_api " GET" " /destroy" " " " Checking destroy queue"
152+
153+ # Test 13: List proxies after deletion
154+ call_api " GET" " /" " " " Listing proxies after deletion"
155+ else
156+ print_error " No proxies available to delete"
157+ fi
158+
159+ # Get another proxy IP for restart
160+ second_proxy_ip=$( curl -s -X GET " http://localhost:8000/" -H " accept: application/json" |
161+ jq -r ' .proxies[0].ip' )
162+
163+ if [ -n " $second_proxy_ip " ] && [ " $second_proxy_ip " != " null" ]; then
164+ # Test 14: Restart a specific proxy
165+ call_api " DELETE" " /restart?ip_address=$second_proxy_ip " " " " Restarting proxy with IP $second_proxy_ip "
166+ else
167+ print_error " No proxies available to restart"
168+ fi
169+
170+ # Test 15: Scale down DigitalOcean
171+ call_api " PATCH" " /providers/digitalocean" ' {"min_scaling": 1, "max_scaling": 2}' " Scaling down DigitalOcean"
172+
173+ # Wait for scaling down to take effect
174+ print_info " Waiting for scaling down to take effect..."
175+ sleep 10
176+
177+ # Test 16: Check providers again after scale down
178+ call_api " GET" " /providers" " " " Checking providers after scaling down"
179+
180+ # Test 17: Final list of proxies
181+ call_api " GET" " /" " " " Final list of all proxies"
182+
183+ # Check if UI and docs are accessible
184+ print_header " CHECKING WEB INTERFACES"
185+ ui_status=$( curl -s -o /dev/null -w " %{http_code}" http://localhost:8000/ui/)
186+ docs_status=$( curl -s -o /dev/null -w " %{http_code}" http://localhost:8000/docs)
187+
188+ if [ " $ui_status " -eq 200 ]; then
189+ print_success " UI is accessible at http://localhost:8000/ui/"
190+ else
191+ print_error " UI is not accessible, status code: $ui_status "
192+ fi
193+
194+ if [ " $docs_status " -eq 200 ]; then
195+ print_success " API docs are accessible at http://localhost:8000/docs"
196+ else
197+ print_error " API docs are not accessible, status code: $docs_status "
198+ fi
199+
200+ print_header " TEST SUMMARY"
201+ print_success " All tests completed. CloudProxy container is working properly."
202+ print_info " You can access the UI at: http://localhost:8000/ui/"
203+ print_info " You can access the API docs at: http://localhost:8000/docs"
204+ print_info " To stop the container run: docker stop cloudproxy-test"
205+ print_info " To remove the container run: docker rm cloudproxy-test"
0 commit comments