Skip to content

Instantly share code, notes, and snippets.

@lazypower
Last active August 29, 2015 13:56
Show Gist options
  • Save lazypower/9146114 to your computer and use it in GitHub Desktop.
Save lazypower/9146114 to your computer and use it in GitHub Desktop.
Vagrant Charm Testing

Testing Juju Charms with Vagrant

Notes:

Place Vagrantfile and juju-test-runner.sh in the root of your Charm's directory (where config.yaml lives). Note: This may move at a later date. Also fetch the supporting vagrant-test-runner.sh script - this is the driver behind the vagrant automation until we find a better process to execute the code.

Instructions on use:

Install Vagrant and Virtualbox

$ sudo apt-get install virtualbox vagrant

Place the Vagrant file and juju-vagrant-setup.sh in the root of your charm directory and run vagrant up

cd my_charm
wget https://gist.githubusercontent.com/chuckbutler/9146114/raw/Vagrantfile
wget https://gist.githubusercontent.com/chuckbutler/9146114/raw/vagrant-test-runner.sh
vagrant up

Vagrant will fetch the base box, import it into Virtualbox, and kick off a test execution.

The script does the following things for you:

  • Setup a clean Trusty Tahr server image with juju-core installed by default
  • Installs the Juju Local provider
  • Configures your local provider, and generates an SSH key for the vagrant user
  • Installs your tests pre-setup file 00-setup (this is a convention used by charm add tests)
  • Kicks off any tests to be run against the local provider, as long as they are chmod +x

If there are executable files in the tests directory - they will automatically be run.

Workflow

charm add tests

hack away at your awesome amulet test, making sure you add any dependencies to 00-setup

vagrant up

on your first test execution. Afterwords you are free to do

vagrant provision

your tests will be executed in place, and should be a faster run as the environment pre-setup and installation has already completed.

When you're done

vagrant destroy

To learn more about working with vagrant boxes, and how to fully remove the cached box, visit the awesome Vagrant documentation on boxes

To keep the Vagrant Bits out of your VCS

add the following lines to your .bzrignore or .gitignore respectively

.vagrant
Vagrantfile
vagrant-test-runner.sh
vagrant-log

All bugreports against the basebox are welcome.

chuckbutler/juju-vagrant-veewee-definitions

echo "Installing juju-local provider support"
apt-get update
apt-get install -y juju-local
if [ ! -f /home/vagrant/.ssh/id_rsa ]; then
sudo -u vagrant -H ssh-keygen -t rsa -N '' -f id_rsa
fi
if [ ! -d /home/vagrant/.juju ]; then
sudo -u vagrant -H juju init
# set the admin key to vagrant
sed -i.bak s/#\ network-bridge\:\ lxcbr0/admin-secret\:\ vagrant/ /home/vagrant/.juju/environments.yaml
sudo -u vagrant -H juju switch local
fi
echo "Running test-pre-setup script"
sudo -u vagrant -H /bin/bash /vagrant/tests/00-setup
echo "running tests"
for test in `find /vagrant/tests -maxdepth 1 -perm -111 -type f \( ! -name "00-setup" \)`; do
cd /vagrant
sudo -u vagrant -H charm test -e local -o vagrant-log $test
done
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "ubuntu-trusty-alpha2"
#Note! the size of this box is not trivial - coming in at 801 mb
config.vm.box_url = "http://dasroot.net/ubuntu-trusty-alpha2.box"
#Perform the provisioner tasks of doing setup
config.vm.provision "shell",
path: "vagrant-test-runner.sh"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment