test: minimal amazon-ebs test

This commit is contained in:
Mitchell Hashimoto 2013-12-11 16:13:49 -08:00
parent bf10af6401
commit f599e2d0ce
4 changed files with 99 additions and 0 deletions

View File

@ -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.

19
test/builder_amazon_ebs.bats Executable file
View File

@ -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 ]
}

13
test/fixtures/amazon-ebs/minimal.json vendored Normal file
View File

@ -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"
}
}]
}

32
test/test_helper.bash Normal file
View File

@ -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
}