From 77ee2a0d8511870f62d7282e99341e7fd2f3788b Mon Sep 17 00:00:00 2001 From: Igor Pecovnik Date: Fri, 20 Sep 2024 08:36:43 +0200 Subject: [PATCH] Add unit test action --- .github/workflows/unit-tests.yml | 139 +++++++++++++++++++++++ lib/armbian-configng/config.ng.jobs.json | 4 +- lib/armbian-configng/config.ng.system.sh | 22 ++++ tests/S17.conf | 3 + tests/S18.conf | 2 + 5 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/unit-tests.yml create mode 100644 tests/S17.conf create mode 100644 tests/S18.conf diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 00000000..52f3b2ad --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,139 @@ +name: "Unit tests" +on: + workflow_dispatch: + workflow_call: + schedule: + - cron: '0 2 * * *' + push: + paths: + - tests/** + - lib/** + +env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_NUMBER: ${{ github.event.number }} + +jobs: + + test: + name: "Unit tests" + runs-on: ubuntu-latest + outputs: + matrix: ${{steps.json.outputs.JSON_CONTENT}} + steps: + + - name: Checkout + uses: actions/checkout@v4 + with: + path: 'config' + + - name: "Make JSON" + id: json + run: | + + echo 'JSON_CONTENT<> $GITHUB_OUTPUT + # define docker images where we will run test install + dockerimages=("debian:bookworm" "debian:trixie" "ubuntu:jammy" "ubuntu:noble") + + # read tests cases and loop them + tests=($(grep -rw config/tests/*.conf -e "ENABLED=true" | cut -d"/" -f3 | cut -d"." -f1)) + for i in "${tests[@]}"; do + unset RELEASE + source config/tests/${i}.conf + if [[ -z "${RELEASE}" ]]; then RELEASE=all; fi + # if we speficy releases, we need to loop docker images and use if there is a match + if [[ $RELEASE != all || -z $RELEASE ]]; then + for j in ${dockerimages[@]}; do + elements=($(echo $RELEASE | tr ':' "\n")) + for SELECTED_RELEASE in "${elements[@]}"; do + if [[ $j == *"${SELECTED_RELEASE}"* ]]; then + echo "{\"package\":\"${i}\",\"image\":\"$j\"}" + fi + done + done + else + for j in ${dockerimages[@]}; do + echo "{\"package\":\"${i}\",\"image\":\"$j\"}" + done + fi + + done | jq -s >> $GITHUB_OUTPUT + echo 'EOF' >> $GITHUB_OUTPUT + + gradle: + needs: test + strategy: + fail-fast: false + max-parallel: 32 + matrix: + image: ${{ fromJSON(needs.test.outputs.matrix) }} + + name: "I" + runs-on: ubuntu-latest + container: + image: "${{ matrix.image.image }}" + steps: + + - name: Checkout + uses: actions/checkout@v4 + with: + path: 'config' + + - name: Install + run: | + + # source vars + . "config/tests/${{ matrix.image.package }}.conf" + echo ${TEST_TITLE} + + export DEBIAN_FRONTEND=noninteractive + RELEASE=$(echo "${{ matrix.image.image }}" | cut -d":" -f2) + apt update + apt -y install wget gpg + + # add armbian repository + URL=apt.armbian.com + wget https://${URL}/armbian.key -O key + gpg --dearmor < key | tee /usr/share/keyrings/armbian.gpg > /dev/null + chmod go+r /usr/share/keyrings/armbian.gpg + echo "deb [signed-by=/usr/share/keyrings/armbian.gpg] http://${URL} $RELEASE main ${RELEASE}-utils ${RELEASE}-desktop" | tee /etc/apt/sources.list.d/armbian.list + apt update -y + apt upgrade -y + apt -y install sudo procps systemd whiptail jq lsb-release iproute2 + + # install packages / except howdy as its too large + export DEBIAN_FRONTEND=noninteractive + cd config + eval "$PREINSTALL" + sudo bash bin/armbian-configng --cmd "${{ matrix.image.package }}" + eval "$CONDITION" + + # stats + FILENAME="data-"$(echo ${{ matrix.image.image }} | sed "s/:/-/g")"-${{ matrix.image.package }}.teststats" + echo $RELEASE >> ../${FILENAME} + bash bin/armbian-configng --cmd | grep "${{ matrix.image.package }}" | xargs >> ../${FILENAME} + echo " " >> ../${FILENAME} + + - name: Upload test + uses: actions/upload-artifact@v3.2.1-node20 + with: + name: TESTDATA + path: data-*.teststats + + stop: + name: "Merge test artifacts" + if: always() + needs: gradle + runs-on: ubuntu-latest + steps: + + - name: "Download changes" + uses: actions/download-artifact@v3-node20 + with: + name: TESTDATA + + - name: Install + run: | + + echo "# Succesful tests:" >> $GITHUB_STEP_SUMMARY + cat ./*.teststats | sed '$ s/.$//' >> $GITHUB_STEP_SUMMARY \ No newline at end of file diff --git a/lib/armbian-configng/config.ng.jobs.json b/lib/armbian-configng/config.ng.jobs.json index ba348195..2889c6ae 100644 --- a/lib/armbian-configng/config.ng.jobs.json +++ b/lib/armbian-configng/config.ng.jobs.json @@ -250,7 +250,7 @@ "export BASHLOCATION=$(grep /bash$ /etc/shells | tail -1)", "sed -i \"s|^SHELL=.*|SHELL=${BASHLOCATION}|\" /etc/default/useradd", "sed -i \"s|^DSHELL=.*|DSHELL=${BASHLOCATION}|\" /etc/adduser.conf", - "debconf-apt-progress -- apt-get -y purge armbian-zsh", + "apt_install_wrapper apt-get -y purge armbian-zsh", "update_skel", "awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534 || $3 == 0) print $1}' /etc/passwd | xargs -L1 chsh -s $(grep /bash$ /etc/shells | tail -1)" ], @@ -267,7 +267,7 @@ "export ZSHLOCATION=$(grep /zsh$ /etc/shells | tail -1)", "sed -i \"s|^SHELL=.*|SHELL=${ZSHLOCATION}|\" /etc/default/useradd", "sed -i \"s|^DSHELL=.*|DSHELL=${ZSHLOCATION}|\" /etc/adduser.conf", - "debconf-apt-progress -- apt-get -y install armbian-zsh", + "apt_install_wrapper apt-get -y install armbian-zsh", "update_skel", "awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534 || $3 == 0) print $1}' /etc/passwd | xargs -L1 chsh -s $(grep /zsh$ /etc/shells | tail -1)" ], diff --git a/lib/armbian-configng/config.ng.system.sh b/lib/armbian-configng/config.ng.system.sh index 9ef446e3..d9fa1a61 100644 --- a/lib/armbian-configng/config.ng.system.sh +++ b/lib/armbian-configng/config.ng.system.sh @@ -205,4 +205,26 @@ else sudo service ssh restart fi +} + + +module_options+=( +["apt_install_wrapper,author"]="igorpecovnik" +["apt_install_wrapper,ref_link"]="" +["apt_install_wrapper,feature"]="Install wrapper" +["apt_install_wrapper,desc"]="Install wrapper" +["apt_install_wrapper,example"]="apt_install_wrapper apt-get -y purge armbian-zsh" +["apt_install_wrapper,status"]="Active" +) +# +# @description Use TUI / GUI for apt install if exists +# +function apt_install_wrapper +{ +if [ -t 0 ] ; then + debconf-apt-progress -- $@ +else + # Terminal not defined - proceed without TUI + $@ +fi } \ No newline at end of file diff --git a/tests/S17.conf b/tests/S17.conf new file mode 100644 index 00000000..12b75eca --- /dev/null +++ b/tests/S17.conf @@ -0,0 +1,3 @@ +ENABLED=true +PREINSTALL="bash bin/armbian-configng --cmd S18" +CONDITION="[ ! -f /usr/bin/zsh ]" diff --git a/tests/S18.conf b/tests/S18.conf new file mode 100644 index 00000000..bea6f840 --- /dev/null +++ b/tests/S18.conf @@ -0,0 +1,2 @@ +ENABLED=true +CONDITION="[ -f /usr/bin/zsh ]"