name: Ubuntu

on: [push, pull_request]

jobs:
  build:
    name: Build on Ubuntu
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1

    - name: Build
      run: go build

  test-unit:
    name: Unit Testing
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1

    - name: Install dependencies for tests
      run: |
        sudo apt-get update
        sudo apt-get install qemu-system-x86

    - name: Bootstrap
      run: ./tools/qemu-ubuntu-img/bootstrap.sh

    - name: Unit Testing
      run: go test -parallel 1 -v ./...

  test-end-to-end:
    name: End-to-End Testing
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1

    - name: Build
      run: go build

    - name: Install dependencies for tests
      run: |
        sudo apt-get update
        sudo apt-get install qemu-system-x86

    - name: End-to-End Testing [Kernel Module]
      run: |
        cd examples/kernel-module
        ../../out-of-tree --log-level=debug kernel autogen --max=1
        ../../out-of-tree --log-level=debug pew --qemu-timeout=10m

    - name: End-to-End Testing [Kernel Exploit]
      run: |
        cd examples/kernel-exploit
        ../../out-of-tree --log-level=debug kernel autogen --max=1
        ../../out-of-tree --log-level=debug pew --threshold=0 --qemu-timeout=10m

    - name: Archive logs
      uses: actions/upload-artifact@v3
      with:
        name: test-end-to-end-logs
        path: /home/runner/.out-of-tree/logs/out-of-tree.log

  test-end-to-end-kernels:
    name: End-to-End Testing (kernels)
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1

    - name: Build
      run: go build

    - name: Install dependencies for tests
      run: |
        sudo apt-get update
        sudo apt-get install qemu-system-x86

    - name: End-to-End Testing [Install one Ubuntu 18.04 kernel]
      run: |
        ./out-of-tree --log-level=debug kernel install --distro=Ubuntu --ver=18.04 --kernel=4.15.0-70-generic

    - name: End-to-End Testing [Reinstall one Ubuntu 18.04 kernel]
      run: |
        ./out-of-tree --log-level=debug kernel install --distro=Ubuntu --ver=18.04 --kernel=4.15.0-70-generic --force

    - name: End-to-End Testing [Install one Ubuntu 22.04 kernel w/o headers]
      run: |
        ./out-of-tree --log-level=debug kernel install --distro=Ubuntu --ver=22.04 --kernel=5.19.0-28-generic --no-headers

    - name: End-to-End Testing [Install one CentOS 7 kernel]
      run: |
        ./out-of-tree --log-level=debug kernel install --distro=CentOS --ver=7 --kernel=3.10.0-862.6.3

    - name: End-to-End Testing [Install one CentOS 7 kernel w/o headers]
      run: |
        ./out-of-tree --log-level=debug kernel install --distro=CentOS --ver=7 --kernel=3.10.0-1160.71.1 --no-headers

    - name: End-to-End Testing [Install one CentOS 8 kernel]
      run: |
        ./out-of-tree --log-level=debug kernel install --distro=CentOS --ver=8 --kernel=4.18.0-348.7.1

    - name: Archive logs
      uses: actions/upload-artifact@v3
      with:
        name: test-end-to-end-kernels-log
        path: /home/runner/.out-of-tree/logs/out-of-tree.log

  test-end-to-end-genall:
    name: End-to-End Testing (genall)
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1

    - name: Build
      run: go build

    - name: Install dependencies for tests
      run: |
        sudo apt-get update
        sudo apt-get install qemu-system-x86

    - name: End-to-End Testing [Install all Ubuntu 22.04 kernels]
      run: |
        ./out-of-tree --log-level=debug kernel genall --distro=Ubuntu --ver=22.04

    - name: Archive logs
      uses: actions/upload-artifact@v3
      with:
        name: test-end-to-end-genall-logs
        path: /home/runner/.out-of-tree/logs/out-of-tree.log