From f599e2d0ce336b086f470206961d27585d55f440 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 11 Dec 2013 16:13:49 -0800 Subject: [PATCH] test: minimal amazon-ebs test --- test/README.md | 35 +++++++++++++++++++++++++++ test/builder_amazon_ebs.bats | 19 +++++++++++++++ test/fixtures/amazon-ebs/minimal.json | 13 ++++++++++ test/test_helper.bash | 32 ++++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100755 test/builder_amazon_ebs.bats create mode 100644 test/fixtures/amazon-ebs/minimal.json create mode 100644 test/test_helper.bash diff --git a/test/README.md b/test/README.md index 39fef61d0..c1208d60b 100644 --- a/test/README.md +++ b/test/README.md @@ -6,3 +6,38 @@ results are expected. Tests are run using [Bats](https://github.com/sstephenson/bats), and therefore Bash is required to run any tests. + +**Warning:** Many of these tests run using AWS, and therefore have a +real-world cost associated with running the tests. Be aware of that prior +to running the tests. Additionally, many tests will leave left-over artifacts +(AMIs) that you'll have to manually clean up. + +## Required Software + +Before running the tests, you'll need the following installed. If you're +running on Mac OS X, most of these are available with `brew`: + +* [Bats](https://github.com/sstephenson/bats) + +* [AWS cli](http://aws.amazon.com/cli/) +* [jq](http://stedolan.github.io/jq/) + +## Configuring Tests + +**For tests that require AWS credentials:** + +Set the following self-explanatory environmental variables: + +* `AWS_ACCESS_KEY_ID` +* `AWS_SECRET_ACCESS_KEY` + +## Running Tests + +These tests are meant to be run _one file at a time_. There are some +test files (such as the amazon-chroot builder test) that simply won't +run except in special environments, so running all test files will probably +never work. + +If you're working on Packer and want to test that your change didn't +adversely affect something, try running only the test that is related to +your change. diff --git a/test/builder_amazon_ebs.bats b/test/builder_amazon_ebs.bats new file mode 100755 index 000000000..15f618d61 --- /dev/null +++ b/test/builder_amazon_ebs.bats @@ -0,0 +1,19 @@ +#!/usr/bin/env bats +# +# This tests the amazon-ebs builder. The teardown function will automatically +# delete any AMIs with a tag of `packer-test` being equal to "true" so +# be sure any test cases set this. + +load test_helper +fixtures amazon-ebs + +teardown() { + aws ec2 describe-images --owners self --output json --filters 'Name=tag:packer-test,Values=true' \ + | jq -r -M '.Images[]["ImageId"]' + | xargs -n1 aws ec2 deregister-image --image-id +} + +@test "build minimal.json" { + run packer build $FIXTURE_ROOT/minimal.json + [ "$status" -eq 0 ] +} diff --git a/test/fixtures/amazon-ebs/minimal.json b/test/fixtures/amazon-ebs/minimal.json new file mode 100644 index 000000000..bfe7cab63 --- /dev/null +++ b/test/fixtures/amazon-ebs/minimal.json @@ -0,0 +1,13 @@ +{ + "builders": [{ + "type": "amazon-ebs", + "ami_name": "packer-test {{timestamp}}", + "instance_type": "m1.small", + "region": "us-east-1", + "ssh_username": "ubuntu", + "source_ami": "ami-0568456c", + "tags": { + "packer-test": "true" + } + }] +} diff --git a/test/test_helper.bash b/test/test_helper.bash new file mode 100644 index 000000000..5bde7766f --- /dev/null +++ b/test/test_helper.bash @@ -0,0 +1,32 @@ +# Let's verify that the tools we need are installed +declare -a required=(aws jq) +for cmd in "${required[@]}"; do + command -v $cmd >/dev/null 2>&1 || { + echo "'$cmd' must be installed" >&2 + exit 1 + } +done + +# This sets the directory for fixtures by specifying the name of +# the folder with fixtures. +fixtures() { + FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures/$1" +} + +# This allows us to override a function in Bash +save_function() { + local ORIG_FUNC=$(declare -f $1) + local NEWNAME_FUNC="$2${ORIG_FUNC#$1}" + eval "$NEWNAME_FUNC" +} + +# Override the run function so that we always output the output +save_function run old_run +run() { + old_run $@ + + # "$output" gets rid of newlines. This will bring them back. + for line in "${lines[@]}"; do + echo $line + done +}