flatcar-scripts/mod_test_image_for_pyauto.sh
Brian Harring 7f175a59e1 common.sh: output a backtrace and debug information on failure.
Currently, if set -e spots a nonzero exit we basically have
no real debug information- it just stops immediately without stating
where or why.  This forces our scripts to be stupidly verbose so
we can track roughly where they were, thus when they fail we can
use that information to localize the rough exit point.

Instead we should be traping that set -e induced exit and
outputing necessary debug information to run it down.  This includes
outputing the relevant stack trace, or at least what we can get of
it.

The 'die' function is now enhanced to automatically dump the trace
that lead to it.  For most consumers this is desired- however for
commandline parsing induced dies ("--board is missing" for example),
the trace is noise.  For those cases, a 'die_notrace' function was
added that retains the original non-backtrace behaviour.

Example output via instrumenting cros_generate_breakpad_symbols
w/ the failing command '/bin/false' (nonzero exit code).

Before:
./cros_generate_breakpad_symbols  monkeys --board=x86-alex
<no output at all, just exit code 1>

With this CL:
./cros_generate_breakpad_symbols  monkeys --board=x86-alex
ERROR   : script called: ./cros_generate_breakpad_symbols 'monkeys' '--board=x86-alex'
ERROR   : Backtrace:  (most recent call is last)
ERROR   :   file cros_generate_breakpad_symbols, line 207, called: main 'monkeys' '--board=x86-alex'
ERROR   :   file cros_generate_breakpad_symbols, line 163, called: die_err_trap '/bin/false' '1'
ERROR   :
ERROR   : Command failed:
ERROR   :   Command '/bin/false' exited with nonzero code: 1

BUG=chromium-os:30598
TEST=inject a failing command into a script, verify the output.
TEST=inject a 'command not found', verify the output
TEST=cbuildbot x86-generic-full --remote
TEST=cbuildbot arm-tegra2-full --remote
TEST=cbuildbot chromiumos-sdk --remote

Change-Id: I517ffde4d1bb7e2310a74f5a6455b53ba2dea86c
Reviewed-on: https://gerrit.chromium.org/gerrit/17225
Reviewed-by: Brian Harring <ferringb@chromium.org>
Tested-by: Brian Harring <ferringb@chromium.org>
Commit-Ready: Brian Harring <ferringb@chromium.org>
2012-05-07 17:19:41 -07:00

195 lines
6.5 KiB
Bash

