From 8de904e556f044605d39cbd3eb9021047daf2eb4 Mon Sep 17 00:00:00 2001 From: Alexander Kukushkin Date: Mon, 10 Feb 2025 11:04:58 +0100 Subject: [PATCH] Improve replication_state=streaming check in behave (#3269) it was somewhat flaky --- features/standby_cluster.feature | 12 +++--------- features/steps/patroni_api.py | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/features/standby_cluster.feature b/features/standby_cluster.feature index 160ddd48..c4c2aa6b 100644 --- a/features/standby_cluster.feature +++ b/features/standby_cluster.feature @@ -13,9 +13,7 @@ Feature: standby cluster When I start postgres-0 Then "members/postgres-0" key in DCS has state=running after 10 seconds And replication works from postgres-1 to postgres-0 after 15 seconds - When I issue a GET request to http://127.0.0.1:8008/patroni - Then I receive a response code 200 - And I receive a response replication_state streaming + And Response on GET http://127.0.0.1:8008/patroni contains replication_state=streaming after 10 seconds And "members/postgres-0" key in DCS has replication_state=streaming after 10 seconds @slot-advance @@ -35,9 +33,7 @@ Feature: standby cluster Then postgres-1 is a leader of batman1 after 10 seconds When I add the table foo to postgres-0 Then table foo is present on postgres-1 after 20 seconds - When I issue a GET request to http://127.0.0.1:8009/patroni - Then I receive a response code 200 - And I receive a response replication_state streaming + And Response on GET http://127.0.0.1:8009/patroni contains replication_state=streaming after 10 seconds And I sleep for 3 seconds When I issue a GET request to http://127.0.0.1:8009/primary Then I receive a response code 503 @@ -49,9 +45,7 @@ Feature: standby cluster Then postgres-2 role is the replica after 24 seconds And postgres-2 is replicating from postgres-1 after 10 seconds And table foo is present on postgres-2 after 20 seconds - When I issue a GET request to http://127.0.0.1:8010/patroni - Then I receive a response code 200 - And I receive a response replication_state streaming + And Response on GET http://127.0.0.1:8010/patroni contains replication_state=streaming after 10 seconds And postgres-1 does not have a replication slot named test_logical Scenario: check switchover diff --git a/features/steps/patroni_api.py b/features/steps/patroni_api.py index 3606b09b..240e79aa 100644 --- a/features/steps/patroni_api.py +++ b/features/steps/patroni_api.py @@ -160,9 +160,24 @@ def check_http_response(context, url, value, timeout, negate=False): if context.certfile: url = url.replace('http://', 'https://') timeout *= context.timeout_multiplier + if '=' in value: + key, val = value.split('=', 1) + else: + key, val = value, None for _ in range(int(timeout)): r = context.request_executor.request('GET', url) - if (value in r.data.decode('utf-8')) != negate: + data = r.data.decode('utf-8') + if val is not None: + try: + data = json.loads(data) + if negate: + if key not in data or data[key] != val: + break + elif key in data and data[key] == val: + break + except Exception: + pass + elif (value in r.data.decode('utf-8')) != negate: break time.sleep(1) else: