Skip to content

Commit

Permalink
tests: run the tests in travis using LXD containers (#1415)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo Arias authored and sergiusens committed Jul 21, 2017
1 parent 5d5d8f2 commit 8c9a38c
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 12 deletions.
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ jobs:
include:
# Tests, only when not triggered by the daily cron.
- stage: static
script: if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then ./tools/travis/run_tests.sh static; fi
script: if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then sudo ./tools/travis/run_tests.sh static; fi
- stage: unit
script: if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then ./tools/travis/run_tests.sh unit; fi
- script: if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then SNAPCRAFT_TEST_MOCK_MACHINE=armv7l ./tools/travis/run_tests.sh unit; fi
script: if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then sudo ./tools/travis/run_tests.sh unit; fi
- script: if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then SNAPCRAFT_TEST_MOCK_MACHINE=armv7l sudo ./tools/travis/run_tests.sh unit; fi
- stage: integration
script: if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then ./tools/travis/run_tests.sh integration; fi
- script: if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then ./tools/travis/run_tests.sh plugins; fi
- script: if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then ./tools/travis/run_tests.sh store; fi
script: if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then sudo ./tools/travis/run_tests.sh integration; fi
- script: if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then sudo ./tools/travis/run_tests.sh plugins; fi
- script: if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then sudo ./tools/travis/run_tests.sh store; fi
# CLA check, only in pull requests.
- script: if [ "$TRAVIS_PULL_REQUEST" != "false" ] && [ "$TRAVIS_EVENT_TYPE" != 'cron' ]; then ./tools/travis/run_cla_check.sh; fi
# Trigger edge tests, only in the daily cron.
Expand Down
55 changes: 55 additions & 0 deletions tools/travis/run_lxc_container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash
#
# Copyright (C) 2017 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Start a lxc container to run jobs in travis.
# Arguments:
# name: The name of the container.

set -ev

if [ "$#" -lt 1 ]; then
echo "Usage: "$0" <name>"
exit 1
fi

script_path="$(dirname "$0")"
project_path="$(readlink -f "$script_path/../..")"
name="$1"

lxc="/snap/bin/lxc"

echo "Start the LXC container."
$lxc launch --ephemeral ubuntu:xenial "$name"
# This is likely needed to wait for systemd in the container to start and get
# an IP, configure DNS. First boot is always a bit slow because cloud-init
# needs to run too.
$lxc exec "$name" -- sh -c "for i in {1..50}; do ping -c1 www.ubuntu.com &> /dev/null && break; done"
sleep 5

$lxc config set "$name" environment.TRAVIS_COMMIT_RANGE "$TRAVIS_COMMIT_RANGE"
$lxc config set "$name" environment.TEST_USER_EMAIL "$TEST_USER_EMAIL"
$lxc config set "$name" environment.TEST_USER_PASSWORD "$TEST_USER_PASSWORD"
$lxc config set "$name" environment.TEST_STORE "$TEST_STORE"
$lxc config set "$name" environment.TEST_SNAP_WITH_TRACKS "$TEST_SNAP_WITH_TRACKS"
$lxc config set "$name" environment.GITHUB_TEST_USER_NAME "$GITHUB_TEST_USER_NAME"
$lxc config set "$name" environment.GITHUB_TEST_PASSWORD "$GITHUB_TEST_PASSWORD"
$lxc config set "$name" environment.SNAPCRAFT_AUTOPKGTEST_SECRET "$SNAPCRAFT_AUTOPKGTEST_SECRET"
$lxc config set "$name" environment.SNAPCRAFT_TEST_MOCK_MACHINE "$SNAPCRAFT_TEST_MOCK_MACHINE"
$lxc config set "$name" environment.GH_TOKEN "$GH_TOKEN"
$lxc config set "$name" environment.CODECOV_TOKEN "$CODECOV_TOKEN"
$lxc config set "$name" environment.LC_ALL "C.UTF-8"

$lxc exec "$name" -- apt update
55 changes: 55 additions & 0 deletions tools/travis/run_lxd_container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash
#
# Copyright (C) 2017 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Start a LXD container to run jobs in travis.
# Arguments:
# name: The name of the container.

set -ev

if [ "$#" -lt 1 ]; then
echo "Usage: "$0" <name>"
exit 1
fi

script_path="$(dirname "$0")"
project_path="$(readlink -f "$script_path/../..")"
name="$1"

lxc="/snap/bin/lxc"

echo "Starting the LXD container."
$lxc launch --ephemeral ubuntu:xenial "$name"
# This is likely needed to wait for systemd in the container to start and get
# an IP, configure DNS. First boot is always a bit slow because cloud-init
# needs to run too.
$lxc exec "$name" -- sh -c "for i in {1..50}; do ping -c1 www.ubuntu.com &> /dev/null && break; done"
sleep 5

$lxc config set "$name" environment.TRAVIS_COMMIT_RANGE "$TRAVIS_COMMIT_RANGE"
$lxc config set "$name" environment.TEST_USER_EMAIL "$TEST_USER_EMAIL"
$lxc config set "$name" environment.TEST_USER_PASSWORD "$TEST_USER_PASSWORD"
$lxc config set "$name" environment.TEST_STORE "$TEST_STORE"
$lxc config set "$name" environment.TEST_SNAP_WITH_TRACKS "$TEST_SNAP_WITH_TRACKS"
$lxc config set "$name" environment.GITHUB_TEST_USER_NAME "$GITHUB_TEST_USER_NAME"
$lxc config set "$name" environment.GITHUB_TEST_PASSWORD "$GITHUB_TEST_PASSWORD"
$lxc config set "$name" environment.SNAPCRAFT_AUTOPKGTEST_SECRET "$SNAPCRAFT_AUTOPKGTEST_SECRET"
$lxc config set "$name" environment.SNAPCRAFT_TEST_MOCK_MACHINE "$SNAPCRAFT_TEST_MOCK_MACHINE"
$lxc config set "$name" environment.GH_TOKEN "$GH_TOKEN"
$lxc config set "$name" environment.CODECOV_TOKEN "$CODECOV_TOKEN"
$lxc config set "$name" environment.LC_ALL "C.UTF-8"

$lxc exec "$name" -- apt update
18 changes: 12 additions & 6 deletions tools/travis/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,20 @@ else
fi

script_path="$(dirname "$0")"
"$script_path/run_docker_container.sh" test-runner
docker exec -i test-runner sh -c "$dependencies"
docker exec -i test-runner ./runtests.sh $test $pattern
project_path="$(readlink -f "$script_path/../..")"

lxc="/snap/bin/lxc"

"$script_path/setup_lxd.sh"
"$script_path/run_lxd_container.sh" test-runner
$lxc file push --recursive $project_path test-runner/root/
$lxc exec test-runner -- sh -c "cd snapcraft && $dependencies"
$lxc exec test-runner -- sh -c "cd snapcraft && ./runtests.sh $test $pattern"

if [ "$test" = "unit" ]; then
# Report code coverage.
docker exec -i test-runner sh -c "python3 -m coverage xml"
docker exec -i test-runner sh -c "codecov --token=$CODECOV_TOKEN"
$lxc exec test-runner -- sh -c "cd snapcraft && python3 -m coverage xml"
$lxc exec test-runner -- sh -c "cd snapcraft && codecov --token=$CODECOV_TOKEN"
fi

docker rm -f test-runner
$lxc stop test-runner
38 changes: 38 additions & 0 deletions tools/travis/setup_lxd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
#
# Copyright (C) 2017 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Set up LXD in travis.

set -ev

apt-get update
apt-get install --yes snapd
# Use edge because the feature to copy links to the container has not yet been
# released to stable:
# https://github.com/lxc/lxd/commit/004e7c361e1d914795d3ba7582654622e32ff193
snap install lxd --edge
# Wait while LXD first generates its keys. In a low entropy environment this
# can take a while.

# From LXD's CI.
# shellcheck disable=SC2034
for i in $(seq 12); do
lxd waitready --timeout=10 >/dev/null 2>&1 && break
done

/snap/bin/lxd init --auto
/snap/bin/lxc network create testbr0
/snap/bin/lxc network attach-profile testbr0 default eth0

0 comments on commit 8c9a38c

Please sign in to comment.