Use Bamboo to Test Odoo on Ubuntu 14.04

Using Bamboo to Test Odoo on Ubuntu

—-

[[[TOC]]]

= Foreword =

This article will walk you through the process of using [[https://www.atlassian.com/software/bamboo|Atlassian Bamboo]] to test [[https://odoo.com|Odoo]] on Ubuntu 14.04 and above.

The Odoo community typically uses [[https://travis-ci.org/laslabs|TravisCI]] & [[https://runbot.laslabs.io/runbot|RunBot]] to perform all unit/acceptance testing. This is an excellent workflow if you exclusively use [[https://github.com|Github]] for version control. This is not the case at [[https://laslabs.com|LasLabs]], so the TravisCI workflow is rather cumbersome.

This tutorial is also slightly relevant for [[https://jenkins-ci.org/|Jenkins]], as it is also compatible with JUnit XML test reports. If you are using Jenkins, you will need to adapt the Bamboo steps slightly, but the build script will transfer almost transfer over 1:1 (aside from the environment variables).

= Security Note =

The Docker group is basically equivalent to root, which means that this setup can introduce significant security holes into your infrastructure if the proper precautions are not taken.

As always, test infrastructure should be completely isolated from production in order to limit attack surface from any exploit that may result from the many stages of development.

In order to learn more about Docker security and possible implications in your environment, take a look at the [[https://docs.docker.com/engine/security/security/|Docker Security Docs]].

= Docker =

The first piece of this puzzle is [[https://www.docker.com/|Docker]], which allows us to setup a semi-isolated container for testing.

Following is a brief set of instructions for installation of Docker. For a more extended walkthrough, take a look at [[https://docs.docker.com/engine/installation/linux|the Docker installation docs]]

# Add the Docker GPG key

{{{ lang=bash
sudo apt-key adv \
–keyserver hkp://p80.pool.sks-keyservers.net:80 \
–recv-keys 58118E89F3A912897C070ADBF76221572C52609D
}}}

# Add the Docker repo to apt sources

{{{ lang=bash
RELEASE=`lsb_release –codename | cut -d ‘:’ -f2 | tr -d ‘\t’`
echo “deb https://apt.dockerproject.org/repo ubuntu-$RELEASE main” > \
/etc/apt/sources.list.d/docker.list
}}}

# Update package index & existing applications

{{{ lang=bash
sudo apt-get update &&
sudo apt-get upgrade -y
}}}

# Purge the old Docker repo & add a policy to pull from the correct repo

{{{ lang=bash
apt-get purge lxc-docker &&
apt-cache policy docker-engine
}}}

# Install Prerequisites

{{{ lang=bash
sudo apt-get install -y linux-image-extra-$(uname -r) &&
sudo apt-get install -y apparmor
}}}

# Install Docker

{{{ lang=bash
sudo apt-get install docker-engine
}}}

# Start Docker

{{{ lang=bash
sudo service docker start
}}}

# Verify Docker is installed correctly. downloads a test image and runs it in a container. When the container runs, it prints an informational message. Then, it exits.

{{{ lang=bash
sudo docker run hello-world
}}}

# The above command should output something similar to the below

{{{
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
03f4658f8b78: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7
Status: Downloaded newer image for hello-world:latest

Hello from Docker.
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker Hub account:
https://hub.docker.com

For more examples and ideas, visit:
https://docs.docker.com/userguide/
}}}

= Bamboo =

This article assumes that you already have a working Bamboo installation. Please follow the [[https://confluence.atlassian.com/bamboo/bamboo-installation-guide-289276785.html|install docs]] if you have not already.

== Permissions ==

# Add the Bamboo user to the Docker group. Switch `bamboo` for the user that Bamboo runs under in your environment

{{{ lang=bash
sudo usermod -a -G docker bamboo
}}}

# Restart Bamboo so that it picks up the new group permissions, and is able to use Docker

{{{ lang=bash
sudo service bamboo restart
}}}

== Travis2Docker ==

The next piece of this puzzle is created by [[https://www.vauxoo.com|Vauxoo]], and is called [[https://github.com/Vauxoo/travis2docker|Travis2Docker]].

Travis2Docker will automatically create a Dockerfile and some nifty build scripts based on a `.travis.yml` file in a repository.

As mentioned in the Foreword, the primary testing strategy of Odoo is TravisCI, so nearly every Odoo repo already has a `travis.yml` file. Aside from this, the [[https://odoo-community.org/|Odoo Community Association]] has tailored a significant amount of scripts around the Travis build environment.

Using the Travis files that are already integrated into the repos provides us the benefit of not having to recreate our build processes, as well as guarantees consistency of builds should a module be moved into the community repos.

We will install Travis2Docker as part of the build plan in order to guarantee that we are always using the new version

== Build Plan ==

Now we need to create a build plan to run tests on our repo

# Log in to Bamboo, click Create at the top, then Create a new plan
[[image:Screen-Shot-2016-02-09-at-8.58.06-PM.png|medium|link=source]]

# Fill out the form to create the plan & link it to the desired repo
[[image:Screen-Shot-2016-02-09-at-8.59.25-PM.png|medium|link=source]]

# The first build task should be the `Source Code Checkout` task that is added by default. No changes need to be made to this task.

# Add a new Script Task, and name it whatever you would like. We will use this task to generate the Docker image, run the container, and run the tests.
[[image:Screen-Shot-2016-02-09-at-9.00.20-PM.png|medium|link=source]]

# Add the following script to the new task:
{{{ lang=bash
# Setup virtualenv
virtualenv _venv

# Install Pip
curl –silent –show-error –retry 5 https://bootstrap.pypa.io/get-pip.py | _venv/bin/python

# Configure environment
export PATH=${bamboo.build.working.directory}/tmp/usr/local/bin:$PATH
export PYTHONPATH=${bamboo.build.working.directory}/tmp/usr/local/lib/python2.7/dist-packages:$PYTHONPATH

# Fix https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning
# Note – requires libffi-dev
_venv/bin/pip install –upgrade pyopenssl ndg-httpsclient pyasn1

# Install Travis2Docker
_venv/bin/pip install –upgrade git+https://github.com/Vauxoo/travis2docker.git@2.1.0

# Clear the old images
rm -Rf ${bamboo.build.working.directory}/docker

# Generate Docker Scripts
_venv/bin/travisfile2dockerfile \
${bamboo.planRepository.repositoryUrl} \
${bamboo.planRepository.branchName} \
–root-path=${bamboo.build.working.directory}/docker \
–docker-image=”laslabs/docker-odoo-image” \
–include-cleanup \
–build-extra-args=”–no-cache” \
–exclude-after-success \
–run-extra-args=”-tP -e LANG=C.UTF-8 -v ${bamboo.build.working.directory}/_results:/_results”

# Uncomment below for Lints tests
# cd ${bamboo.build.working.directory}/docker/script/*/*/*/1

# Uncomment below for standard tests server – odoo/odoo
cd ${bamboo.build.working.directory}/docker/script/*/*/*/2

# Uncomment below for OCA backports test server – OCA/OCB
# cd ${bamboo.build.working.directory}/docker/script/*/*/*/3

# Build
./10-build.sh

# Rewrite the default options for compat with Bamboo
# sed -i ‘s/ -itP / -tP /g’ ./20-run.sh

# Start container and run tests
./20-run.sh
}}}

# Note that the above may need to be tailored a bit depending on the Travis file. The above is the standard organization, but it could vary. Look in `travis.yml` for the `env` section. The above example is for the following `env`:

{{{
env:
– VERSION=”9.0″ LINT_CHECK=”1″
– VERSION=”9.0″ ODOO_REPO=”odoo/odoo” TESTS=”1″ LINT_CHECK=”0″
– VERSION=”9.0″ ODOO_REPO=”OCA/OCB” TESTS=”1″ LINT_CHECK=”0″
– VERSION=”9.0″ UNIT_TEST=”1″ LINT_CHECK=”0″
}}}
# Add a new JUnit Parser task, which will allow Bamboo to parse the test results instead of just viewing a log. Name it whatever you would like.
[[image:Screen-Shot-2016-02-09-at-8.31.25-PM.png|medium|link=source]]

# Change the results directory to `**/_results/*.xml`, and save.
[[image:Screen-Shot-2016-02-09-at-8.33.12-PM.png|medium|link=source]]

# Save and run the plan – we should have green lights! Note that the first build will take an extraordinarily long time due to the initial download of the Docker image. Subsequent tests should be much quicker.
[[image:Screen-Shot-2016-02-09-at-6.17.04-PM.png|medium|link=source]]

# From here, you would want to copy a new job in the same project for each of the Travis build jobs (typically 3 total). You can easily clone the existing job, then just change the docker script directory from `1` to `2` or `3`.


Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *