diff --git a/Makefile b/Makefile index e9e0a1e4..58711607 100644 --- a/Makefile +++ b/Makefile @@ -33,10 +33,7 @@ all: src/pylorax/version.py src/composer/version.py install: all $(PYTHON) setup.py install --root=$(DESTDIR) --prefix=$(PREFIX) mkdir -p $(DESTDIR)/$(mandir)/man1 - install -m 644 docs/man/lorax.1 $(DESTDIR)/$(mandir)/man1 - install -m 644 docs/man/livemedia-creator.1 $(DESTDIR)/$(mandir)/man1 - install -m 644 docs/man/lorax-composer.1 $(DESTDIR)/$(mandir)/man1 - install -m 644 docs/man/composer-cli.1 $(DESTDIR)/$(mandir)/man1 + install -m 644 docs/man/*.1 $(DESTDIR)/$(mandir)/man1 mkdir -p $(DESTDIR)/etc/bash_completion.d install -m 644 etc/bash_completion.d/composer-cli $(DESTDIR)/etc/bash_completion.d @@ -69,6 +66,9 @@ test_images: test_cli: sudo -E ./tests/test_cli.sh +test_mkksiso: + sudo -E ./tests/mkksiso/test_mkksiso.sh + clean_cloud_envs: # clean beakerlib logs from previous executions sudo rm -rf /var/tmp/beakerlib-*/ diff --git a/tests/mkksiso/ks/extra-repo.ks b/tests/mkksiso/ks/extra-repo.ks new file mode 100644 index 00000000..83d1de67 --- /dev/null +++ b/tests/mkksiso/ks/extra-repo.ks @@ -0,0 +1,17 @@ +url --url="https://dl.fedoraproject.org/pub/fedora/linux/development/rawhide/Everything/$basearch/os/" +repo --name=extra-repo --baseurl=file:///run/install/repo/extra-repo/ + +lang en_US.UTF-8 +keyboard us +rootpw --plaintext asdasd +timezone --utc America/New_York + +# partitioning - nuke and start fresh +clearpart --initlabel --all +autopart --type=plain +bootloader --location=mbr +shutdown + +%packages +extra-package +%end diff --git a/tests/mkksiso/ks/liveimg.ks b/tests/mkksiso/ks/liveimg.ks new file mode 100644 index 00000000..405edace --- /dev/null +++ b/tests/mkksiso/ks/liveimg.ks @@ -0,0 +1,13 @@ +install +lang en_US.UTF-8 +keyboard us +rootpw --plaintext asdasd +timezone --utc America/New_York + +# partitioning - nuke and start fresh +clearpart --initlabel --all +autopart --type=plain +bootloader --location=mbr +shutdown + +liveimg --url=file:///run/install/repo/root.tar.xz diff --git a/tests/mkksiso/mk-fake-rpm b/tests/mkksiso/mk-fake-rpm new file mode 100755 index 00000000..9e8c831a --- /dev/null +++ b/tests/mkksiso/mk-fake-rpm @@ -0,0 +1,54 @@ +#!/usr/bin/python3 + +# Copyright (C) 2019 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +from contextlib import contextmanager +from rpmfluff import SimpleRpmBuild, expectedArch +import os +import shutil +import sys +import tempfile + +@contextmanager +def in_tempdir(prefix='tmp'): + """Execute a block of code with chdir in a temporary location""" + oldcwd = os.getcwd() + tmpdir = tempfile.mkdtemp(prefix=prefix) + os.chdir(tmpdir) + try: + yield + finally: + os.chdir(oldcwd) + shutil.rmtree(tmpdir) + + +def makeFakeRPM(repo_dir, name, epoch, version, release): + """Make a fake rpm file in repo_dir""" + p = SimpleRpmBuild(name, version, release) + if epoch: + p.epoch = epoch + p.add_simple_payload_file_random() + with in_tempdir("lorax-test-rpms."): + p.make() + rpmfile = p.get_built_rpm(expectedArch) + shutil.move(rpmfile, repo_dir) + +if __name__ == '__main__': + if len(sys.argv) != 3: + print("%s DIRECTORY RPM-NAME" % os.path.basename(sys.argv[0])) + sys.exit(1) + + makeFakeRPM(os.path.abspath(sys.argv[1]), sys.argv[2], 0, "1.0.0", "1") diff --git a/tests/mkksiso/test_boot_repo.sh b/tests/mkksiso/test_boot_repo.sh new file mode 100755 index 00000000..639fd6d6 --- /dev/null +++ b/tests/mkksiso/test_boot_repo.sh @@ -0,0 +1,58 @@ +#!/usr/bin/bash +# Note: execute this file from the project root directory +set -e + +[ -z "$1" ] && (echo "$0: Missing path to iso"; exit 1) +[ -z "$2" ] && (echo "$0: Missing kickstart"; exit 1) +[ "$(id -u)" -eq 0 ] || (echo "$0 must be run as root"; exit 1) + +. /usr/share/beakerlib/beakerlib.sh +CLI="${CLI:-./src/sbin/mkksiso}" + +rlJournalStart + rlPhaseStartSetup "Setup repo with fake rpm" + TMP_DIR=$(mktemp -d -p /var/tmp) + rlRun -t -c "mkdir $TMP_DIR/extra-repo/" + rlRun -t -c "$(dirname "$0")/mk-fake-rpm $TMP_DIR/extra-repo/ extra-package" + rlRun -t -c "createrepo_c $TMP_DIR/extra-repo/" + rlPhaseEnd + + rlPhaseStartTest "Make new iso with kickstart and extra-repo" + rlRun -t -c "$CLI --add $TMP_DIR/extra-repo $2 $1 $TMP_DIR/ks-boot.iso" + rlAssertExists "$TMP_DIR/ks-boot.iso" + rlPhaseEnd + + rlPhaseStartTest "Check the new ISO" + ISO_DIR="$TMP_DIR/mnt-iso/" + rlRun -t -c "mkdir $ISO_DIR" + rlRun "mount $TMP_DIR/ks-boot.iso $ISO_DIR" + fail=0 + rlLogInfo "Checking for kickstart $(basename "$2")" + if [ ! -e "$ISO_DIR$(basename "$2")" ]; then + rlLogError "The kickstart is missing from the iso" + fail=1 + fi + for cfg in isolinux/isolinux.cfg EFI/BOOT/grub.cfg EFI/BOOT/BOOT.conf \ + boot/grub/grub.cfg images/generic.prm; do + if [ -e "$ISO_DIR$cfg" ]; then + rlLogInfo "Checking $cfg" + if ! grep -q "$(basename "$2")" "$ISO_DIR$cfg"; then + rlLogError "$cfg is missing the kickstart" + fail=1 + fi + fi + done + rlLogInfo "Checking for /extra-repo/ on iso" + if [ ! -e "$TMP_DIR/extra-repo/" ]; then + rlLogError"The extra-repo directory is missing from the iso" + fail=1 + fi + rlAssertEquals "All checks have passed" $fail 0 + rlPhaseEnd + + rlPhaseStartCleanup + rlRun -t -c "umount $ISO_DIR" + rlRun -t -c "rm -rf $TMP_DIR" + rlPhaseEnd +rlJournalEnd +rlJournalPrintText diff --git a/tests/mkksiso/test_liveimg.sh b/tests/mkksiso/test_liveimg.sh new file mode 100755 index 00000000..1d79998c --- /dev/null +++ b/tests/mkksiso/test_liveimg.sh @@ -0,0 +1,58 @@ +#!/usr/bin/bash +# Note: execute this file from the project root directory +set -e + +[ -z "$1" ] && (echo "$0: Missing path to iso"; exit 1) +[ -z "$2" ] && (echo "$0: Missing kickstart"; exit 1) +[ "$(id -u)" -eq 0 ] || (echo "$0 must be run as root"; exit 1) + +. /usr/share/beakerlib/beakerlib.sh +CLI="${CLI:-./src/sbin/mkksiso}" + +rlJournalStart + rlPhaseStartSetup "Setup a fake root image" + TMP_DIR=$(mktemp -d -p /var/tmp) + rlRun -t -c "mkdir -p $TMP_DIR/fake-root/etc" + rlRun -t -c "touch $TMP_DIR/fake-root/etc/passwd" + rlRun -t -c "tar -cvaf $TMP_DIR/root.tar.xz -C $TMP_DIR/fake-root/ ." + rlPhaseEnd + + rlPhaseStartTest "Make a new iso with kickstart and root.tar.xz" + rlRun -t -c "$CLI --add $TMP_DIR/root.tar.xz $2 $1 $TMP_DIR/liveimg-boot.iso" + rlAssertExists "$TMP_DIR/liveimg-boot.iso" + rlPhaseEnd + + rlPhaseStartTest "Check the new ISO" + ISO_DIR="$TMP_DIR/mnt-iso/" + rlRun -t -c "mkdir $ISO_DIR" + rlRun "mount $TMP_DIR/liveimg-boot.iso $ISO_DIR" + fail=0 + rlLogInfo "Checking for kickstart $(basename "$2")" + if [ ! -e "$ISO_DIR$(basename "$2")" ]; then + rlLogError "The kickstart is missing from the iso" + fail=1 + fi + for cfg in isolinux/isolinux.cfg EFI/BOOT/grub.cfg EFI/BOOT/BOOT.conf \ + boot/grub/grub.cfg images/generic.prm; do + if [ -e "$ISO_DIR$cfg" ]; then + rlLogInfo "Checking $cfg" + if ! grep -q "$(basename "$2")" "$ISO_DIR$cfg"; then + rlLogError "$cfg is missing the kickstart" + fail=1 + fi + fi + done + rlLogInfo "Checking for root.tar.xz on iso" + if [ ! -e "$TMP_DIR/root.tar.xz" ]; then + rlLogError "The root.tar.xz file is missing from the iso" + fail=1 + fi + rlAssertEquals "All checks have passed" $fail 0 + rlPhaseEnd + + rlPhaseStartCleanup + rlRun -t -c "umount $ISO_DIR" + rlRun -t -c "rm -rf $TMP_DIR" + rlPhaseEnd +rlJournalEnd +rlJournalPrintText diff --git a/tests/mkksiso/test_mkksiso.sh b/tests/mkksiso/test_mkksiso.sh new file mode 100755 index 00000000..ea0886e5 --- /dev/null +++ b/tests/mkksiso/test_mkksiso.sh @@ -0,0 +1,54 @@ +#!/bin/bash +# Note: execute this file from the project root directory + +set -eu + +[ "$(id -u)" -eq 0 ] || (echo "$0 must be run as root"; exit 1) + +BEAKERLIB_DIR=$(mktemp -d /tmp/composer-test.XXXXXX) +export BEAKERLIB_DIR +CLI="${CLI:-}" + +# Override downloading the iso +TEST_ISO="${TEST_ISO:-}" + +if [ -z "$CLI" ]; then + top_srcdir=$(pwd) + export top_srcdir + . ./tests/testenv.sh +fi + +# Fetch the boot.iso for the current arch and return the path to a temporary directory +ISO_DIR=$(mktemp -d -p /var/tmp/) +function finish { + [ -n "$ISO_DIR" ] && rm -rf "$ISO_DIR" +} +trap finish EXIT + +pushd "$ISO_DIR" +ARCH=$(uname -m) + +if [ -z "$TEST_ISO" ]; then + # Use the Fedora mirrors to select the iso source + BASEURL=$(curl "https://mirrors.fedoraproject.org/mirrorlist?repo=fedora-31&arch=$ARCH" | \ + grep -v "^#" | head -n 1) + curl --remote-name-all "$BASEURL/images/boot.iso" + TEST_ISO="$ISO_DIR/boot.iso" +elif [ ! -e "$TEST_ISO" ]; then + echo "$TEST_ISO is missing." + exit 1 +fi +popd + +export BEAKERLIB_JOURNAL=0 +./tests/mkksiso/test_boot_repo.sh "$TEST_ISO" ./tests/mkksiso/ks/extra-repo.ks +./tests/mkksiso/test_liveimg.sh "$TEST_ISO" ./tests/mkksiso/ks/liveimg.ks + +. $BEAKERLIB_DIR/TestResults + +if [ "$TESTRESULT_RESULT_ECODE" != 0 ]; then + echo "Test failed. Leaving log in $BEAKERLIB_DIR" + exit "$TESTRESULT_RESULT_ECODE" +fi + +rm -rf "$BEAKERLIB_DIR"