diff --git a/test/builder_googlecompute.bats b/test/builder_googlecompute.bats index d7062a9a8..1051bb99a 100755 --- a/test/builder_googlecompute.bats +++ b/test/builder_googlecompute.bats @@ -7,28 +7,26 @@ load test_helper fixtures builder-googlecompute # Required parameters -: ${GC_BUCKET_NAME:?} : ${GC_ACCOUNT_FILE:?} : ${GC_PROJECT_ID:?} -command -v gcutil >/dev/null 2>&1 || { - echo "'gcutil' must be installed" >&2 +command -v gcloud >/dev/null 2>&1 || { + echo "'gcloud' must be installed" >&2 exit 1 } -USER_VARS="-var bucket_name=${GC_BUCKET_NAME}" USER_VARS="${USER_VARS} -var account_file=${GC_ACCOUNT_FILE}" USER_VARS="${USER_VARS} -var project_id=${GC_PROJECT_ID}" # This tests if GCE has an image that contains the given parameter. gc_has_image() { - gcutil --format=names --project=${GC_PROJECT_ID} listimages \ + gcloud compute --format='table[no-heading](name)' --project=${GC_PROJECT_ID} images list \ | grep $1 | wc -l } teardown() { - gcutil --format=names --project=${GC_PROJECT_ID} listimages \ + gcloud compute --format='table[no-heading](name)' --project=${GC_PROJECT_ID} images list \ | grep packerbats \ - | xargs -n1 gcutil --project=${GC_PROJECT_ID} deleteimage --force + | xargs -n1 gcloud compute --project=${GC_PROJECT_ID} images delete } @test "googlecompute: build minimal.json" { diff --git a/test/fixtures/builder-googlecompute/minimal.json b/test/fixtures/builder-googlecompute/minimal.json index 218e98b9a..95dd99254 100644 --- a/test/fixtures/builder-googlecompute/minimal.json +++ b/test/fixtures/builder-googlecompute/minimal.json @@ -1,13 +1,11 @@ { "variables": { - "bucket_name": null, "account_file": null, "project_id": null }, "builders": [{ "type": "googlecompute", - "bucket_name": "{{user `bucket_name`}}", "account_file": "{{user `account_file`}}", "project_id": "{{user `project_id`}}", diff --git a/test/fixtures/provisioner-ansible/all_options.json b/test/fixtures/provisioner-ansible/all_options.json new file mode 100644 index 000000000..4628c4709 --- /dev/null +++ b/test/fixtures/provisioner-ansible/all_options.json @@ -0,0 +1,39 @@ +{ + "variables": {}, + "provisioners": [ + { + "type": "shell-local", + "command": "echo 'TODO(bc): write the public key to $HOME/.ssh/known_hosts and stop using ANSIBLE_HOST_KEY_CHECKING=False'" + }, { + "type": "shell", + "inline": [ + "apt-get update", + "apt-get -y install python openssh-sftp-server", + "ls -l /usr/lib" + ] + }, { + "type": "ansible", + "playbook_file": "./playbook.yml", + "extra_arguments": [ + "-vvvv", "--private-key", "ansible-test-id" + ], + "sftp_command": "/usr/lib/sftp-server -e -l INFO", + "ansible_env_vars": ["PACKER_ANSIBLE_TEST", "1", "ANSIBLE_HOST_KEY_CHECKING", "False"], + "groups": ["PACKER_TEST"], + "empty_groups": ["PACKER_EMPTY_GROUP"], + "host_alias": "packer-test", + "user": "packer", + "local_port": 2222, + "ssh_host_key_file": "ansible-server.key", + "ssh_authorized_key_file": "ansible-test-id.pub" + } + ], + "builders": [{ + "type": "googlecompute", + "account_file": "{{user `account_file`}}", + "project_id": "{{user `project_id`}}", + "image_name": "packerbats-alloptions-{{timestamp}}", + "source_image": "debian-7-wheezy-v20141108", + "zone": "us-central1-a" + }] +} diff --git a/test/fixtures/provisioner-ansible/dir/file.txt b/test/fixtures/provisioner-ansible/dir/file.txt new file mode 100644 index 000000000..0637880d7 --- /dev/null +++ b/test/fixtures/provisioner-ansible/dir/file.txt @@ -0,0 +1 @@ +This is a file diff --git a/test/fixtures/provisioner-ansible/largish-file.txt b/test/fixtures/provisioner-ansible/largish-file.txt new file mode 100644 index 000000000..4df7b0668 Binary files /dev/null and b/test/fixtures/provisioner-ansible/largish-file.txt differ diff --git a/test/fixtures/provisioner-ansible/minimal.json b/test/fixtures/provisioner-ansible/minimal.json new file mode 100644 index 000000000..141aebb6d --- /dev/null +++ b/test/fixtures/provisioner-ansible/minimal.json @@ -0,0 +1,20 @@ +{ + "variables": {}, + "provisioners": [ + { + "type": "ansible", + "playbook_file": "./playbook.yml" + } + ], + "builders": [ + { + "type": "googlecompute", + "account_file": "{{user `account_file`}}", + "project_id": "{{user `project_id`}}", + + "image_name": "packerbats-minimal-{{timestamp}}", + "source_image": "debian-7-wheezy-v20141108", + "zone": "us-central1-a" + } + ] +} diff --git a/test/fixtures/provisioner-ansible/playbook.yml b/test/fixtures/provisioner-ansible/playbook.yml new file mode 100644 index 000000000..f0e20bb2a --- /dev/null +++ b/test/fixtures/provisioner-ansible/playbook.yml @@ -0,0 +1,13 @@ +--- +- hosts: default + gather_facts: no + tasks: + - raw: touch /root/ansible-raw-test + - raw: date + - command: echo "the command module" + - command: mkdir /tmp/remote-dir + args: + creates: /tmp/remote-dir + - copy: src=dir/file.txt dest=/tmp/remote-dir/file.txt + - fetch: src=/tmp/remote-dir/file.txt dest=fetched-dir validate=yes fail_on_missing=yes + - copy: src=largish-file.txt dest=/tmp/largish-file.txt diff --git a/test/provisioner_ansible.bats b/test/provisioner_ansible.bats new file mode 100755 index 000000000..78acdaa2a --- /dev/null +++ b/test/provisioner_ansible.bats @@ -0,0 +1,58 @@ +#!/usr/bin/env bats +# +# This tests the ansible provisioner on Google Cloud Provider (i.e. +# googlecompute). The teardown function will delete any images with the text +# "packerbats" within the name. + +load test_helper +fixtures provisioner-ansible + +# Required parameters +: ${GC_ACCOUNT_FILE:?} +: ${GC_PROJECT_ID:?} +command -v gcloud >/dev/null 2>&1 || { + echo "'gcloud' must be installed" >&2 + exit 1 +} + +USER_VARS="${USER_VARS} -var account_file=${GC_ACCOUNT_FILE}" +USER_VARS="${USER_VARS} -var project_id=${GC_PROJECT_ID}" + +# This tests if GCE has an image that contains the given parameter. +gc_has_image() { + gcloud compute --format='table[no-heading](name)' --project=${GC_PROJECT_ID} images list \ + | grep $1 | wc -l +} + +setup(){ + rm -f $FIXTURE_ROOT/ansible-test-id + rm -f $FIXTURE_ROOT/ansible-server.key + ssh-keygen -N "" -f $FIXTURE_ROOT/ansible-test-id + ssh-keygen -N "" -f $FIXTURE_ROOT/ansible-server.key +} + +teardown() { + gcloud compute --format='table[no-heading](name)' --project=${GC_PROJECT_ID} images list \ + | grep packerbats \ + | xargs -n1 gcloud compute --project=${GC_PROJECT_ID} images delete + + rm -f $FIXTURE_ROOT/ansible-test-id + rm -f $FIXTURE_ROOT/ansible-test-id.pub + rm -f $FIXTURE_ROOT/ansible-server.key + rm -f $FIXTURE_ROOT/ansible-server.key.pub + rm -rf $FIXTURE_ROOT/fetched-dir +} + +@test "ansible provisioner: build minimal.json" { + cd $FIXTURE_ROOT + run packer build ${USER_VARS} $FIXTURE_ROOT/minimal.json + [ "$status" -eq 0 ] + [ "$(gc_has_image "packerbats-minimal")" -eq 1 ] +} + +@test "ansible provisioner: build all_options.json" { + cd $FIXTURE_ROOT + run packer build ${USER_VARS} $FIXTURE_ROOT/all_options.json + [ "$status" -eq 0 ] + [ "$(gc_has_image "packerbats-alloptions")" -eq 1 ] +}