name: test-install-script

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

on:
  push:
    branches:
      - master
    paths:
      - '.github/workflows/test-install-script.yml'
      - 'utils/install.sh'
      - 'utils/uninstall.sh'
      - 'utils/install.py'
  pull_request:
    branches:
      - master
    paths:
      - '.github/workflows/test-install-script.yml'
      - 'utils/install.sh'
      - 'utils/uninstall.sh'
      - 'utils/install.py'

env:
  INSTALL_PY_PATH: utils/install.py

jobs:
  linux:
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: CentOS 8 Stream
            host_runner: ubuntu-latest
            package_manager: yum
            docker_image: quay.io/centos/centos:stream8
            python_package: python3 python2
            python2_ex: python2
            python3_ex: python3
            extra_setup_command: yum install -y which
          - name: CentOS 7
            host_runner: ubuntu-latest
            package_manager: yum
            docker_image: centos:7.9.2009
            python_package: python3 python2
            python2_ex: python2
            python3_ex: python3
            extra_setup_command: yum install -y which
          - name: Ubuntu 20.04
            host_runner: ubuntu-latest
            package_manager: apt
            docker_image: ubuntu:20.04
            python_package: python2 python3
            python2_ex: python2
            python3_ex: python3
            extra_setup_command: echo "No extra command"
          - name: Ubuntu 18.04
            host_runner: ubuntu-latest
            package_manager: apt
            docker_image: ubuntu:18.04
            python_package: python3 python2.7
            python2_ex: python2.7
            python3_ex: python3
            extra_setup_command: echo "No extra command"
          - name: Ubuntu 16.04
            host_runner: ubuntu-latest
            package_manager: apt
            docker_image: ubuntu:16.04
            python_package: python2.7
            python2_ex: python2.7
            python3_ex: python3.7
            extra_setup_command: apt update -y && apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget && wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz && tar xzf Python-3.7.4.tgz && cd Python-3.7.4 && ./configure && make -j && make install && cd ..
          - name: manylinux2014 aarch64
            host_runner: linux-arm64
            package_manager: yum
            docker_image: wasmedge/wasmedge:manylinux2014_aarch64
            python_package: python2 python3
            python2_ex: python2
            python3_ex: python3
            extra_setup_command: echo "No extra command"
    name: ${{ matrix.name }}
    runs-on: ${{ matrix.host_runner }}
    container:
      image: ${{ matrix.docker_image }}

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0

    - name: Install git and curl
      run: |
        ${{ matrix.extra_setup_command }}
        ${{ matrix.package_manager }} update -y
        ${{ matrix.package_manager }} install -y git curl
    - name: Install Python
      run: |
        ${{ matrix.package_manager }} install -y ${{ matrix.python_package }}
    - name: Install WasmEdge latest release
      run: |
        bash utils/install.sh -D
    - name: Uninstall WasmEdge
      run: |
        bash utils/uninstall.sh -q -V
    - name: Install WasmEdge latest release with all extensions
      run: |
        bash utils/install.sh -e all -D
    - name: Uninstall WasmEdge with all extensions
      run: |
        bash utils/uninstall.sh -q -V
    - name: Install deprecated WasmEdge specific release (0.10.0)
      run: |
        bash utils/install.sh -v 0.10.0 -D
    - name: Uninstall WasmEdge
      run: |
        bash utils/uninstall.sh -q -V
    - name: Install WasmEdge specific release (0.10.0) with all extensions
      run: |
        bash utils/install.sh -e all -v 0.10.0 -D
    - name: Install WasmEdge and test for arm extension installation
      run: |
        bash utils/install.sh -e all --version=0.10.0-alpha.1 \
        --tf-version=0.10.0-alpha.1 --tf-deps-version=0.10.0-alpha.1  --tf-tools-version=0.10.0-alpha.1  \
        --image-version=0.10.0-alpha.1
    - name: Uninstall WasmEdge with all extensions
      run: |
        bash utils/uninstall.sh -q -V
    - name: Install WasmEdge and wasi_nn-ggml plugin (0.13.4)
      run: |
        bash utils/install.sh -v 0.13.4 --plugins wasi_nn-ggml -D
        ls ~/.wasmedge/plugin/ | grep libwasmedgePluginWasiNN.so && echo "Pass: WASI_NN-ggml Plugin found" || (echo "Fail: WASI_NN-ggml not found" && exit 1)
    - name: Uninstall WasmEdge with wasi_nn-ggml
      run: |
        bash utils/uninstall.sh -q -V
    - name: Install versions multiple times
      run: |
        bash utils/install.sh -e all -D
        bash utils/install.sh -e all -v 0.10.1 -D
        bash utils/install.sh -e all -v 0.10.0 -D
    - name: Uninstall WasmEdge
      run: |
        bash utils/uninstall.sh -q -V
    - name: Tensorflow changes - libtensorflow_cc 2.6.0 will replace the old libtensorflow 2.6.0
      if: ${{ matrix.name != 'manylinux2014 aarch64' }}
      run: |
        bash utils/install.sh -v 0.12.0-alpha.2 -e tensorflow --tf-version 0.12.0-alpha.2 --tf-deps-version 0.12.0-alpha.2 --tf-tools-version 0.12.0-alpha.2
        ls ~/.wasmedge/lib/ | grep libtensorflow_cc* && echo "Pass: libtensorflow_cc found" || (echo "Fail: libtensorflow_cc found not found" && exit 1)
    - name: Uninstall WasmEdge
      if: ${{ matrix.name != 'manylinux2014 aarch64' }}
      run: |
        bash utils/uninstall.sh -q -V
    - name: Plugins Install check
      run: |
        bash utils/install.sh -v 0.10.0-alpha.1
        ls ~/.wasmedge/plugin/ | grep .so && echo "Pass: Plugins found" || (echo "Fail: Plugins not found" && exit 1)
        bash utils/install.sh -v 0.10.0-alpha.1 -p ~/new_wasmedge
        ls ~/new_wasmedge/plugin/ | grep .so && echo "Pass: Plugins found" || (echo "Fail: Plugins not found" && exit 1)
        bash utils/install.sh -v 0.10.0-alpha.1 -p /usr
        ls /usr/lib/wasmedge/ | grep libwasmedgePluginWasmEdgeProcess.so && echo "Pass: Plugins found" || (echo "Fail: Plugins not found" && exit 1)
    - name: Latest Release Check
      run: |
        _res_git_=$(git ls-remote --refs --tags "https://github.com/WasmEdge/WasmEdge.git" | cut -d '/' -f 3 | awk '{ if ($1 ~ /-/) print; else print $0"_" ; }' | sort --version-sort | sed 's/_$//' | grep -e '^[0-9]\+.[0-9]\+.[0-9]\+$' | tail -1)
        _res_curl_=$(curl -w "%{url_effective}\n" -I -L -s -S https://github.com/WasmEdge/WasmEdge/releases/latest -o /dev/null | grep -Eo '[0-9]+.[0-9]+.[0-9]+*')
        [ "$_res_git_" = "$_res_curl_" ] && echo "Pass: Fetched version equals release" ||
        (echo "Fail: Fetched version does not equal GitHub Ref\nFetched:$_res_git_\nCI:$_res_curl_" && exit 1)


  macos:
    strategy:
      matrix:
        include:
          - name: MacOS-latest
            host_runner: macos-latest
            package_manager: brew
          - name: MacOS-arm64
            host_runner: mac-arm64
            package_manager: brew
    name: ${{ matrix.name }}
    runs-on: ${{ matrix.host_runner }}

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0

    - name: Install WasmEdge latest release
      run: |
        bash utils/install.sh -D
    - name: Uninstall WasmEdge
      run: |
        bash utils/uninstall.sh -q -V
    - name: Check for bashrc generation
      if: ${{ matrix.name != 'MacOS-arm64' }}
      run: |
        rm ~/.bashrc
        bash utils/install.sh -D
        if ls -a ~ | grep .bashrc; then echo "Fail: Bashrc found" && exit 1; else echo "Pass: Bashrc not found"; fi;
    - name: Check for bashrc generation
      if: ${{ matrix.name == 'MacOS-arm64' }}
      run: |
        rm ~/.zshenv
        bash utils/install.sh -D
        if ls -a ~ | grep .zshenv; then echo echo "Pass: zshenv found"; else "Fail: zshenv not found" && exit 1; fi;
    - name: Uninstall WasmEdge
      run: |
        bash utils/uninstall.sh -q -V
    - name: Install WasmEdge latest release with all extensions
      if: ${{ matrix.name != 'MacOS-arm64' }}
      run: |
        bash utils/install.sh -e all -D
    - name: Uninstall WasmEdge with all extensions
      if: ${{ matrix.name != 'MacOS-arm64' }}
      run: |
        bash utils/uninstall.sh -q -V
    - name: Install deprecated WasmEdge specific release (0.10.0)
      run: |
        bash utils/install.sh -v 0.10.0 -D
    - name: Uninstall WasmEdge
      run: |
        bash utils/uninstall.sh -q -V
    - name: Install WasmEdge specific release (0.10.0) with all extensions
      if: ${{ matrix.name != 'MacOS-arm64' }}
      run: |
        bash utils/install.sh -e all -v 0.10.0 -D
    - name: Uninstall WasmEdge with all extensions
      if: ${{ matrix.name != 'MacOS-arm64' }}
      run: |
        bash utils/uninstall.sh -q -V
    - name: Plugins Install check MacOS-x86
      if: ${{ matrix.name != 'MacOS-arm64' }}
      run: |
        bash utils/install.sh -v 0.10.0-alpha.1
        ls ~/.wasmedge/plugin/ | grep .dylib && echo "Pass: Plugins found" || (echo "Fail: Plugins not found" && exit 1)
        bash utils/install.sh -v 0.13.1 --plugins wasmedge_tensorflow
        ls ~/.wasmedge/plugin/ | grep libwasmedgePluginWasmEdgeTensorflow.dylib && echo "Pass: Plugins found" || (echo "Fail: Plugins not found" && exit 1)
        bash utils/install.sh -v 0.13.1 --plugins wasmedge_tensorflowlite
        ls ~/.wasmedge/plugin/ | grep libwasmedgePluginWasmEdgeTensorflowLite.dylib && echo "Pass: Plugins found" || (echo "Fail: Plugins not found" && exit 1)
        bash utils/install.sh -v 0.10.0-alpha.1 -p ~/new_wasmedge
        ls ~/new_wasmedge/plugin/ | grep .dylib && echo "Pass: Plugins found" || (echo "Fail: Plugins not found" && exit 1)
        # MacOS container does not have access for /usr directory
        # bash utils/install.sh -v 0.10.0-alpha.1 -p /usr
        # ls /usr/lib/wasmedge/ | grep libwasmedgePluginWasmEdgeProcess.dylib && echo "Pass: Plugins found" || (echo "Fail: Plugins not found" && exit 1)

    - name: Plugins Install check MacOS-arm64
      if: ${{ matrix.name == 'MacOS-arm64' }}
      run: |
        bash utils/install.sh -v 0.13.1 --plugins wasmedge_tensorflow
        ls ~/.wasmedge/plugin/ | grep libwasmedgePluginWasmEdgeTensorflow.dylib && echo "Pass: Plugins found" || (echo "Fail: Plugins not found" && exit 1)
        bash utils/install.sh -v 0.13.1 --plugins wasmedge_tensorflowlite
        ls ~/.wasmedge/plugin/ | grep libwasmedgePluginWasmEdgeTensorflowLite.dylib && echo "Pass: Plugins found" || (echo "Fail: Plugins not found" && exit 1)

    - name: Tensorflow changes - libtensorflow_cc 2.6.0 will replace the old libtensorflow 2.6.0
      if: ${{ matrix.name != 'MacOS-arm64' }}
      run: |
        bash utils/install.sh -v 0.12.0-alpha.2 -e tensorflow --tf-version 0.12.0-alpha.2 --tf-deps-version 0.12.0-alpha.2 --tf-tools-version 0.12.0-alpha.2
        ls ~/.wasmedge/lib/ | grep libtensorflow_cc* && echo "Pass: libtensorflow_cc found" || (echo "Fail: libtensorflow_cc found not found" && exit 1)
    - name: Uninstall WasmEdge
      run: |
        bash utils/uninstall.sh -q -V
    - name: Latest Release Check
      run: |
        _res_git_=$(git ls-remote --refs --tags "https://github.com/WasmEdge/WasmEdge.git" | cut -d '/' -f 3 | awk '{ if ($1 ~ /-/) print; else print $0"_" ; }' | sort --version-sort | sed 's/_$//' | grep -e '^[0-9]\+.[0-9]\+.[0-9]\+$' | tail -1)
        _res_curl_=$(curl -w "%{url_effective}\n" -I -L -s -S https://github.com/WasmEdge/WasmEdge/releases/latest -o /dev/null | grep -Eo '[0-9]+.[0-9]+.[0-9]+')
        [ "$_res_git_" = "$_res_curl_" ] && echo "Pass: Fetched version equals release" ||
        (echo "Fail: Fetched version does not equal GitHub Ref\nFetched:$_res_git_\nCI:$_res_curl_" && exit 1)
