name: Execute Tests
permissions:
  actions: none
  checks: none
  contents: none
  deployments: none
  issues: none
  packages: none
  pull-requests: none
  repository-projects: none
  security-events: none
  statuses: none
      
on:
  workflow_dispatch:
    inputs:
      sha:
        description: 'uazo/bromite SHA'
        required: true
        default: 'ce2f07034a3d3b257e9bada65d8293a6786bb95f'
        
env:
  BROMITE_SHA: ${{ github.event.inputs.sha }}
  REMOVEDOCKERSUPPORT: true
  USELOCALIMAGE: true

jobs:
  get_image:
    runs-on: self-hosted
    steps:
      - name: Mark image to test
        shell: bash
        run: |
          docker tag uazo/bromite-build:$BROMITE_SHA uazo/bromite-build:test
          
  exec-test:
    runs-on: self-hosted
    needs: get_image
    if: success()
    
    container:
      image: uazo/bromite-build:test
      env:
        REMOVEDOCKERSUPPORT: true # CUSTOM RUNNER: remove sharing of docker socket
        USELOCALIMAGE: true       # CUSTOM RUNNER: permit use of local images
        USEINTERNALNETWORK: true  # CUSTOM RUNNER: create the docker network as internal
        WORKSPACE: /home/lg/working_dir
        ARTIFACS_DIR: /home/lg/working_dir/artifacs
      volumes:
        - /storage/images/${{ github.event.inputs.sha }}/out:/home/lg/working_dir/chromium/src/out/bromite:r
        - /storage/images/${{ github.event.inputs.sha }}/out/tests:/home/lg/working_dir/chromium/src/out/tests
      options: --device=/dev/kvm
        
    steps:    
      - name: Prepare Test Container
        shell: bash
        run: |
          # set workspace paths
          PATH=$WORKSPACE/chromium/src/third_party/llvm-build/Release+Asserts/bin:$WORKSPACE/depot_tools/:/usr/local/go/bin:$WORKSPACE/mtool/bin:$PATH
          cd $WORKSPACE
                    
          # copy artifacts
          #cd chromium/src
          #mkdir -p out/bromite
          #cp -arp $ARTIFACS_DIR/out/* out/bromite
          
          rm -rf out/tests/*
          #mkdir -p out/tests
          
          # reset proxy env
          #HTTP_PROXY=
          #HTTPS_PROXY=
          #http_proxy=
          #https_proxy=

#      - name: Wait forever
#        shell: bash
#        run: |
#          sleep infinity
      
      - name: Execute chrome junit test
        shell: bash
        run: |
          PATH=$WORKSPACE/chromium/src/third_party/llvm-build/Release+Asserts/bin:$WORKSPACE/depot_tools/:/usr/local/go/bin:$WORKSPACE/mtool/bin:$PATH
          cd $WORKSPACE/chromium/src
          out/bromite/bin/run_chrome_junit_tests --gtest_filter=*Bromite* || KO=1

      - name: Start Android Emulator
        shell: bash
        run: |
          PATH=$WORKSPACE/chromium/src/third_party/llvm-build/Release+Asserts/bin:$WORKSPACE/depot_tools/:/usr/local/go/bin:$WORKSPACE/mtool/bin:$PATH
          cd $WORKSPACE/chromium/src

          sudo mkdir -p /github/home/.android
          sudo chmod 666 /github/home/.android/
          tools/android/avd/avd.py install --avd-config ../../generic_android31.textpb -v
          tools/android/avd/avd.py start --avd-config ../../generic_android31.textpb -v
          
      - name: Execute unit tests
        shell: bash
        run: |
          PATH=$WORKSPACE/chromium/src/third_party/llvm-build/Release+Asserts/bin:$WORKSPACE/depot_tools/:/usr/local/go/bin:$WORKSPACE/mtool/bin:$PATH
          cd $WORKSPACE/chromium/src
          out/bromite/unit_tests -v --fast-local-dev --gtest_filter=*Bromite* || KO=1

      - name: Restart Android Emulator
        shell: bash
        run: |
          PATH=$WORKSPACE/chromium/src/third_party/llvm-build/Release+Asserts/bin:$WORKSPACE/depot_tools/:/usr/local/go/bin:$WORKSPACE/mtool/bin:$PATH
          cd $WORKSPACE/chromium/src
          pkill -f qemu-system-x86
          tools/android/avd/avd.py start --avd-config ../../generic_android31.textpb -v
       
      - name: Execute instrumentation tests
        shell: bash
        run: |
          PATH=$WORKSPACE/chromium/src/third_party/llvm-build/Release+Asserts/bin:$WORKSPACE/depot_tools/:/usr/local/go/bin:$WORKSPACE/mtool/bin:$PATH
          cd $WORKSPACE/chromium/src
          out/bromite/bin/run_chrome_public_test_apk -v --num-retries=1 --gtest_filter=*Bromite* || KO=1

      #- name: Wait forever
      #  shell: bash
      #  run: |
      #    sleep infinity

      - name: Copy results
        shell: bash
        run: |
          sudo cp -r $WORKSPACE/chromium/src/out/bromite/TEST_RESULTS* $WORKSPACE/chromium/src/out/tests/

