-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtesting-local.sh
More file actions
executable file
·552 lines (472 loc) · 18.5 KB
/
Copy pathtesting-local.sh
File metadata and controls
executable file
·552 lines (472 loc) · 18.5 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#!/bin/bash
set -e # Stop script if any error occurs
# Clean up docker logs from previous runs
rm -rf docker-logs
mkdir -p docker-logs
# Parse flags
HEADED_MODE=false
RUN_TEST=true
ARGS=()
for arg in "$@"; do
if [[ "$arg" == "--headed" ]]; then
HEADED_MODE=true
elif [[ "$arg" == "--noTest" ]]; then
RUN_TEST=false
else
ARGS+=("$arg")
fi
done
PROXY_BR=${ARGS[0]}
PROXY_REPO=${ARGS[1]}
CHARGING_BR=${ARGS[2]}
CHARGING_REPO=${ARGS[3]}
FRONTEND_BR=${ARGS[4]}
FRONTEND_REPO=${ARGS[5]}
TM_VERSION=${ARGS[6]}
if [[ -z $PROXY_BR || -z $CHARGING_BR || -z $FRONTEND_BR || -z $TM_VERSION || -z $PROXY_REPO || -z $CHARGING_REPO || -z $FRONTEND_REPO ]]; then
echo -e "use structure: command [--headed] [--noTest] PROXY_BRANCH PROXY_REPO CHARGING_BRANCH CHARGING_REPO FRONTEND_BRANCH FRONTEND_REPO TMFORUM_VERSION\033[0m"
echo -e "\033[33mOptional flags:\033[0m"
echo -e " --headed Run Cypress tests with GUI (default: headless)\033[0m"
echo -e " --noTest Skip running tests (only build and deploy)\033[0m"
exit 1
fi
# Verify Docker is installed and running
echo -e "\033[35mVerifying Docker installation...\033[0m"
if ! command -v docker &> /dev/null; then
echo -e "\033[31mError: Docker not found. Please install Docker first.\033[0m"
echo -e "\033[33mFor installation instructions, visit: https://docs.docker.com/get-docker/\033[0m"
exit 1
fi
if ! docker info &> /dev/null; then
echo -e "\033[31mError: Docker daemon is not running. Please start Docker.\033[0m"
echo -e "\033[33mTroubleshooting tips:\033[0m"
echo -e " - On Linux/WSL: Run 'sudo systemctl start docker' or 'sudo service docker start'\033[0m"
echo -e " - On macOS/Windows: Start Docker Desktop application\033[0m"
echo -e " - If using WSL and getting permission errors, try: 'sudo dpkg --configure -a'\033[0m"
exit 1
fi
echo -e "\033[35mDocker is installed and running!\033[0m"
# Clone repo and build docker image
# $1 = repo URL, $2 = branch, $3 = target folder, $4 = docker image name
clone_repo_branch () {
echo -e "\033[35mCloning $1 with the branch $2...\033[0m"
git clone -b $2 $1 "$3"
cd "$3" || exit 1
echo -e "\033[35m$3 repo cloned successfully.\033[0m"
echo -e "\033[35mbuilding docker image...\033[0m"
docker build -qt $4 -f docker/Dockerfile .
cd ..
}
# Clone frontend without docker build
# $1 = repo URL, $2 = branch, $3 = target folder
clone_frontend () {
echo -e "\033[35mCloning frontend $1 with branch $2...\033[0m"
git clone -b $2 $1 "$3"
cd "$3" || exit 1
echo -e "\033[35mfrontend cloned successfully.\033[0m"
cd ..
}
wait_server() {
set +e # Disable exit on error for this function
URL=$1
TIMEOUT=600
INTERVAL=5
echo -e "\033[35mwaiting until the server is ready ($URL)...\033[0m"
SECONDS_WAITED=0
while true; do
RESPONSE=$(curl -s --head --request GET "$URL")
echo "$RESPONSE" | head -n 1
if echo "$RESPONSE" | grep "200 OK" > /dev/null; then
break
fi
sleep $INTERVAL
SECONDS_WAITED=$((SECONDS_WAITED + INTERVAL))
if [ $SECONDS_WAITED -ge $TIMEOUT ]; then
echo -e "\033[31mTimeout error: waited $TIMEOUT seconds.\033[0m"
set -e # Re-enable exit on error
exit 1
fi
done
echo -e "\033[35m$2 ready in $SECONDS_WAITED seconds\033[0m"
set -e # Re-enable exit on error
}
echo "git token: $GIT_TOKEN"
echo "test: $ERT"
# TODO: repos need to be set dinamically
if [ -z $GIT_TOKEN ]; then
echo -e "\033[33mGIT_TOKEN is not available, using public access\033[0m"
PROXY_RP="https://github.com/$PROXY_REPO.git"
CHARGING_RP="https://github.com/$CHARGING_REPO.git"
FRONTEND_RP="https://github.com/$FRONTEND_REPO.git"
else
echo -e "\033[32mGIT_TOKEN available, using authenticated access\033[0m"
PROXY_RP="https://$GIT_TOKEN:@github.com/$PROXY_REPO.git"
CHARGING_RP="https://$GIT_TOKEN:@github.com/$CHARGING_REPO.git"
FRONTEND_RP="https://$GIT_TOKEN:@github.com/$FRONTEND_REPO.git"
fi
echo proxy git: $PROXY_RP
echo charging git: $CHARGING_RP
echo frontend git: $FRONTEND_RP
# 1. install pre-requirement (adapted for macOS and Linux)
echo -e "\033[35mSTART BUILDING\033[0m"
echo -e "\033[35mdetecting OS and installing pre-requisites\033[0m"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo -e "\033[35mLinux detected, using apt-get\033[0m"
echo -e "\033[31mplatform: AMD64\033[0m"
export PLATFORM="linux/amd64"
apt-get update && apt-get install -y \
python3 \
python3-pip \
vim \
zip
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo -e "\033[35mmacOS detected, using Homebrew\033[0m"
if [[ "$(uname -m)" == "arm64" ]]; then
export PLATFORM="linux/arm64"
echo -e "\033[31mplatform: ARM64\033[0m"
else
echo -e "\033[31mplatform: AMD64\033[0m"
export PLATFORM="linux/amd64"
fi
# Check if Homebrew is installed
if ! command -v brew &> /dev/null; then
echo -e "\033[31mHomebrew not found. Please install it first:\033[0m"
echo -e "\033[33m/bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"\033[0m"
exit 1
fi
brew install python3 vim zip
else
echo -e "\033[31mUnsupported OS: $OSTYPE\033[0m"
exit 1
fi
echo -e "\033[35mpython3 version:\033[0m"
python3 --version
echo -e "\033[35mpip3 version:\033[0m"
pip3 --version
# Setup virtual environment for macOS
if [[ "$OSTYPE" == "darwin"* ]]; then
echo -e "\033[35msetting up virtual environment...\033[0m"
if [ ! -d "venv" ]; then
python3 -m venv venv
echo -e "\033[35mvirtual environment created\033[0m"
fi
source venv/bin/activate
echo -e "\033[35mvirtual environment activated\033[0m"
fi
if [ -f "requirements.txt" ]; then
echo -e "\033[35mInstalling dependencies from requirements.txt...\033[0m"
pip3 install --no-cache-dir -r requirements.txt
else
echo -e "\033[35mrequirements.txt not found\033[0m"
exit 1
fi
# 2. clone specified repo and build the docker images
echo -e "\033[35mcreating docker networks\033[0m"
docker network create dome 2>/dev/null || echo -e "\033[33mnetwork 'dome' already exists\033[0m"
docker network create main 2>/dev/null || echo -e "\033[33mnetwork 'main' already exists\033[0m"
echo -e "\033[35mcloning the specified repo\033[0m"
echo -e "\033[35mcloning proxy\033[0m"
clone_repo_branch $PROXY_RP $PROXY_BR proxy-repo proxy-system-dev || { echo -e "Docker clone failed."; exit 1; }
echo -e "\033[35mcloning charging\033[0m"
clone_repo_branch $CHARGING_RP $CHARGING_BR charging-repo charging-system-dev || { echo -e "Docker clone failed."; exit 1; }
echo -e "\033[35mcloning frontend\033[0m"
clone_frontend $FRONTEND_RP $FRONTEND_BR frontend-repo || { echo -e "Frontend clone failed."; exit 1; }
# 3. docker up and register app in idm
cd scorpiodb
docker compose up -d > /dev/null 2>&1
echo -e "\033[35mscorpio deployed\033[0m"
cd ..
cd api
export TM_VERSION
docker compose up -d > /dev/null 2>&1
echo -e "\033[35mtmforum api deployed\033[0m"
cd ..
wait_server http://localhost:8633/tmf-api/productCatalogManagement/v4/productSpecification productCatalog
wait_server http://localhost:8633/tmf-api/resourceCatalog/v4/resourceCatalog resourceCatalog
wait_server http://localhost:8633/tmf-api/serviceCatalogManagement/v4/serviceCatalog serviceCatalog
wait_server http://localhost:8633/tmf-api/customerBillManagement/v4/customerBill customerBill
cd idm-docker
docker compose up -d > /dev/null 2>&1
echo -e "\033[35midm server deployed\033[0m"
cd ..
wait_server http://localhost:3000/version idm
echo -e "\033[35mregistering app in idm ...\033[0m"
admin_token=$(curl -X POST \
-H "Content-Type:application/json" \
-d '{ "name": "admin@test.com",
"password": "1234"
}' \
-i \
http://localhost:3000/v1/auth/tokens | grep -i "X-Subject-Token:" | awk '{print $2}' | sed 's/[[:space:]]//g')
echo -e "\033[35midm admin token saved:\033[0m $admin_token"
app_json=$(curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json"\
-d '{
"application": {
"name": "Test_application 1",
"description": "description",
"redirect_uri": "http://proxy.docker:8004/auth/fiware/callback",
"redirect_sign_out_uri": "http://localhost/logout",
"url": "http://proxy.docker:8004",
"grant_type": [
"authorization_code",
"implicit",
"password",
"refresh_token",
"client_credentials"
],
"token_types": [
"jwt",
"permanent"
]
}
}' \
http://localhost:3000/v1/applications)
echo -e "\033[35mapp registering response\033[0m"
echo -e $app_json
CLIENT_ID=$(python3 auth_cred.py --attr application --key id --sjson "$app_json")
CLIENT_SECRET=$(python3 auth_cred.py --attr application --key secret --sjson "$app_json")
OIDC_KEY=$(python3 auth_cred.py --attr application --key jwt_secret --sjson "$app_json")
export CLIENT_ID
export CLIENT_SECRET
export OIDC_KEY
# needed on WSL where shell env vars don't propagate to Dockerr daemon
cat > proxy-docker/.env <<EOF
CLIENT_ID=$CLIENT_ID
CLIENT_SECRET=$CLIENT_SECRET
OIDC_KEY=$OIDC_KEY
EOF
echo -e "\033[35mapp client id and secret saved!\033[0m"
echo -e "\033[35mclient_id: $CLIENT_ID\033[0m"
echo -e "\033[35mclient_secret: $CLIENT_SECRET\033[0m"
echo -e "\033[35moidc_key: $OIDC_KEY\033[0m"
echo -e "\033[35msetting seller role in idm\033[0m"
seller=$(curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json"\
-d '
{
"role": {
"name": "seller"
}
}' \
http://localhost:3000/v1/applications/$CLIENT_ID/roles)
seller_id=$(python3 auth_cred.py --attr role --key id --sjson $seller)
echo -e "\033[35msetting customer role in idm\033[0m"
customer=$(curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json"\
-d '
{
"role": {
"name": "customer"
}
}' \
http://localhost:3000/v1/applications/$CLIENT_ID/roles)
customer_id=$(python3 auth_cred.py --attr role --key id --sjson $customer)
echo -e "\033[35msetting admin role in idm\033[0m"
admin=$(curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json"\
-d '
{
"role": {
"name": "admin"
}
}' \
http://localhost:3000/v1/applications/$CLIENT_ID/roles)
admin_id=$(python3 auth_cred.py --attr role --key id --sjson $admin)
echo -e "\033[35msetting orgAdmin role in idm\033[0m"
orgAdmin=$(curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json"\
-d '
{
"role": {
"name": "orgAdmin"
}
}' \
http://localhost:3000/v1/applications/$CLIENT_ID/roles)
orgAdmin_id=$(python3 auth_cred.py --attr role --key id --sjson $orgAdmin)
echo -e "\033[35massigning roles to the user\033[0m"
echo -e "\033[35massigning seller role to the user\033[0m"
curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/applications/$CLIENT_ID/users/admin/roles/$seller_id
echo -e "\033[35massigning customer role to the user\033[0m"
curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/applications/$CLIENT_ID/users/admin/roles/$customer_id
echo -e "\033[35massigning admin role to the user\033[0m"
curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/applications/$CLIENT_ID/users/admin/roles/$admin_id
echo -e "\033[35massigning orgAdmin role to the user\033[0m"
curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/applications/$CLIENT_ID/users/admin/roles/$orgAdmin_id
# TODO INIT - Create and configure organizations
echo -e "\033[35mcreating SELLER ORG organization\033[0m"
seller_org=$(curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
-d '{
"organization": {
"name": "SELLER ORG",
"description": "Seller organization for testing"
}
}' \
http://localhost:3000/v1/organizations)
seller_org_id=$(python3 auth_cred.py --attr organization --key id --sjson "$seller_org")
echo -e "\033[35mSELLER ORG created with id: $seller_org_id\033[0m"
echo -e "\033[35mcreating BUYER ORG organization\033[0m"
buyer_org=$(curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
-d '{
"organization": {
"name": "BUYER ORG",
"description": "Buyer organization for testing"
}
}' \
http://localhost:3000/v1/organizations)
buyer_org_id=$(python3 auth_cred.py --attr organization --key id --sjson "$buyer_org")
echo -e "\033[35mBUYER ORG created with id: $buyer_org_id\033[0m"
echo -e "\033[35madding admin user to SELLER ORG as owner\033[0m"
add_seller_owner=$(curl -X PUT \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/organizations/$seller_org_id/users/admin/organization_roles/owner)
echo $add_seller_owner
echo -e "\033[35madding admin user to BUYER ORG as owner\033[0m"
add_buyer_owner=$(curl -X PUT \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/organizations/$buyer_org_id/users/admin/organization_roles/owner)
echo $add_buyer_owner
echo -e "\033[35massigning roles to SELLER ORG in application (as owner)\033[0m"
curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/applications/$CLIENT_ID/organizations/$seller_org_id/roles/$seller_id/organization_roles/owner
curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/applications/$CLIENT_ID/organizations/$seller_org_id/roles/$customer_id/organization_roles/owner
curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/applications/$CLIENT_ID/organizations/$seller_org_id/roles/$admin_id/organization_roles/owner
curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/applications/$CLIENT_ID/organizations/$seller_org_id/roles/$orgAdmin_id/organization_roles/owner
echo -e "\033[35massigning roles to BUYER ORG in application (as owner)\033[0m"
curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/applications/$CLIENT_ID/organizations/$buyer_org_id/roles/$seller_id/organization_roles/owner
curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/applications/$CLIENT_ID/organizations/$buyer_org_id/roles/$customer_id/organization_roles/owner
curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/applications/$CLIENT_ID/organizations/$buyer_org_id/roles/$admin_id/organization_roles/owner
curl -X POST \
-H "X-Auth-token: $admin_token" \
-H "Content-Type: application/json" \
http://localhost:3000/v1/applications/$CLIENT_ID/organizations/$buyer_org_id/roles/$orgAdmin_id/organization_roles/owner
echo -e "\033[35morganizations authorized successfully\033[0m"
# TODO END
echo -e "\033[35mdeploying charging\033[0m"
cd charging-docker
docker compose up -d > /dev/null 2>&1 || { echo -e "Docker compose up failed."; exit 1; }
cd ..
echo -e "\033[35mdeploying proxy\033[0m"
cd proxy-docker
docker compose up -d > /dev/null 2>&1 || { echo -e "Docker compose up failed."; exit 1; }
cd ..
echo -e "\033[35mproxy and charging deployed\033[0m"
# 4. execute cloned dockers
echo -e "\033[35mwaiting for services to start...\033[0m"
wait_server http://localhost:8004/version proxy || { echo -e "\033[31mProxy server failed to start\033[0m"; docker logs proxy-docker-proxy-1; exit 1; }
echo -e "\033[35mproxy server is ready\033[0m"
wait_server http://localhost:8004/service charging || { echo -e "\033[31mCharging server failed to start\033[0m"; docker logs charging-docker-charging-1; exit 1; }
echo -e "\033[35mcharging server is ready\033[0m"
echo -e "\033[35mstarting frontend...\033[0m"
# Configure /etc/hosts
echo -e "\033[35mconfiguring /etc/hosts\033[0m"
if ! grep -q "proxy.docker" /etc/hosts; then
echo "127.0.0.1 proxy.docker" | sudo tee -a /etc/hosts
fi
if ! grep -q "charging.docker" /etc/hosts; then
echo "127.0.0.1 charging.docker" | sudo tee -a /etc/hosts
fi
if ! grep -q "idm.docker" /etc/hosts; then
echo "127.0.0.1 idm.docker" | sudo tee -a /etc/hosts
fi
cd frontend-repo
if [ ! -d "node_modules" ]; then
echo -e "\033[35minstalling frontend dependencies...\033[0m"
npm install || { echo -e "npm install failed."; exit 1; }
fi
echo -e "\033[35mstarting frontend dev server...\033[0m"
if [ -f "../frontend.pid" ]; then
OLD_PID=$(cat ../frontend.pid)
echo -e "\033[35mkilling old frontend process (PID: $OLD_PID)...\033[0m"
kill -9 $OLD_PID 2>/dev/null || true
fi
nohup npm run start > ../frontend.log 2>&1 &
FRONTEND_PID=$!
echo $FRONTEND_PID > ../frontend.pid
echo -e "\033[35mfrontend started with PID: $FRONTEND_PID\033[0m"
cd ..
wait_server http://localhost:4200 frontend
cd billing-server
echo -e "\033[35mStarting billing-server deployment\033[0m"
docker compose up -d
cd ..
wait_server http://localhost:3000/api/product-providers/payment-gateways/count billing-server
echo -e "\033[35mBUILD FINISHED\033[0m"
# Save key environment variables
cat > .env_keys <<EOF
export CLIENT_ID=$CLIENT_ID
export CLIENT_SECRET=$CLIENT_SECRET
export OIDC_KEY=$OIDC_KEY
EOF
echo -e "\033[35mEnvironment variables saved. Run: source .env_keys\033[0m"
# 5. run system test
if [ "$RUN_TEST" = true ]; then
echo -e "\033[35mrunning system test\033[0m"
npm install
if [ "$HEADED_MODE" = true ]; then
echo -e "\033[35mrunning Cypress with GUI (headed mode)\033[0m"
npx cypress open --e2e
else
echo -e "\033[35mrunning Cypress in headless mode\033[0m"
npx cypress run --e2e --headless defaultCommandTimeout=90000 || {
echo -e "\033[31mSaving docker logs to docker-logs/...\033[0m"
docker logs --timestamps proxy-docker-proxy-1 > docker-logs/proxy.log 2>&1 || true
docker logs --timestamps charging-docker-charging-1 > docker-logs/charging.log 2>&1 || true
echo -e "\033[31msystem tests failed.\033[0m"
exit 1
}
echo -e "\033[35msystem tests passed\033[0m"
fi
else
echo -e "\033[33mSkipping tests (--noTest flag provided)\033[0m"
fi
# 6. docker down
# cd charging-docker
# docker compose down
# cd ..
# cd proxy-docker
# docker compose down
# cd ..