Skip to content

Commit

Permalink
Add testing using a GitHub Actions CI environment (#77)
Browse files Browse the repository at this point in the history
Signed-off-by: Ethan Dye <[email protected]>
  • Loading branch information
ecdye authored Feb 1, 2022
1 parent 5585866 commit c85cc84
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/shellcheck-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ jobs:
uses: actions/checkout@v2
- name: Setup environment
run: sudo -E bash -c set
- name: "ShellCheck tests/*.bash"
run: |
echo "ShellCheck zram-config..."
if shellcheck -x -s bash tests/*.bash; then echo "OK"; else echo "FAILED"; exit 1; fi
shell: bash
- name: "ShellCheck install.bash"
run: |
echo "ShellCheck install.bash..."
Expand Down
55 changes: 55 additions & 0 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Test

# Trigger the workflow on push or pull request
on:
push:
paths:
- 'zram-config'
- '**.bash'
- 'tests/**'
- '.github/workflows/test-action.yml'
pull_request:
paths:
- 'zram-config'
- '**.bash'
- 'tests/**'
- '.github/workflows/test-action.yml'

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/[email protected]
- name: Setup environment
id: setup
run: |
sudo -E bash -c set
sudo add-apt-repository ppa:canonical-server/server-backports
sudo apt-get update
sudo apt-get install --yes gnupg unzip expect systemd-container qemu-user-static qemu-utils qemu-system-arm
echo "::set-output name=imagezip::$(basename "$(curl "https://downloads.raspberrypi.org/raspios_lite_armhf_latest" -s -L -I -o /dev/null -w '%{url_effective}')")"
echo "::set-output name=image::$(echo "$(basename "$(curl "https://downloads.raspberrypi.org/raspios_lite_armhf_latest" -s -L -I -o /dev/null -w '%{url_effective}')")" | sed -e 's/.zip/.img/')"
- name: Cache Raspberry Pi OS 32bit image
uses: actions/[email protected]
with:
path: ${{ steps.setup.outputs.image }}
key: ${{ steps.setup.outputs.image }}-${{ secrets.CACHE_VERSION }}
- name: Build image
run: sudo -E ./tests/image.bash "setup" "${{ steps.setup.outputs.imagezip }}" "${{ steps.setup.outputs.image }}"
shell: bash
- name: Run tests
run: |
cp "${{ steps.setup.outputs.image }}" raspios.img
sudo expect ./tests/run.exp
shell: bash
- name: Copy logs
if: always()
run: sudo ./tests/image.bash "copy-logs" "raspios.img"
- name: Upload logs
if: always()
uses: actions/upload-artifact@v2
with:
name: logs.tar
path: logs.tar
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.DS_Store
*.zip
*.img
*.sig
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# zram-config
[![GitHub](https://img.shields.io/github/license/ecdye/zram-config)](https://github.com/ecdye/zram-config/blob/main/LICENSE.md)
[![License](https://img.shields.io/github/license/ecdye/zram-config)](https://github.com/ecdye/zram-config/blob/main/LICENSE.md)
[![ShellCheck](https://github.com/ecdye/zram-config/workflows/ShellCheck/badge.svg)](https://github.com/ecdye/zram-config/actions?query=workflow%3AShellCheck)
[![Test](https://github.com/ecdye/zram-config/actions/workflows/test-action.yml/badge.svg)](https://github.com/ecdye/zram-config/actions/workflows/test-action.yml)

## Overview

Expand Down Expand Up @@ -49,8 +50,7 @@ See [raspberrypi/linux@cef3970381](https://github.com/raspberrypi/linux/commit/c
``` shell
sudo apt-get install git
git clone https://github.com/ecdye/zram-config
cd zram-config
sudo ./install.bash
sudo ./zram-config/install.bash
```

Note: The recommended way to stop the `zram-config.service` is to run
Expand All @@ -68,8 +68,7 @@ The service will stop normally on reboot, there is no need to manually stop it.
### Update

``` shell
cd /path/to/zram-config/
sudo ./update.bash
sudo /path/to/zram-config/update.bash
```

### Uninstall
Expand Down
28 changes: 16 additions & 12 deletions install.bash
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/usr/bin/env bash

BASEDIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

if [[ "$(id -u)" -ne 0 ]]; then
echo "ERROR: You need to be ROOT (sudo can be used)."
exit 1
fi
if [[ $(systemctl is-active zram-config.service) == "active" ]]; then
echo -e "ERROR: zram-config service is still running.\\nPlease run \"sudo ./update.bash\" to update zram-config instead."
echo -e "ERROR: zram-config service is still running.\\nPlease run \"sudo ${BASEDIR}/update.bash\" to update zram-config instead."
exit 1
fi
if [[ -f /usr/local/sbin/zram-config ]]; then
echo -e "ERROR: zram-config is already installed.\\nPlease run \"sudo ./update.bash\" to update zram-config instead."
echo -e "ERROR: zram-config is already installed.\\nPlease run \"sudo ${BASEDIR}/update.bash\" to update zram-config instead."
exit 1
fi

Expand All @@ -24,25 +26,27 @@ if [[ "$(grep -o '^ID=.*$' /etc/os-release | cut -d'=' -f2)" == "ubuntu" ]] && [
fi
fi
cd overlayfs-tools || exit 1
make
cd ..
make --always-make --directory="${BASEDIR}/overlayfs-tools"
echo "Installing zram-config files"
install -m 755 zram-config /usr/local/sbin/
install -m 644 zram-config.service /etc/systemd/system/zram-config.service
install -m 644 ztab /etc/ztab
install -m 755 "${BASEDIR}/zram-config" /usr/local/sbin/
install -m 644 "${BASEDIR}/zram-config.service" /etc/systemd/system/zram-config.service
install -m 644 "${BASEDIR}/zram-config-shutdown.service" /etc/systemd/system/zram-config-shutdown.service
install -m 644 "${BASEDIR}/ztab" /etc/ztab
mkdir -p /usr/local/share/zram-config/log
ln -s /usr/local/share/zram-config/log /var/log/zram-config
install -m 755 uninstall.bash /usr/local/share/zram-config/uninstall.bash
install -m 644 zram-config.logrotate /etc/logrotate.d/zram-config
install -m 755 "${BASEDIR}/uninstall.bash" /usr/local/share/zram-config/uninstall.bash
install -m 644 "${BASEDIR}/zram-config.logrotate" /etc/logrotate.d/zram-config
mkdir -p /usr/local/lib/zram-config/
install -m 755 overlayfs-tools/overlay /usr/local/lib/zram-config/overlay
install -m 755 "${BASEDIR}/overlayfs-tools/overlay" /usr/local/lib/zram-config/overlay
echo "ReadWritePaths=/usr/local/share/zram-config/log" >> /lib/systemd/system/logrotate.service
echo "Starting zram-config.service"
systemctl daemon-reload
systemctl enable --now zram-config.service
systemctl enable --now zram-config.service zram-config-shutdown.service
until [[ $(systemctl show -p SubState --value zram-config) == "dead" ]]; do
sleep 5
done
echo "##### zram-config is now installed and running #####"
echo "##### edit /etc/ztab to configure options #####"
42 changes: 42 additions & 0 deletions tests/image.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash

mountImageFile() {
local loopPrefix

if [[ $1 == "mount" ]]; then
loopPrefix="$(kpartx -asv "$2" | grep -oE "loop([0-9]+)" | head -n 1)"

mkdir -p tests/{fs,kernel,dtb}
mount -o rw -t ext4 "/dev/mapper/${loopPrefix}p2" "tests/fs"
mount -o rw -t vfat "/dev/mapper/${loopPrefix}p1" "tests/fs/boot"
elif [[ $1 == "umount" ]]; then
umount tests/fs/boot
umount tests/fs
kpartx -d "$2"
fi
}

if [[ $1 == "setup" ]]; then
if ! [[ -f $3 ]]; then
curl -s -L "https://downloads.raspberrypi.org/raspios_lite_armhf_latest" -o "$2"
curl -s "$(curl "https://downloads.raspberrypi.org/raspios_lite_armhf_latest" -s -L -I -o /dev/null -w '%{url_effective}')".sig -o "${2}.sig"
gpg -q --keyserver keyserver.ubuntu.com --recv-key 0x8738CD6B956F460C
gpg -q --trust-model always --verify "${2}.sig" "$2"
unzip -q "$2" -d .
qemu-img resize -f raw "$3" 4G
echo ", +" | sfdisk -N 2 "$3"
fi
mountImageFile "mount" "$3"
rsync -avr --exclude="*.zip" --exclude="*.img" --exclude="*.sig" --exclude="tests/fs" --exclude="tests/dtb" --exclude="tests/kernel" ./ tests/fs/opt/zram
systemd-nspawn --directory="tests/fs" /opt/zram/tests/install-packages.bash
echo "set enable-bracketed-paste off" >> tests/fs/etc/inputrc # Prevents weird character output when running the image in tests
cp tests/fs/boot/kernel* tests/kernel
cp tests/fs/boot/*.dtb tests/dtb
mountImageFile "umount" "$3"
elif [[ $1 == "copy-logs" ]]; then
mountImageFile "mount" "$2"
cp tests/fs/opt/zram/logs.tar .
mountImageFile "umount" "$2"
fi

exit 0
13 changes: 13 additions & 0 deletions tests/install-packages.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

export DEBIAN_FRONTEND="noninteractive"

apt-get --quiet update
apt-get --quiet upgrade --yes
apt-get --quiet install --fix-broken --yes
apt-get --quiet install --yes gcc make libc6-dev
apt-get --quiet autoremove --yes
systemctl mask rpi-eeprom-update.service hciuart.service systemd-logind.service
rm -f /var/lib/apt/lists/lock
rm -f /var/cache/apt/archives/lock
rm -f /var/lib/dpkg/lock*
47 changes: 47 additions & 0 deletions tests/run.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/expect -f

set retval 0
set timeout -1
set loginUser "pi"
set loginPassword "raspberry"

spawn qemu-system-arm -machine "raspi2b" -cpu arm1176 -m 1G -drive "format=raw,file=raspios.img" -dtb "./tests/dtb/bcm2710-rpi-2-b.dtb" -kernel "./tests/kernel/kernel7.img" -append "loglevel=0 quiet console=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 fsck.repair=yes rootwait" -nographic -serial mon:stdio

# Login process
expect {
"raspberrypi login: " {
send "$loginUser\r"
exp_continue
}
"Password: " {
send "$loginPassword\r"
}
}

# Run tests
expect "pi@raspberrypi:~$ "
send "sudo /opt/zram/install.bash\r"
expect "pi@raspberrypi:~$ "
send "/opt/zram/tests/test-zram-devices.bash\r"
expect {
-re "Test failed:.*$" {
set retval 1
exp_continue
}
"pi@raspberrypi:~$ " {
send "sudo /opt/zram/uninstall.bash\r"
}
}
expect "pi@raspberrypi:~$ "
send "/opt/zram/tests/test-zram-devices.bash removal\r"
expect {
-re "Test failed:.*$" {
set retval 1
exp_continue
}
"pi@raspberrypi:~$ " {
send "sudo shutdown -h now\r"
}
}

exit $retval
81 changes: 81 additions & 0 deletions tests/test-zram-devices.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env bash

BASEDIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

check_zram_mounts() {
while read -r line; do
case "$line" in
"#"*)
# Skip comment line
continue
;;

"")
# Skip empty line
continue
;;

*)
# shellcheck disable=SC2086
set -- $line
ZTYPE="$1"
TARGET_DIR="$5"
if [[ $ZTYPE == "swap" ]]; then
if [[ "$(swapon | awk '/zram/ { print $1 }' | tr -d '0-9')" != "/dev/zram" ]]; then
echo "Test failed: swap not on zram."
zramctl --output-all
return 1
fi
elif [[ $ZTYPE == "dir" ]] || [[ $ZTYPE == "log" ]]; then
if [[ "$(df "$TARGET_DIR" | awk '/overlay/ { print $1 }' | tr -d '0-9')" != "overlay" ]]; then
echo "Test failed: overlay for '$TARGET_DIR' not found."
zramctl --output-all
return 1
fi
fi
;;
esac
done < "${BASEDIR}/../ztab"
}

check_zram_removal() {
while read -r line; do
case "$line" in
"#"*)
# Skip comment line
continue
;;

"")
# Skip empty line
continue
;;

*)
# shellcheck disable=SC2086
set -- $line
ZTYPE="$1"
TARGET_DIR="$5"
if [[ $ZTYPE == "swap" ]]; then
if [[ "$(swapon | awk '/zram/ { print $1 }' | tr -d '0-9')" == "/dev/zram" ]]; then
echo "Test failed: swap on zram."
zramctl --output-all
return 1
fi
elif [[ $ZTYPE == "dir" ]] || [[ $ZTYPE == "log" ]]; then
if [[ "$(df "$TARGET_DIR" | awk '/overlay/ { print $1 }' | tr -d '0-9')" == "overlay" ]]; then
echo "Test failed: overlay for '$TARGET_DIR' found."
zramctl --output-all
return 1
fi
fi
;;
esac
done < "${BASEDIR}/../ztab"
}

if [[ $1 == "removal" ]]; then
check_zram_removal || exit 1
else
check_zram_mounts || exit 1
fi
7 changes: 5 additions & 2 deletions uninstall.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

BASEDIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

if [[ "$(id -u)" -ne 0 ]]; then
echo "ERROR: You need to be ROOT (sudo can be used)."
exit 1
Expand All @@ -10,8 +12,9 @@ if ! [[ -f /usr/local/sbin/zram-config ]]; then
fi

zram-config "stop"
systemctl disable zram-config.service
rm -f /etc/systemd/system/zram-config.service
tar -cf "${BASEDIR}/logs.tar" --directory=/usr/local/share/zram-config log
systemctl disable zram-config.service zram-config-shutdown.service
rm -f /etc/systemd/system/zram-config.service /etc/systemd/system/zram-config-shutdown.service
sed -i '\|^ReadWritePaths=/usr/local/share/zram-config/log$|d' /lib/systemd/system/logrotate.service
systemctl daemon-reload
rm -f /usr/local/sbin/zram-config
Expand Down
Loading

0 comments on commit c85cc84

Please sign in to comment.