Move mocks of designate API discovery calls to base test class

As API discovery call has to be mocked in each Designate's
test case it's better to move it to base test class and reuse
later.
This commit moves it to tests.unit.base.RequestsMockTestCase that
it can be added to mocks in setUp() method before each test.

Change-Id: I3f00c2d51619d634e4e44f3ebf276949544c8575
This commit is contained in:
Sławek Kapłoński 2017-05-14 09:34:09 +00:00
parent 7cfb886b6f
commit f94880c6d8
3 changed files with 45 additions and 45 deletions

View File

@ -476,6 +476,12 @@ class RequestsMockTestCase(BaseTestCase):
return dict(method='GET', uri='https://image.example.com/',
text=open(discovery_fixture, 'r').read())
def get_designate_discovery_mock_dict(self):
discovery_fixture = os.path.join(
self.fixtures_directory, "dns.json")
return dict(method='GET', uri="https://dns.example.com/",
text=open(discovery_fixture, 'r').read())
def use_glance(self, image_version_json='image-version.json'):
# NOTE(notmorgan): This method is only meant to be used in "setUp"
# where the ordering of the url being registered is tightly controlled
@ -485,6 +491,15 @@ class RequestsMockTestCase(BaseTestCase):
self.__do_register_uris([
self.get_glance_discovery_mock_dict(image_version_json)])
def use_designate(self):
# NOTE(slaweq): This method is only meant to be used in "setUp"
# where the ordering of the url being registered is tightly controlled
# if the functionality of .use_designate is meant to be used during an
# actual test case, use .get_designate_discovery_mock and apply to the
# right location in the mock_uris when calling .register_uris
self.__do_register_uris([
self.get_designate_discovery_mock_dict()])
def register_uris(self, uri_mock_list=None):
"""Mock a list of URIs and responses via requests mock.

View File

@ -0,0 +1,22 @@
{
"values": [{
"id": "v1",
"links": [
{
"href": "https://dns.example.com/v1",
"rel": "self"
}
],
"status": "DEPRECATED"
}, {
"id": "v2",
"links": [
{
"href": "https://dns.example.com/v2",
"rel": "self"
}
],
"status": "CURRENT"
}]
}

View File

@ -17,28 +17,6 @@ import shade
from shade.tests.unit import base
api_versions = {
"values": [{
"id": "v1",
"links": [
{
"href": "https://dns.example.com/v1",
"rel": "self"
}
],
"status": "DEPRECATED"
}, {
"id": "v2",
"links": [
{
"href": "https://dns.example.com/v2",
"rel": "self"
}
],
"status": "CURRENT"
}]
}
zone_dict = {
'name': 'example.net.',
'type': 'PRIMARY',
@ -53,10 +31,12 @@ new_zone_dict['id'] = '1'
class TestZone(base.RequestsMockTestCase):
def setUp(self):
super(TestZone, self).setUp()
self.use_designate()
def test_create_zone(self):
self.register_uris([
dict(method='GET',
uri="https://dns.example.com/", json=api_versions),
dict(method='POST',
uri=self.get_mock_url(
'dns', 'public', append=['zones']),
@ -76,8 +56,6 @@ class TestZone(base.RequestsMockTestCase):
def test_create_zone_exception(self):
self.register_uris([
dict(method='GET',
uri="https://dns.example.com/", json=api_versions),
dict(method='POST',
uri=self.get_mock_url(
'dns', 'public', append=['zones']),
@ -95,22 +73,17 @@ class TestZone(base.RequestsMockTestCase):
updated_zone = copy.copy(new_zone_dict)
updated_zone['ttl'] = new_ttl
self.register_uris([
dict(method='GET',
uri="https://dns.example.com/", json=api_versions),
dict(method='GET',
uri=self.get_mock_url(
'dns', 'public', append=['zones']),
json={"zones": [new_zone_dict]}),
dict(method='GET',
uri="https://dns.example.com/", json=api_versions),
self.get_designate_discovery_mock_dict(),
dict(method='GET',
uri=self.get_mock_url(
'dns', 'public', append=['zones'],
qs_elements=['name=1']),
json={'zones': [new_zone_dict]}),
dict(method='GET',
uri="https://dns.example.com/",
json=api_versions),
self.get_designate_discovery_mock_dict(),
dict(method='PATCH',
uri=self.get_mock_url(
'dns', 'public', append=['zones', '1']),
@ -124,21 +97,17 @@ class TestZone(base.RequestsMockTestCase):
def test_delete_zone(self):
self.register_uris([
dict(method='GET',
uri="https://dns.example.com/", json=api_versions),
dict(method='GET',
uri=self.get_mock_url(
'dns', 'public', append=['zones']),
json={"zones": [new_zone_dict]}),
dict(method='GET',
uri="https://dns.example.com/", json=api_versions),
self.get_designate_discovery_mock_dict(),
dict(method='GET',
uri=self.get_mock_url(
'dns', 'public', append=['zones'],
qs_elements=['name=1']),
json={'zones': [new_zone_dict]}),
dict(method='GET',
uri="https://dns.example.com/", json=api_versions),
self.get_designate_discovery_mock_dict(),
dict(method='DELETE',
uri=self.get_mock_url(
'dns', 'public', append=['zones', '1']),
@ -149,8 +118,6 @@ class TestZone(base.RequestsMockTestCase):
def test_get_zone_by_id(self):
self.register_uris([
dict(method='GET',
uri="https://dns.example.com/", json=api_versions),
dict(method='GET',
uri=self.get_mock_url(
'dns', 'public', append=['zones']),
@ -162,8 +129,6 @@ class TestZone(base.RequestsMockTestCase):
def test_get_zone_by_name(self):
self.register_uris([
dict(method='GET',
uri="https://dns.example.com/", json=api_versions),
dict(method='GET',
uri=self.get_mock_url(
'dns', 'public', append=['zones']),
@ -175,8 +140,6 @@ class TestZone(base.RequestsMockTestCase):
def test_get_zone_not_found_returns_false(self):
self.register_uris([
dict(method='GET',
uri="https://dns.example.com/", json=api_versions),
dict(method='GET',
uri=self.get_mock_url(
'dns', 'public', append=['zones']),