name: Component testing
on: [push, workflow_dispatch]

jobs:
  hub-test:
    name: Hub testing
    runs-on: host-arch-x86_64
    steps:
      - name: Clone repository
        run: git clone --revision ${{ gitea.sha }} --depth 1 ${{ gitea.server_url }}/${{ gitea.repository }}

      - name: Build Hub testing container
        working-directory: IoT-Systems
        run: docker build -t local/hub/${{gitea.sha}} -f hub/Dockerfile-test .

      - name: Run Hub tests
        working-directory: IoT-Systems
        run: docker run --rm -it local/hub/${{gitea.sha}}

      - name: Clean up containers
        if: ${{always()}}
        run: docker image rm local/hub/${{gitea.sha}}

  store-test:
    name: Store testing
    runs-on: host-arch-x86_64
    steps:
      - name: Clone repository
        run: git clone --revision ${{ gitea.sha }} --depth 1 ${{ gitea.server_url }}/${{ gitea.repository }}

      - name: Start postgres_db container for testing
        working-directory: IoT-Systems
        run: docker-compose up -d postgres_db

      - name: Build Store testing container
        working-directory: IoT-Systems
        run: docker build -t local/store/${{gitea.sha}} -f store/Dockerfile-test .

      - name: Run Store tests
        working-directory: IoT-Systems
        run: docker run --network host --rm -it local/store/${{gitea.sha}}

      - name: Clean up containers
        if: ${{always()}}
        run: docker image rm local/store/${{gitea.sha}}

      - name: Clean up docker-compose
        if: ${{always()}}
        working-directory: IoT-Systems
        run: docker-compose down -v --remove-orphans

  integration-smoke-test:
    name: Integration smoke testing
    runs-on: host-arch-x86_64
    needs:
      - hub-test
      - store-test
    steps:
      - name: Clone repository
        run: git clone --revision ${{ gitea.sha }} --depth 1 ${{ gitea.server_url }}/${{ gitea.repository }}

      - name: Build all production containers
        working-directory: IoT-Systems
        run: docker-compose build

      - name: Start all production containers
        working-directory: IoT-Systems
        run: docker-compose up -d

      - name: Wait for crashes to happen
        run: sleep 30

      - name: Check for dead containers
        working-directory: IoT-Systems
        run: docker ps -a | python3 utils/check-up.py

      - name: Clean up
        if: ${{always()}}
        working-directory: IoT-Systems
        run: docker-compose down -v
