name: Rust

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      minio:
        image: fclairamb/minio-github-actions
        ports:
          - 9000:9000
        env:
          MINIO_DOMAIN: localhost
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: rFcLGNkgsNtksg6Pgtn9CumL4xXBQ7
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432
      colonies:
        image: colonyos/colonies:v1.9.6
        env:
          LANG: en_US.UTF-8
          LANGUAGE: en_US.UTF-8
          LC_ALL: en_US.UTF-8
          LC_CTYPE: UTF-8
          TZ: Europe/Stockholm
          COLONIES_TLS: "false"
          COLONIES_SERVER_HOST: localhost
          COLONIES_SERVER_PORT: 50080
          COLONIES_MONITOR_PORT: 21120
          COLONIES_MONITOR_INTERVAL: 1
          COLONIES_SERVER_ID: 039231c7644e04b6895471dd5335cf332681c54e27f81fac54f9067b3f2c0103
          COLONIES_SERVER_PRVKEY: fcc79953d8a751bf41db661592dc34d30004b1a651ffa0725b03ac227641499d
          COLONIES_DB_HOST: postgres
          COLONIES_DB_USER: postgres
          COLONIES_DB_PORT: 5432
          COLONIES_DB_PASSWORD: rFcLGNkgsNtksg6Pgtn9CumL4xXBQ7
          COLONIES_COLONY_NAME: test
          COLONIES_COLONY_ID: 4787a5071856a4acf702b2ffcea422e3237a679c681314113d86139461290cf4
          COLONIES_COLONY_PRVKEY: ba949fa134981372d6da62b6a56f336ab4d843b22c02a4257dcf7d0d73097514
          COLONIES_EXECUTOR_ID: 3fc05cf3df4b494e95d6a3d297a34f19938f7daa7422ab0d4f794454133341ac
          COLONIES_PRVKEY: ddf7f7791208083b6a9ed975a72684f6406a269cfa36f1b1c32045c0a71fff05
          COLONIES_EXECUTOR_TYPE: cli
          COLONIES_CRON_CHECKER_PERIOD: 1000
          COLONIES_GENERATOR_CHECKER_PERIOD: 1000
          COLONIES_EXCLUSIVE_ASSIGN: true
          COLONIES_INITDB: true
        ports:
          - 50080:50080
        options: >-
          --health-cmd "colonies server alive"
          --health-interval 1s
          --health-timeout 60s
          --health-retries 20

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Build
        run: cargo build --verbose

      - name: Run unit tests
        run: cargo test --lib --verbose

      - name: Run integration tests
        env:
          COLONIES_SERVER_HOST: localhost
          COLONIES_SERVER_PORT: 50080
          COLONIES_TLS: "false"
          COLONIES_SERVER_PRVKEY: fcc79953d8a751bf41db661592dc34d30004b1a651ffa0725b03ac227641499d
          COLONIES_COLONY_NAME: test
          COLONIES_COLONY_ID: 4787a5071856a4acf702b2ffcea422e3237a679c681314113d86139461290cf4
          COLONIES_COLONY_PRVKEY: ba949fa134981372d6da62b6a56f336ab4d843b22c02a4257dcf7d0d73097514
          COLONIES_EXECUTOR_ID: 3fc05cf3df4b494e95d6a3d297a34f19938f7daa7422ab0d4f794454133341ac
          COLONIES_PRVKEY: ddf7f7791208083b6a9ed975a72684f6406a269cfa36f1b1c32045c0a71fff05
          COLONIES_EXECUTOR_TYPE: cli
        run: cargo test --test integration_test --verbose

      - name: Install cargo-tarpaulin
        run: cargo install cargo-tarpaulin

      - name: Generate code coverage
        env:
          COLONIES_SERVER_HOST: localhost
          COLONIES_SERVER_PORT: 50080
          COLONIES_TLS: "false"
          COLONIES_SERVER_PRVKEY: fcc79953d8a751bf41db661592dc34d30004b1a651ffa0725b03ac227641499d
          COLONIES_COLONY_NAME: test
          COLONIES_COLONY_ID: 4787a5071856a4acf702b2ffcea422e3237a679c681314113d86139461290cf4
          COLONIES_COLONY_PRVKEY: ba949fa134981372d6da62b6a56f336ab4d843b22c02a4257dcf7d0d73097514
          COLONIES_EXECUTOR_ID: 3fc05cf3df4b494e95d6a3d297a34f19938f7daa7422ab0d4f794454133341ac
          COLONIES_PRVKEY: ddf7f7791208083b6a9ed975a72684f6406a269cfa36f1b1c32045c0a71fff05
          COLONIES_EXECUTOR_TYPE: cli
        run: cargo tarpaulin --out xml --output-dir ./coverage

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./coverage/cobertura.xml
          fail_ci_if_error: false
          verbose: true
