Add BATS tests with automated github workflow (#34)

Added tests, including test cases for:
* the -b option
* the -c (connman) option
* the -e option
* the -k option
* the -l option
* portage dirs


Signed-off-by: Lucian Poston <lucianposton@pm.me>
This commit is contained in:
lucianposton 2020-02-06 17:59:18 -06:00 committed by GitHub
parent e0c3fc7244
commit 94b0040499
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 474 additions and 0 deletions

17
.github/workflows/tests.yml vendored Normal file
View file

@ -0,0 +1,17 @@
name: "Tests"
on: [push, pull_request]
jobs:
bats:
name: "BATS job"
runs-on: ubuntu-latest
steps:
- name: "Setup BATS testing framework"
uses: mig4/setup-bats@v1.0.1
with:
bats-version: 1.1.0
- name: "Check out code"
uses: actions/checkout@v1
- name: "Run BATS tests"
run: bats -r tests

183
tests/0001.bats Normal file
View file

@ -0,0 +1,183 @@
#!/usr/bin/env bats
load test_helper
setup() {
f test/.hiddenfile
f test/usr/bin/ping
f test/usr/bin/lost+found
f test/usr/src/linux-"$test_uname"/.config
f test/usr/src/linux-"$test_uname"/vmlinux
f test/lib/modules/"$test_uname"/mod.ko
f test/lib64/modules/"$test_uname"/mod.ko
f test/dev/sda
f test/proc/cpuinfo
f test/run/pid
d test/sys/fs
f test/tmp/junk
d test/media/cdrom
f test/mnt/1/content
f test/root/.bash_history
f test/home/user/.bash_history
f test/home/user/chroot/var/tmp/file
f test/boot/kernel
f test/var/tmp/file
f test/var/lock/lockfile
f test/var/run/pid
f test/var/lib/docker/image
f test/var/log/messages
f test/var/log/portage/elog/.keep_sys-apps_portage-0
mkstage4.sh -q -t test test
}
teardown() {
rm -rf test test.tar.bz2
}
@test "/usr/bin/ping is included" {
assert_tar_includes test/usr/bin/ping
}
@test "/usr/bin/lost+found is included" {
assert_tar_includes test/usr/bin/lost+found
}
@test "/usr/src/linux-uname/.config is included" {
assert_tar_includes test/usr/src/linux-"$test_uname"/.config
}
@test "/usr/src/linux-uname/vmlinux is included" {
assert_tar_includes test/usr/src/linux-"$test_uname"/vmlinux
}
@test "/lib/modules/uname/mod.ko is included" {
assert_tar_includes test/lib/modules/"$test_uname"/mod.ko
}
@test "/lib64/modules/uname/mod.ko is included" {
assert_tar_includes test/lib64/modules/"$test_uname"/mod.ko
}
@test "/dev/* is excluded" {
assert_tar_excludes test/dev/sda
}
@test "/var/tmp/* is excluded" {
assert_tar_excludes test/var/tmp/file
}
@test "/media/* is excluded" {
assert_tar_excludes test/media/cdrom
}
@test "/mnt/* is excluded" {
assert_tar_excludes test/mnt/1
}
@test "/proc/* is excluded" {
assert_tar_excludes test/proc/cpuinfo
}
@test "/run/* is excluded" {
assert_tar_excludes test/run/pid
}
@test "/sys/* is excluded" {
assert_tar_excludes test/sys/fs
}
@test "/tmp/* is excluded" {
assert_tar_excludes test/tmp/junk
}
@test "/var/lock/* is excluded" {
assert_tar_excludes test/var/lock/lockfile
}
@test "/var/log/* is excluded" {
assert_tar_excludes test/var/log/messages
}
@test "/var/run/* is excluded" {
assert_tar_excludes test/var/run/pid
}
@test "/var/lib/docker/* is excluded" {
assert_tar_excludes test/var/lib/docker/image
}
@test "/dev/ is included" {
assert_tar_includes test/dev/
}
@test "/var/tmp/ is included" {
assert_tar_includes test/var/tmp/
}
@test "/media/ is included" {
assert_tar_includes test/media/
}
@test "/mnt/ is included" {
assert_tar_includes test/mnt/
}
@test "/proc/ is included" {
assert_tar_includes test/proc/
}
@test "/run/ is included" {
assert_tar_includes test/run/
}
@test "/sys/ is included" {
assert_tar_includes test/sys/
}
@test "/tmp/ is included" {
assert_tar_includes test/tmp/
}
@test "/var/lock/ is included" {
assert_tar_includes test/var/lock/
}
@test "/var/log/ is included" {
assert_tar_includes test/var/log/
}
@test "/var/run/ is included" {
assert_tar_includes test/var/run/
}
@test "/var/lib/docker/ is included" {
assert_tar_includes test/var/lib/docker/
}
@test "/boot/kernel is included" {
assert_tar_includes test/boot/kernel
}
@test "/.hiddenfile is included" {
skip "TODO: Not yet implemented"
assert_tar_includes test/.hiddenfile
}
@test "/var/log/**/.keep is included" {
skip "TODO: Not yet implemented"
assert_tar_includes test/var/log/portage/elog/.keep_sys-apps_portage-0
}
@test "/home/user/.bash_history is excluded" {
assert_tar_excludes test/home/user/.bash_history
}
@test "/root/.bash_history is included" {
assert_tar_includes test/root/.bash_history
}
@test "/home/user/chroot/var/tmp/file is included" {
assert_tar_includes test/home/user/chroot/var/tmp/file
}
# vim: ft=bash

28
tests/0002.bats Normal file
View file

@ -0,0 +1,28 @@
#!/usr/bin/env bats
load test_helper
setup() {
f test/usr/bin/ping
f test/usr/bin/lost+found
f test/lost+found
mkstage4.sh -q -l -t test test
}
teardown() {
rm -rf test test.tar.bz2
}
@test "/usr/bin/ping is included" {
assert_tar_includes test/usr/bin/ping
}
@test "/usr/bin/lost+found is excluded" {
assert_tar_excludes test/usr/bin/lost+found
}
@test "/lost+found is excluded" {
assert_tar_excludes test/lost+found
}
# vim: ft=bash

54
tests/0003.bats Normal file
View file

@ -0,0 +1,54 @@
#!/usr/bin/env bats
load test_helper
setup() {
f test/usr/bin/ping
f test/usr/src/linux-"$test_uname"/.config
f test/usr/src/linux-"$test_uname"/vmlinux
f test/lib/modules/"$test_uname"/mod.ko
f test/lib64/modules/"$test_uname"/mod.ko
mkstage4.sh -k -q -t test test
}
teardown() {
rm -rf test test.tar.bz2 test.tar.bz2.ksrc test.tar.bz2.kmod
}
@test "/usr/bin/ping is included" {
assert_tar_includes test/usr/bin/ping
}
@test "/usr/src/linux-uname/.config is excluded" {
assert_tar_excludes test/usr/src/linux-"$test_uname"/.config
}
@test "/usr/src/linux-uname/vmlinux is excluded" {
assert_tar_excludes test/usr/src/linux-"$test_uname"/vmlinux
}
@test "/lib/modules/uname/mod.ko is excluded" {
assert_tar_excludes test/lib/modules/"$test_uname"/mod.ko
}
@test "/lib64/modules/uname/mod.ko is excluded" {
assert_tar_excludes test/lib64/modules/"$test_uname"/mod.ko
}
@test "/usr/src/linux-uname/.config is included" {
assert_tar_includes test/usr/src/linux-"$test_uname"/.config test.tar.bz2.ksrc
}
@test "/usr/src/linux-uname/vmlinux is included" {
assert_tar_includes test/usr/src/linux-"$test_uname"/vmlinux test.tar.bz2.ksrc
}
@test "/lib/modules/uname/mod.ko is included" {
assert_tar_includes test/lib/modules/"$test_uname"/mod.ko test.tar.bz2.kmod
}
@test "/lib64/modules/uname/mod.ko is included" {
assert_tar_includes test/lib64/modules/"$test_uname"/mod.ko test.tar.bz2.kmod
}
# vim: ft=bash

24
tests/0004.bats Normal file
View file

@ -0,0 +1,24 @@
#!/usr/bin/env bats
load test_helper
setup() {
f test/boot/kernel
d test/boot/boot
mkstage4.sh -b -q -t test test
}
teardown() {
rm -rf test test.tar.bz2
}
@test "/boot/kernel is excluded" {
assert_tar_excludes test/boot/kernel
}
@test "/boot/boot is included" {
skip "TODO: Not yet implemented"
assert_tar_includes test/boot/boot
}
# vim: ft=bash

22
tests/0005.bats Normal file
View file

@ -0,0 +1,22 @@
#!/usr/bin/env bats
load test_helper
setup() {
f test/var/lib/connman/file
mkstage4.sh -c -q -t test test
}
teardown() {
rm -rf test test.tar.bz2
}
@test "/var/lib/connman/file is excluded" {
assert_tar_excludes test/var/lib/connman/file
}
@test "/var/lib/connman/ is included" {
assert_tar_includes test/var/lib/connman/
}
# vim: ft=bash

40
tests/0006.bats Normal file
View file

@ -0,0 +1,40 @@
#!/usr/bin/env bats
load test_helper
setup() {
f test/var/db/repos/gentoo/app-backup/mkstage4/ebuild
f test/var/cache/distfiles/mkstage4.tar.gz
f test/usr/portage/Manifest
mkstage4.sh -q -t test test
}
teardown() {
rm -rf test test.tar.bz2
}
@test "/var/db/repos/gentoo/app-backup is excluded" {
assert_tar_excludes test/var/db/repos/gentoo/app-backup
}
@test "/var/db/repos/gentoo/ is included" {
assert_tar_includes test/var/db/repos/gentoo/
}
@test "/var/cache/distfiles/mkstage4.tar.gz is excluded" {
assert_tar_excludes test/var/cache/distfiles/mkstage4.tar.gz
}
@test "/var/cache/distfiles/ is included" {
assert_tar_includes test/var/cache/distfiles/
}
@test "/usr/portage/Manifest is excluded" {
assert_tar_excludes test/usr/portage/Manifest
}
@test "/usr/portage/ is included" {
assert_tar_includes test/usr/portage/
}
# vim: ft=bash

62
tests/0007.bats Normal file
View file

@ -0,0 +1,62 @@
#!/usr/bin/env bats
load test_helper
setup() {
f test/usr/bin/lost+found
f test/lost+found
f test/home/user/.dotfile
f test/home/user/chroot/var/tmp/file
f test/home/user/ccache/file
f test/root/ccache/file
f test/etc/secrets/key
mkstage4.sh -q -t test test0
mkstage4.sh \
-e 'test/lost+found' \
-e 'user/.*' \
-e 'test/home/user/chroot/var/tmp/file' \
-e 'ccache/*' \
-e 'secrets' \
-q -t test test
}
teardown() {
rm -rf test test.tar.bz2 test0.tar.bz2
}
@test "-e '/lost+found'" {
assert_tar_includes test/usr/bin/lost+found test0.tar.bz2
assert_tar_includes test/usr/bin/lost+found
assert_tar_includes test/lost+found test0.tar.bz2
assert_tar_excludes test/lost+found
}
@test "-e 'user/.*'" {
assert_tar_includes test/home/user/.dotfile test0.tar.bz2
assert_tar_excludes test/home/user/.dotfile
assert_tar_includes test/home/user/
}
@test "-e 'test/home/user/chroot/var/tmp/file'" {
assert_tar_includes test/home/user/chroot/var/tmp/file test0.tar.bz2
assert_tar_excludes test/home/user/chroot/var/tmp/file
}
@test "-e 'ccache/*'" {
assert_tar_includes test/home/user/ccache/file test0.tar.bz2
assert_tar_excludes test/home/user/ccache/file
assert_tar_includes test/home/user/ccache/
assert_tar_includes test/root/ccache/file test0.tar.bz2
assert_tar_excludes test/root/ccache/file
assert_tar_includes test/root/ccache/
}
@test "-e 'secrets'" {
assert_tar_includes test/etc/secrets/key test0.tar.bz2
assert_tar_excludes test/etc/secrets/key
assert_tar_excludes test/etc/secrets/
}
# vim: ft=bash

44
tests/test_helper.bash Normal file
View file

@ -0,0 +1,44 @@
#!/bin/bash
test_directory="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export PATH="$test_directory/..:$PATH"
test_uname="test-uname"
uname() { echo "$test_uname"; }
export -f uname
# bypasses mkstage4.sh root check
whoami() { echo "root"; }
export -f whoami
skip_if_not_root() {
if [ "$EUID" -ne 0 ]; then
skip "Must be root for this test."
fi
}
d() {
mkdir -p "${1}"
}
f() {
mkdir -p "$(dirname "${1}")" && touch "${1}"
}
assert_tar_includes() {
test -f "${1}" || test -d "${1}"
tar --list -f "${2-test.tar.bz2}" | grep -q "^${1}$"
}
assert_tar_includes_partial() {
tar --list -f "${2-test.tar.bz2}" | grep -q "${1}"
}
assert_tar_excludes() {
test -f "${1}" || test -d "${1}"
! tar --list -f "${2-test.tar.bz2}" | grep -q "^${1}$"
}
assert_tar_excludes_partial() {
! tar --list -f "${2-test.tar.bz2}" | grep -q "${1}"
}