Skip to content

Commit

Permalink
tests: add "quiet" wrapper function that only prints output on failure (
Browse files Browse the repository at this point in the history
  • Loading branch information
chipaca authored and niemeyer committed Jan 17, 2017
1 parent 190f508 commit 4c7ad5c
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 119 deletions.
128 changes: 9 additions & 119 deletions spread.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -202,132 +202,22 @@ prepare: |
trap "sysctl -w net.ipv6.conf.all.disable_ipv6=0" EXIT
# Unpack delta, or move content out of the prefixed directory (see rename and repack above).
# (needs to be in spread.yaml directly because there's nothing else on the filesystem yet)
if [ -f current.delta ]; then
apt-get update
apt-get install -y xdelta3 curl
curl -s -o - https://codeload.github.com/snapcore/snapd/tar.gz/$DELTA_REF | gunzip > delta-ref.tar
xdelta3 -d -s delta-ref.tar current.delta | tar x --strip-components=1
tf=$(mktemp)
# poor-man's "quiet"
apt-get update >& "$tf" || ( cat "$tf"; exit 1 )
apt-get install -y xdelta3 curl >& "$tf" || ( cat "$tf"; exit 1 )
rm -f "$tf"
curl -sS -o - https://codeload.github.com/snapcore/snapd/tar.gz/$DELTA_REF | gunzip > delta-ref.tar
xdelta3 -q -d -s delta-ref.tar current.delta | tar x --strip-components=1
rm -f delta-ref.tar current.delta
elif [ -d $DELTA_PREFIX ]; then
find $DELTA_PREFIX -mindepth 1 -maxdepth 1 -exec mv {} . \;
rmdir $DELTA_PREFIX
fi
# Set REUSE_PROJECT to reuse the previous prepare when also reusing the server.
[ "$REUSE_PROJECT" != 1 ] || exit 0
echo "Running with SNAP_REEXEC: $SNAP_REEXEC"
# check that we are not updating
. "$TESTSLIB/boot.sh"
if [ "$(bootenv snap_mode)" = "try" ]; then
echo "Ongoing reboot upgrade process, please try again when finished"
exit 1
fi
if [ "$SPREAD_BACKEND" = external ]; then
# build test binaries
if [ ! -f $GOPATH/bin/snapbuild ]; then
mkdir -p $GOPATH/bin
snap install --devmode --edge classic
classic "sudo apt update && apt install -y git golang-go build-essential"
classic "GOPATH=$GOPATH go get ../..${PROJECT_PATH}/tests/lib/snapbuild"
snap remove classic
fi
# stop and disable autorefresh
systemctl disable --now snapd.refresh.timer
exit 0
fi
if [ "$SPREAD_BACKEND" = qemu ]; then
# treat APT_PROXY as a location of apt-cacher-ng to use
if [ -d /etc/apt/apt.conf.d ] && [ -n "${APT_PROXY:-}" ]; then
printf 'Acquire::http::Proxy "%s";\n' "$APT_PROXY" > /etc/apt/apt.conf.d/99proxy
fi
fi
apt-get update
# XXX: This seems to be required. Otherwise package build
# fails with unmet dependency on "build-essential:native"
apt-get install -y build-essential
apt-get install -y software-properties-common
if [[ "$SPREAD_SYSTEM" == ubuntu-14.04-* ]]; then
if [ ! -d debian-ubuntu-14.04 ]; then
echo "no debian-ubuntu-14.04/ directory "
echo "broken test setup"
exit 1
fi
# 14.04 has its own packaging
rm -rf debian
mv debian-ubuntu-14.04 debian
echo 'deb http://archive.ubuntu.com/ubuntu/ trusty-proposed main universe' >> /etc/apt/sources.list
apt-get update
add-apt-repository ppa:snappy-dev/image
apt-get update
apt-get install -y --install-recommends linux-generic-lts-xenial
apt-get install -y --force-yes apparmor libapparmor1 seccomp libseccomp2 systemd cgroup-lite util-linux
fi
apt-get purge -y snapd snap-confine ubuntu-core-launcher
apt-get update
# utilities
apt-get install -y curl devscripts expect gdebi-core jq rng-tools git
# in 16.04: apt build-dep -y ./
apt-get install -y $(gdebi --quiet --apt-line ./debian/control)
# update vendoring
if [ "$(which govendor)" = "" ]; then
rm -rf $GOPATH/src/github.com/kardianos/govendor
go get -u github.com/kardianos/govendor
fi
govendor sync
# increment version so upgrade can work, use "zzz" as version
# component to ensure that its higher than any "ubuntuN" version
# that might also be in the archive
dch -lzzz "testing build"
if ! id test >& /dev/null; then
# manually setting the UID and GID to 12345 because we need to
# know the numbers match for when we set up the user inside
# the all-snap, which has its own user & group database.
# Nothing special about 12345 beyond it being high enough it's
# unlikely to ever clash with anything, and easy to remember.
addgroup --quiet --gid 12345 test
adduser --quiet --uid 12345 --gid 12345 --disabled-password --gecos '' test
fi
owner=$( stat -c "%U:%G" /home/test )
if [ "$owner" != "test:test" ]; then
echo "expected /home/test to be test:test but it's $owner"
exit 1
fi
unset owner
echo 'test ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
chown test.test -R ..
su -l -c "cd $PWD && DEB_BUILD_OPTIONS='nocheck testkeys' dpkg-buildpackage -tc -b -Zgzip" test
# put our debs to a safe place
cp ../*.deb $GOPATH
# Build snapbuild.
apt-get install -y git
go get ./tests/lib/snapbuild
# Build fakestore.
fakestore_tags=
if [ "$REMOTE_STORE" = staging ]; then
fakestore_tags="-tags withstagingkeys"
fi
go get $fakestore_tags ./tests/lib/fakestore/cmd/fakestore
# Build fakedevicesvc.
go get ./tests/lib/fakedevicesvc
. "$TESTSLIB/prepare-project.sh"
restore: |
if [ "$SPREAD_BACKEND" = external ]; then
Expand Down
114 changes: 114 additions & 0 deletions tests/lib/prepare-project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/bin/bash

# Set REUSE_PROJECT to reuse the previous prepare when also reusing the server.
[ "$REUSE_PROJECT" != 1 ] || exit 0
echo "Running with SNAP_REEXEC: $SNAP_REEXEC"

# check that we are not updating
. "$TESTSLIB/boot.sh"
if [ "$(bootenv snap_mode)" = "try" ]; then
echo "Ongoing reboot upgrade process, please try again when finished"
exit 1
fi

# declare the "quiet" wrapper
. "$TESTSLIB/quiet.sh"

if [ "$SPREAD_BACKEND" = external ]; then
# build test binaries
if [ ! -f $GOPATH/bin/snapbuild ]; then
mkdir -p $GOPATH/bin
snap install --devmode --edge classic
classic "sudo apt update && apt install -y git golang-go build-essential"
classic "GOPATH=$GOPATH go get ../..${PROJECT_PATH}/tests/lib/snapbuild"
snap remove classic
fi
# stop and disable autorefresh
systemctl disable --now snapd.refresh.timer
exit 0
fi

if [ "$SPREAD_BACKEND" = qemu ]; then
# treat APT_PROXY as a location of apt-cacher-ng to use
if [ -d /etc/apt/apt.conf.d ] && [ -n "${APT_PROXY:-}" ]; then
printf 'Acquire::http::Proxy "%s";\n' "$APT_PROXY" > /etc/apt/apt.conf.d/99proxy
fi
fi

if [[ "$SPREAD_SYSTEM" == ubuntu-14.04-* ]]; then
if [ ! -d debian-ubuntu-14.04 ]; then
echo "no debian-ubuntu-14.04/ directory "
echo "broken test setup"
exit 1
fi

# 14.04 has its own packaging
rm -rf debian
mv debian-ubuntu-14.04 debian

quiet apt-get update
quiet apt-get install -y software-properties-common

echo 'deb http://archive.ubuntu.com/ubuntu/ trusty-proposed main universe' >> /etc/apt/sources.list
quiet add-apt-repository ppa:snappy-dev/image
quiet apt-get update

quiet apt-get install -y --install-recommends linux-generic-lts-xenial
quiet apt-get install -y --force-yes apparmor libapparmor1 seccomp libseccomp2 systemd cgroup-lite util-linux
fi

quiet apt-get purge -y snapd snap-confine ubuntu-core-launcher
# utilities
# XXX: build-essential seems to be required. Otherwise package build
# fails with unmet dependency on "build-essential:native"
quiet apt-get install -y build-essential curl devscripts expect gdebi-core jq rng-tools git

# in 16.04: apt build-dep -y ./
quiet apt-get install -y $(gdebi --quiet --apt-line ./debian/control)

# update vendoring
if [ "$(which govendor)" = "" ]; then
rm -rf $GOPATH/src/github.com/kardianos/govendor
go get -u github.com/kardianos/govendor
fi
quiet govendor sync

# increment version so upgrade can work, use "zzz" as version
# component to ensure that its higher than any "ubuntuN" version
# that might also be in the archive
dch -lzzz "testing build"

if ! id test >& /dev/null; then
# manually setting the UID and GID to 12345 because we need to
# know the numbers match for when we set up the user inside
# the all-snap, which has its own user & group database.
# Nothing special about 12345 beyond it being high enough it's
# unlikely to ever clash with anything, and easy to remember.
addgroup --quiet --gid 12345 test
adduser --quiet --uid 12345 --gid 12345 --disabled-password --gecos '' test
fi

owner=$( stat -c "%U:%G" /home/test )
if [ "$owner" != "test:test" ]; then
echo "expected /home/test to be test:test but it's $owner"
exit 1
fi
unset owner

echo 'test ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
chown test.test -R ..
quiet su -l -c "cd $PWD && DEB_BUILD_OPTIONS='nocheck testkeys' dpkg-buildpackage -tc -b -Zgzip" test
# put our debs to a safe place
cp ../*.deb $GOPATH

# Build snapbuild.
go get ./tests/lib/snapbuild
# Build fakestore.

fakestore_tags=
if [ "$REMOTE_STORE" = staging ]; then
fakestore_tags="-tags withstagingkeys"
fi
go get $fakestore_tags ./tests/lib/fakestore/cmd/fakestore
# Build fakedevicesvc.
go get ./tests/lib/fakedevicesvc
30 changes: 30 additions & 0 deletions tests/lib/quiet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

# use "quiet foo" when you expect "foo" to produce a lot of output
# that isn't useful unless foo itself fails.
quiet() (
# note this is a subshell (parens instead of braces around the function)
# so this set only affects this function and not the caller
{ set +x; } >&/dev/null

# not strictly needed because it's a subshell, but good practice
local tf retval

tf="$(tempfile)"

set +e
"$@" >& "$tf"
retval=$?
set -e

if [ "$retval" != "0" ]; then
echo "quiet: $*" >&2
echo "quiet: exit status $retval. Output follows:" >&2
cat "$tf" >&2
echo "quiet: end of output." >&2
fi

rm -f -- "$tf"

return $retval
)

0 comments on commit 4c7ad5c

Please sign in to comment.