3

I am using shUnit2 to do unit testing in Bash shell scripts.

I have code like this:

cat > /etc/somefile <<EOF
some file content
EOF

I want to write unit tests to test this code, but to do that I would need to avoid the file I/O redirection.

I realise I could refactor the code to move the bits that redirect file output inside functions, e.g.

cat_somefile() {
  cat > /etc/somefile <<EOF
some file content
EOF
}

And then I could stub this function in my tests.

Is there any way to test this code without refactoring it though?

Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
  • 1
    You could use `chroot` to make your script see some arbitrary directory (like `/home/aharvey/test`) as its root file system (so that what it thinks is `/etc/somefile` is actually `/home/aharvey/test/etc/somefile`). This requires a bit of setup, though, since you would also need, for example, `/home/aharvey/test/bin/cat` available for your script to use as `/bin/cat`. – chepner Jul 02 '17 at 14:21

1 Answers1

2

As suggested by @chepner (and see also ref) chroot can help here.

To set this all up:

1/ Grant sudo access to run the chroot command.

$ sudo visudo
...
%admin    ALL = (ALL) NOPASSWD: /usr/sbin/chroot

2/ Create an include script and a function to set up the jail.

FAKE_ROOT=./fake_root

copy_to_jail() {
    d=$(dirname $1)
    mkdir -p $FAKE_ROOT/$d
    cp -p $1 $FAKE_ROOT/$d
}

jail_files() {

    # All of these commands are used at least once by one of the
    # scripts under test.

    commands="
        /bin/cat
        /bin/chmod
        /bin/date
        /bin/mkdir
        /bin/rm
        /usr/bin/awk
        /usr/bin/cut
        /usr/bin/tee
    "

    local OPTIND script wrappers
    while getopts "s:w:" o
    do
        case "${o}" in
        s)
            script="${OPTARG}"
            ;;
        w)
            wrappers="${wrappers} ${OPTARG}"
            ;;
        esac
    done
    shift $((OPTIND-1))

    # Without /bin/sh the jail doesn't work.
    # I didn't spend time to figure out why.

    for f in /bin/sh $script $wrappers $commands
    do
        copy_to_jail $f
    done

    # We figure out all the library files we need to build
    # a working jail just by trying to start it, observing which file
    # it says is missing, copying the missing file, and repeating
    # until it starts.

    case $(uname -s) in
    Darwin)
        # Copy Bash 4 and GNU Sed.
        cp -p /usr/local/bin/bash $FAKE_ROOT/bin
        cp -p /usr/local/bin/gsed "$FAKE_ROOT/bin/sed"

        initial_libs=$(
            for f in /usr/local/bin/bash
            do
                otool -L $f | awk 'NR > 1 {print $1}'
            done | sort -u)
        initial_libs="/usr/lib/dyld $initial_libs"

        for f in $initial_libs
        do
            copy_to_jail $f
        done

        who_i_am=$(whoami)

        set -o pipefail
        while true
        do
            missing_lib=$(sudo chroot -u $who_i_am $FAKE_ROOT /bin/sh -c echo 2>&1 | \
                awk '/dyld: Library not loaded:/ {print $5}')
            [ "$?" -eq 0 ] && break
            copy_to_jail $missing_lib
        done
        ;;

    *)
        echo "Platform $(uname -s) not supported"
        exit 1
        ;;
    esac
}

clean_up_jail() {
    rm -rf $FAKE_ROOT
}

3/ In the test file:

. shunit2/include.sh

script_under_test=path/to/my/script.sh

make_jail() {

    # Copy any binaries that the script depends on
    # to the jail.

    jail_files \
        -s $script_under_test

    # Create any directories that are expected to exist.
    mkdir -p $FAKE_ROOT/etc/apt/preferences.d
    mkdir -p $FAKE_ROOT/etc/{default,init.d}
    mkdir -p $FAKE_ROOT/etc/apache2/sites-enabled
    mkdir -p $FAKE_ROOT/tmp
    ...

    # Set up any files with initial content.
    echo "some initial content" > $FAKE_ROOT/etc/hosts

    who_i_am=$(whoami)
    sudo chroot -u $who_i_am $FAKE_ROOT \
        /bin/$(basename $script_under_test) > /dev/null
}

oneTimeSetUp() {
    make_jail
}

oneTimeTearDown() {
    clean_up_jail
}

# Write actual tests.
test_something() {
  ...
}

test_something_else() {
  ...
}

# Call shUnit2.
. shunit2

I will write a blog post about this at some stage.

Alex Harvey
  • 14,494
  • 5
  • 61
  • 97