Skip to content

Commit fdafd0d

Browse files
committed
fixup!test: rework to share principles with redis sample
1 parent b986ee9 commit fdafd0d

File tree

10 files changed

+165
-52
lines changed

10 files changed

+165
-52
lines changed

.github/workflows/build-using-cnb-buildpack.yml

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ on:
99
- "v?[0-9]+.[0-9]+.[0-9]+*"
1010
env:
1111
CNB_IMAGE_NAME: cnb-app
12+
SERVICE_NAME: 'mongodb'
1213
SERVICE_VERSION: '6.0'
14+
SERVICE_IMAGE: "mongo:7.0.14"
1315
SERVICE_PORT: 27017 # Default value: 27017
1416
SERVICE_USERNAME: '22032e25-4aba-417f-a394-8bbd78d920cd'
1517
SERVICE_PASSWORD: 'StbQ4EovUpwQjD0cT1Hr7PKBG'
1618
DATABASE_NAME: my-mongo-db
19+
DEBUG: 0
1720

1821
jobs:
1922
package-app:
@@ -40,65 +43,31 @@ jobs:
4043
-
4144
name: Setup pack for ${{ github.repository }}
4245
uses: buildpacks/github-actions/setup-pack@v5.0.0
43-
-
44-
name: Start MongoDB testing server
45-
uses: supercharge/mongodb-github-action@1.11.0
46-
with:
47-
mongodb-version: ${{ env.SERVICE_VERSION }}
48-
mongodb-port: ${{env.SERVICE_PORT}}
49-
mongodb-username: ${{env.SERVICE_USERNAME}}
50-
mongodb-password: ${{env.SERVICE_PASSWORD}}
51-
mongodb-db: ${{env.DATABASE_NAME}}
52-
# mongodb-container-name: mongodb-${{ env.SERVICE_VERSION }}
46+
# -
47+
# name: Start MongoDB testing server
48+
# uses: supercharge/mongodb-github-action@1.11.0
49+
# with:
50+
# mongodb-version: ${{ env.SERVICE_VERSION }}
51+
# mongodb-port: ${{env.SERVICE_PORT}}
52+
# mongodb-username: ${{env.SERVICE_USERNAME}}
53+
# mongodb-password: ${{env.SERVICE_PASSWORD}}
54+
# mongodb-db: ${{env.DATABASE_NAME}}
55+
## mongodb-container-name: mongodb-${{ env.SERVICE_VERSION }}
5356

5457
-
5558
name: Packaging app from ${{ github.repository }} pushed by ${{ github.actor }}
5659
shell: bash
5760
run: |
5861
echo "Pack Build ${{env.CNB_IMAGE_NAME}} (${{github.repository}})"
59-
pack build ${{env.CNB_IMAGE_NAME}} --builder paketobuildpacks/builder-jammy-base
62+
pack build ${{env.CNB_IMAGE_NAME}}
6063
echo "Pack Inspect ${{env.CNB_IMAGE_NAME}} (${{github.repository}})"
6164
pack inspect ${{env.CNB_IMAGE_NAME}}
6265
-
6366
name: Testing app
6467
shell: bash
6568
run: |
66-
cat vcap-services-template.sh|envsubst >vcap-service.env
67-
cat vcap-service.env
68-
echo "Starting sample app"
69-
container_id=$(docker run -d -e PORT=80 --env-file vcap-service.env -p 8080:80 --name "cnb-app-container" ${{env.CNB_IMAGE_NAME}})
70-
echo "Cnb app started (id: $container_id)"
71-
docker inspect -f '{{.HostConfig.LogConfig.Type}}' $container_id
72-
# docker run -e PORT=80 --env-file vcap-service.env -p 8080:80 --name "cnb-app-container" ${{env.CNB_IMAGE_NAME}}
73-
docker logs -f cnb-app-container &> cnb-app-container.log &
74-
docker logs mongodb
75-
cat /etc/hosts
76-
docker ps -a
77-
env|sort
78-
export APP="http://127.0.0.1:8080"
79-
exit_status=0
80-
if nc -vz 127.0.0.1 8080;then echo "port 8080 available";else echo "port 8080 UNAVAILABLE";exit_status=1;fi
81-
if nc -vz 127.0.0.1 ${SERVICE_PORT};then echo "port ${SERVICE_PORT} available";else echo "port ${SERVICE_PORT} UNAVAILABLE";exit_status=1;fi
82-
if mongosh --version;then echo "mongosh available";else echo "mongosh UNAVAILABLE";fi
83-
echo "Create using 'curl -X POST $APP/myCollection'"
84-
if ! curl -vvv -X POST $APP/myCollection -H 'Content-Length: 0';then
85-
echo "Create failed"
86-
exit_status=1
87-
else
88-
echo "Create successful"
89-
fi
90-
echo "Delete using 'curl -X DELETE $APP/myCollection'"
91-
if ! curl -vvv -X DELETE $APP/myCollection;then
92-
echo "Delete failed"
93-
exit_status=1
94-
else
95-
echo "Delete successful"
96-
fi
97-
cat cnb-app-container.log
98-
echo "Dumping logs using docker logs cnb-app-container"
99-
docker logs cnb-app-container
100-
ls -lrt
101-
exit $exit_status
69+
./setup-prerequisite.sh
70+
./run-tests.sh
10271
-
10372
name: Tag version ${{github.ref_name}}
10473
if: github.ref_type == 'tag'

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/vendor
22
/tmp
33
.idea/
4+
*.log
5+
*.iml
6+
/vcap-service.env

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ GEM
4848
nenv (~> 0.1)
4949
shellany (~> 0.0)
5050
openssl (3.0.2)
51+
ostruct (0.6.0)
5152
pry (0.14.2)
5253
coderay (~> 1.1)
5354
method_source (~> 1.0)
@@ -100,6 +101,7 @@ DEPENDENCIES
100101
guard
101102
guard-rspec
102103
mongo
104+
ostruct (~> 0.6.0)
103105
rack-test
104106
rspec
105107
sinatra

