name: Internal - Tests for action

on:
  workflow_call:

permissions:
  contents: read

jobs:
  test-action-with-services:
    runs-on: ubuntu-latest
    name: Test with services
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Act
        uses: ./
        with:
          compose-file: "./test/docker-compose.yml"
          services: |
            service-b
            service-c

      - name: "Assert: only expected services are running"
        run: |
          docker compose -f ./test/docker-compose.yml ps

          docker compose -f ./test/docker-compose.yml ps | grep test-service-b-1 || (echo "Service service-b is not running" && exit 1)
          docker compose -f ./test/docker-compose.yml ps | grep test-service-c-1 || (echo "Service service-c is not running" && exit 1)
          (docker compose -f ./test/docker-compose.yml ps | grep test-service-a-1 && echo "Unexpected service service-a is running" && exit 1) || true

  test-action-with-down-flags:
    runs-on: ubuntu-latest
    name: Test compose action
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Act
        uses: ./
        with:
          compose-file: "./test/docker-compose.yml"
          down-flags: "--volumes"

  test-action-with-compose-flags:
    runs-on: ubuntu-latest
    name: Test with compose flags
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Act
        uses: ./
        with:
          compose-file: "./test/docker-compose.yml"
          compose-flags: "--profile profile-1"
          down-flags: "--volumes"

      - name: "Assert: profile is used"
        run: |
          docker compose -f ./test/docker-compose.yml -p profile-1 ps || (echo "Profile not used" && exit 1)

  test-action-with-env:
    runs-on: ubuntu-latest
    name: Test with env
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Act
        uses: ./
        with:
          compose-file: "./test/docker-compose-with-env.yml"
        env:
          IMAGE_NAME: busybox

      - name: "Assert: env is used"
        env:
          IMAGE_NAME: busybox
        run: |
          docker compose -f ./test/docker-compose-with-env.yml ps

          docker compose -f ./test/docker-compose-with-env.yml ps | grep test-service-a-1 || (echo "Service service-a is not running" && exit 1)

  test-action-with-multiple-compose-files:
    runs-on: ubuntu-latest
    name: Test with multiple compose files
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Act
        uses: ./
        with:
          compose-file: |
            ./test/docker-compose.yml
            ./test/docker-compose.ci.yml
          services: |
            service-b
            service-d

      - name: "Assert: only expected services are running"
        run: |
          docker compose -f ./test/docker-compose.yml -f ./test/docker-compose.ci.yml ps

          docker compose -f ./test/docker-compose.yml -f ./test/docker-compose.ci.yml ps | grep test-service-b-1 || (echo "Service service-b is not running" && exit 1)
          docker compose -f ./test/docker-compose.yml -f ./test/docker-compose.ci.yml ps | grep test-service-d-1 || (echo "Service service-d is not running" && exit 1)
          (docker compose -f ./test/docker-compose.yml -f ./test/docker-compose.ci.yml ps | grep test-service-a-1 && echo "Unexpected service service-a is running" && exit 1) || true
          (docker compose -f ./test/docker-compose.yml -f ./test/docker-compose.ci.yml ps | grep test-service-c-1 && echo "Unexpected service service-c is running" && exit 1) || true

  test-action-with-cwd:
    runs-on: ubuntu-latest
    name: Test with cwd
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Act
        uses: ./
        with:
          compose-file: "docker-compose.yml"
          cwd: "./test"
          services: |
            service-b
            service-c

      - name: "Assert: only expected services are running"
        run: |
          docker compose -f ./test/docker-compose.yml ps

          docker compose -f ./test/docker-compose.yml ps | grep test-service-b-1 || (echo "Service service-b is not running" && exit 1)
          docker compose -f ./test/docker-compose.yml ps | grep test-service-c-1 || (echo "Service service-c is not running" && exit 1)
          (docker compose -f ./test/docker-compose.yml ps | grep test-service-a-1 && echo "Unexpected service service-a is running" && exit 1) || true

  test-action-with-absolute-path:
    runs-on: ubuntu-latest
    name: Test with absolute path
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Act
        uses: ./
        with:
          compose-file: "${{ github.workspace }}/test/docker-compose.yml"
          services: |
            service-b
            service-c

      - name: "Assert: only expected services are running"
        run: |
          docker compose -f ./test/docker-compose.yml ps

          docker compose -f ./test/docker-compose.yml ps | grep test-service-b-1 || (echo "Service service-b is not running" && exit 1)
          docker compose -f ./test/docker-compose.yml ps | grep test-service-c-1 || (echo "Service service-c is not running" && exit 1)
          (docker compose -f ./test/docker-compose.yml ps | grep test-service-a-1 && echo "Unexpected service service-a is running" && exit 1) || true

  test-abort-on-container-exit:
    runs-on: ubuntu-latest
    name: Test with --abort-on-container-exit
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Act
        uses: ./
        with:
          compose-file: "test/docker-compose-web-mysql.yml"
          up-flags: "--build --abort-on-container-exit --exit-code-from=web"

  test-attach-dependencies-failure:
    runs-on: ubuntu-latest
    name: Test with --attach-dependencies and service failure
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Act
        uses: ./
        with:
          compose-file: "test/docker-compose-fail.yml"
          up-flags: "--attach-dependencies"

      - name: Assert
        run: |
          EXIT_CODE=$(docker compose -f ./test/docker-compose-fail.yml ps service-a --all --format json | jq ".ExitCode")
          [ "$EXIT_CODE" == "1" ] || (echo "Service service-a did not exit with code 1" && exit 1)

  test-action-with-compose-version:
    runs-on: ubuntu-latest
    name: Test with compose version
    env:
      DOCKER_COMPOSE_VERSION: "2.29.0"
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: "Arrange: ensure original docker compose version is not the expected one"
        run: |
          CURRENT_DOCKER_COMPOSE_VERSION=$(docker compose version --short)
          echo "Current docker compose version: $CURRENT_DOCKER_COMPOSE_VERSION"

          if [ "$CURRENT_DOCKER_COMPOSE_VERSION" == "$DOCKER_COMPOSE_VERSION" ]; then
            echo "Docker compose version is already in $DOCKER_COMPOSE_VERSION version"
            exit 1
          fi

      - name: Act
        uses: ./
        with:
          compose-file: "./test/docker-compose.yml"
          compose-version: "2.29.0"

      - name: "Assert: compose version is used"
        run: |
          CURRENT_DOCKER_COMPOSE_VERSION=$(docker compose version --short)
          echo "Current docker compose version: $CURRENT_DOCKER_COMPOSE_VERSION"

          if [ "$CURRENT_DOCKER_COMPOSE_VERSION" != "$DOCKER_COMPOSE_VERSION" ]; then
            echo "Docker compose version is not in $DOCKER_COMPOSE_VERSION version"
            exit 1
          fi

  test-action-with-compose-version-latest:
    runs-on: ubuntu-latest
    name: Test with compose version latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: "Arrange: retrieve latest version of docker compose"
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          script: |
            const dockerComposeVersion = (await github.rest.repos.getLatestRelease({
                owner: "docker",
                repo: "compose",
              })).data.tag_name.replace("v", "");

            core.exportVariable('DOCKER_COMPOSE_VERSION', dockerComposeVersion);

      - name: "Arrange: ensure original docker compose version is not the expected one"
        run: |
          CURRENT_DOCKER_COMPOSE_VERSION=$(docker compose version --short)
          echo "Current docker compose version: $CURRENT_DOCKER_COMPOSE_VERSION"

          if [ "$CURRENT_DOCKER_COMPOSE_VERSION" == "$DOCKER_COMPOSE_VERSION" ]; then
            echo "Docker compose version is already in $DOCKER_COMPOSE_VERSION version"
            exit 1
          fi

      - name: Act
        uses: ./
        with:
          compose-file: "./test/docker-compose.yml"
          compose-version: "latest"

      - name: "Assert: compose version is used"
        run: |
          CURRENT_DOCKER_COMPOSE_VERSION=$(docker compose version --short)
          echo "Current docker compose version: $CURRENT_DOCKER_COMPOSE_VERSION"

          if [ "$CURRENT_DOCKER_COMPOSE_VERSION" != "$DOCKER_COMPOSE_VERSION" ]; then
            echo "Docker compose version is not in $DOCKER_COMPOSE_VERSION version"
            exit 1
          fi

  test-action-with-docker-context:
    runs-on: ubuntu-latest
    name: Test with docker context
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Set up Docker
        uses: docker/setup-docker-action@b60f85385d03ac8acfca6d9996982511d8620a19 # v4.3.0
        with:
          context: test-context

      - name: Act
        uses: ./
        with:
          docker-flags: "--context test-context"
          compose-file: "./test/docker-compose.yml"
          compose-version: "latest"
