Add regex support to gce_tag module, add unit tests and update integration test. (#19087)

The gce_tag module can support updating tags on multiple instances via an instance_pattern field.  Full Python regex is supported in the instance_pattern field.

'instance_pattern' and 'instance_name' are mutually exclusive and one must be specified.

The integration test for the gce_tag module has been updated to support the instance_pattern parameter.  Unit tests have been added to test the list-manipulation functionality.

Run the integration test with:

TEST_FLAGS='--tags "test_gce_tag"' make gce

Run the unit tests with:

python test/units/modules/cloud/google/test_gce_tag.py
This commit is contained in:
Tom Melendez 2016-12-29 05:45:20 -08:00 committed by Ryan Brown
commit d09ad82e71
4 changed files with 250 additions and 96 deletions

View file

@ -2,11 +2,12 @@
## Parameter checking tests ##
# ============================================================
- name: "test missing param: instance_name"
- name: "test missing param: instance_name or instance_pattern"
gce_tag:
service_account_email: "{{ service_account_email }}"
pem_file: "{{ pem_file }}"
project_id: "{{ project_id }}"
tags: foo,bar
register: result
ignore_errors: true
tags:
@ -16,7 +17,7 @@
assert:
that:
- 'result.failed'
- 'result.msg == "missing required arguments: instance_name"'
- 'result.msg == "one of the following is required: instance_name,instance_pattern"'
# ============================================================
@ -36,8 +37,27 @@
assert:
that:
- 'result.failed'
- 'result.msg == "Must specify \"tags\""'
- 'result.msg == "missing required arguments: tags"'
# ============================================================
- name: "test bad regex: instance_pattern"
gce_tag:
service_account_email: "{{ service_account_email }}"
pem_file: "{{ pem_file }}"
project_id: "{{ project_id }}"
zone: "{{ zone }}"
tags: foo,bar
instance_pattern: "&23424--["
register: result
ignore_errors: true
tags:
- param-check
- name: "assert failure when instance_pattern is invalid"
assert:
that:
- 'result.failed'
- 'result.msg == "Regex error for pattern &23424--[: unexpected end of regular expression"'
## Non-existant instance tests ##
# # ============================================================
@ -114,6 +134,45 @@
- 'result.changed == False'
- 'result.tags == None'
# # ============================================================
- name: "add tags using pattern (state==present)"
gce_tag:
service_account_email: "{{ service_account_email }}"
pem_file: "{{ pem_file }}"
project_id: "{{ project_id }}"
instance_pattern: "{{ instance_name }}"
zone: "{{ zone }}"
tags: instance-pattern-test
state: present
register: result
- name: "assert tag using pattern successful"
assert:
that:
- 'result.changed == True'
- 'result.tags == ["instance-pattern-test"]'
- 'result.instances_updated[0].instance_name == "{{ instance_name }}"'
- 'result.instances_updated[0].tags_changed[0] == "instance-pattern-test"'
# # ============================================================
- name: "add existing tags with pattern, no change (state==present)"
gce_tag:
service_account_email: "{{ service_account_email }}"
pem_file: "{{ pem_file }}"
project_id: "{{ project_id }}"
instance_pattern: "{{ instance_name }}"
zone: "{{ zone }}"
tags: instance-pattern-test
state: present
register: result
- name: "assert tag with pattern no change"
assert:
that:
- 'result.changed == False'
- 'result.tags == None'
- 'result.instances_updated|length == 0'
# # ============================================================
- name: "test tags removed from instance (state==absent)"
gce_tag:
@ -131,3 +190,24 @@
that:
- 'result.changed'
- 'result.tags == ["foo", "bar"]'
# # ============================================================
- name: "test tags removed with instance_pattern (state==absent)"
gce_tag:
service_account_email: "{{ service_account_email }}"
pem_file: "{{ pem_file }}"
project_id: "{{ project_id }}"
instance_pattern: "{{ instance_name }}"
zone: "{{ zone }}"
tags: instance-pattern-test
state: absent
register: result
- name: "assert tags removed with instance_pattern"
assert:
that:
- 'result.changed'
- 'result.tags == ["instance-pattern-test"]'
- 'result.instances_updated[0].instance_name == "{{ instance_name }}"'
- 'result.instances_updated[0].tags_changed[0] == "instance-pattern-test"'