Skip to content

Commit

Permalink
fixup!test: rework to share principles with redis sample
Browse files Browse the repository at this point in the history
  • Loading branch information
o-orand committed Oct 2, 2024
1 parent b986ee9 commit fdafd0d
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 52 deletions.
63 changes: 16 additions & 47 deletions .github/workflows/build-using-cnb-buildpack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ on:
- "v?[0-9]+.[0-9]+.[0-9]+*"
env:
CNB_IMAGE_NAME: cnb-app
SERVICE_NAME: 'mongodb'
SERVICE_VERSION: '6.0'
SERVICE_IMAGE: "mongo:7.0.14"
SERVICE_PORT: 27017 # Default value: 27017
SERVICE_USERNAME: '22032e25-4aba-417f-a394-8bbd78d920cd'
SERVICE_PASSWORD: 'StbQ4EovUpwQjD0cT1Hr7PKBG'
DATABASE_NAME: my-mongo-db
DEBUG: 0

jobs:
package-app:
Expand All @@ -40,65 +43,31 @@ jobs:
-
name: Setup pack for ${{ github.repository }}
uses: buildpacks/github-actions/[email protected]
-
name: Start MongoDB testing server
uses: supercharge/[email protected]
with:
mongodb-version: ${{ env.SERVICE_VERSION }}
mongodb-port: ${{env.SERVICE_PORT}}
mongodb-username: ${{env.SERVICE_USERNAME}}
mongodb-password: ${{env.SERVICE_PASSWORD}}
mongodb-db: ${{env.DATABASE_NAME}}
# mongodb-container-name: mongodb-${{ env.SERVICE_VERSION }}
# -
# name: Start MongoDB testing server
# uses: supercharge/[email protected]
# with:
# mongodb-version: ${{ env.SERVICE_VERSION }}
# mongodb-port: ${{env.SERVICE_PORT}}
# mongodb-username: ${{env.SERVICE_USERNAME}}
# mongodb-password: ${{env.SERVICE_PASSWORD}}
# mongodb-db: ${{env.DATABASE_NAME}}
## mongodb-container-name: mongodb-${{ env.SERVICE_VERSION }}

-
name: Packaging app from ${{ github.repository }} pushed by ${{ github.actor }}
shell: bash
run: |
echo "Pack Build ${{env.CNB_IMAGE_NAME}} (${{github.repository}})"
pack build ${{env.CNB_IMAGE_NAME}} --builder paketobuildpacks/builder-jammy-base
pack build ${{env.CNB_IMAGE_NAME}}
echo "Pack Inspect ${{env.CNB_IMAGE_NAME}} (${{github.repository}})"
pack inspect ${{env.CNB_IMAGE_NAME}}
-
name: Testing app
shell: bash
run: |
cat vcap-services-template.sh|envsubst >vcap-service.env
cat vcap-service.env
echo "Starting sample app"
container_id=$(docker run -d -e PORT=80 --env-file vcap-service.env -p 8080:80 --name "cnb-app-container" ${{env.CNB_IMAGE_NAME}})
echo "Cnb app started (id: $container_id)"
docker inspect -f '{{.HostConfig.LogConfig.Type}}' $container_id
# docker run -e PORT=80 --env-file vcap-service.env -p 8080:80 --name "cnb-app-container" ${{env.CNB_IMAGE_NAME}}
docker logs -f cnb-app-container &> cnb-app-container.log &
docker logs mongodb
cat /etc/hosts
docker ps -a
env|sort
export APP="http://127.0.0.1:8080"
exit_status=0
if nc -vz 127.0.0.1 8080;then echo "port 8080 available";else echo "port 8080 UNAVAILABLE";exit_status=1;fi
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
if mongosh --version;then echo "mongosh available";else echo "mongosh UNAVAILABLE";fi
echo "Create using 'curl -X POST $APP/myCollection'"
if ! curl -vvv -X POST $APP/myCollection -H 'Content-Length: 0';then
echo "Create failed"
exit_status=1
else
echo "Create successful"
fi
echo "Delete using 'curl -X DELETE $APP/myCollection'"
if ! curl -vvv -X DELETE $APP/myCollection;then
echo "Delete failed"
exit_status=1
else
echo "Delete successful"
fi
cat cnb-app-container.log
echo "Dumping logs using docker logs cnb-app-container"
docker logs cnb-app-container
ls -lrt
exit $exit_status
./setup-prerequisite.sh
./run-tests.sh
-
name: Tag version ${{github.ref_name}}
if: github.ref_type == 'tag'
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/vendor
/tmp
.idea/
*.log
*.iml
/vcap-service.env
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ GEM
nenv (~> 0.1)
shellany (~> 0.0)
openssl (3.0.2)
ostruct (0.6.0)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -100,6 +101,7 @@ DEPENDENCIES
guard
guard-rspec
mongo
ostruct (~> 0.6.0)
rack-test
rspec
sinatra
Expand Down
9 changes: 9 additions & 0 deletions local.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export CNB_IMAGE_NAME=mongodb-cnb-app
export SERVICE_NAME=mongodb
export SERVICE_HEALTH_CMD=mongo
export SERVICE_PORT=27001
export DATABASE_NAME=myCollection
export SERVICE_IMAGE="mongo:7.0.14"
export SERVICE_HOST="dynamically_generated"
export SERVICE_PASSWORD=my-passwwwwwwwword15;
#export DEBUG=0
13 changes: 13 additions & 0 deletions project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[_]
schema-version = "0.2"
id = "com.orange-cloudfoundry.sample-apps.mongodb"
name = "MongoDB sample app"
#version = "1.0.0"

[io.buildpacks]
builder = "paketobuildpacks/builder-jammy-base"
exclude = [
".github",
"renovate.json",
"README.md"
]
79 changes: 79 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env bash
set -e

./vcap-services-template-reformat.sh
cat vcap-service.env
echo "Starting sample app"
container_name="cnb-app-container"
container_id=$(docker run -d --rm -e PORT=80 -p 8080:80 --env-file vcap-service.env --name "$container_name" ${CNB_IMAGE_NAME})

echo "Cnb app started (id: $container_id)"
echo "Waiting to ensure app is up and running"
while [ "$( docker container inspect -f '{{.State.Status}}' $container_name )" != "running" ]; do
echo "waiting for $container_name to be running current: $(docker inspect -f '{{.State.Status}}' $container_name)"
sleep 1
done
sleep 3 # to ensure app is up and running
#docker inspect -f '{{.HostConfig.LogConfig.Type}}' $container_id
service_container_name="$(docker ps -f "ancestor=$SERVICE_IMAGE" --format "{{.Names}}")"
CONTAINER_APP_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container_name)
echo "CONTAINER_APP_IP: $CONTAINER_APP_IP"

if [ "$DEBUG" -eq 1 ]; then
echo "----------------- $container_name --------------------"
docker inspect -f '{{json .NetworkSettings}}' $container_name

echo "----------------- $SERVICE_NAME: $service_container_name --------------------"
docker inspect -f '{{json .NetworkSettings}}' "$service_container_name"
fi
echo "=== Redirect logs to cnb-app-container.log ==="
docker logs -f cnb-app-container &> cnb-app-container.log &
docker ps -a

echo "=== Check connectivity ==="
if nc -vz 127.0.0.1 8080;then echo "port 8080 available";else echo "port 8080 UNAVAILABLE";exit_status=1;fi
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
#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)"
#echo "Checking redis server $SERVICE_HOST : PING ==> $(redis-cli -a ${SERVICE_PASSWORD} -h ${SERVICE_HOST} -p ${SERVICE_PORT} --no-auth-warning ping)"

function check_service() {
type="$1"
cmd="$2"
cmd_prefix="$3"
if [ -z "$cmd_prefix" ];then
cmd_prefix="curl"
fi
status=0
echo "$type using > $cmd <" 1>&2
if ! $cmd;then
echo "" 1>&2
echo "$type failed: retry in verbose mode" 1>&2
$cmd_prefix -vvv ${cmd##$cmd_prefix}
status=1
else
echo "" 1>&2
echo "$type successful" 1>&2
fi
echo "check_service - status: $status" 1>&2
return $status
}
export APP="http://127.0.0.1:8080"
set +e
exit_status=0
create_service_output="$(check_service "Create" "curl -sSLf -X POST $APP/myCollection -H 'Content-Length: 0' )"
create_service=$?
#get_service_output="$(check_service "Get" "curl -sSLf -X GET $APP/foo" )"
#get_service=$?
delete_service_output="$(check_service "Delete" "curl -sSLf DELETE $APP/myCollection" )"
delete_service=$?
set -e
exit_status=$(($create_service + $delete_service))
echo "exit status: $exit_status"
echo "======================================================================================================"
echo "Dumping logs using docker logs cnb-app-container"
docker logs cnb-app-container 2>&1
ls -lrt *.log
exit $exit_status
17 changes: 17 additions & 0 deletions setup-prerequisite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -e

