Skip to content

Commit

Permalink
slight fix to update_node_pool, unit tests for update_node_pool
Browse files Browse the repository at this point in the history
  • Loading branch information
James McGuinness committed Aug 2, 2013
1 parent 01457ce commit b9e88c3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
48 changes: 48 additions & 0 deletions tests/core/serviceinstance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,54 @@ def test_get_by_number(self):
instance = self.collection.get_by_number(3)
assert_equal(instance, instances[3])

def test_update_node_pool_same_pool(self):
with mock.patch('tron.node.NodePoolRepository', autospec=True) as pool_patch:
get_mock = pool_patch.get_instance().get_by_name
get_mock.configure_mock(return_value=self.collection.node_pool)
self.collection.update_node_pool()
assert_equal(pool_patch.get_instance.call_count, 2)
get_mock.assert_called_once_with(self.collection.config.node)

def test_update_node_pool_diff_pool_same_nodes(self):
new_instances = [mock.Mock(), mock.Mock()]
self.collection.instances = new_instances
nodes = [instance.node for instance in new_instances]
node_pool = mock.Mock(get_by_name=mock.Mock(side_effect=iter(nodes)))

with mock.patch('tron.node.NodePoolRepository', autospec=True) as pool_patch:
get_mock = pool_patch.get_instance().get_by_name
get_mock.configure_mock(return_value=node_pool)

self.collection.update_node_pool()

assert_equal(pool_patch.get_instance.call_count, 2)
get_mock.assert_called_once_with(self.collection.config.node)
assert_equal(self.collection.node_pool, node_pool)
calls = [mock.call(instance.node.name) for instance in new_instances]
node_pool.get_by_name.assert_calls(calls)
assert not any([instance.stop.called for instance in new_instances])
assert_equal(self.collection.instances, new_instances)

def test_update_node_pool_diff_everything(self):
new_instances = [mock.Mock(), mock.Mock()]
self.collection.instances = [mock.Mock(), mock.Mock()]
nodes = [instance.node for instance in new_instances]
node_pool = mock.Mock(get_by_name=mock.Mock(side_effect=iter(nodes)))

with mock.patch('tron.node.NodePoolRepository', autospec=True) as pool_patch:
get_mock = pool_patch.get_instance().get_by_name
get_mock.configure_mock(return_value=node_pool)

self.collection.update_node_pool()

assert_equal(pool_patch.get_instance.call_count, 2)
get_mock.assert_called_once_with(self.collection.config.node)
assert_equal(self.collection.node_pool, node_pool)
calls = [mock.call(instance.node.name) for instance in self.collection.instances]
node_pool.get_by_name.assert_calls(calls)
assert all([instance.stop.called for instance in self.collection.instances])
assert_equal(self.collection.instances, [])


if __name__ == "__main__":
run()
4 changes: 2 additions & 2 deletions tron/core/serviceinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ def update_node_pool(self):
needs_new_node = []

for instance in self.instances:
old_node = self.node_pool.get_by_name(instance.node)
if old_node != instance.node:
new_node = self.node_pool.get_by_name(instance.node.name)
if new_node != instance.node:
instance.stop()
needs_new_node.append(instance)

Expand Down

0 comments on commit b9e88c3

Please sign in to comment.