summaryrefslogtreecommitdiffstats
path: root/run_test_suite
blob: acfcd2523f77cac7c227242a554fda61c9eeb22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# Note that we must use bash since we are gonna source files
# that have bashisms in them.

set -e

NAME=$(basename ${0})

usage() {
    echo "Usage: $NAME [OPTION]... ISO [FEATURE]...
Tests the FEATUREs (all by default) of ISO. Note that this command must be run
from the Tails source directory.

Options:
  --capture=FILE     Captures the test session into FILE using VP8 encoding.
                     Requires ffmpeg and libvpx0.
  --view             Shows the test session in a windows. Requires x11vnc
                     and xtightvncviewer.
  --vnc-server-only  Starts a VNC server for the test session. Requires x11vnc.
"
}

error() {
    echo "${NAME}: error: ${*}" >&2
    usage
    exit 1
}

check_dependency() {
    if ! which "${1}" >/dev/null && ! dpkg -s "${1}" >/dev/null 2>&1; then
        error "'${1}' is missing, please install it and run again. Aborting..."
    fi
}

display_in_use() {
    [ -e "/tmp/.X${1#:}-lock" ]
}

next_free_display() {
    display_nr=0
    while display_in_use ":${display_nr}"; do
	display_nr=$((display_nr+1))
    done
    echo ":${display_nr}"
}

start_xvfb() {
    Xvfb $TARGET_DISPLAY -screen 0 1024x768x24+32 >/dev/null 2>&1 &
    XVFB_PID=$!
    trap "kill -9 ${XVFB_PID}" EXIT
    # Wait for Xvfb to run on TARGET_DISPLAY
    until display_in_use $TARGET_DISPLAY; do
	sleep 1
    done
    echo "Virtual X framebuffer started on display ${TARGET_DISPLAY}"
    # Hide the mouse cursor so it won't mess up Sikuli's screen scanning
    unclutter -display $TARGET_DISPLAY -root -idle 0 >/dev/null 2>&1 &
}

start_vnc_server() {
    check_dependency x11vnc
    VNC_SERVER_PORT="$(x11vnc -listen localhost -display ${TARGET_DISPLAY} \
                              -bg -nopw 2>&1 | \
                                  grep -m 1 "^PORT=[0-9]\+" | sed 's/^PORT=//')"
    echo "VNC server running on: localhost:${VNC_SERVER_PORT}"
}

start_vnc_viewer() {
    check_dependency xtightvncviewer
    xtightvncviewer -viewonly localhost:${VNC_SERVER_PORT} 1>/dev/null 2>&1 &
}

capture_session() {
    check_dependency ffmpeg
    check_dependency libvpx0
    echo "Capturing guest display into ${CAPTURE_FILE}"
    ffmpeg -f x11grab -s 1024x768 -r 15 -i ${TARGET_DISPLAY}.0 -an \
        -vcodec libvpx -y "${CAPTURE_FILE}" >/dev/null 2>&1 &
}

# main script

unset CAPTURE_FILE VNC_VIEWER VNC_SERVER
SHORTOPTS="a:c"
LONGOPTS="view,vnc-server-only,capture:,help"
OPTS=$(getopt -o $SHORTOPTS --longoptions $LONGOPTS -n "${NAME}" -- "$@")
eval set -- "$OPTS"
while [ $# -gt 0 ]; do
    case $1 in
        --view)
            VNC_VIEWER=yes
            VNC_SERVER=yes
            ;;
        --vnc-server-only)
            VNC_VIEWER=
            VNC_SERVER=yes
            ;;
        --capture)
            shift
            CAPTURE_FILE="$1"
            ;;
        --help)
	    usage
            exit 0
            ;;
        --)
            shift
            if [ -n "$1" ]; then
                ISO="$(readlink -f $1)"
            fi
            shift
            break
            ;;
    esac
    shift
done

if [ -z "${ISO}" ]; then
    error "No ISO provided. Aborting..."
fi

for dep in git libvirt-bin libvirt-dev virt-viewer libsikuli-script-java \
            libxslt1-dev libxml2-dev tcpdump xvfb; do
    check_dependency "${dep}"
done

TARGET_DISPLAY=$(next_free_display)

start_xvfb

if [ -n "${CAPTURE_FILE}" ]; then
    capture_session
fi
if [ -n "${VNC_SERVER}" ]; then
    start_vnc_server
fi
if [ -n "${VNC_VIEWER}" ]; then
    start_vnc_viewer
fi

# Workaround: when dumping the guest's memory via core_dump(), libvirt
# will create files that only root can read. We therefore pre-create
# them with more permissible permissions, which libvirt will preserve
# (although it will change ownership). so that the user running the
# script can grep the dumps for the fillram pattern.
for file in features/tmpfs/after_wipe.dump features/tmpfs/before_wipe.dump; do
    if [ ! -e "${file}" ]; then
        touch "${file}"
    fi
    if [ -O "${file}" ]; then
        chmod a+r "${file}"
    fi
done

# Workaround: similarly to above, when libvirt takes ownership of the
# ISO image it may become unreadable for the amnesia user inside guest
# in the host-to-guest share used for some tests.
if [ -O "${ISO}" ]; then
    chmod a+r "${ISO}"
fi

export SIKULI_HOME="/usr/share/java"
export DISPLAY=${TARGET_DISPLAY}
export ISO="${ISO}"
. features/.rvmrc
check_dependency cucumber
if [ -z "${*}" ]; then
    cucumber features/cucumber/
else
    cucumber features/cucumber/step_definitions features/cucumber/support ${*}
fi