name: DockerTests

on: [push, pull_request]

jobs:
  cache:
    name: Caching
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [ 3.9 ]

    steps:
      - uses: actions/checkout@v3
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      - name: Get pip cache dir
        id: pip-cache-dir
        run: |
          echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
      - name: pip cache
        id: pip-cache
        uses: actions/cache@v3
        with:
          path: ${{ steps.pip-cache-dir.outputs.dir }}
          key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}
      - name: Update pip
        if: ${{ steps.pip-cache.outputs.cache-hit != 'true' }}
        run: |
          python -m pip install --upgrade pip
      - name: Install project dependencies
        if: ${{ steps.pip-cache.outputs.cache-hit != 'true' }}
        run: |
          pip install -r requirements-dev.txt

  test_custom_port:
    name: Test Custom Port
    runs-on: ubuntu-latest
    needs: cache
    strategy:
      matrix:
        python-version: [3.9]

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
    - name: Start MotoServer on an unusual port
      run: |
        python setup.py sdist
        docker run --rm -t --name motoserver -e TEST_SERVER_MODE=true -e AWS_SECRET_ACCESS_KEY=server_secret -e MOTO_PORT=4555 -e AWS_ACCESS_KEY_ID=server_key -v `pwd`:/moto -p 4555:4555 -v /var/run/docker.sock:/var/run/docker.sock python:3.7-buster /moto/scripts/ci_moto_server.sh &
        MOTO_PORT=4555 python scripts/ci_wait_for_server.py
    - name: Get pip cache dir
      id: pip-cache
      run: |
        echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
    - name: pip cache
      uses: actions/cache@v3
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}
    - name: Update pip
      run: |
        python -m pip install --upgrade pip
    - name: Install project dependencies
      run: |
        pip install -r requirements-dev.txt
    - name: Test
      env:
        TEST_SERVER_MODE: ${{ true }}
        MOTO_PORT: 4555
      run: |
        pytest -sv tests/test_awslambda/test_lambda_invoke.py::test_invoke_lambda_using_environment_port tests/test_cloudformation/test_cloudformation_custom_resources.py
    - name: Collect Logs
      if: always()
      run: |
        mkdir serverlogs1
        pwd
        ls -la
        cp server_output.log serverlogs1/server_output.log
        docker stop motoserver
    - name: Archive Logs
      if: always()
      uses: actions/upload-artifact@v3
      with:
        name: motoserver-${{ matrix.python-version }}
        path: |
          serverlogs1/*

  test_custom_name:
    name: Test Custom Network Name
    runs-on: ubuntu-latest
    needs: cache
    strategy:
      matrix:
        python-version: [3.9]

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
    - name: Start MotoServer on a custom Docker network bridge
      run: |
        python setup.py sdist
        docker network create -d bridge my-custom-network
        docker run --rm -t -e TEST_SERVER_MODE=true -e MOTO_DOCKER_NETWORK_NAME=my-custom-network -e AWS_SECRET_ACCESS_KEY=server_secret -e AWS_ACCESS_KEY_ID=server_key -v `pwd`:/moto -p 5000:5000 --network my-custom-network -v /var/run/docker.sock:/var/run/docker.sock python:3.7-buster /moto/scripts/ci_moto_server.sh &
        python scripts/ci_wait_for_server.py
    - name: Get pip cache dir
      id: pip-cache
      run: |
        echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
    - name: pip cache
      uses: actions/cache@v3
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}
    - name: Update pip
      run: |
        python -m pip install --upgrade pip
    - name: Install project dependencies
      run: |
        pip install -r requirements-dev.txt
    - name: Test
      env:
        TEST_SERVER_MODE: ${{ true }}
      run: |
        pytest -sv tests/test_awslambda/test_lambda_invoke.py::test_invoke_lambda_using_environment_port tests/test_cloudformation/test_cloudformation_custom_resources.py
    - name: Collect Logs
      if: always()
      run: |
        mkdir serverlogs2
        pwd
        ls -la
        cp server_output.log serverlogs2/server_output.log
    - name: Archive logs
      if: always()
      uses: actions/upload-artifact@v3
      with:
        name: motoserver-${{ matrix.python-version }}
        path: |
          serverlogs2/*

  test_custom_networkmode:
    name: Pass NetworkMode to AWSLambda
    runs-on: ubuntu-latest
    needs: cache
    strategy:
      matrix:
        python-version: [3.9]

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
    - name: Start MotoServer on an unusual port
      run: |
        python setup.py sdist
        docker run --rm -t -e MOTO_DOCKER_NETWORK_MODE=host -e TEST_SERVER_MODE=true -e AWS_SECRET_ACCESS_KEY=server_secret -e MOTO_PORT=4555 -e AWS_ACCESS_KEY_ID=server_key -v `pwd`:/moto -p 4555:4555 -v /var/run/docker.sock:/var/run/docker.sock python:3.7-buster /moto/scripts/ci_moto_server.sh &
        MOTO_PORT=4555 python scripts/ci_wait_for_server.py
    - name: Get pip cache dir
      id: pip-cache
      run: |
        echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
    - name: pip cache
      uses: actions/cache@v3
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}
    - name: Update pip
      run: |
        python -m pip install --upgrade pip
    - name: Install project dependencies
      run: |
        pip install -r requirements-dev.txt
    - name: Test
      env:
        TEST_SERVER_MODE: ${{ true }}
        MOTO_PORT: 4555
        MOTO_DOCKER_NETWORK_MODE: host
      run: |
        pytest -sv tests/test_awslambda/test_lambda_invoke.py::test_invoke_lambda_using_networkmode tests/test_cloudformation/test_cloudformation_custom_resources.py
    - name: Collect Logs
      if: always()
      run: |
        mkdir serverlogs3
        pwd
        ls -la
        cp server_output.log serverlogs3/server_output.log
    - name: Archive Logs
      if: always()
      uses: actions/upload-artifact@v3
      with:
        name: motoserver-${{ matrix.python-version }}
        path: |
          serverlogs3/*