#!/bin/bash
# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Script to modify a keyfob-based chromeos test image to install pyauto.
SCRIPT_ROOT=$(dirname $(readlink -f "$0"))
. "${SCRIPT_ROOT}/common.sh" || { echo "Unable to load common.sh"; exit 1; }
cleanup() {
"${SCRIPTS_DIR}/mount_gpt_image.sh" -u -r "$ROOT_FS_DIR" -s "$STATEFUL_FS_DIR"
}
# Need to be inside the chroot to load chromeos-common.sh
assert_inside_chroot
get_default_board
DEFINE_string board "$DEFAULT_BOARD" "Board for which the image was built" b
DEFINE_string image "$FLAGS_image" "Location of the test image file" i
DEFINE_boolean scrub "$FLAGS_FALSE" "Don't include pyauto tests and data" s
DEFINE_string build_root "/build" \
"The root location for board sysroots, override with autotest bundle"
# Parse command line
FLAGS "$@" || exit 1
eval set -- "$FLAGS_ARGV"
if [ "${FLAGS_build_root}" = "/build" ]; then
FLAGS_build_root="/build/${FLAGS_board}/usr/local/autotest"
fi
FLAGS_build_root=$(eval readlink -f "${FLAGS_build_root}")
FLAGS_image=$(eval readlink -f "${FLAGS_image}")
IMAGE_DIR=$(dirname "${FLAGS_image}")
IMAGE_NAME=$(basename "${FLAGS_image}")
ROOT_FS_DIR="${IMAGE_DIR}/rootfs"
STATEFUL_FS_DIR="${IMAGE_DIR}/stateful"
PYAUTO_DEP="${FLAGS_build_root}/client/deps/pyauto_dep"
CHROME_DEP="${FLAGS_build_root}/client/deps/chrome_test"
VBOOT_DIR="${CHROOT_TRUNK_DIR}/src/platform/vboot_reference/scripts/"\
"image_signing"
if [ ! -d $PYAUTO_DEP ]; then
die_notrace "The required path: $PYAUTO_DEP does not exist. Did you mean \
to pass --build_root and the path to the autotest bundle?"
fi
if [ ! -d $CHROME_DEP ]; then
die_notrace "The required path: $CHROME_DEP does not exist. Did you mean \
to pass --build_root and the path to the autotest bundle?"
fi
if [ ! -d $VBOOT_DIR ]; then
die_notrace "The required path: $VBOOT_DIR does not exist. This directory \
needs to be sync'd into your chroot.\n $ cros_workon start vboot_reference \
--board ${FLAGS_board}"
fi
if [ ! -d "${FLAGS_build_root}/client/cros" ]; then
die "The required path: ${FLAGS_build_root}/client/cros does not exist."
fi
trap cleanup EXIT
cleanup EXIT
# Mounts gpt image and sets up var, /usr/local and symlinks.
"$SCRIPTS_DIR/mount_gpt_image.sh" -i "$IMAGE_NAME" -f "$IMAGE_DIR" \
-r "$ROOT_FS_DIR" -s "$STATEFUL_FS_DIR"
STATEFUL_FS_AUTOTEST_DIR="${STATEFUL_FS_DIR}/dev_image/autotest"
IMAGE_TEST_SRC_DIR="${STATEFUL_FS_AUTOTEST_DIR}/deps/chrome_test/test_src"
IMAGE_RELEASE_DIR="${IMAGE_TEST_SRC_DIR}/out/Release"
sudo mkdir -p "${STATEFUL_FS_AUTOTEST_DIR}"
sudo mkdir -p "${IMAGE_TEST_SRC_DIR}"
sudo mkdir -p "${IMAGE_RELEASE_DIR}"
sudo cp -f -r "${FLAGS_build_root}/client/cros" "${STATEFUL_FS_AUTOTEST_DIR}"
# We want to copy everything that is in this directory except the out folder
# since it has very large test binaries that we don't need for pyauto.
info "Copying test source depedencies..."
for item in base chrome content net pdf third_party; do
info "Copying $item to ${IMAGE_TEST_SRC_DIR}"
sudo cp -f -r "${CHROME_DEP}/test_src/$item" "${IMAGE_TEST_SRC_DIR}"
done
info "Copying release binaries..."
binaries=( chromedriver pyautolib.py _pyautolib.so pyproto setup_test_links.sh \
test_data )
for item in "${binaries[@]}"; do
info "Copying $item to ${IMAGE_RELEASE_DIR}"
sudo cp -f -r "${CHROME_DEP}/test_src/out/Release/$item" \
"${IMAGE_RELEASE_DIR}/"
done
info "Copying pyauto dependencies..."
sudo cp -r $PYAUTO_DEP "${STATEFUL_FS_AUTOTEST_DIR}/deps"
if [ $FLAGS_scrub -eq $FLAGS_TRUE ]; then
IMAGE_TEST_DIR="${IMAGE_TEST_SRC_DIR}/chrome"
sudo rm -rf \
"${IMAGE_TEST_DIR}/data/" \
"${IMAGE_TEST_DIR}/functional/"
sudo mkdir \
"${IMAGE_TEST_DIR}/data/" \
"${IMAGE_TEST_DIR}/functional/"
sudo cp "${CHROME_DEP}/test_src/chrome/test/functional/pyauto_functional.py" \
"${IMAGE_TEST_DIR}/functional/"
# Create an example pyauto test.
echo -e "#!/usr/bin/python\n\
# Copyright (c) 2011 The Chromium Authors. All rights reserved.\n\
# Use of this source code is governed by a BSD-style license that can be\n\
# found in the LICENSE file.\n\
\n\
import os\n\
import subprocess\n\
\n\
import pyauto_functional # Must be imported before pyauto\n\
import pyauto\n\
\n\
class ChromeosDemo(pyauto.PyUITest):\n\
\"\"\"Example PyAuto test for ChromeOS.\n\
\n\
To run this test, you must be logged into the Chromebook as root. Then run\n\
the following command:\n\
$ python example.py\n\
\"\"\"\n\
\n\
assert os.geteuid() == 0, 'Need to run this test as root'\n\
\n\
def testLoginAsGuest(self):\n\
\"\"\"Test we can login with guest mode.\"\"\"\n\
self.LoginAsGuest()\n\
login_info = self.GetLoginInfo()\n\
self.assertTrue(login_info['is_logged_in'], msg='Not logged in at all.')\n\
self.assertTrue(login_info['is_guest'], msg='Not logged in as guest.')\n\
\n\
if __name__ == '__main__':\n\
pyauto_functional.Main()" > "/tmp/example.py"
sudo cp "/tmp/example.py" \
"${STATEFUL_FS_AUTOTEST_DIR}/deps/chrome_test/test_src/\
chrome/test/functional/example.py"
fi
# In some chroot configurations chronos is not configured, so we use 1000
sudo chown -R 1000 "${STATEFUL_FS_AUTOTEST_DIR}"
sudo chgrp -R 1000 "${STATEFUL_FS_AUTOTEST_DIR}"
# Based on how the autotest package is extracted, the user running in the chroot
# may not have access to navigate into this folder because only the owner
# (chronos) has access. This fixes that so anyone can access.
sudo chmod 747 -R "${STATEFUL_FS_AUTOTEST_DIR}"
# Setup permissions and symbolic links
for item in chrome_test pyauto_dep; do
pushd .
cd "${STATEFUL_FS_AUTOTEST_DIR}/deps/$item/test_src/out/Release"
sudo cp "${ROOT_FS_DIR}/usr/local/bin/python2.6" suid-python
sudo chown root:root suid-python
sudo chmod 4755 suid-python
sudo sh setup_test_links.sh
popd
done
# Add an easy link to get to the functional folder
sudo ln -f -s \
"/usr/local/autotest/deps/chrome_test/test_src/chrome/test/functional" \
"${ROOT_FS_DIR}/pyauto"
cleanup
# cros_make_image_bootable is unstable (crosbug.com/18709)
DEVKEYS_DIR="${CHROOT_TRUNK_DIR}/src/platform/vboot_reference/tests/devkeys/"
TMP_BIN_PATH="$(dirname "${FLAGS_image}")/pyauto_tmp.bin"
echo ${TMP_BIN_PATH}
rm -f "${TMP_BIN_PATH}"
"${VBOOT_DIR}/sign_official_build.sh" usb "${FLAGS_image}" \
"${DEVKEYS_DIR}" \
"${TMP_BIN_PATH}" \
rm -f "${FLAGS_image}"
mv "${TMP_BIN_PATH}" "${FLAGS_image}"