polling tests
This commit is contained in:
@@ -68,22 +68,27 @@ class ZoneState(object):
|
|||||||
marked as offline."""
|
marked as offline."""
|
||||||
self.last_exception = exception
|
self.last_exception = exception
|
||||||
self.last_exception_time = datetime.now()
|
self.last_exception_time = datetime.now()
|
||||||
logging.warning(_("%s error talking to zone %s") % (exception,
|
logging.warning(_("'%s' error talking to zone %s") % (exception,
|
||||||
zone.api_url, FLAGS.zone_failures_to_offline))
|
self.api_url))
|
||||||
|
|
||||||
self.attempt += 1
|
self.attempt += 1
|
||||||
if self.attempt >= FLAGS.zone_failures_to_offline:
|
if self.attempt >= FLAGS.zone_failures_to_offline:
|
||||||
self.is_active = False
|
self.is_active = False
|
||||||
logging.error(_("No answer from zone %s after %d "
|
logging.error(_("No answer from zone %s after %d "
|
||||||
"attempts. Marking inactive.") % (zone.api_url,
|
"attempts. Marking inactive.") % (self.api_url,
|
||||||
FLAGS.zone_failures_to_offline))
|
FLAGS.zone_failures_to_offline))
|
||||||
|
|
||||||
|
|
||||||
|
def _call_novatools(zone):
|
||||||
|
"""Call novatools. Broken out for testing purposes."""
|
||||||
|
os = novatools.OpenStack(zone.username, zone.password, zone.api_url)
|
||||||
|
return os.zones.info()._info
|
||||||
|
|
||||||
def _poll_zone(zone):
|
def _poll_zone(zone):
|
||||||
"""Eventlet worker to poll a zone."""
|
"""Eventlet worker to poll a zone."""
|
||||||
logging.debug("_POLL_ZONE: STARTING")
|
logging.debug("_POLL_ZONE: STARTING")
|
||||||
os = novatools.OpenStack(zone.username, zone.password, zone.api_url)
|
|
||||||
try:
|
try:
|
||||||
zone.update_metadata(os.zones.info()._info)
|
zone.update_metadata(_call_novatools(zone))
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
zone.log_error(e)
|
zone.log_error(e)
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ Tests For ZoneManager
|
|||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import mox
|
import mox
|
||||||
|
import novatools
|
||||||
|
|
||||||
from nova import context
|
from nova import context
|
||||||
from nova import db
|
from nova import db
|
||||||
@@ -30,6 +31,8 @@ from nova import utils
|
|||||||
from nova.auth import manager as auth_manager
|
from nova.auth import manager as auth_manager
|
||||||
from nova.scheduler import zone_manager
|
from nova.scheduler import zone_manager
|
||||||
|
|
||||||
|
FLAGS = flags.FLAGS
|
||||||
|
|
||||||
|
|
||||||
class FakeZone:
|
class FakeZone:
|
||||||
"""Represents a fake zone from the db"""
|
"""Represents a fake zone from the db"""
|
||||||
@@ -38,6 +41,11 @@ class FakeZone:
|
|||||||
setattr(self, k, v)
|
setattr(self, k, v)
|
||||||
|
|
||||||
|
|
||||||
|
def exploding_novatools(zone):
|
||||||
|
"""Used when we want to simulate a novatools call failing."""
|
||||||
|
raise Exception("kaboom")
|
||||||
|
|
||||||
|
|
||||||
class ZoneManagerTestCase(test.TestCase):
|
class ZoneManagerTestCase(test.TestCase):
|
||||||
"""Test case for zone manager"""
|
"""Test case for zone manager"""
|
||||||
def test_ping(self):
|
def test_ping(self):
|
||||||
@@ -130,3 +138,36 @@ class ZoneManagerTestCase(test.TestCase):
|
|||||||
|
|
||||||
self.assertEquals(len(zm.zone_states), 1)
|
self.assertEquals(len(zm.zone_states), 1)
|
||||||
self.assertEquals(zm.zone_states[2].username, 'user2')
|
self.assertEquals(zm.zone_states[2].username, 'user2')
|
||||||
|
|
||||||
|
def test_poll_zone(self):
|
||||||
|
self.mox.StubOutWithMock(zone_manager, '_call_novatools')
|
||||||
|
zone_manager._call_novatools(mox.IgnoreArg()).AndReturn(
|
||||||
|
dict(name='zohan', capabilities='hairdresser'))
|
||||||
|
|
||||||
|
zone_state = zone_manager.ZoneState()
|
||||||
|
zone_state.update_credentials(FakeZone(id=2,
|
||||||
|
api_url='http://foo.com', username='user2',
|
||||||
|
password='pass2'))
|
||||||
|
zone_state.attempt = 1
|
||||||
|
|
||||||
|
self.mox.ReplayAll()
|
||||||
|
zone_manager._poll_zone(zone_state)
|
||||||
|
self.mox.VerifyAll()
|
||||||
|
self.assertEquals(zone_state.attempt, 0)
|
||||||
|
self.assertEquals(zone_state.name, 'zohan')
|
||||||
|
|
||||||
|
def test_poll_zone_fails(self):
|
||||||
|
self.stubs.Set(zone_manager, "_call_novatools", exploding_novatools)
|
||||||
|
|
||||||
|
zone_state = zone_manager.ZoneState()
|
||||||
|
zone_state.update_credentials(FakeZone(id=2,
|
||||||
|
api_url='http://foo.com', username='user2',
|
||||||
|
password='pass2'))
|
||||||
|
zone_state.attempt = FLAGS.zone_failures_to_offline - 1
|
||||||
|
|
||||||
|
self.mox.ReplayAll()
|
||||||
|
zone_manager._poll_zone(zone_state)
|
||||||
|
self.mox.VerifyAll()
|
||||||
|
self.assertEquals(zone_state.attempt, 3)
|
||||||
|
self.assertFalse(zone_state.is_active)
|
||||||
|
self.assertEquals(zone_state.name, None)
|
||||||
|
|||||||
Reference in New Issue
Block a user