local.env

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export CNB_IMAGE_NAME=mongodb-cnb-app
2+
export SERVICE_NAME=mongodb
3+
export SERVICE_HEALTH_CMD=mongo
4+
export SERVICE_PORT=27001
5+
export DATABASE_NAME=myCollection
6+
export SERVICE_IMAGE="mongo:7.0.14"
7+
export SERVICE_HOST="dynamically_generated"
8+
export SERVICE_PASSWORD=my-passwwwwwwwword15;
9+
#export DEBUG=0

project.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[_]
2+
schema-version = "0.2"
3+
id = "com.orange-cloudfoundry.sample-apps.mongodb"
4+
name = "MongoDB sample app"
5+
#version = "1.0.0"
6+
7+
[io.buildpacks]
8+
builder = "paketobuildpacks/builder-jammy-base"
9+
exclude = [
10+
".github",
11+
"renovate.json",
12+
"README.md"
13+
]

run-tests.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
./vcap-services-template-reformat.sh
5+
cat vcap-service.env
6+
echo "Starting sample app"
7+
container_name="cnb-app-container"
8+
container_id=$(docker run -d --rm -e PORT=80 -p 8080:80 --env-file vcap-service.env --name "$container_name" ${CNB_IMAGE_NAME})
9+
10+
echo "Cnb app started (id: $container_id)"
11+
echo "Waiting to ensure app is up and running"
12+
while [ "$( docker container inspect -f '{{.State.Status}}' $container_name )" != "running" ]; do
13+
echo "waiting for $container_name to be running current: $(docker inspect -f '{{.State.Status}}' $container_name)"
14+
sleep 1
15+
done
16+
sleep 3 # to ensure app is up and running
17+
#docker inspect -f '{{.HostConfig.LogConfig.Type}}' $container_id
18+
service_container_name="$(docker ps -f "ancestor=$SERVICE_IMAGE" --format "{{.Names}}")"
19+
CONTAINER_APP_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container_name)
20+
echo "CONTAINER_APP_IP: $CONTAINER_APP_IP"
21+
22+
if [ "$DEBUG" -eq 1 ]; then
23+
echo "----------------- $container_name --------------------"
24+
docker inspect -f '{{json .NetworkSettings}}' $container_name
25+
26+
echo "----------------- $SERVICE_NAME: $service_container_name --------------------"
27+
docker inspect -f '{{json .NetworkSettings}}' "$service_container_name"
28+
fi
29+
echo "=== Redirect logs to cnb-app-container.log ==="
30+
docker logs -f cnb-app-container &> cnb-app-container.log &
31+
docker ps -a
32+
33+
echo "=== Check connectivity ==="
34+
if nc -vz 127.0.0.1 8080;then echo "port 8080 available";else echo "port 8080 UNAVAILABLE";exit_status=1;fi
35+
if nc -vz 127.0.0.1 "${SERVICE_PORT}";then echo "port ${SERVICE_PORT} available";else echo "port ${SERVICE_PORT} UNAVAILABLE";exit_status=1;fi
36+
#echo "Checking redis server 127.0.0.1 : PING ==> $(redis-cli -a ${SERVICE_PASSWORD} -h 127.0.0.1 -p ${SERVICE_PORT} --no-auth-warning ping)"
37+
#echo "Checking redis server $SERVICE_HOST : PING ==> $(redis-cli -a ${SERVICE_PASSWORD} -h ${SERVICE_HOST} -p ${SERVICE_PORT} --no-auth-warning ping)"
38+
39+
function check_service() {
40+
type="$1"
41+
cmd="$2"
42+
cmd_prefix="$3"
43+
if [ -z "$cmd_prefix" ];then
44+
cmd_prefix="curl"
45+
fi
46+
status=0
47+
echo "$type using > $cmd <" 1>&2
48+
if ! $cmd;then
49+
echo "" 1>&2
50+
echo "$type failed: retry in verbose mode" 1>&2
51+
$cmd_prefix -vvv ${cmd##$cmd_prefix}
52+
status=1
53+
else
54+
echo "" 1>&2
55+
echo "$type successful" 1>&2
56+
fi
57+
echo "check_service - status: $status" 1>&2
58+
return $status
59+
}
60+
export APP="http://127.0.0.1:8080"
61+
set +e
62+
exit_status=0
63+
create_service_output="$(check_service "Create" "curl -sSLf -X POST $APP/myCollection -H 'Content-Length: 0' )"
64+
create_service=$?
65+
66+
#get_service_output="$(check_service "Get" "curl -sSLf -X GET $APP/foo" )"
67+
#get_service=$?
68+
69+
delete_service_output="$(check_service "Delete" "curl -sSLf DELETE $APP/myCollection" )"
70+
delete_service=$?
71+
set -e
72+
73+
exit_status=$(($create_service + $delete_service))
74+
echo "exit status: $exit_status"
75+
echo "======================================================================================================"
76+
echo "Dumping logs using docker logs cnb-app-container"
77+
docker logs cnb-app-container 2>&1
78+
ls -lrt *.log
79+
exit $exit_status

