mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-05 11:46:57 +00:00
Move modules and module_utils unit tests to correct place (#81)
* Move modules and module_utils unit tests to correct place. * Update ignore.txt * Fix imports. * Fix typos. * Fix more typos.
This commit is contained in:
parent
ab3c2120fb
commit
be191cce6c
1170 changed files with 732 additions and 751 deletions
0
tests/unit/plugins/module_utils/gcp/__init__.py
Normal file
0
tests/unit/plugins/module_utils/gcp/__init__.py
Normal file
171
tests/unit/plugins/module_utils/gcp/test_auth.py
Normal file
171
tests/unit/plugins/module_utils/gcp/test_auth.py
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# (c) 2016, Tom Melendez (@supertom) <tom@supertom.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/>.
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import mock, unittest
|
||||
from ansible_collections.community.general.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.general.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.general.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.general.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)
|
||||
245
tests/unit/plugins/module_utils/gcp/test_gcp_utils.py
Normal file
245
tests/unit/plugins/module_utils/gcp/test_gcp_utils.py
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# (c) 2016, Tom Melendez <tom@supertom.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/>.
|
||||
from ansible_collections.community.general.tests.unit.compat import unittest
|
||||
from ansible_collections.google.cloud.plugins.module_utils.gcp_utils import (GcpRequest,
|
||||
navigate_hash,
|
||||
remove_nones_from_dict,
|
||||
replace_resource_dict)
|
||||
|
||||
|
||||
class ReplaceResourceDictTestCase(unittest.TestCase):
|
||||
def test_given_dict(self):
|
||||
value = {
|
||||
'selfLink': 'value'
|
||||
}
|
||||
self.assertEqual(replace_resource_dict(value, 'selfLink'), value['selfLink'])
|
||||
|
||||
def test_given_array(self):
|
||||
value = {
|
||||
'selfLink': 'value'
|
||||
}
|
||||
self.assertEqual(replace_resource_dict([value] * 3, 'selfLink'), [value['selfLink']] * 3)
|
||||
|
||||
|
||||
class NavigateHashTestCase(unittest.TestCase):
|
||||
def test_one_level(self):
|
||||
value = {
|
||||
'key': 'value'
|
||||
}
|
||||
self.assertEqual(navigate_hash(value, ['key']), value['key'])
|
||||
|
||||
def test_multilevel(self):
|
||||
value = {
|
||||
'key': {
|
||||
'key2': 'value'
|
||||
}
|
||||
}
|
||||
self.assertEqual(navigate_hash(value, ['key', 'key2']), value['key']['key2'])
|
||||
|
||||
def test_default(self):
|
||||
value = {
|
||||
'key': 'value'
|
||||
}
|
||||
default = 'not found'
|
||||
self.assertEqual(navigate_hash(value, ['key', 'key2'], default), default)
|
||||
|
||||
|
||||
class RemoveNonesFromDictTestCase(unittest.TestCase):
|
||||
def test_remove_nones(self):
|
||||
value = {
|
||||
'key': None,
|
||||
'good': 'value'
|
||||
}
|
||||
value_correct = {
|
||||
'good': 'value'
|
||||
}
|
||||
self.assertEqual(remove_nones_from_dict(value), value_correct)
|
||||
|
||||
def test_remove_empty_arrays(self):
|
||||
value = {
|
||||
'key': [],
|
||||
'good': 'value'
|
||||
}
|
||||
value_correct = {
|
||||
'good': 'value'
|
||||
}
|
||||
self.assertEqual(remove_nones_from_dict(value), value_correct)
|
||||
|
||||
def test_remove_empty_dicts(self):
|
||||
value = {
|
||||
'key': {},
|
||||
'good': 'value'
|
||||
}
|
||||
value_correct = {
|
||||
'good': 'value'
|
||||
}
|
||||
self.assertEqual(remove_nones_from_dict(value), value_correct)
|
||||
|
||||
|
||||
class GCPRequestDifferenceTestCase(unittest.TestCase):
|
||||
def test_simple_no_difference(self):
|
||||
value1 = {
|
||||
'foo': 'bar',
|
||||
'test': 'original'
|
||||
}
|
||||
request = GcpRequest(value1)
|
||||
self.assertEqual(request, request)
|
||||
|
||||
def test_simple_different(self):
|
||||
value1 = {
|
||||
'foo': 'bar',
|
||||
'test': 'original'
|
||||
}
|
||||
value2 = {
|
||||
'foo': 'bar',
|
||||
'test': 'different'
|
||||
}
|
||||
difference = {
|
||||
'test': 'original'
|
||||
}
|
||||
request1 = GcpRequest(value1)
|
||||
request2 = GcpRequest(value2)
|
||||
self.assertNotEquals(request1, request2)
|
||||
self.assertEqual(request1.difference(request2), difference)
|
||||
|
||||
def test_nested_dictionaries_no_difference(self):
|
||||
value1 = {
|
||||
'foo': {
|
||||
'quiet': {
|
||||
'tree': 'test'
|
||||
},
|
||||
'bar': 'baz'
|
||||
},
|
||||
'test': 'original'
|
||||
}
|
||||
request = GcpRequest(value1)
|
||||
self.assertEqual(request, request)
|
||||
|
||||
def test_nested_dictionaries_with_difference(self):
|
||||
value1 = {
|
||||
'foo': {
|
||||
'quiet': {
|
||||
'tree': 'test'
|
||||
},
|
||||
'bar': 'baz'
|
||||
},
|
||||
'test': 'original'
|
||||
}
|
||||
value2 = {
|
||||
'foo': {
|
||||
'quiet': {
|
||||
'tree': 'baz'
|
||||
},
|
||||
'bar': 'hello'
|
||||
},
|
||||
'test': 'original'
|
||||
}
|
||||
difference = {
|
||||
'foo': {
|
||||
'quiet': {
|
||||
'tree': 'test'
|
||||
},
|
||||
'bar': 'baz'
|
||||
}
|
||||
}
|
||||
request1 = GcpRequest(value1)
|
||||
request2 = GcpRequest(value2)
|
||||
self.assertNotEquals(request1, request2)
|
||||
self.assertEqual(request1.difference(request2), difference)
|
||||
|
||||
def test_arrays_strings_no_difference(self):
|
||||
value1 = {
|
||||
'foo': [
|
||||
'baz',
|
||||
'bar'
|
||||
]
|
||||
}
|
||||
request = GcpRequest(value1)
|
||||
self.assertEqual(request, request)
|
||||
|
||||
def test_arrays_strings_with_difference(self):
|
||||
value1 = {
|
||||
'foo': [
|
||||
'baz',
|
||||
'bar',
|
||||
]
|
||||
}
|
||||
|
||||
value2 = {
|
||||
'foo': [
|
||||
'baz',
|
||||
'hello'
|
||||
]
|
||||
}
|
||||
difference = {
|
||||
'foo': [
|
||||
'bar',
|
||||
]
|
||||
}
|
||||
request1 = GcpRequest(value1)
|
||||
request2 = GcpRequest(value2)
|
||||
self.assertNotEquals(request1, request2)
|
||||
self.assertEqual(request1.difference(request2), difference)
|
||||
|
||||
def test_arrays_dicts_with_no_difference(self):
|
||||
value1 = {
|
||||
'foo': [
|
||||
{
|
||||
'test': 'value',
|
||||
'foo': 'bar'
|
||||
},
|
||||
{
|
||||
'different': 'dict'
|
||||
}
|
||||
]
|
||||
}
|
||||
request = GcpRequest(value1)
|
||||
self.assertEqual(request, request)
|
||||
|
||||
def test_arrays_dicts_with_difference(self):
|
||||
value1 = {
|
||||
'foo': [
|
||||
{
|
||||
'test': 'value',
|
||||
'foo': 'bar'
|
||||
},
|
||||
{
|
||||
'different': 'dict'
|
||||
}
|
||||
]
|
||||
}
|
||||
value2 = {
|
||||
'foo': [
|
||||
{
|
||||
'test': 'value2',
|
||||
'foo': 'bar2'
|
||||
},
|
||||
]
|
||||
}
|
||||
difference = {
|
||||
'foo': [
|
||||
{
|
||||
'test': 'value',
|
||||
'foo': 'bar'
|
||||
}
|
||||
]
|
||||
}
|
||||
request1 = GcpRequest(value1)
|
||||
request2 = GcpRequest(value2)
|
||||
self.assertNotEquals(request1, request2)
|
||||
self.assertEqual(request1.difference(request2), difference)
|
||||
371
tests/unit/plugins/module_utils/gcp/test_utils.py
Normal file
371
tests/unit/plugins/module_utils/gcp/test_utils.py
Normal file
|
|
@ -0,0 +1,371 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# (c) 2016, Tom Melendez <tom@supertom.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/>.
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat import mock, unittest
|
||||
from ansible_collections.community.general.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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue