Skip to content

Commit

Permalink
Add minimal containers and ports clean up before test (#1291)
Browse files Browse the repository at this point in the history
Signed-off-by: chensuyue <[email protected]>
  • Loading branch information
chensuyue authored Dec 26, 2024
1 parent 0b23cba commit 6b6a08d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
28 changes: 16 additions & 12 deletions .github/workflows/_run-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ jobs:
ref: ${{ needs.get-test-case.outputs.CHECKOUT_REF }}
fetch-depth: 0

- name: Clean up container before test
shell: bash
run: |
docker ps
cd ${{ github.workspace }}/${{ inputs.example }}
export test_case=${{ matrix.test_case }}
export hardware=${{ inputs.hardware }}
bash ${{ github.workspace }}/.github/workflows/scripts/docker_compose_clean_up.sh "containers"
bash ${{ github.workspace }}/.github/workflows/scripts/docker_compose_clean_up.sh "ports"
docker ps
- name: Run test
shell: bash
env:
Expand All @@ -131,21 +142,14 @@ jobs:
if [[ "$IMAGE_REPO" == "" ]]; then export IMAGE_REPO="${OPEA_IMAGE_REPO}opea"; fi
if [ -f ${test_case} ]; then timeout 30m bash ${test_case}; else echo "Test script {${test_case}} not found, skip test!"; fi
- name: Clean up container
- name: Clean up container after test
shell: bash
if: cancelled() || failure()
run: |
cd ${{ github.workspace }}/${{ inputs.example }}/docker_compose
test_case=${{ matrix.test_case }}
flag=${test_case%_on_*}
flag=${flag#test_}
yaml_file=$(find . -type f -wholename "*${{ inputs.hardware }}/${flag}.yaml")
echo $yaml_file
container_list=$(cat $yaml_file | grep container_name | cut -d':' -f2)
for container_name in $container_list; do
cid=$(docker ps -aq --filter "name=$container_name")
if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi
done
cd ${{ github.workspace }}/${{ inputs.example }}
export test_case=${{ matrix.test_case }}
export hardware=${{ inputs.hardware }}
bash ${{ github.workspace }}/.github/workflows/scripts/docker_compose_clean_up.sh "containers"
docker system prune -f
docker rmi $(docker images --filter reference="*:5000/*/*" -q) || true
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/manual-docker-clean.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
clean_list:
default: ""
description: "docker command to clean"
required: true
required: false
type: string

jobs:
Expand All @@ -24,6 +24,8 @@ jobs:
run: |
docker ps
if [ "${{ inputs.clean_list }}" ]; then
echo "----------stop and remove containers----------"
docker stop ${{ inputs.clean_list }} && docker rm ${{ inputs.clean_list }}
echo "----------container removed----------"
docker ps
fi
1 change: 1 addition & 0 deletions .github/workflows/push-image-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
- "**.py"
- "**Dockerfile*"
- "**docker_image_build/build.yaml"
- "**/ui/**"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-on-push
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/scripts/docker_compose_clean_up.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

# The test machine used by several opea projects, so the test scripts can't use `docker compose down` to clean up
# the all the containers, ports and networks directly.
# So we need to use the following script to minimize the impact of the clean up.

test_case=${test_case:-"test_compose_on_gaudi.sh"}
hardware=${hardware:-"gaudi"}
flag=${test_case%_on_*}
flag=${flag#test_}
yaml_file=$(find . -type f -wholename "*${hardware}/${flag}.yaml")
echo $yaml_file

case "$1" in
containers)
echo "Stop and remove all containers used by the services in $yaml_file ..."
containers=$(cat $yaml_file | grep container_name | cut -d':' -f2)
for container_name in $containers; do
cid=$(docker ps -aq --filter "name=$container_name")
if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi
done
;;
ports)
echo "Release all ports used by the services in $yaml_file ..."
pip install jq yq
ports=$(yq '.services[].ports[] | split(":")[0]' $yaml_file | grep -o '[0-9a-zA-Z_-]\+')
echo "$ports"
for port in $ports; do
if [[ $port =~ [a-zA-Z_-] ]]; then
port=$(grep -E "export $port=" tests/$test_case | cut -d'=' -f2)
fi
echo $port
cid=$(docker ps --filter "publish=${port}" --format "{{.ID}}")
if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi
done
;;
*)
echo "Unknown function: $1"
;;
esac
1 change: 0 additions & 1 deletion CodeGen/tests/test_compose_on_rocm.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash

# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

Expand Down

0 comments on commit 6b6a08d

Please sign in to comment.