def test_remove_node(self):
        cluster_config = {
            '1': {'zone': 'a'},
            '2': {'zone': 'b'},
            '3': {'zone': 'a'},
            '4': {'zone': 'b'},
            '5': {'zone': 'c'},
            '6': {'zone': 'c'},
        }
        cluster = Cluster(cluster_config)

        cluster.remove_node('4', node_zone='b')
        self.assertEqual(5, len(cluster.nodes))
        self.assertEqual(['a', 'b', 'c'], cluster.zones)
        self.assertEqual(3, len(cluster.zone_members))
        self.assertEqual(['1', '3'], sorted(cluster.zone_members['a']))
        self.assertEqual(['2'], sorted(cluster.zone_members['b']))
        self.assertEqual(['5', '6'], sorted(cluster.zone_members['c']))
        self.assertEqual(3, len(cluster.rings))

        cluster.remove_node('2', node_zone='b')
        self.assertEqual(4, len(cluster.nodes))
        self.assertEqual(['a', 'c'], cluster.zones)
        self.assertEqual(2, len(cluster.zone_members))
        self.assertEqual(['1', '3'], sorted(cluster.zone_members['a']))
        self.assertEqual([], sorted(cluster.zone_members['b']))
        self.assertEqual(['5', '6'], sorted(cluster.zone_members['c']))
        self.assertEqual(2, len(cluster.rings))
    def test_remove_zone(self):
        cluster_config = {
            '1': {'name': 'node1', 'zone': 'a'},
            '2': {'name': 'node2', 'zone': 'a'},
            '3': {'name': 'node3', 'zone': 'b'},
            '4': {'name': 'node4', 'zone': 'b'},
            '5': {'name': 'node5', 'zone': 'c'},
            '6': {'name': 'node6', 'zone': 'c'},
            '7': {'name': 'node7', 'zone': 'a'},
            '8': {'name': 'node8', 'zone': 'a'},
            '9': {'name': 'node9', 'zone': 'b'},
            '10': {'name': 'node10', 'zone': 'b'},
            '11': {'name': 'node11', 'zone': 'c'},
            '12': {'name': 'node12', 'zone': 'c'},
        }
        cluster = Cluster(cluster_config)

        placements = {}
        for i in cluster.nodes:
            placements[i] = []
        for i in range(10000):
            nodes = cluster.find_nodes(str(i))
            for node in nodes:
                placements[node].append(i)

        cluster.remove_node('5', node_name='node5', node_zone='c')
        cluster.remove_node('6', node_name='node6', node_zone='c')
        cluster.remove_node('11', node_name='node11', node_zone='c')
        cluster.remove_node('12', node_name='node12', node_zone='c')
        self.assertRaises(ValueError, cluster.remove_node, '12', 'node12', 'c')

        new_placements = {}
        for i in cluster.nodes:
            new_placements[i] = []
        for i in range(10000):
            nodes = cluster.find_nodes(str(i))
            for node in nodes:
                new_placements[node].append(i)

        keys = [k for sublist in placements.values() for k in sublist]
        new_keys = [k for sublist in new_placements.values() for k in sublist]
        self.assertEqual(sorted(keys), sorted(new_keys))

        added = 0
        removed = 0
        for node, assignments in placements.items():
            after = set(assignments)
            before = set(new_placements.get(node, []))
            removed += len(before.difference(after))
            added += len(after.difference(before))

        self.assertEqual(added, removed)
        self.assertEqual(13332, (added + removed))