service_container_id=$(docker run -d -p $SERVICE_PORT:$SERVICE_PORT \
-e MONGO_INITDB_ROOT_USERNAME="$SERVICE_USERNAME" \
-e MONGO_INITDB_ROOT_PASSWORD="$SERVICE_PASSWORD" \
-e MONGO_INITDB_DATABASE="$DATABASE_NAME" \
--name "$SERVICE_NAME-service" \
--health-cmd "$SERVICE_HEALTH_CMD" \
--health-interval 10s --health-timeout 5s --health-retries 5 \
${SERVICE_IMAGE})
service_container_name="$(docker ps -f "ancestor=$SERVICE_IMAGE" --format "{{.Names}}")"
SERVICE_CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$service_container_name")
echo "$SERVICE_CONTAINER_IP: $SERVICE_CONTAINER_IP"
export SERVICE_HOST=$SERVICE_CONTAINER_IP
docker exec -i "$service_container_name" 'if mongosh --version;then echo "mongosh available";else echo "mongosh UNAVAILABLE";fi'
echo "SERVICE_HOST: $SERVICE_HOST"
22 changes: 22 additions & 0 deletions start-cnd-app-locally.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -e

for c in cnb-app-container mongodb-service;do
if docker container stop $c;then
docker container rm -f $c
fi
done

source local.env
source ./setup-prerequisite.sh
./run-tests.sh



#./vcap-services-template-reformat.sh >vcap-service.env
#cat vcap-service.env
#echo ""
#echo "Starting app using ${CNB_IMAGE_NAME}"
##docker run -it --rm -e PORT=8081 --env-file vcap-service.env -p 8080:8081 --name "cnb-app" ${CNB_IMAGE_NAME}
#docker run --rm -e PORT=80 --env-file vcap-service.env -p 8080:80 --name "cnb-app" ${CNB_IMAGE_NAME}
##docker run -d --rm -e PORT=8081 --env-file vcap-service.env -p 8080:8081 --name "cnb-app" ${CNB_IMAGE_NAME}
1 change: 1 addition & 0 deletions vcap-services-template-reformat.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo VCAP_SERVICES="$(cat vcap-services-template.json|jq -c '.'|envsubst)" > vcap-service.env
8 changes: 3 additions & 5 deletions vcap-services-template.sh → vcap-services-template.json
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
VCAP_SERVICES='
{
"mongodb-dedicated":[
{
"binding_guid":"${SERVICE_USERNAME}",
"binding_name":null,
"credentials":{
"database":"${DATABASE_NAME}",
"hostname":"localhost:${SERVICE_PORT}",
"hostname":"${SERVICE_HOST}:${SERVICE_PORT}",
"password":"${SERVICE_PASSWORD}",
"uri":"mongodb://${SERVICE_USERNAME}:${SERVICE_PASSWORD}@localhost:${SERVICE_PORT}/${DATABASE_NAME}?replicaSet=rs0",
"uri":"mongodb://${SERVICE_USERNAME}:${SERVICE_PASSWORD}@${SERVICE_HOST}:${SERVICE_PORT}/${DATABASE_NAME}?replicaSet=rs0",
"username":"${SERVICE_USERNAME}"
},
"instance_guid":"cc252ef5-2541-49fd-8a57-fe73957905a5",
Expand All @@ -22,8 +21,7 @@ VCAP_SERVICES='
"mongodb",
"document"
],
"volume_mounts":[]
"volume_mounts": [ ]
}
]
}
'

0 comments on commit fdafd0d

Please sign in to comment.