leader verify ack token with request

This commit is contained in:
Edward Hope-Morley 2014-12-05 13:21:52 +00:00
parent 640a82dafc
commit 0decd8106e
2 changed files with 23 additions and 5 deletions

View File

@ -41,6 +41,7 @@ from charmhelpers.contrib.hahelpers.cluster import (
)
from charmhelpers.core.hookenv import (
config,
local_unit,
unit_get,
relation_set,
relation_ids,
@ -271,6 +272,13 @@ def all_peers_stopped(responses):
To be safe, default expectation is that api is still running.
"""
key = 'stop-proxy-service-ack'
token = relation_get(attribute='stop-proxy-service-rq',
unit=local_unit())
if not token or token != responses[0].get(key):
log("Unmatched token in ack (expected=%s, got=%s)" %
(token, responses[0].get(key)), level=DEBUG)
return False
if not all_responses_equal(responses, key):
return False

View File

@ -9,9 +9,13 @@ with mock.patch('charmhelpers.core.hookenv.log'):
class SwiftHooksTestCase(unittest.TestCase):
def test_all_peers_stopped(self):
@mock.patch("swift_hooks.relation_get")
@mock.patch("swift_hooks.local_unit")
def test_all_peers_stopped(self, mock_local_unit, mock_relation_get):
token1 = str(uuid.uuid4())
token2 = str(uuid.uuid4())
mock_relation_get.return_value = token1
responses = [{'some-other-key': token1}]
self.assertFalse(swift_hooks.all_peers_stopped(responses))
@ -19,11 +23,17 @@ class SwiftHooksTestCase(unittest.TestCase):
{'stop-proxy-service-ack': token2}]
self.assertFalse(swift_hooks.all_peers_stopped(responses))
responses = [{'stop-proxy-service-ack': token1},
{'stop-proxy-service-ack': token1}]
self.assertTrue(swift_hooks.all_peers_stopped(responses))
responses = [{'stop-proxy-service-ack': token1},
{'stop-proxy-service-ack': token1},
{'some-other-key': token1}]
self.assertFalse(swift_hooks.all_peers_stopped(responses))
responses = [{'stop-proxy-service-ack': token1},
{'stop-proxy-service-ack': token1}]
self.assertTrue(swift_hooks.all_peers_stopped(responses))
mock_relation_get.return_value = token2
responses = [{'stop-proxy-service-ack': token1},
{'stop-proxy-service-ack': token1}]
self.assertFalse(swift_hooks.all_peers_stopped(responses))