summary: Ensure that the writable paths on the image are correct

details: |
    The file writable-paths allows specific paths in the root partition filesystem
    to be made writable, whilst leaving the rest of the root filesystem read-only.
    It does this by creating overlaying writable mounts on top of the read-only
    root filesystem. These writable mounts are backed to the writable partition.

    This test ensures everything in writable-paths is actually writable by checking
    in the paths declared in /etc/system-image/writable-paths.

execute: |
    echo "Ensure everything in writable-paths is actually writable"
    #shellcheck disable=SC2002
    cat /etc/system-image/writable-paths | while read -r line; do
        line=$(echo "$line" | sed -e '/\s*#.*$/d')
        if [ -z "$line" ]; then
            continue;
        fi

        # a writable-path may be either a file or a directory
        dir_or_file=$(echo "$line"|cut -f1 -d' ')

        # fun! systemd is playing tricks with /etc/machine-id
        # and mounts a RO tmpfs on top of the writable one which
        # means we cannot touch this file
        if [ "$dir_or_file" = "/etc/machine-id" ]; then
            continue;
        fi

        if [ ! -e "$dir_or_file" ]; then
            echo "$dir_or_file" >> missing
        elif [ -f "$dir_or_file" ]; then
            if ! touch "$dir_or_file"; then
                echo "$dir_or_file" >> broken
            fi
        elif ! touch "$dir_or_file"/random-name-that-I-made-up; then
            echo "$dir_or_file" >> broken
        fi
        rm -f "$dir_or_file"/random-name-that-I-made-up
    done

    if [ -s "broken" ]; then
        echo "The following writable paths are not writable:"
        cat broken
    fi
    if [ -s "missing" ]; then
        echo "The following writable paths are missing:"
        cat missing
    fi
    # FIMXE: make missing fatal as well
    #if [ -s missing ] || [ -s broken ]; then
    #    exit 1
    #fi
    if [ -s broken ]; then
         exit 1
    fi
