Comments for bugfix800759 and pep8
This commit is contained in:
@@ -125,7 +125,7 @@ class ZoneManager(object):
|
||||
# But it's likely to change once we understand what the Best-Match
|
||||
# code will need better.
|
||||
combined = {} # { <service>_<cap> : (min, max), ... }
|
||||
stale_host_services = {} # { host1 : [svc1, svc2], host2 :[svc1]}
|
||||
stale_host_services = {} # { host1 : [svc1, svc2], host2 :[svc1]}
|
||||
for host, host_dict in hosts_dict.iteritems():
|
||||
for service_name, service_dict in host_dict.iteritems():
|
||||
|
||||
@@ -136,11 +136,10 @@ class ZoneManager(object):
|
||||
stale_host_services[host].append(service_name)
|
||||
continue
|
||||
for cap, value in service_dict.iteritems():
|
||||
if cap == "timestamp": # Timestamp is not needed
|
||||
if cap == "timestamp": # Timestamp is not needed
|
||||
continue
|
||||
key = "%s_%s" % (service_name, cap)
|
||||
min_value, max_value = combined.get(key, \
|
||||
(value, value))
|
||||
min_value, max_value = combined.get(key, (value, value))
|
||||
min_value = min(min_value, value)
|
||||
max_value = max(max_value, value)
|
||||
combined[key] = (min_value, max_value)
|
||||
@@ -185,7 +184,7 @@ class ZoneManager(object):
|
||||
logging.debug(_("Received %(service_name)s service update from "
|
||||
"%(host)s: %(capabilities)s") % locals())
|
||||
service_caps = self.service_states.get(host, {})
|
||||
capabilities["timestamp"] = utils.utcnow() # Reported time
|
||||
capabilities["timestamp"] = utils.utcnow() # Reported time
|
||||
service_caps[service_name] = capabilities
|
||||
self.service_states[host] = service_caps
|
||||
|
||||
@@ -204,6 +203,5 @@ class ZoneManager(object):
|
||||
service_caps = self.service_states[host]
|
||||
for service in services:
|
||||
del service_caps[service]
|
||||
if len(service_caps) == 0: # Delete host if no services
|
||||
if len(service_caps) == 0: # Delete host if no services
|
||||
del self.service_states[host]
|
||||
|
||||
|
@@ -199,31 +199,42 @@ class ZoneManagerTestCase(test.TestCase):
|
||||
self.assertFalse(zone_state.is_active)
|
||||
self.assertEquals(zone_state.name, None)
|
||||
|
||||
def test_host_service_caps_stale(self):
|
||||
def test_host_service_caps_stale_no_stale_service(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
expiry_time = (FLAGS.periodic_interval * 3) + 1
|
||||
|
||||
# services just updated capabilities
|
||||
# services just updated capabilities
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
zm.update_service_capabilities("svc2", "host1", dict(a=3, b=4))
|
||||
self.assertFalse(zm.host_service_caps_stale("host1", "svc1"))
|
||||
self.assertFalse(zm.host_service_caps_stale("host1", "svc2"))
|
||||
|
||||
def test_host_service_caps_stale_all_stale_services(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
expiry_time = (FLAGS.periodic_interval * 3) + 1
|
||||
|
||||
# Both services became stale
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
zm.update_service_capabilities("svc2", "host1", dict(a=3, b=4))
|
||||
time_future = utils.utcnow() + datetime.timedelta(seconds=expiry_time)
|
||||
utils.set_time_override(time_future)
|
||||
self.assertTrue(zm.host_service_caps_stale("host1", "svc1"))
|
||||
self.assertTrue(zm.host_service_caps_stale("host1", "svc2"))
|
||||
utils.clear_time_override()
|
||||
|
||||
def test_host_service_caps_stale_one_stale_service(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
expiry_time = (FLAGS.periodic_interval * 3) + 1
|
||||
|
||||
# One service became stale
|
||||
utils.clear_time_override()
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
zm.update_service_capabilities("svc2", "host1", dict(a=3, b=4))
|
||||
caps = zm.service_states["host1"]["svc1"]
|
||||
caps["timestamp"] = utils.utcnow() - \
|
||||
datetime.timedelta(seconds=expiry_time)
|
||||
self.assertTrue(zm.host_service_caps_stale("host1", "svc1"))
|
||||
self.assertFalse(zm.host_service_caps_stale("host1", "svc2"))
|
||||
|
||||
def test_delete_expired_host_services(self):
|
||||
|
||||
def test_delete_expired_host_services_del_one_service(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
|
||||
# Delete one service in a host
|
||||
@@ -234,12 +245,19 @@ class ZoneManagerTestCase(test.TestCase):
|
||||
self.assertFalse("svc1" in zm.service_states["host1"])
|
||||
self.assertTrue("svc2" in zm.service_states["host1"])
|
||||
|
||||
def test_delete_expired_host_services_del_all_hosts(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
|
||||
# Delete all services in a host
|
||||
zm.update_service_capabilities("svc2", "host1", dict(a=3, b=4))
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
stale_host_services = {"host1": ["svc1", "svc2"]}
|
||||
zm.delete_expired_host_services(stale_host_services)
|
||||
self.assertFalse("host1" in zm.service_states)
|
||||
|
||||
def test_delete_expired_host_services_del_one_service_per_host(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
|
||||
# Delete one service per host
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
zm.update_service_capabilities("svc1", "host2", dict(a=3, b=4))
|
||||
@@ -247,45 +265,70 @@ class ZoneManagerTestCase(test.TestCase):
|
||||
zm.delete_expired_host_services(stale_host_services)
|
||||
self.assertFalse("host1" in zm.service_states)
|
||||
self.assertFalse("host2" in zm.service_states)
|
||||
|
||||
def test_get_zone_capabilities(self):
|
||||
|
||||
def test_get_zone_capabilities_one_host(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
expiry_time = (FLAGS.periodic_interval * 3) + 1
|
||||
|
||||
# Service capabilities recent
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
caps = zm.get_zone_capabilities(None)
|
||||
self.assertEquals(caps, dict(svc1_a=(1, 1), svc1_b=(2, 2)))
|
||||
|
||||
def test_get_zone_capabilities_expired_host(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
expiry_time = (FLAGS.periodic_interval * 3) + 1
|
||||
|
||||
# Service capabilities stale
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
time_future = utils.utcnow() + datetime.timedelta(seconds=expiry_time)
|
||||
utils.set_time_override(time_future)
|
||||
caps = zm.get_zone_capabilities(None)
|
||||
self.assertEquals(caps, {})
|
||||
utils.clear_time_override()
|
||||
|
||||
def test_get_zone_capabilities_multiple_hosts(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
|
||||
# Both host service capabilities recent
|
||||
utils.clear_time_override()
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
zm.update_service_capabilities("svc1", "host2", dict(a=3, b=4))
|
||||
caps = zm.get_zone_capabilities(None)
|
||||
self.assertEquals(caps, dict(svc1_a=(1, 3), svc1_b=(2, 4)))
|
||||
|
||||
def test_get_zone_capabilities_one_stale_host(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
expiry_time = (FLAGS.periodic_interval * 3) + 1
|
||||
|
||||
# One host service capabilities become stale
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
zm.update_service_capabilities("svc1", "host2", dict(a=3, b=4))
|
||||
serv_caps = zm.service_states["host1"]["svc1"]
|
||||
serv_caps["timestamp"] = utils.utcnow() - \
|
||||
datetime.timedelta(seconds=expiry_time)
|
||||
caps = zm.get_zone_capabilities(None)
|
||||
self.assertEquals(caps, dict(svc1_a=(3, 3), svc1_b=(4, 4)))
|
||||
|
||||
# Multiple services per host
|
||||
def test_get_zone_capabilities_multiple_service_per_host(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
|
||||
# Multiple services per host
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
zm.update_service_capabilities("svc1", "host2", dict(a=3, b=4))
|
||||
zm.update_service_capabilities("svc2", "host1", dict(a=5, b=6))
|
||||
zm.update_service_capabilities("svc2", "host2", dict(a=7, b=8))
|
||||
caps = zm.get_zone_capabilities(None)
|
||||
self.assertEquals(caps, dict(svc1_a=(1, 3), svc1_b=(2, 4),
|
||||
svc2_a=(5, 7), svc2_b=(6, 8)))
|
||||
|
||||
# Two host services among four become stale
|
||||
def test_get_zone_capabilities_one_stale_service_per_host(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
expiry_time = (FLAGS.periodic_interval * 3) + 1
|
||||
|
||||
# Two host services among four become stale
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
zm.update_service_capabilities("svc1", "host2", dict(a=3, b=4))
|
||||
zm.update_service_capabilities("svc2", "host1", dict(a=5, b=6))
|
||||
zm.update_service_capabilities("svc2", "host2", dict(a=7, b=8))
|
||||
serv_caps_1 = zm.service_states["host1"]["svc2"]
|
||||
serv_caps_1["timestamp"] = utils.utcnow() - \
|
||||
datetime.timedelta(seconds=expiry_time)
|
||||
@@ -296,9 +339,15 @@ class ZoneManagerTestCase(test.TestCase):
|
||||
self.assertEquals(caps, dict(svc1_a=(1, 1), svc1_b=(2, 2),
|
||||
svc2_a=(7, 7), svc2_b=(8, 8)))
|
||||
|
||||
# Three host services among four become stale
|
||||
zm.update_service_capabilities("svc2", "host1", dict(a=5, b=6))
|
||||
def test_get_zone_capabilities_three_stale_host_services(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
expiry_time = (FLAGS.periodic_interval * 3) + 1
|
||||
|
||||
# Three host services among four become stale
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
zm.update_service_capabilities("svc1", "host2", dict(a=3, b=4))
|
||||
zm.update_service_capabilities("svc2", "host1", dict(a=5, b=6))
|
||||
zm.update_service_capabilities("svc2", "host2", dict(a=7, b=8))
|
||||
serv_caps_1 = zm.service_states["host1"]["svc2"]
|
||||
serv_caps_1["timestamp"] = utils.utcnow() - \
|
||||
datetime.timedelta(seconds=expiry_time)
|
||||
@@ -311,11 +360,16 @@ class ZoneManagerTestCase(test.TestCase):
|
||||
caps = zm.get_zone_capabilities(None)
|
||||
self.assertEquals(caps, dict(svc1_a=(1, 1), svc1_b=(2, 2)))
|
||||
|
||||
# All the host services become stale
|
||||
zm.update_service_capabilities("svc2", "host1", dict(a=5, b=6))
|
||||
def test_get_zone_capabilities_all_stale_host_services(self):
|
||||
zm = zone_manager.ZoneManager()
|
||||
expiry_time = (FLAGS.periodic_interval * 3) + 1
|
||||
|
||||
# All the host services become stale
|
||||
zm.update_service_capabilities("svc1", "host1", dict(a=1, b=2))
|
||||
zm.update_service_capabilities("svc1", "host2", dict(a=3, b=4))
|
||||
zm.update_service_capabilities("svc2", "host1", dict(a=5, b=6))
|
||||
zm.update_service_capabilities("svc2", "host2", dict(a=7, b=8))
|
||||
time_future = utils.utcnow() + datetime.timedelta(seconds=expiry_time)
|
||||
utils.set_time_override(time_future)
|
||||
caps = zm.get_zone_capabilities(None)
|
||||
self.assertEquals(caps, {})
|
||||
|
||||
|
Reference in New Issue
Block a user