From 07dd02c25a024e00d27f3152a4bb3c401cb9e3e4 Mon Sep 17 00:00:00 2001 From: James Laska Date: Thu, 13 Mar 2014 09:52:36 -0400 Subject: [PATCH] [test_ec2*] cloud integration test updates To support parallel cloud test execution, create and provide a random string to cloud integration tests. The variable 'resource_prefix' can be used in cloud roles and during resource cleanup to safely create/destroy cloud-based resources. Additional changes include: * The roles test_ec2_key and test_ec2_group were updated to use to {{resource_prefix}}. * Additionally, the Makefile was updated to set resource_prefix to a random string. The Makefile will also use 'resource_prefix' during cloud_cleanup. * All test_ec2* roles were updated to add 'setup_ec2' as a role dependency. --- test/integration/Makefile | 17 +++++++++++------ test/integration/cleanup_ec2.py | 14 +++++++++----- .../roles/setup_ec2/defaults/main.yml | 2 ++ test/integration/roles/test_ec2/meta/main.yml | 4 ++-- .../roles/test_ec2_ami/meta/main.yml | 4 ++-- .../roles/test_ec2_eip/meta/main.yml | 4 ++-- .../roles/test_ec2_elb/meta/main.yml | 4 ++-- .../roles/test_ec2_elb_lb/meta/main.yml | 4 ++-- .../roles/test_ec2_facts/meta/main.yml | 4 ++-- .../roles/test_ec2_group/defaults/main.yml | 2 +- .../roles/test_ec2_key/defaults/main.yml | 2 +- .../roles/test_ec2_tag/meta/main.yml | 4 ++-- .../roles/test_ec2_vol/meta/main.yml | 4 ++-- .../roles/test_ec2_vpc/meta/main.yml | 4 ++-- 14 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 test/integration/roles/setup_ec2/defaults/main.yml diff --git a/test/integration/Makefile b/test/integration/Makefile index 7a4072d44ae..94d97b46a40 100644 --- a/test/integration/Makefile +++ b/test/integration/Makefile @@ -1,6 +1,11 @@ INVENTORY ?= inventory VARS_FILE ?= integration_config.yml +# Create a semi-random string for use when testing cloud-based resources +ifndef CLOUD_RESOURCE_PREFIX +CLOUD_RESOURCE_PREFIX := $(shell python -c "import string,random; print 'ansible-testing-' + ''.join(random.choice(string.ascii_letters + string.digits) for _ in xrange(8));") +endif + all: non_destructive destructive check_mode test_hash non_destructive: @@ -21,24 +26,24 @@ cloud: amazon rackspace cloud_cleanup: amazon_cleanup rackspace_cleanup amazon_cleanup: - python cleanup_ec2.py -y + python cleanup_ec2.py -y --match="^$(CLOUD_RESOURCE_PREFIX)" rackspace_cleanup: @echo "FIXME - cleanup_rax.py not yet implemented" - @#python cleanup_rax.py -y + @# python cleanup_rax.py -y --match="^$(CLOUD_RESOURCE_PREFIX)" credentials.yml: @echo "No credentials.yml file found. A file named 'credentials.yml' is needed to provide credentials needed to run cloud tests. See sample 'credentials.template' file." @exit 1 amazon: credentials.yml - ansible-playbook amazon.yml -i $(INVENTORY) -e @$(VARS_FILE) -e @credentials.yml -v $(TEST_FLAGS) ; \ + ansible-playbook amazon.yml -i $(INVENTORY) -e @$(VARS_FILE) -e @credentials.yml -e "resource_prefix=$(CLOUD_RESOURCE_PREFIX)" -v $(TEST_FLAGS) ; \ RC=$$? ; \ - make amazon_cleanup ; \ + CLOUD_RESOURCE_PREFIX="$(CLOUD_RESOURCE_PREFIX)" make amazon_cleanup ; \ exit $$RC; rackspace: credentials.yml - ansible-playbook rackspace.yml -i $(INVENTORY) -e @$(VARS_FILE) -e @credentials.yml -v $(TEST_FLAGS) ; \ + ansible-playbook rackspace.yml -i $(INVENTORY) -e @$(VARS_FILE) -e @credentials.yml -e "resource_prefix=$(CLOUD_RESOURCE_PREFIX)" -v $(TEST_FLAGS) ; \ RC=$$? ; \ - make rackspace_cleanup ; \ + CLOUD_RESOURCE_PREFIX="$(CLOUD_RESOURCE_PREFIX)" make rackspace_cleanup ; \ exit $$RC; diff --git a/test/integration/cleanup_ec2.py b/test/integration/cleanup_ec2.py index 0e974c2089e..d82dc4f340b 100644 --- a/test/integration/cleanup_ec2.py +++ b/test/integration/cleanup_ec2.py @@ -15,7 +15,7 @@ def delete_aws_resources(get_func, attr, opts): for item in get_func(): val = getattr(item, attr) if re.search(opts.match_re, val): - prompt_and_delete(item, "Delete object with %s=%s? [y/n]: " % (attr, val), opts.assumeyes) + prompt_and_delete(item, "Delete matching %s? [y/n]: " % (item,), opts.assumeyes) def prompt_and_delete(item, prompt, assumeyes): if not assumeyes: @@ -23,6 +23,7 @@ def prompt_and_delete(item, prompt, assumeyes): assert hasattr(item, 'delete'), "Class <%s> has no delete attribute" % item.__class__ if assumeyes: item.delete() + print ("Deleted %s" % item) def parse_args(): # Load details from credentials.yml @@ -74,8 +75,11 @@ if __name__ == '__main__': aws = boto.connect_ec2(aws_access_key_id=opts.ec2_access_key, aws_secret_access_key=opts.ec2_secret_key) - # Delete matching keys - delete_aws_resources(aws.get_all_key_pairs, 'name', opts) + try: + # Delete matching keys + delete_aws_resources(aws.get_all_key_pairs, 'name', opts) - # Delete matching groups - delete_aws_resources(aws.get_all_security_groups, 'name', opts) + # Delete matching groups + delete_aws_resources(aws.get_all_security_groups, 'name', opts) + except KeyboardInterrupt, e: + print "\nExiting on user command." diff --git a/test/integration/roles/setup_ec2/defaults/main.yml b/test/integration/roles/setup_ec2/defaults/main.yml new file mode 100644 index 00000000000..fb1f88b1ecb --- /dev/null +++ b/test/integration/roles/setup_ec2/defaults/main.yml @@ -0,0 +1,2 @@ +--- +resource_prefix: 'ansible-testing-' diff --git a/test/integration/roles/test_ec2/meta/main.yml b/test/integration/roles/test_ec2/meta/main.yml index 1050c23ce30..1f64f1169a9 100644 --- a/test/integration/roles/test_ec2/meta/main.yml +++ b/test/integration/roles/test_ec2/meta/main.yml @@ -1,3 +1,3 @@ -dependencies: +dependencies: - prepare_tests - + - setup_ec2 diff --git a/test/integration/roles/test_ec2_ami/meta/main.yml b/test/integration/roles/test_ec2_ami/meta/main.yml index 1050c23ce30..1f64f1169a9 100644 --- a/test/integration/roles/test_ec2_ami/meta/main.yml +++ b/test/integration/roles/test_ec2_ami/meta/main.yml @@ -1,3 +1,3 @@ -dependencies: +dependencies: - prepare_tests - + - setup_ec2 diff --git a/test/integration/roles/test_ec2_eip/meta/main.yml b/test/integration/roles/test_ec2_eip/meta/main.yml index 1050c23ce30..1f64f1169a9 100644 --- a/test/integration/roles/test_ec2_eip/meta/main.yml +++ b/test/integration/roles/test_ec2_eip/meta/main.yml @@ -1,3 +1,3 @@ -dependencies: +dependencies: - prepare_tests - + - setup_ec2 diff --git a/test/integration/roles/test_ec2_elb/meta/main.yml b/test/integration/roles/test_ec2_elb/meta/main.yml index 1050c23ce30..1f64f1169a9 100644 --- a/test/integration/roles/test_ec2_elb/meta/main.yml +++ b/test/integration/roles/test_ec2_elb/meta/main.yml @@ -1,3 +1,3 @@ -dependencies: +dependencies: - prepare_tests - + - setup_ec2 diff --git a/test/integration/roles/test_ec2_elb_lb/meta/main.yml b/test/integration/roles/test_ec2_elb_lb/meta/main.yml index 1050c23ce30..1f64f1169a9 100644 --- a/test/integration/roles/test_ec2_elb_lb/meta/main.yml +++ b/test/integration/roles/test_ec2_elb_lb/meta/main.yml @@ -1,3 +1,3 @@ -dependencies: +dependencies: - prepare_tests - + - setup_ec2 diff --git a/test/integration/roles/test_ec2_facts/meta/main.yml b/test/integration/roles/test_ec2_facts/meta/main.yml index 1050c23ce30..1f64f1169a9 100644 --- a/test/integration/roles/test_ec2_facts/meta/main.yml +++ b/test/integration/roles/test_ec2_facts/meta/main.yml @@ -1,3 +1,3 @@ -dependencies: +dependencies: - prepare_tests - + - setup_ec2 diff --git a/test/integration/roles/test_ec2_group/defaults/main.yml b/test/integration/roles/test_ec2_group/defaults/main.yml index e10da44d847..4063791af4b 100644 --- a/test/integration/roles/test_ec2_group/defaults/main.yml +++ b/test/integration/roles/test_ec2_group/defaults/main.yml @@ -1,5 +1,5 @@ --- # defaults file for test_ec2_group -ec2_group_name: 'ansible-testing-{{ random_string }}' +ec2_group_name: '{{resource_prefix}}' ec2_group_description: 'Created by ansible integration tests' diff --git a/test/integration/roles/test_ec2_key/defaults/main.yml b/test/integration/roles/test_ec2_key/defaults/main.yml index 2242ea07093..df0082d999b 100644 --- a/test/integration/roles/test_ec2_key/defaults/main.yml +++ b/test/integration/roles/test_ec2_key/defaults/main.yml @@ -1,3 +1,3 @@ --- # defaults file for test_ec2_key -ec2_key_name: 'ansible-testing-{{ random_string }}' +ec2_key_name: '{{resource_prefix}}' diff --git a/test/integration/roles/test_ec2_tag/meta/main.yml b/test/integration/roles/test_ec2_tag/meta/main.yml index 1050c23ce30..1f64f1169a9 100644 --- a/test/integration/roles/test_ec2_tag/meta/main.yml +++ b/test/integration/roles/test_ec2_tag/meta/main.yml @@ -1,3 +1,3 @@ -dependencies: +dependencies: - prepare_tests - + - setup_ec2 diff --git a/test/integration/roles/test_ec2_vol/meta/main.yml b/test/integration/roles/test_ec2_vol/meta/main.yml index 1050c23ce30..1f64f1169a9 100644 --- a/test/integration/roles/test_ec2_vol/meta/main.yml +++ b/test/integration/roles/test_ec2_vol/meta/main.yml @@ -1,3 +1,3 @@ -dependencies: +dependencies: - prepare_tests - + - setup_ec2 diff --git a/test/integration/roles/test_ec2_vpc/meta/main.yml b/test/integration/roles/test_ec2_vpc/meta/main.yml index 1050c23ce30..1f64f1169a9 100644 --- a/test/integration/roles/test_ec2_vpc/meta/main.yml +++ b/test/integration/roles/test_ec2_vpc/meta/main.yml @@ -1,3 +1,3 @@ -dependencies: +dependencies: - prepare_tests - + - setup_ec2