|
1 | 1 | import mock
|
2 | 2 | import redis
|
| 3 | +from mock import patch |
| 4 | + |
| 5 | +from test_base import TestCaseBase, MockNewNode |
| 6 | +from ruskit.cluster import Cluster, ClusterNode |
3 | 7 |
|
4 | 8 |
|
5 | 9 | class MockNode(object):
|
@@ -119,3 +123,86 @@ def test_distribute(monkeypatch):
|
119 | 123 |
|
120 | 124 | for s in manager.slaves:
|
121 | 125 | assert master_map[s.unassigned_master].host != s.host
|
| 126 | + |
| 127 | + |
| 128 | +def clear_slots(node): |
| 129 | + node._cached_node_info['slots'] = [] |
| 130 | + return mock.MagicMock() |
| 131 | + |
| 132 | + |
| 133 | +class TestCluster(TestCaseBase): |
| 134 | + @patch.object(Cluster, 'migrate_node', side_effect=clear_slots) |
| 135 | + def test_delete(self, migrate_node): |
| 136 | + cluster = self.cluster |
| 137 | + a = cluster.nodes[0] |
| 138 | + b = cluster.nodes[1] |
| 139 | + c = cluster.nodes[2] |
| 140 | + cluster.delete_node(a) |
| 141 | + migrate_node.assert_called_with(a) |
| 142 | + self.assertEqual(cluster.get_node(a.name), None) |
| 143 | + self.assert_exec_cmd(a, 'CLUSTER RESET') |
| 144 | + self.assert_exec_cmd(b, 'CLUSTER FORGET', a.name) |
| 145 | + self.assert_exec_cmd(c, 'CLUSTER FORGET', a.name) |
| 146 | + |
| 147 | + @patch.object(Cluster, 'migrate_slot') |
| 148 | + def test_fix_node_migrating(self, migrate_slot): |
| 149 | + cluster = self.cluster |
| 150 | + a = cluster.nodes[0] |
| 151 | + b = cluster.nodes[1] |
| 152 | + c = cluster.nodes[2] |
| 153 | + for n in cluster.nodes: |
| 154 | + n.node_info # gen node_info |
| 155 | + a._cached_node_info['migrating'] = { |
| 156 | + '233': b.name, |
| 157 | + '666': c.name, |
| 158 | + '99': 'name_not_in_cluster', |
| 159 | + } |
| 160 | + b._cached_node_info['importing'] = {'233': a.name} |
| 161 | + cluster.fix_node(a) |
| 162 | + self.assert_no_exec(a, 'CLUSTER SETSLOT', '233', 'STABLE') |
| 163 | + self.assert_no_exec(b, 'CLUSTER SETSLOT', '233', 'STABLE') |
| 164 | + self.assert_exec_cmd(a, 'CLUSTER SETSLOT', '666', 'STABLE') |
| 165 | + self.assert_exec_cmd(a, 'CLUSTER SETSLOT', '99', 'STABLE') |
| 166 | + migrate_slot.assert_called_with(a, b, '233') |
| 167 | + self.assert_not_called_with(a, c, '666') |
| 168 | + |
| 169 | + @patch.object(Cluster, 'migrate_slot') |
| 170 | + def test_fix_node_importing(self, migrate_slot): |
| 171 | + cluster = self.cluster |
| 172 | + a = cluster.nodes[0] |
| 173 | + b = cluster.nodes[1] |
| 174 | + c = cluster.nodes[2] |
| 175 | + for n in cluster.nodes: |
| 176 | + n.node_info # gen node_info |
| 177 | + a._cached_node_info['importing'] = { |
| 178 | + '233': b.name, |
| 179 | + '666': c.name, |
| 180 | + '99': 'name_not_in_cluster', |
| 181 | + } |
| 182 | + b._cached_node_info['migrating'] = {'233': a.name} |
| 183 | + cluster.fix_node(a) |
| 184 | + self.assert_no_exec(a, 'CLUSTER SETSLOT', '233', 'STABLE') |
| 185 | + self.assert_no_exec(b, 'CLUSTER SETSLOT', '233', 'STABLE') |
| 186 | + self.assert_exec_cmd(a, 'CLUSTER SETSLOT', '666', 'STABLE') |
| 187 | + self.assert_exec_cmd(a, 'CLUSTER SETSLOT', '99', 'STABLE') |
| 188 | + migrate_slot.assert_called_with(b, a, '233') |
| 189 | + self.assert_not_called_with(migrate_slot, c, a, '666') |
| 190 | + |
| 191 | + def test_fill_slots(self): |
| 192 | + cluster = self.cluster |
| 193 | + a = cluster.nodes[0] |
| 194 | + b = cluster.nodes[1] |
| 195 | + c = cluster.nodes[2] |
| 196 | + for n in cluster.nodes: |
| 197 | + n.node_info # gen node_info |
| 198 | + missing_slots = a._cached_node_info['slots'][-6:] |
| 199 | + a._cached_node_info['slots'] = a._cached_node_info['slots'][:-6] |
| 200 | + cluster.fill_slots() |
| 201 | + added_slots = [] |
| 202 | + for n in cluster.nodes: |
| 203 | + added_slots.extend( |
| 204 | + [list(args[1:]) for args, kwargs \ |
| 205 | + in n.r.execute_command.call_args_list \ |
| 206 | + if args[0] == 'CLUSTER ADDSLOTS']) |
| 207 | + added_slots = sum(added_slots, []) |
| 208 | + self.assertEqual(set(added_slots), set(missing_slots)) |
0 commit comments