4

I am working through the Testdriven.io: Test-Driven Development with FastAPI and Docker course and am currently on the continuous integration section. In this section you use github actions to build your docker image and run tests and linting and what not.

During the Test Docker Image step of the flow I am receiving the following error when pytest is attempted: Error: Process completed with exit code 137

enter image description here

Workflow main.yml:

name: Continuous Integration and Delivery

on: [ push ]

env:
  IMAGE: docker.pkg.github.com/$(echo $GITHUB_REPOSITORY | tr '[A-Z]' '[a-z]')/summarizer

jobs:

  build:
    name: Build Docker Image
    runs-on: ubuntu-latest
    steps:
      - name: Checkout master
        uses: actions/checkout@v2.3.4
      - name: Log in to GitHub Packages
        run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Pull image
        run: |
          docker pull ${{ env.IMAGE }}:latest || true
      - name: Build image
        run: |
          docker build \
            --cache-from ${{ env.IMAGE }}:latest \
            --tag ${{ env.IMAGE }}:latest \
            --file ./project/Dockerfile.prod \
            "./project"
      - name: Push image
        run: |
          docker push ${{ env.IMAGE }}:latest

  test:
    name: Test Docker Image
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Checkout master
        uses: actions/checkout@v2.3.4
      - name: Log in to GitHub Packages
        run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Pull image
        run: |
          docker pull ${{ env.IMAGE }}:latest || true
      - name: Build image
        run: |
          docker build \
            --cache-from ${{ env.IMAGE }}:latest \
            --tag ${{ env.IMAGE }}:latest \
            --file ./project/Dockerfile.prod \
            "./project"
      - name: Run container
        run: |
          docker run \
            -d \
            --name fastapi-tdd \
            -e PORT=8765 \
            -e ENVIRONMENT=dev \
            -e DATABASE_TEST_URL=sqlite://sqlite.db \
            -p 5003:8765 \
            ${{ env.IMAGE }}:latest
      - name: Pytest
        run: docker exec fastapi-tdd python -m pytest tests
      - name: Flake8
        run: docker exec fastapi-tdd python -m flake8 .
      - name: Black
        run: docker exec fastapi-tdd python -m black . --check
      - name: isort
        run: docker exec fastapi-tdd python -m isort . --check-only

Running pytest against the docker container locally works. There doesn't appear to be any other logs that I can access that would shed more light onto why this is happening. Any help would be appreciated.

spetz83
  • 494
  • 6
  • 21
  • 1
    It may be related to the OS memory usage: _Exit code 137 means that your process was killed by (signal 9) SIGKILL in the case you manually stopped it. If you didn't manually stop the script and still got this error code, then the script was killed by your OS. In most of the cases, it is caused by excessive memory usage_. Reference: https://stackoverflow.com/questions/43268156/process-finished-with-exit-code-137-in-pycharm – GuiFalourd Oct 24 '21 at 20:24
  • Interesting... It dies immediately after trying to execute pytest too... I would have thought that Github actions would be able to handle that, especially since it is literally like 5 small tests for this tutorial. That leads me to believe there is something very wrong with the command or something... – spetz83 Oct 24 '21 at 21:21
  • Did you try using another docker image on the workflow to check if something may be wrong in the build job that is used on the test job afterwards? – GuiFalourd Oct 24 '21 at 23:59

1 Answers1

2

Add the DATABASE_URL environment variable in the docker run command.

Refer to this issue: https://github.com/testdrivenio/fastapi-tdd-docker/issues/1

Here is how I fixed it for me:

- name: Run container
    run: |
      docker run \
        -d \
        --name fastapi-tdd \
        -e PORT=8765 \
        -e ENVIRONMENT=dev \
        -e DATABASE_URL=sqlite://sqlite.db \
        -e DATABASE_TEST_URL=sqlite://sqlite.db \
        -p 5003:8765 \
        ${{ env.IMAGE }}:latest
Zaffer
  • 1,290
  • 13
  • 32