mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-15 17:40:50 -07:00
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:
parent
a10209cbe9
commit
d09ad82e71
4 changed files with 250 additions and 96 deletions
0
test/units/modules/cloud/google/__init__.py
Normal file
0
test/units/modules/cloud/google/__init__.py
Normal file
68
test/units/modules/cloud/google/test_gce_tag.py
Normal file
68
test/units/modules/cloud/google/test_gce_tag.py
Normal 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()
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue