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"'

View file

@ -0,0 +1,68 @@
#!/usr/bin/env python
import unittest
from ansible.modules.cloud.google.gce_tag import _get_changed_items, _intersect_items, _union_items
class TestGCETag(unittest.TestCase):
"""Unit tests for gce_tag module."""
def test_union_items(self):
"""
Combine items in both lists
removing duplicates.
"""
listA = [1, 2, 3, 4, 5, 8, 9]
listB = [1, 2, 3, 4, 5, 6, 7]
want = [1, 2, 3, 4, 5, 6, 7, 8, 9]
got = _union_items(listA, listB)
self.assertEqual(want, got)
def test_intersect_items(self):
"""
All unique items from either list.
"""
listA = [1, 2, 3, 4, 5, 8, 9]
listB = [1, 2, 3, 4, 5, 6, 7]
want = [1, 2, 3, 4, 5]
got = _intersect_items(listA, listB)
self.assertEqual(want, got)
# tags removed
new_tags = ['one', 'two']
existing_tags = ['two']
want = ['two'] # only remove the tag that was present
got = _intersect_items(existing_tags, new_tags)
self.assertEqual(want, got)
def test_get_changed_items(self):
"""
All the items from left list that don't match
any item from the right list.
"""
listA = [1, 2, 3, 4, 5, 8, 9]
listB = [1, 2, 3, 4, 5, 6, 7]
want = [8, 9]
got = _get_changed_items(listA, listB)
self.assertEqual(want, got)
# simulate new tags added
tags_to_add = ['one', 'two']
existing_tags = ['two']
want = ['one']
got = _get_changed_items(tags_to_add, existing_tags)
self.assertEqual(want, got)
# simulate removing tags
# specifying one tag on right that doesn't exist
tags_to_remove = ['one', 'two']
existing_tags = ['two', 'three']
want = ['three']
got = _get_changed_items(existing_tags, tags_to_remove)
self.assertEqual(want, got)
if __name__ == '__main__':
unittest.main()