Fix api_server agent test that broke due to a dep change

The test was slightly incorrect to begin with, but the issue didn't
become apparent until the oslo.log update from 1.13.0 to 2.0.0 for
whatever reason. open() raises IOError() not Exception() so it was not
being handled properly.

Also taking this opportunity to standardize the use of builtins to
match our other tests.

Change-Id: Idab637b09e8a437333f810f82f45b3e9c027dfdd
This commit is contained in:
Adam Harwell 2015-11-24 14:21:06 -06:00
parent 822c0be975
commit 900e8a5256

View File

@ -30,9 +30,10 @@ import octavia.tests.unit.base as base
RANDOM_ERROR = 'random error' RANDOM_ERROR = 'random error'
OK = dict(message='OK') OK = dict(message='OK')
BUILTINS = '__builtin__' if six.PY2:
if six.PY3: import __builtin__ as builtins
BUILTINS = 'builtins' else:
import builtins
class ServerTestCase(base.TestCase): class ServerTestCase(base.TestCase):
@ -53,7 +54,7 @@ class ServerTestCase(base.TestCase):
m = mock.mock_open() m = mock.mock_open()
# happy case upstart file exists # happy case upstart file exists
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION + rv = self.app.put('/' + api_server.VERSION +
'/listeners/123/haproxy', data='test') '/listeners/123/haproxy', data='test')
self.assertEqual(202, rv.status_code) self.assertEqual(202, rv.status_code)
@ -69,9 +70,9 @@ class ServerTestCase(base.TestCase):
'/var/lib/octavia/123/haproxy.cfg') '/var/lib/octavia/123/haproxy.cfg')
# exception writing # exception writing
m = mock.Mock() m = mock.mock_open()
m.side_effect = Exception() # open crashes m.side_effect = IOError() # open crashes
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION + rv = self.app.put('/' + api_server.VERSION +
'/listeners/123/haproxy', data='test') '/listeners/123/haproxy', data='test')
self.assertEqual(500, rv.status_code) self.assertEqual(500, rv.status_code)
@ -81,7 +82,7 @@ class ServerTestCase(base.TestCase):
m = mock.mock_open() m = mock.mock_open()
# happy case upstart file exists # happy case upstart file exists
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION + rv = self.app.put('/' + api_server.VERSION +
'/listeners/123/haproxy', data='test') '/listeners/123/haproxy', data='test')
self.assertEqual(202, rv.status_code) self.assertEqual(202, rv.status_code)
@ -96,7 +97,7 @@ class ServerTestCase(base.TestCase):
mock_exists.return_value = True mock_exists.return_value = True
mock_subprocess.side_effect = [subprocess.CalledProcessError( mock_subprocess.side_effect = [subprocess.CalledProcessError(
7, 'test', RANDOM_ERROR)] 7, 'test', RANDOM_ERROR)]
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION + rv = self.app.put('/' + api_server.VERSION +
'/listeners/123/haproxy', data='test') '/listeners/123/haproxy', data='test')
self.assertEqual(400, rv.status_code) self.assertEqual(400, rv.status_code)
@ -246,7 +247,7 @@ class ServerTestCase(base.TestCase):
mock_exists.side_effect = [True] mock_exists.side_effect = [True]
m = mock.mock_open(read_data=CONTENT) m = mock.mock_open(read_data=CONTENT)
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.get('/' + api_server.VERSION + rv = self.app.get('/' + api_server.VERSION +
'/listeners/123/haproxy') '/listeners/123/haproxy')
self.assertEqual(200, rv.status_code) self.assertEqual(200, rv.status_code)
@ -399,7 +400,7 @@ class ServerTestCase(base.TestCase):
m = mock.mock_open(read_data=CONTENT) m = mock.mock_open(read_data=CONTENT)
mock_exists.return_value = True mock_exists.return_value = True
mock_exists.side_effect = None mock_exists.side_effect = None
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.get('/' + api_server.VERSION + rv = self.app.get('/' + api_server.VERSION +
'/listeners/123/certificates/test.pem') '/listeners/123/certificates/test.pem')
self.assertEqual(200, rv.status_code) self.assertEqual(200, rv.status_code)
@ -421,7 +422,7 @@ class ServerTestCase(base.TestCase):
mock_exists.side_effect = [True, True, True] mock_exists.side_effect = [True, True, True]
m = mock.mock_open() m = mock.mock_open()
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION + rv = self.app.put('/' + api_server.VERSION +
'/listeners/123/certificates/test.pem', '/listeners/123/certificates/test.pem',
data='TestTest') data='TestTest')
@ -434,7 +435,7 @@ class ServerTestCase(base.TestCase):
mock_exists.side_effect = [True, False] mock_exists.side_effect = [True, False]
m = mock.mock_open() m = mock.mock_open()
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION + rv = self.app.put('/' + api_server.VERSION +
'/listeners/123/certificates/test.pem', '/listeners/123/certificates/test.pem',
data='TestTest') data='TestTest')
@ -448,7 +449,7 @@ class ServerTestCase(base.TestCase):
certificate_update.BUFFER = 5 # test the while loop certificate_update.BUFFER = 5 # test the while loop
m = mock.mock_open() m = mock.mock_open()
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION + rv = self.app.put('/' + api_server.VERSION +
'/certificate', '/certificate',
data='TestTest') data='TestTest')
@ -491,7 +492,7 @@ class ServerTestCase(base.TestCase):
mock_ifaddress.side_effect = [[netifaces.AF_LINK], mock_ifaddress.side_effect = [[netifaces.AF_LINK],
{netifaces.AF_LINK: [{'addr': '123'}]}] {netifaces.AF_LINK: [{'addr': '123'}]}]
m = mock.mock_open() m = mock.mock_open()
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.post('/' + api_server.VERSION + "/plug/network", rv = self.app.post('/' + api_server.VERSION + "/plug/network",
content_type='application/json', content_type='application/json',
data=json.dumps(port_info)) data=json.dumps(port_info))
@ -514,7 +515,7 @@ class ServerTestCase(base.TestCase):
7, 'test', RANDOM_ERROR), subprocess.CalledProcessError( 7, 'test', RANDOM_ERROR), subprocess.CalledProcessError(
7, 'test', RANDOM_ERROR)] 7, 'test', RANDOM_ERROR)]
m = mock.mock_open() m = mock.mock_open()
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.post('/' + api_server.VERSION + "/plug/network", rv = self.app.post('/' + api_server.VERSION + "/plug/network",
content_type='application/json', content_type='application/json',
data=json.dumps(port_info)) data=json.dumps(port_info))
@ -569,7 +570,7 @@ class ServerTestCase(base.TestCase):
mock_ifaddress.side_effect = [[netifaces.AF_LINK], mock_ifaddress.side_effect = [[netifaces.AF_LINK],
{netifaces.AF_LINK: [{'addr': '123'}]}] {netifaces.AF_LINK: [{'addr': '123'}]}]
m = mock.mock_open() m = mock.mock_open()
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.post('/' + api_server.VERSION + rv = self.app.post('/' + api_server.VERSION +
"/plug/vip/203.0.113.2", "/plug/vip/203.0.113.2",
content_type='application/json', content_type='application/json',
@ -598,7 +599,7 @@ class ServerTestCase(base.TestCase):
7, 'test', RANDOM_ERROR), subprocess.CalledProcessError( 7, 'test', RANDOM_ERROR), subprocess.CalledProcessError(
7, 'test', RANDOM_ERROR)] 7, 'test', RANDOM_ERROR)]
m = mock.mock_open() m = mock.mock_open()
with mock.patch('%s.open' % BUILTINS, m, create=True): with mock.patch.object(builtins, 'open', m):
rv = self.app.post('/' + api_server.VERSION + rv = self.app.post('/' + api_server.VERSION +
"/plug/vip/203.0.113.2", "/plug/vip/203.0.113.2",
content_type='application/json', content_type='application/json',