Use real mocking in unit tests test_cname_lookup
Replacing function without mocking creates weird behaviors as the results of the tests sometimes depends on the order of the execution. Use real mocking to avoid such behaviors. Example: using real mock showed that test test_host_is_storage_domain was wrong because it was not "reseting" the lookup_cname function at the beginning of the test. Change-Id: Ibc60697c9c850e6e3a108f0d906e8906c3f53ced
This commit is contained in:
parent
b522edf96d
commit
110d7856de
@ -40,9 +40,6 @@ def start_response(*args):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
original_lookup = cname_lookup.lookup_cname
|
|
||||||
|
|
||||||
|
|
||||||
class TestCNAMELookup(unittest.TestCase):
|
class TestCNAMELookup(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -52,8 +49,6 @@ class TestCNAMELookup(unittest.TestCase):
|
|||||||
{'lookup_depth': 2})
|
{'lookup_depth': 2})
|
||||||
|
|
||||||
def test_pass_ip_addresses(self):
|
def test_pass_ip_addresses(self):
|
||||||
cname_lookup.lookup_cname = original_lookup
|
|
||||||
|
|
||||||
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
||||||
headers={'Host': '10.134.23.198'})
|
headers={'Host': '10.134.23.198'})
|
||||||
resp = self.app(req.environ, start_response)
|
resp = self.app(req.environ, start_response)
|
||||||
@ -64,12 +59,9 @@ class TestCNAMELookup(unittest.TestCase):
|
|||||||
resp = self.app(req.environ, start_response)
|
resp = self.app(req.environ, start_response)
|
||||||
self.assertEqual(resp, 'FAKE APP')
|
self.assertEqual(resp, 'FAKE APP')
|
||||||
|
|
||||||
|
@mock.patch('swift.common.middleware.cname_lookup.lookup_cname',
|
||||||
|
new=lambda d: (0, d))
|
||||||
def test_passthrough(self):
|
def test_passthrough(self):
|
||||||
|
|
||||||
def my_lookup(d):
|
|
||||||
return 0, d
|
|
||||||
cname_lookup.lookup_cname = my_lookup
|
|
||||||
|
|
||||||
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
||||||
headers={'Host': 'foo.example.com'})
|
headers={'Host': 'foo.example.com'})
|
||||||
resp = self.app(req.environ, start_response)
|
resp = self.app(req.environ, start_response)
|
||||||
@ -84,14 +76,11 @@ class TestCNAMELookup(unittest.TestCase):
|
|||||||
resp = self.app(req.environ, start_response)
|
resp = self.app(req.environ, start_response)
|
||||||
self.assertEqual(resp, 'FAKE APP')
|
self.assertEqual(resp, 'FAKE APP')
|
||||||
|
|
||||||
|
@mock.patch('swift.common.middleware.cname_lookup.lookup_cname',
|
||||||
|
new=lambda d: (0, '%s.example.com' % d))
|
||||||
def test_good_lookup(self):
|
def test_good_lookup(self):
|
||||||
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
||||||
headers={'Host': 'mysite.com'})
|
headers={'Host': 'mysite.com'})
|
||||||
|
|
||||||
def my_lookup(d):
|
|
||||||
return 0, '%s.example.com' % d
|
|
||||||
cname_lookup.lookup_cname = my_lookup
|
|
||||||
|
|
||||||
resp = self.app(req.environ, start_response)
|
resp = self.app(req.environ, start_response)
|
||||||
self.assertEqual(resp, 'FAKE APP')
|
self.assertEqual(resp, 'FAKE APP')
|
||||||
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
||||||
@ -116,40 +105,33 @@ class TestCNAMELookup(unittest.TestCase):
|
|||||||
elif d == 'level2.foo.com':
|
elif d == 'level2.foo.com':
|
||||||
site = 'bar.example.com'
|
site = 'bar.example.com'
|
||||||
return 0, site
|
return 0, site
|
||||||
cname_lookup.lookup_cname = my_lookup
|
|
||||||
|
|
||||||
resp = self.app(req.environ, start_response)
|
with mock.patch('swift.common.middleware.cname_lookup.lookup_cname',
|
||||||
self.assertEqual(resp, ['CNAME lookup failed after 2 tries'])
|
new=my_lookup):
|
||||||
|
resp = self.app(req.environ, start_response)
|
||||||
|
self.assertEqual(resp, ['CNAME lookup failed after 2 tries'])
|
||||||
|
|
||||||
|
@mock.patch('swift.common.middleware.cname_lookup.lookup_cname',
|
||||||
|
new=lambda d: (0, 'some.invalid.site.com'))
|
||||||
def test_lookup_chain_bad_target(self):
|
def test_lookup_chain_bad_target(self):
|
||||||
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
||||||
headers={'Host': 'mysite.com'})
|
headers={'Host': 'mysite.com'})
|
||||||
|
|
||||||
def my_lookup(d):
|
|
||||||
return 0, 'some.invalid.site.com'
|
|
||||||
cname_lookup.lookup_cname = my_lookup
|
|
||||||
|
|
||||||
resp = self.app(req.environ, start_response)
|
resp = self.app(req.environ, start_response)
|
||||||
self.assertEqual(resp,
|
self.assertEqual(resp,
|
||||||
['CNAME lookup failed to resolve to a valid domain'])
|
['CNAME lookup failed to resolve to a valid domain'])
|
||||||
|
|
||||||
|
@mock.patch('swift.common.middleware.cname_lookup.lookup_cname',
|
||||||
|
new=lambda d: (0, None))
|
||||||
def test_something_weird(self):
|
def test_something_weird(self):
|
||||||
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
||||||
headers={'Host': 'mysite.com'})
|
headers={'Host': 'mysite.com'})
|
||||||
|
|
||||||
def my_lookup(d):
|
|
||||||
return 0, None
|
|
||||||
cname_lookup.lookup_cname = my_lookup
|
|
||||||
|
|
||||||
resp = self.app(req.environ, start_response)
|
resp = self.app(req.environ, start_response)
|
||||||
self.assertEqual(resp,
|
self.assertEqual(resp,
|
||||||
['CNAME lookup failed to resolve to a valid domain'])
|
['CNAME lookup failed to resolve to a valid domain'])
|
||||||
|
|
||||||
|
@mock.patch('swift.common.middleware.cname_lookup.lookup_cname',
|
||||||
|
new=lambda d: (0, '%s.example.com' % d))
|
||||||
def test_with_memcache(self):
|
def test_with_memcache(self):
|
||||||
def my_lookup(d):
|
|
||||||
return 0, '%s.example.com' % d
|
|
||||||
cname_lookup.lookup_cname = my_lookup
|
|
||||||
|
|
||||||
class memcache_stub(object):
|
class memcache_stub(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.cache = {}
|
self.cache = {}
|
||||||
@ -245,29 +227,23 @@ class TestCNAMELookup(unittest.TestCase):
|
|||||||
self.assertEqual(m.call_count, 2)
|
self.assertEqual(m.call_count, 2)
|
||||||
self.assertFalse('cname-mysite5.com' in memcache.cache)
|
self.assertFalse('cname-mysite5.com' in memcache.cache)
|
||||||
|
|
||||||
|
@mock.patch('swift.common.middleware.cname_lookup.lookup_cname',
|
||||||
|
new=lambda d: (0, 'c.aexample.com'))
|
||||||
def test_cname_matching_ending_not_domain(self):
|
def test_cname_matching_ending_not_domain(self):
|
||||||
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
||||||
headers={'Host': 'foo.com'})
|
headers={'Host': 'foo.com'})
|
||||||
|
|
||||||
def my_lookup(d):
|
|
||||||
return 0, 'c.aexample.com'
|
|
||||||
cname_lookup.lookup_cname = my_lookup
|
|
||||||
|
|
||||||
resp = self.app(req.environ, start_response)
|
resp = self.app(req.environ, start_response)
|
||||||
self.assertEqual(resp,
|
self.assertEqual(resp,
|
||||||
['CNAME lookup failed to resolve to a valid domain'])
|
['CNAME lookup failed to resolve to a valid domain'])
|
||||||
|
|
||||||
|
@mock.patch('swift.common.middleware.cname_lookup.lookup_cname',
|
||||||
|
new=lambda d: (0, None))
|
||||||
def test_cname_configured_with_empty_storage_domain(self):
|
def test_cname_configured_with_empty_storage_domain(self):
|
||||||
app = cname_lookup.CNAMELookupMiddleware(FakeApp(),
|
app = cname_lookup.CNAMELookupMiddleware(FakeApp(),
|
||||||
{'storage_domain': '',
|
{'storage_domain': '',
|
||||||
'lookup_depth': 2})
|
'lookup_depth': 2})
|
||||||
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
|
||||||
headers={'Host': 'c.a.example.com'})
|
headers={'Host': 'c.a.example.com'})
|
||||||
|
|
||||||
def my_lookup(d):
|
|
||||||
return 0, None
|
|
||||||
cname_lookup.lookup_cname = my_lookup
|
|
||||||
|
|
||||||
resp = app(req.environ, start_response)
|
resp = app(req.environ, start_response)
|
||||||
self.assertEqual(resp, 'FAKE APP')
|
self.assertEqual(resp, 'FAKE APP')
|
||||||
|
|
||||||
@ -324,7 +300,7 @@ class TestCNAMELookup(unittest.TestCase):
|
|||||||
headers={'Host': host})
|
headers={'Host': host})
|
||||||
return app(req.environ, start_response)
|
return app(req.environ, start_response)
|
||||||
|
|
||||||
bad_domain = ['CNAME lookup failed after 2 tries']
|
bad_domain = ['CNAME lookup failed to resolve to a valid domain']
|
||||||
resp = do_test('c.badtest.com')
|
resp = do_test('c.badtest.com')
|
||||||
self.assertEqual(resp, bad_domain)
|
self.assertEqual(resp, bad_domain)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user