setup-prerequisite.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
service_container_id=$(docker run -d -p $SERVICE_PORT:$SERVICE_PORT \
5+
-e MONGO_INITDB_ROOT_USERNAME="$SERVICE_USERNAME" \
6+
-e MONGO_INITDB_ROOT_PASSWORD="$SERVICE_PASSWORD" \
7+
-e MONGO_INITDB_DATABASE="$DATABASE_NAME" \
8+
--name "$SERVICE_NAME-service" \
9+
--health-cmd "$SERVICE_HEALTH_CMD" \
10+
--health-interval 10s --health-timeout 5s --health-retries 5 \
11+
${SERVICE_IMAGE})
12+
service_container_name="$(docker ps -f "ancestor=$SERVICE_IMAGE" --format "{{.Names}}")"
13+
SERVICE_CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$service_container_name")
14+
echo "$SERVICE_CONTAINER_IP: $SERVICE_CONTAINER_IP"
15+
export SERVICE_HOST=$SERVICE_CONTAINER_IP
16+
docker exec -i "$service_container_name" 'if mongosh --version;then echo "mongosh available";else echo "mongosh UNAVAILABLE";fi'
17+
echo "SERVICE_HOST: $SERVICE_HOST"

start-cnd-app-locally.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
for c in cnb-app-container mongodb-service;do
5+
if docker container stop $c;then
6+
docker container rm -f $c
7+
fi
8+
done
9+
10+
source local.env
11+
source ./setup-prerequisite.sh
12+
./run-tests.sh
13+
14+
15+
16+
#./vcap-services-template-reformat.sh >vcap-service.env
17+
#cat vcap-service.env
18+
#echo ""
19+
#echo "Starting app using ${CNB_IMAGE_NAME}"
20+
##docker run -it --rm -e PORT=8081 --env-file vcap-service.env -p 8080:8081 --name "cnb-app" ${CNB_IMAGE_NAME}
21+
#docker run --rm -e PORT=80 --env-file vcap-service.env -p 8080:80 --name "cnb-app" ${CNB_IMAGE_NAME}
22+
##docker run -d --rm -e PORT=8081 --env-file vcap-service.env -p 8080:8081 --name "cnb-app" ${CNB_IMAGE_NAME}

vcap-services-template-reformat.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
echo VCAP_SERVICES="$(cat vcap-services-template.json|jq -c '.'|envsubst)" > vcap-service.env
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
VCAP_SERVICES='
21
{
32
"mongodb-dedicated":[
43
{
54
"binding_guid":"${SERVICE_USERNAME}",
65
"binding_name":null,
76
"credentials":{
87
"database":"${DATABASE_NAME}",
9-
"hostname":"localhost:${SERVICE_PORT}",
8+
"hostname":"${SERVICE_HOST}:${SERVICE_PORT}",
109
"password":"${SERVICE_PASSWORD}",
11-
"uri":"mongodb://${SERVICE_USERNAME}:${SERVICE_PASSWORD}@localhost:${SERVICE_PORT}/${DATABASE_NAME}?replicaSet=rs0",
10+
"uri":"mongodb://${SERVICE_USERNAME}:${SERVICE_PASSWORD}@${SERVICE_HOST}:${SERVICE_PORT}/${DATABASE_NAME}?replicaSet=rs0",
1211
"username":"${SERVICE_USERNAME}"
1312
},
1413
"instance_guid":"cc252ef5-2541-49fd-8a57-fe73957905a5",
@@ -22,8 +21,7 @@ VCAP_SERVICES='
2221
"mongodb",
2322
"document"
2423
],
25-
"volume_mounts":[]
24+
"volume_mounts": [ ]
2625
}
2726
]
2827
}
29-
'

0 commit comments

Comments
 (0)