-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
executable file
·192 lines (161 loc) · 4.53 KB
/
test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
# Function to print section headers
print_header() {
echo -e "\n${BLUE}=== $1 ===${NC}\n"
}
# Function to check if a command was successful
check_result() {
if [ $1 -eq 0 ]; then
echo -e "${GREEN}✓ $2 completed successfully${NC}"
return 0
else
echo -e "${RED}✗ $2 failed${NC}"
return 1
fi
}
# Function to wait for server
wait_for_server() {
local max_attempts=30
local attempt=1
local url="$1"
echo "Waiting for server to start..."
while [ $attempt -le $max_attempts ]; do
if curl -s "$url" > /dev/null; then
echo "Server is up!"
return 0
fi
echo "Attempt $attempt of $max_attempts..."
sleep 1
((attempt++))
done
echo "Server failed to start"
return 1
}
# Check required tools
check_requirements() {
print_header "Checking Requirements"
# Check if ansible is installed
if ! command -v ansible-playbook &> /dev/null; then
echo -e "${RED}Ansible is not installed. Please install it first.${NC}"
return 1
fi
# Check if docker is installed
if ! command -v docker &> /dev/null; then
echo -e "${RED}Docker is not installed. Please install it first.${NC}"
return 1
fi
echo -e "${GREEN}All requirements satisfied${NC}"
return 0
}
# Main testing sequence
echo -e "${BLUE}Starting test suite...${NC}"
# Initialize error counter
errors=0
# Check requirements first
if ! check_requirements; then
exit 1
fi
# 1. Setup test environment
print_header "Setting up test environment"
# Create necessary directories
mkdir -p data uploads/documents uploads/images uploads/other
check_result $? "Create directories"
# Initialize main database
if [ -f "data/database.sqlite" ]; then
rm data/database.sqlite
fi
# Create main database and set permissions
cat schema.sql | sqlite3 data/database.sqlite
chmod 666 data/database.sqlite
# Initialize admin database
if [ -f "admin/admin.sqlite" ]; then
rm admin/admin.sqlite
fi
# Create admin database and set permissions
cat admin/schema.sql | sqlite3 admin/admin.sqlite
chmod 666 admin/admin.sqlite
check_result $? "Initialize database"
# Make scripts executable
chmod +x api/tests/api_tests.sh
chmod +x tests/e2e/run-tests.sh
check_result $? "Make test scripts executable"
# Kill any existing PHP server on port 8007
pkill -f "php -S localhost:8007" || true
sleep 2
# Start PHP development server in background
php -S localhost:8007 > /dev/null 2>&1 &
SERVER_PID=$!
# Wait for server to be ready
if wait_for_server "http://localhost:8007"; then
check_result 0 "Start PHP server"
else
check_result 1 "Start PHP server"
exit 1
fi
# 2. Create test user
print_header "Creating Test User"
php admin/scripts/create_user.php
check_result $? "Create test user"
# 3. Run API Tests
print_header "Running API Tests"
./api/tests/api_tests.sh
if ! check_result $? "API Tests"; then
((errors++))
fi
# 4. Run Admin Panel Tests
print_header "Running Admin Panel Tests"
if [ -f "vendor/bin/phpunit" ]; then
./vendor/bin/phpunit tests/Admin/AdminPanelTest.php
if ! check_result $? "Admin Panel Tests"; then
((errors++))
fi
else
echo -e "${RED}PHPUnit not found. Please run 'composer install' first.${NC}"
((errors++))
fi
# 5. Run E2E Tests
print_header "Running E2E Tests"
# Install required Ansible collections
echo "Installing required Ansible collections..."
ansible-galaxy collection install community.docker
# Run the e2e tests
cd tests/e2e && ./run-tests.sh
if ! check_result $? "E2E Tests"; then
((errors++))
fi
cd ../..
# 6. Run Ansible Tests
print_header "Running Ansible Tests"
# Run ansible-playbook in check mode to validate playbooks
ansible-playbook tests/e2e/docker-compose-test.yml --check
if ! check_result $? "Ansible Playbook Validation"; then
((errors++))
fi
# Run ansible-lint if available
if command -v ansible-lint &> /dev/null; then
ansible-lint tests/e2e/docker-compose-test.yml
if ! check_result $? "Ansible Lint"; then
((errors++))
fi
else
echo -e "${BLUE}ansible-lint not found. Skipping linting.${NC}"
fi
# Cleanup
if [ ! -z "$SERVER_PID" ]; then
kill $SERVER_PID
wait $SERVER_PID 2>/dev/null || true
fi
# Final results
echo -e "\n${BLUE}=== Test Results ===${NC}"
if [ $errors -eq 0 ]; then
echo -e "\n${GREEN}All tests completed successfully!${NC}"
exit 0
else
echo -e "\n${RED}Tests completed with $errors error(s)${NC}"
exit 1
fi