diff options
Diffstat (limited to 'collections-debian-merged/ansible_collections/community/google/tests')
12 files changed, 805 insertions, 0 deletions
diff --git a/collections-debian-merged/ansible_collections/community/google/tests/sanity/ignore-2.10.txt b/collections-debian-merged/ansible_collections/community/google/tests/sanity/ignore-2.10.txt new file mode 100644 index 00000000..c233efa5 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/google/tests/sanity/ignore-2.10.txt @@ -0,0 +1,22 @@ +plugins/modules/gce_eip.py pylint:blacklisted-name +plugins/modules/gce_eip.py validate-modules:parameter-list-no-elements +plugins/modules/gce_img.py pylint:blacklisted-name +plugins/modules/gce_instance_template.py pylint:blacklisted-name +plugins/modules/gce_instance_template.py validate-modules:doc-missing-type +plugins/modules/gce_instance_template.py validate-modules:parameter-list-no-elements +plugins/modules/gce_labels.py validate-modules:parameter-list-no-elements +plugins/modules/gce_lb.py pylint:blacklisted-name +plugins/modules/gce_lb.py validate-modules:parameter-list-no-elements +plugins/modules/gce_mig.py pylint:blacklisted-name +plugins/modules/gce_mig.py validate-modules:parameter-list-no-elements +plugins/modules/gce_net.py pylint:blacklisted-name +plugins/modules/gce_net.py validate-modules:parameter-list-no-elements +plugins/modules/gce_pd.py pylint:blacklisted-name +plugins/modules/gce_pd.py validate-modules:parameter-list-no-elements +plugins/modules/gce_snapshot.py pylint:blacklisted-name +plugins/modules/gce_snapshot.py validate-modules:parameter-list-no-elements +plugins/modules/gce_tag.py pylint:blacklisted-name +plugins/modules/gce_tag.py validate-modules:parameter-list-no-elements +plugins/modules/gcpubsub.py validate-modules:parameter-list-no-elements +plugins/modules/gcpubsub_info.py validate-modules:parameter-state-invalid-choice +scripts/inventory/gce.py pylint:blacklisted-name diff --git a/collections-debian-merged/ansible_collections/community/google/tests/sanity/ignore-2.11.txt b/collections-debian-merged/ansible_collections/community/google/tests/sanity/ignore-2.11.txt new file mode 100644 index 00000000..c233efa5 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/google/tests/sanity/ignore-2.11.txt @@ -0,0 +1,22 @@ +plugins/modules/gce_eip.py pylint:blacklisted-name +plugins/modules/gce_eip.py validate-modules:parameter-list-no-elements +plugins/modules/gce_img.py pylint:blacklisted-name +plugins/modules/gce_instance_template.py pylint:blacklisted-name +plugins/modules/gce_instance_template.py validate-modules:doc-missing-type +plugins/modules/gce_instance_template.py validate-modules:parameter-list-no-elements +plugins/modules/gce_labels.py validate-modules:parameter-list-no-elements +plugins/modules/gce_lb.py pylint:blacklisted-name +plugins/modules/gce_lb.py validate-modules:parameter-list-no-elements +plugins/modules/gce_mig.py pylint:blacklisted-name +plugins/modules/gce_mig.py validate-modules:parameter-list-no-elements +plugins/modules/gce_net.py pylint:blacklisted-name +plugins/modules/gce_net.py validate-modules:parameter-list-no-elements +plugins/modules/gce_pd.py pylint:blacklisted-name +plugins/modules/gce_pd.py validate-modules:parameter-list-no-elements +plugins/modules/gce_snapshot.py pylint:blacklisted-name +plugins/modules/gce_snapshot.py validate-modules:parameter-list-no-elements +plugins/modules/gce_tag.py pylint:blacklisted-name +plugins/modules/gce_tag.py validate-modules:parameter-list-no-elements +plugins/modules/gcpubsub.py validate-modules:parameter-list-no-elements +plugins/modules/gcpubsub_info.py validate-modules:parameter-state-invalid-choice +scripts/inventory/gce.py pylint:blacklisted-name diff --git a/collections-debian-merged/ansible_collections/community/google/tests/sanity/ignore-2.9.txt b/collections-debian-merged/ansible_collections/community/google/tests/sanity/ignore-2.9.txt new file mode 100644 index 00000000..f2dffaa4 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/google/tests/sanity/ignore-2.9.txt @@ -0,0 +1,11 @@ +plugins/modules/gce_eip.py pylint:blacklisted-name +plugins/modules/gce_img.py pylint:blacklisted-name +plugins/modules/gce_instance_template.py pylint:blacklisted-name +plugins/modules/gce_instance_template.py validate-modules:doc-missing-type +plugins/modules/gce_lb.py pylint:blacklisted-name +plugins/modules/gce_mig.py pylint:blacklisted-name +plugins/modules/gce_net.py pylint:blacklisted-name +plugins/modules/gce_pd.py pylint:blacklisted-name +plugins/modules/gce_snapshot.py pylint:blacklisted-name +plugins/modules/gce_tag.py pylint:blacklisted-name +scripts/inventory/gce.py pylint:blacklisted-name diff --git a/collections-debian-merged/ansible_collections/community/google/tests/unit/compat/__init__.py b/collections-debian-merged/ansible_collections/community/google/tests/unit/compat/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/google/tests/unit/compat/__init__.py diff --git a/collections-debian-merged/ansible_collections/community/google/tests/unit/compat/mock.py b/collections-debian-merged/ansible_collections/community/google/tests/unit/compat/mock.py new file mode 100644 index 00000000..0972cd2e --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/google/tests/unit/compat/mock.py @@ -0,0 +1,122 @@ +# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com> +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +''' +Compat module for Python3.x's unittest.mock module +''' +import sys + +# Python 2.7 + +# Note: Could use the pypi mock library on python3.x as well as python2.x. It +# is the same as the python3 stdlib mock library + +try: + # Allow wildcard import because we really do want to import all of mock's + # symbols into this compat shim + # pylint: disable=wildcard-import,unused-wildcard-import + from unittest.mock import * +except ImportError: + # Python 2 + # pylint: disable=wildcard-import,unused-wildcard-import + try: + from mock import * + except ImportError: + print('You need the mock library installed on python2.x to run tests') + + +# Prior to 3.4.4, mock_open cannot handle binary read_data +if sys.version_info >= (3,) and sys.version_info < (3, 4, 4): + file_spec = None + + def _iterate_read_data(read_data): + # Helper for mock_open: + # Retrieve lines from read_data via a generator so that separate calls to + # readline, read, and readlines are properly interleaved + sep = b'\n' if isinstance(read_data, bytes) else '\n' + data_as_list = [l + sep for l in read_data.split(sep)] + + if data_as_list[-1] == sep: + # If the last line ended in a newline, the list comprehension will have an + # extra entry that's just a newline. Remove this. + data_as_list = data_as_list[:-1] + else: + # If there wasn't an extra newline by itself, then the file being + # emulated doesn't have a newline to end the last line remove the + # newline that our naive format() added + data_as_list[-1] = data_as_list[-1][:-1] + + for line in data_as_list: + yield line + + def mock_open(mock=None, read_data=''): + """ + A helper function to create a mock to replace the use of `open`. It works + for `open` called directly or used as a context manager. + + The `mock` argument is the mock object to configure. If `None` (the + default) then a `MagicMock` will be created for you, with the API limited + to methods or attributes available on standard file handles. + + `read_data` is a string for the `read` methoddline`, and `readlines` of the + file handle to return. This is an empty string by default. + """ + def _readlines_side_effect(*args, **kwargs): + if handle.readlines.return_value is not None: + return handle.readlines.return_value + return list(_data) + + def _read_side_effect(*args, **kwargs): + if handle.read.return_value is not None: + return handle.read.return_value + return type(read_data)().join(_data) + + def _readline_side_effect(): + if handle.readline.return_value is not None: + while True: + yield handle.readline.return_value + for line in _data: + yield line + + global file_spec + if file_spec is None: + import _io + file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO)))) + + if mock is None: + mock = MagicMock(name='open', spec=open) + + handle = MagicMock(spec=file_spec) + handle.__enter__.return_value = handle + + _data = _iterate_read_data(read_data) + + handle.write.return_value = None + handle.read.return_value = None + handle.readline.return_value = None + handle.readlines.return_value = None + + handle.read.side_effect = _read_side_effect + handle.readline.side_effect = _readline_side_effect() + handle.readlines.side_effect = _readlines_side_effect + + mock.return_value = handle + return mock diff --git a/collections-debian-merged/ansible_collections/community/google/tests/unit/compat/unittest.py b/collections-debian-merged/ansible_collections/community/google/tests/unit/compat/unittest.py new file mode 100644 index 00000000..98f08ad6 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/google/tests/unit/compat/unittest.py @@ -0,0 +1,38 @@ +# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com> +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +''' +Compat module for Python2.7's unittest module +''' + +import sys + +# Allow wildcard import because we really do want to import all of +# unittests's symbols into this compat shim +# pylint: disable=wildcard-import,unused-wildcard-import +if sys.version_info < (2, 7): + try: + # Need unittest2 on python2.6 + from unittest2 import * + except ImportError: + print('You need unittest2 installed on python2.6.x to run tests') +else: + from unittest import * diff --git a/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/module_utils/__init__.py b/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/module_utils/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/module_utils/__init__.py diff --git a/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/module_utils/test_auth.py b/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/module_utils/test_auth.py new file mode 100644 index 00000000..845234ff --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/module_utils/test_auth.py @@ -0,0 +1,162 @@ +# -*- coding: utf-8 -*- +# (c) 2016, Tom Melendez (@supertom) <tom@supertom.com> +# +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os + +import pytest + +from ansible_collections.community.google.tests.unit.compat import mock, unittest +from ansible_collections.community.google.plugins.module_utils.gcp import (_get_gcp_ansible_credentials, _get_gcp_credentials, _get_gcp_environ_var, + _get_gcp_environment_credentials, + _validate_credentials_file) + +# Fake data/function used for testing +fake_env_data = {'GCE_EMAIL': 'gce-email'} + + +def fake_get_gcp_environ_var(var_name, default_value): + if var_name not in fake_env_data: + return default_value + else: + return fake_env_data[var_name] + +# Fake AnsibleModule for use in tests + + +class FakeModule(object): + class Params(): + data = {} + + def get(self, key, alt=None): + if key in self.data: + return self.data[key] + else: + return alt + + def __init__(self, data=None): + data = {} if data is None else data + + self.params = FakeModule.Params() + self.params.data = data + + def fail_json(self, **kwargs): + raise ValueError("fail_json") + + def deprecate(self, **kwargs): + return None + + +class GCPAuthTestCase(unittest.TestCase): + """Tests to verify different Auth mechanisms.""" + + def setup_method(self, method): + global fake_env_data + fake_env_data = {'GCE_EMAIL': 'gce-email'} + + def test_get_gcp_ansible_credentials(self): + input_data = {'service_account_email': 'mysa', + 'credentials_file': 'path-to-file.json', + 'project_id': 'my-cool-project'} + + module = FakeModule(input_data) + actual = _get_gcp_ansible_credentials(module) + expected = tuple(input_data.values()) + self.assertEqual(sorted(expected), sorted(actual)) + + def test_get_gcp_environ_var(self): + # Chose not to mock this so we could really verify that it + # works as expected. + existing_var_name = 'gcp_ansible_auth_test_54321' + non_existing_var_name = 'doesnt_exist_gcp_ansible_auth_test_12345' + os.environ[existing_var_name] = 'foobar' + self.assertEqual('foobar', _get_gcp_environ_var( + existing_var_name, None)) + del os.environ[existing_var_name] + self.assertEqual('default_value', _get_gcp_environ_var( + non_existing_var_name, 'default_value')) + + def test_validate_credentials_file(self): + # TODO(supertom): Only dealing with p12 here, check the other states + # of this function + module = FakeModule() + with mock.patch('ansible_collections.community.google.plugins.module_utils.gcp.open', + mock.mock_open(read_data='foobar'), create=True): + # pem condition, warning is suppressed with the return_value + credentials_file = '/foopath/pem.pem' + with self.assertRaises(ValueError): + _validate_credentials_file(module, + credentials_file=credentials_file, + require_valid_json=False, + check_libcloud=False) + + @mock.patch('ansible_collections.community.google.plugins.module_utils.gcp._get_gcp_environ_var', + side_effect=fake_get_gcp_environ_var) + def test_get_gcp_environment_credentials(self, mockobj): + global fake_env_data + + actual = _get_gcp_environment_credentials(None, None, None) + expected = tuple(['gce-email', None, None]) + self.assertEqual(expected, actual) + + fake_env_data = {'GCE_PEM_FILE_PATH': '/path/to/pem.pem'} + expected = tuple([None, '/path/to/pem.pem', None]) + actual = _get_gcp_environment_credentials(None, None, None) + self.assertEqual(expected, actual) + + # pem and creds are set, expect creds + fake_env_data = {'GCE_PEM_FILE_PATH': '/path/to/pem.pem', + 'GCE_CREDENTIALS_FILE_PATH': '/path/to/creds.json'} + expected = tuple([None, '/path/to/creds.json', None]) + actual = _get_gcp_environment_credentials(None, None, None) + self.assertEqual(expected, actual) + + # expect GOOGLE_APPLICATION_CREDENTIALS over PEM + fake_env_data = {'GCE_PEM_FILE_PATH': '/path/to/pem.pem', + 'GOOGLE_APPLICATION_CREDENTIALS': '/path/to/appcreds.json'} + expected = tuple([None, '/path/to/appcreds.json', None]) + actual = _get_gcp_environment_credentials(None, None, None) + self.assertEqual(expected, actual) + + # project tests + fake_env_data = {'GCE_PROJECT': 'my-project'} + expected = tuple([None, None, 'my-project']) + actual = _get_gcp_environment_credentials(None, None, None) + self.assertEqual(expected, actual) + + fake_env_data = {'GOOGLE_CLOUD_PROJECT': 'my-cloud-project'} + expected = tuple([None, None, 'my-cloud-project']) + actual = _get_gcp_environment_credentials(None, None, None) + self.assertEqual(expected, actual) + + # data passed in, picking up project id only + fake_env_data = {'GOOGLE_CLOUD_PROJECT': 'my-project'} + expected = tuple(['my-sa-email', '/path/to/creds.json', 'my-project']) + actual = _get_gcp_environment_credentials( + 'my-sa-email', '/path/to/creds.json', None) + self.assertEqual(expected, actual) + + @mock.patch('ansible_collections.community.google.plugins.module_utils.gcp._get_gcp_environ_var', + side_effect=fake_get_gcp_environ_var) + def test_get_gcp_credentials(self, mockobj): + global fake_env_data + + fake_env_data = {} + module = FakeModule() + module.params.data = {} + # Nothing is set, calls fail_json + with pytest.raises(ValueError): + _get_gcp_credentials(module) + + # project_id (only) is set from Ansible params. + module.params.data['project_id'] = 'my-project' + actual = _get_gcp_credentials( + module, require_valid_json=True, check_libcloud=False) + expected = {'service_account_email': '', + 'project_id': 'my-project', + 'credentials_file': ''} + self.assertEqual(expected, actual) diff --git a/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/module_utils/test_utils.py b/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/module_utils/test_utils.py new file mode 100644 index 00000000..7098f705 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/module_utils/test_utils.py @@ -0,0 +1,361 @@ +# -*- coding: utf-8 -*- +# (c) 2016, Tom Melendez <tom@supertom.com> +# +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from ansible_collections.community.google.tests.unit.compat import mock, unittest +from ansible_collections.community.google.plugins.module_utils.gcp import check_min_pkg_version, GCPUtils, GCPInvalidURLError + + +def build_distribution(version): + obj = mock.MagicMock() + obj.version = '0.5.0' + return obj + + +class GCPUtilsTestCase(unittest.TestCase): + params_dict = { + 'url_map_name': 'foo_url_map_name', + 'description': 'foo_url_map description', + 'host_rules': [ + { + 'description': 'host rules description', + 'hosts': [ + 'www.example.com', + 'www2.example.com' + ], + 'path_matcher': 'host_rules_path_matcher' + } + ], + 'path_matchers': [ + { + 'name': 'path_matcher_one', + 'description': 'path matcher one', + 'defaultService': 'bes-pathmatcher-one-default', + 'pathRules': [ + { + 'service': 'my-one-bes', + 'paths': [ + '/', + '/aboutus' + ] + } + ] + }, + { + 'name': 'path_matcher_two', + 'description': 'path matcher two', + 'defaultService': 'bes-pathmatcher-two-default', + 'pathRules': [ + { + 'service': 'my-two-bes', + 'paths': [ + '/webapp', + '/graphs' + ] + } + ] + } + ] + } + + @mock.patch("pkg_resources.get_distribution", side_effect=build_distribution) + def test_check_minimum_pkg_version(self, mockobj): + self.assertTrue(check_min_pkg_version('foobar', '0.4.0')) + self.assertTrue(check_min_pkg_version('foobar', '0.5.0')) + self.assertFalse(check_min_pkg_version('foobar', '0.6.0')) + + def test_parse_gcp_url(self): + # region, resource, entity, method + input_url = 'https://www.googleapis.com/compute/v1/projects/myproject/regions/us-east1/instanceGroupManagers/my-mig/recreateInstances' + actual = GCPUtils.parse_gcp_url(input_url) + self.assertEqual('compute', actual['service']) + self.assertEqual('v1', actual['api_version']) + self.assertEqual('myproject', actual['project']) + self.assertEqual('us-east1', actual['region']) + self.assertEqual('instanceGroupManagers', actual['resource_name']) + self.assertEqual('my-mig', actual['entity_name']) + self.assertEqual('recreateInstances', actual['method_name']) + + # zone, resource, entity, method + input_url = 'https://www.googleapis.com/compute/v1/projects/myproject/zones/us-east1-c/instanceGroupManagers/my-mig/recreateInstances' + actual = GCPUtils.parse_gcp_url(input_url) + self.assertEqual('compute', actual['service']) + self.assertEqual('v1', actual['api_version']) + self.assertEqual('myproject', actual['project']) + self.assertEqual('us-east1-c', actual['zone']) + self.assertEqual('instanceGroupManagers', actual['resource_name']) + self.assertEqual('my-mig', actual['entity_name']) + self.assertEqual('recreateInstances', actual['method_name']) + + # global, resource + input_url = 'https://www.googleapis.com/compute/v1/projects/myproject/global/urlMaps' + actual = GCPUtils.parse_gcp_url(input_url) + self.assertEqual('compute', actual['service']) + self.assertEqual('v1', actual['api_version']) + self.assertEqual('myproject', actual['project']) + self.assertTrue('global' in actual) + self.assertTrue(actual['global']) + self.assertEqual('urlMaps', actual['resource_name']) + + # global, resource, entity + input_url = 'https://www.googleapis.com/compute/v1/projects/myproject/global/urlMaps/my-url-map' + actual = GCPUtils.parse_gcp_url(input_url) + self.assertEqual('myproject', actual['project']) + self.assertTrue('global' in actual) + self.assertTrue(actual['global']) + self.assertEqual('v1', actual['api_version']) + self.assertEqual('compute', actual['service']) + + # global URL, resource, entity, method_name + input_url = 'https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/mybackendservice/getHealth' + actual = GCPUtils.parse_gcp_url(input_url) + self.assertEqual('compute', actual['service']) + self.assertEqual('v1', actual['api_version']) + self.assertEqual('myproject', actual['project']) + self.assertTrue('global' in actual) + self.assertTrue(actual['global']) + self.assertEqual('backendServices', actual['resource_name']) + self.assertEqual('mybackendservice', actual['entity_name']) + self.assertEqual('getHealth', actual['method_name']) + + # no location in URL + input_url = 'https://www.googleapis.com/compute/v1/projects/myproject/targetHttpProxies/mytargetproxy/setUrlMap' + actual = GCPUtils.parse_gcp_url(input_url) + self.assertEqual('compute', actual['service']) + self.assertEqual('v1', actual['api_version']) + self.assertEqual('myproject', actual['project']) + self.assertFalse('global' in actual) + self.assertEqual('targetHttpProxies', actual['resource_name']) + self.assertEqual('mytargetproxy', actual['entity_name']) + self.assertEqual('setUrlMap', actual['method_name']) + + input_url = 'https://www.googleapis.com/compute/v1/projects/myproject/targetHttpProxies/mytargetproxy' + actual = GCPUtils.parse_gcp_url(input_url) + self.assertEqual('compute', actual['service']) + self.assertEqual('v1', actual['api_version']) + self.assertEqual('myproject', actual['project']) + self.assertFalse('global' in actual) + self.assertEqual('targetHttpProxies', actual['resource_name']) + self.assertEqual('mytargetproxy', actual['entity_name']) + + input_url = 'https://www.googleapis.com/compute/v1/projects/myproject/targetHttpProxies' + actual = GCPUtils.parse_gcp_url(input_url) + self.assertEqual('compute', actual['service']) + self.assertEqual('v1', actual['api_version']) + self.assertEqual('myproject', actual['project']) + self.assertFalse('global' in actual) + self.assertEqual('targetHttpProxies', actual['resource_name']) + + # test exceptions + no_projects_input_url = 'https://www.googleapis.com/compute/v1/not-projects/myproject/global/backendServices/mybackendservice/getHealth' + no_resource_input_url = 'https://www.googleapis.com/compute/v1/not-projects/myproject/global' + + no_resource_no_loc_input_url = 'https://www.googleapis.com/compute/v1/not-projects/myproject' + + with self.assertRaises(GCPInvalidURLError) as cm: + GCPUtils.parse_gcp_url(no_projects_input_url) + self.assertTrue(cm.exception, GCPInvalidURLError) + + with self.assertRaises(GCPInvalidURLError) as cm: + GCPUtils.parse_gcp_url(no_resource_input_url) + self.assertTrue(cm.exception, GCPInvalidURLError) + + with self.assertRaises(GCPInvalidURLError) as cm: + GCPUtils.parse_gcp_url(no_resource_no_loc_input_url) + self.assertTrue(cm.exception, GCPInvalidURLError) + + def test_params_to_gcp_dict(self): + + expected = { + 'description': 'foo_url_map description', + 'hostRules': [ + { + 'description': 'host rules description', + 'hosts': [ + 'www.example.com', + 'www2.example.com' + ], + 'pathMatcher': 'host_rules_path_matcher' + } + ], + 'name': 'foo_url_map_name', + 'pathMatchers': [ + { + 'defaultService': 'bes-pathmatcher-one-default', + 'description': 'path matcher one', + 'name': 'path_matcher_one', + 'pathRules': [ + { + 'paths': [ + '/', + '/aboutus' + ], + 'service': 'my-one-bes' + } + ] + }, + { + 'defaultService': 'bes-pathmatcher-two-default', + 'description': 'path matcher two', + 'name': 'path_matcher_two', + 'pathRules': [ + { + 'paths': [ + '/webapp', + '/graphs' + ], + 'service': 'my-two-bes' + } + ] + } + ] + } + + actual = GCPUtils.params_to_gcp_dict(self.params_dict, 'url_map_name') + self.assertEqual(expected, actual) + + def test_get_gcp_resource_from_methodId(self): + input_data = 'compute.urlMaps.list' + actual = GCPUtils.get_gcp_resource_from_methodId(input_data) + self.assertEqual('urlMaps', actual) + input_data = None + actual = GCPUtils.get_gcp_resource_from_methodId(input_data) + self.assertFalse(actual) + input_data = 666 + actual = GCPUtils.get_gcp_resource_from_methodId(input_data) + self.assertFalse(actual) + + def test_get_entity_name_from_resource_name(self): + input_data = 'urlMaps' + actual = GCPUtils.get_entity_name_from_resource_name(input_data) + self.assertEqual('urlMap', actual) + input_data = 'targetHttpProxies' + actual = GCPUtils.get_entity_name_from_resource_name(input_data) + self.assertEqual('targetHttpProxy', actual) + input_data = 'globalForwardingRules' + actual = GCPUtils.get_entity_name_from_resource_name(input_data) + self.assertEqual('forwardingRule', actual) + input_data = '' + actual = GCPUtils.get_entity_name_from_resource_name(input_data) + self.assertEqual(None, actual) + input_data = 666 + actual = GCPUtils.get_entity_name_from_resource_name(input_data) + self.assertEqual(None, actual) + + def test_are_params_equal(self): + params1 = {'one': 1} + params2 = {'one': 1} + actual = GCPUtils.are_params_equal(params1, params2) + self.assertTrue(actual) + + params1 = {'one': 1} + params2 = {'two': 2} + actual = GCPUtils.are_params_equal(params1, params2) + self.assertFalse(actual) + + params1 = {'three': 3, 'two': 2, 'one': 1} + params2 = {'one': 1, 'two': 2, 'three': 3} + actual = GCPUtils.are_params_equal(params1, params2) + self.assertTrue(actual) + + params1 = { + "creationTimestamp": "2017-04-21T11:19:20.718-07:00", + "defaultService": "https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/default-backend-service", + "description": "", + "fingerprint": "ickr_pwlZPU=", + "hostRules": [ + { + "description": "", + "hosts": [ + "*." + ], + "pathMatcher": "path-matcher-one" + } + ], + "id": "8566395781175047111", + "kind": "compute#urlMap", + "name": "newtesturlmap-foo", + "pathMatchers": [ + { + "defaultService": "https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/bes-pathmatcher-one-default", + "description": "path matcher one", + "name": "path-matcher-one", + "pathRules": [ + { + "paths": [ + "/data", + "/aboutus" + ], + "service": "https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/my-one-bes" + } + ] + } + ], + "selfLink": "https://www.googleapis.com/compute/v1/projects/myproject/global/urlMaps/newtesturlmap-foo" + } + params2 = { + "defaultService": "https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/default-backend-service", + "hostRules": [ + { + "description": "", + "hosts": [ + "*." + ], + "pathMatcher": "path-matcher-one" + } + ], + "name": "newtesturlmap-foo", + "pathMatchers": [ + { + "defaultService": "https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/bes-pathmatcher-one-default", + "description": "path matcher one", + "name": "path-matcher-one", + "pathRules": [ + { + "paths": [ + "/data", + "/aboutus" + ], + "service": "https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/my-one-bes" + } + ] + } + ], + } + + # params1 has exclude fields, params2 doesn't. Should be equal + actual = GCPUtils.are_params_equal(params1, params2) + self.assertTrue(actual) + + def test_filter_gcp_fields(self): + input_data = { + u'kind': u'compute#httpsHealthCheck', + u'description': u'', + u'timeoutSec': 5, + u'checkIntervalSec': 5, + u'port': 443, + u'healthyThreshold': 2, + u'host': u'', + u'requestPath': u'/', + u'unhealthyThreshold': 2, + u'creationTimestamp': u'2017-05-16T15:09:36.546-07:00', + u'id': u'8727093129334146639', + u'selfLink': u'https://www.googleapis.com/compute/v1/projects/myproject/global/httpsHealthChecks/myhealthcheck', + u'name': u'myhealthcheck'} + + expected = { + 'name': 'myhealthcheck', + 'checkIntervalSec': 5, + 'port': 443, + 'unhealthyThreshold': 2, + 'healthyThreshold': 2, + 'host': '', + 'timeoutSec': 5, + 'requestPath': '/'} + + actual = GCPUtils.filter_gcp_fields(input_data) + self.assertEqual(expected, actual) diff --git a/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/modules/__init__.py b/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/modules/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/modules/__init__.py diff --git a/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/modules/test_gce_tag.py b/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/modules/test_gce_tag.py new file mode 100644 index 00000000..3a06f18d --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/google/tests/unit/plugins/modules/test_gce_tag.py @@ -0,0 +1,66 @@ +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import unittest + +from ansible_collections.community.google.plugins.modules.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) diff --git a/collections-debian-merged/ansible_collections/community/google/tests/unit/requirements.txt b/collections-debian-merged/ansible_collections/community/google/tests/unit/requirements.txt new file mode 100644 index 00000000..16494a44 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/google/tests/unit/requirements.txt @@ -0,0 +1 @@ +unittest2 ; python_version < '2.7'
\ No newline at end of file |