diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/CODEOWNERS b/CODEOWNERS new file mode 100644 index 0000000..2fc9036 --- /dev/null +++ b/CODEOWNERS @@ -0,0 +1 @@ +* @rahulpatidar0191 \ No newline at end of file diff --git a/README.md b/README.md index 3a9bab6..acd514f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ -# satel-webapp-testing -Centralized GitHub actions to test a webapp +# Satel Python Docker Tests +This centralized GitHub action runs tests on a python app using docker services + +## Usage +```yml +name: Run tests +on: + + + jobs: + code-validation: + # HOST-NAME is self-hosted or the name of server where the action runner is hosted, cosmicray for example + runs-on: + steps: + - name: Validate code + uses: SatelCreative/satel-local-python-tests@1.0.0 + with: + # APP-NAME can be st-pim or sb-pim for example + app-name: + registry: + clean-branch-name: + # WORK-DIR, where all the docker related files are located, optional field, default is root + work-dir: + fastapi-parameter: + container-name: + + +``` \ No newline at end of file diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..5a5d0a5 --- /dev/null +++ b/action.yml @@ -0,0 +1,54 @@ +name: Validate code using docker +description: Run docker services, test them and then remove them +inputs: + app-name: + description: Name of the Webapp + required: false + registry: + description: Provides current name of the registry + required: true + clean-branch-name: + description: Branch name that was pushed to registry + required: true + work-dir: + description: Location of all the docker files + required: false + fastapi-parameter: + description: Extra URL parameter for openapi + required: false + container-name: + description: Name of the docker container for webapp + required: false + default: webapp + +runs: + using: "composite" + steps: + - name: Docker Up + env: + APP_NAME: ${{ inputs.app-name }} + REGISTRY: ${{ inputs.registry }} + CLEAN_BRANCH_NAME: ${{ inputs.clean-branch-name }} + WORK_DIR: ${{ inputs.work-dir }} + run: ${{ github.action_path }}/scripts/DockerUp.sh + shell: bash + + - name: Run docker tests + env: + APP_NAME: ${{ inputs.app-name }} + CLEAN_BRANCH_NAME: ${{ inputs.clean-branch-name }} + WORK_DIR: ${{ inputs.work-dir }} + FAST_PARA: ${{ inputs.fastapi-parameter }} + CONTAINER_NAME: ${{ inputs.container-name }} + run: ${{ github.action_path }}/scripts/ValidateCode.sh + shell: bash + + - name: Docker Down + if: ${{ always() }} + env: + APP_NAME: ${{ inputs.app-name }} + REGISTRY: ${{ inputs.registry }} + CLEAN_BRANCH_NAME: ${{ inputs.clean-branch-name }} + WORK_DIR: ${{ inputs.work-dir }} + run: ${{ github.action_path }}/scripts/DockerDown.sh + shell: bash diff --git a/scripts/DockerDown.sh b/scripts/DockerDown.sh new file mode 100755 index 0000000..5bfab20 --- /dev/null +++ b/scripts/DockerDown.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# Set registry and clean branch name as global variables +export REGISTRY=$REGISTRY +export CLEAN_BRANCH_NAME=$CLEAN_BRANCH_NAME + +if [[ -n $WORK_DIR ]] +then + echo "WORK_DIR ${WORK_DIR}" + cd $WORK_DIR +fi + +echo "Docker down" +docker-compose -f docker-compose.yml -f docker-compose.pipeline.yml down \ No newline at end of file diff --git a/scripts/DockerUp.sh b/scripts/DockerUp.sh new file mode 100755 index 0000000..36bf3be --- /dev/null +++ b/scripts/DockerUp.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# Set registry and clean branch name as global variables +export REGISTRY=$REGISTRY +export CLEAN_BRANCH_NAME=$CLEAN_BRANCH_NAME + +if [[ -n $WORK_DIR ]] +then + echo "WORK_DIR ${WORK_DIR}" + cd $WORK_DIR +fi +echo "Dev image check" +IMG_STR=`cat docker-compose.override.yml | grep 'devenv'| cut -d ":" -f 2-3` +IMG_LIST=( $IMG_STR ) #convert string into an array +for IMG in "${IMG_LIST[@]}" +do + DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect ${IMG} > /dev/null || exit 1 # Check to see if the dev image is on registry or not +done + +echo "Docker up" +docker-compose -f docker-compose.yml -f docker-compose.pipeline.yml up -d \ No newline at end of file diff --git a/scripts/ValidateCode.sh b/scripts/ValidateCode.sh new file mode 100755 index 0000000..f0446c9 --- /dev/null +++ b/scripts/ValidateCode.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +if [[ -n $WORK_DIR ]] +then + echo "WORK_DIR ${WORK_DIR}" + cd $WORK_DIR +fi + +echo "App health check" # Check to see if the app container is running or not +sleep 5 +docker-compose exec -T ${CONTAINER_NAME} python -c "import requests; requests.get('http://localhost:8000/health')" || exit 1 + +echo "Openapi link" # copy openapi files to samba mount +docker-compose exec -T ${CONTAINER_NAME} python -c "import requests; f=open('${CLEAN_BRANCH_NAME}_openapi.json','w',encoding = 'utf-8'); f.write(requests.get('http://localhost:8000${FAST_PARA}/openapi.json').text);f.close()" +[ -d "/mnt/samba/${APP_NAME}" ] || mkdir -p "/mnt/samba/${APP_NAME}" +docker cp "$(docker-compose ps -q ${CONTAINER_NAME})":"/python/app/${CLEAN_BRANCH_NAME}_openapi.json" "/mnt/samba/${APP_NAME}/${CLEAN_BRANCH_NAME}_openapi.json" + +echo "Clean up old reports" +rm -f unittesting.xml coverage.xml typing.xml typing-server.xml typing-integrations.xml + +echo "Code tests" +## Catch the exit codes so we don't exit the whole script before we are done. +## Typing, linting, formatting check & unit and integration testing +if [[ $CONTAINER_NAME != "webapp" ]]; then + echo "flake8 tests" #TODO: remove the if else block once the errors on sb-pim are fixed + docker-compose exec -T ${CONTAINER_NAME} flake8; STATUS1=$? # For Sb-pim only + +else + docker-compose exec -T ${CONTAINER_NAME} validatecodeonce; STATUS1=$? + docker cp "$(docker-compose ps -q ${CONTAINER_NAME})":/python/reports/typing.xml typing.xml + docker cp "$(docker-compose ps -q ${CONTAINER_NAME})":/python/reports/unittesting.xml unittesting.xml + docker cp "$(docker-compose ps -q ${CONTAINER_NAME})":/python/reports/coverage.xml coverage.xml +fi + +## Return the status code +TOTAL=$((STATUS1)) +exit $TOTAL