diff --git a/patroni/api.py b/patroni/api.py index 0702cff9..cd230456 100644 --- a/patroni/api.py +++ b/patroni/api.py @@ -1038,6 +1038,9 @@ class RestApiHandler(BaseHTTPRequestHandler): logger.warning('received failover request with leader specifed - performing switchover') action = 'switchover' + if not data and leader == candidate: + data = 'Switchover target and source are the same' + if not data and not scheduled_at: data = self.is_failover_possible(cluster, leader, candidate, action) if data: diff --git a/tests/test_api.py b/tests/test_api.py index bfad88df..981d5457 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -526,7 +526,6 @@ class TestRestApiHandler(unittest.TestCase): response_mock.assert_called_with( 400, 'Switchover is possible only to a specific candidate in a paused state') - # Switchover without a candidate specified # No healthy nodes to promote in both sync and async mode for is_synchronous_mode, response in ( (True, 'switchover is not possible: can not find sync_standby'), @@ -538,6 +537,12 @@ class TestRestApiHandler(unittest.TestCase): # [Switchover to the candidate specified] + # Candidate to promote is the same as the leader specified + with patch.object(RestApiHandler, 'write_response') as response_mock: + request = post + '53\n\n{"leader": "postgresql2", "candidate": "postgresql2"}' + MockRestApiServer(RestApiHandler, request) + response_mock.assert_called_with(400, 'Switchover target and source are the same') + # Current leader is different from the one specified with patch.object(RestApiHandler, 'write_response') as response_mock: cluster.leader.name = 'postgresql2'