name: Essential Test

on:
  push:
  pull_request:
  schedule:
    # Schedule to run everyday at 6PM UTC (2AM CST)
    - cron:  '0 18 * * *'

jobs:
  Make_test_on_ubuntu:
    runs-on: ubuntu-18.04

    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true

    - name: Get occlum version
      run: echo "OCCLUM_VERSION=$(grep 'Version =' src/pal/include/occlum_version.h | awk '{print $4}')" >> $GITHUB_ENV;

    - name: Create container with ubuntu image
      run: docker run -itd --name=ubuntu-test -v $GITHUB_WORKSPACE:/root/occlum occlum/occlum:${{ env.OCCLUM_VERSION }}-ubuntu18.04

    - name: Check format
      run:  docker exec ubuntu-test bash -c 'cd /root/occlum; info=$(make format-check);
        if [[ -n $info ]]; then
          echo "Format error detected.";
          echo "$info";
          exit 1;
        fi'

    - name: Build dependencies
      run:  docker exec ubuntu-test bash -c "cd /root/occlum; make submodule"

    - name: Build source
      run:  docker exec ubuntu-test bash -c "source /opt/intel/sgxsdk/environment; cd /root/occlum; OCCLUM_RELEASE_BUILD=y SGX_MODE=SIM make"

    - name: Integration test
      run:  docker exec ubuntu-test bash -c "cd /root/occlum; OCCLUM_LOG_LEVEL=trace SGX_MODE=SIM make test"

    - name: Integration test with Glibc
      run:  docker exec ubuntu-test bash -c "cd /root/occlum; OCCLUM_LOG_LEVEL=trace SGX_MODE=SIM make test-glibc"

    - name: Show failed cases
      if: ${{ failure() }}
      run: docker exec ubuntu-test bash -c "cat /root/occlum/build/test/.fail"

  Make_test_on_centos:
    runs-on: ubuntu-18.04

    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true

    - name: Get occlum version
      run: echo "OCCLUM_VERSION=$(grep 'Version =' src/pal/include/occlum_version.h | awk '{print $4}')" >> $GITHUB_ENV;

    - name: Create container with centos image
      run: docker run -itd --name=centos-test -v $GITHUB_WORKSPACE:/root/occlum occlum/occlum:${{ env.OCCLUM_VERSION }}-centos8.2

    - name: Check format
      run:  docker exec centos-test bash -c 'cd /root/occlum; info=$(make format-check);
        if [[ -n $info ]]; then
          echo "Format error detected.";
          echo "$info";
          exit 1;
        fi'

    - name: Build dependencies
      run:  docker exec centos-test bash -c "cd /root/occlum; make submodule"

    - name: Build source
      run:  docker exec centos-test bash -c "source /opt/intel/sgxsdk/environment; cd /root/occlum; OCCLUM_RELEASE_BUILD=y SGX_MODE=SIM make"

    - name: Integration test
      run:  docker exec centos-test bash -c "cd /root/occlum; OCCLUM_LOG_LEVEL=trace SGX_MODE=SIM make test"

    - name: Integration test with Glibc
      run:  docker exec centos-test bash -c "cd /root/occlum; OCCLUM_LOG_LEVEL=trace SGX_MODE=SIM make test-glibc"

    - name: Show failed cases
      if: ${{ failure() }}
      run: docker exec centos-test bash -c "cat /root/occlum/build/test/.fail"
