# Test our docker images are built with the most recent version
# The main purpose of this test is to check the image is working
# and the latest tag points to an image with the most recent
# release.
name: "Packaging tests: Docker images"
"on":
  schedule:
    # run daily 0:00 on main branch
    - cron: '0 0 * * *'
  pull_request:
    paths: .github/workflows/docker-images.yaml
  push:
    tags:
    - '*'
    branches:
    - release_test
    - trigger/package_test
  workflow_dispatch:
jobs:
  docker_tests:
    name: ${{ matrix.image }}
    runs-on: ubuntu-latest
    services:
      ts:
        image: timescale/${{ matrix.image }}
        ports:
          - 5432:5432
        env:
          POSTGRES_HOST_AUTH_METHOD: trust
          POSTGRESQL_PASSWORD: ci

    env:
      PGHOST: localhost
      PGUSER: postgres
      PGPASSWORD: ci

    strategy:
      fail-fast: false
      matrix:
        image: [
            "timescaledb:latest-pg15",
            "timescaledb:latest-pg16",
            "timescaledb:latest-pg17",
            "timescaledb:latest-pg18",
            "timescaledb-ha:pg15",
            "timescaledb-ha:pg16",
            "timescaledb-ha:pg17",
            "timescaledb-ha:pg18",
          ]

    steps:
    - uses: actions/checkout@v4

    - name: Get version of latest release
      id: versions
      env:
        GH_TOKEN: ${{ github.token }}
      run: |
        version=$(gh release list --json tagName,isLatest --jq '.[] | select(.isLatest) | .tagName')
        echo "version=${version}" >>$GITHUB_OUTPUT

    - name: Wait for services to start
      run: |
        sleep 10
        pg_isready -t 30

    - name: Check version
      run: |
        psql -c "SELECT extname,extversion,version() FROM pg_extension WHERE extname='timescaledb'"
        installed_version=$(psql -X -t \
          -c "SELECT extversion FROM pg_extension WHERE extname='timescaledb';" | sed -e 's! !!g')
        if [ "${{ steps.versions.outputs.version }}" != "$installed_version" ];then
          false
        fi

    - name: Create hypertable
      run: |
        psql -c "$(cat <<SQL
          CREATE TABLE metrics(time timestamptz, device text, metric text, value float);
          SELECT create_hypertable('metrics','time');
        SQL
        )"

