Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pg-backup-api so it can handle Barman <= 3.9 #91

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions pg_backup_api/pg_backup_api/logic/utility_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,24 @@ def diagnose() -> 'Response':
else:
server_dict[server] = Server(conf)

available_models = barman.__config__.model_names()
model_dict = {}
for model in available_models: # pyright: ignore
model_dict[model] = barman.__config__.get_model(model)

# errors list with duplicate paths between servers
errors_list = barman.__config__.servers_msg_list

barman_diagnose.exec_diagnose(server_dict,
model_dict,
errors_list,
show_config_source=False)
try:
available_models = barman.__config__.model_names()
model_dict = {}
for model in available_models: # pyright: ignore
model_dict[model] = barman.__config__.get_model(model)

barman_diagnose.exec_diagnose(server_dict,
model_dict,
errors_list,
show_config_source=False)
# An attribute error is thown when calling `model_names()` if using Barman
# older than 3.10, in which case models are not yet implemented, so we fall
# back to the old signature of diagnose command.
except AttributeError:
barman_diagnose.exec_diagnose(server_dict, errors_list)

# new outputs are appended, so grab the last one
stored_output = json.loads(output._writer.json_output["_INFO"][-1])
Expand Down
23 changes: 23 additions & 0 deletions pg_backup_api/pg_backup_api/tests/test_utility_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ def test_diagnose_ok(self, client):
assert response.status_code == 200
assert response.data == b'{"global":{"config":{}}}\n'

@patch("pg_backup_api.logic.utility_controller.barman_diagnose", Mock())
@patch.dict(
"pg_backup_api.logic.utility_controller.output._writer.json_output",
{
'_INFO': ['SOME', 'JSON', 'ENTRIES', '{"global":{"config":{}}}'],
},
)
def test_diagnose_ok_old_barman(self, client):
"""Test ``/diagnose`` endpoint.

Ensure a ``GET`` request returns ``200`` and the expected JSON output,
even if using Barman 3.9 or older, in which case models implementation
is not available.
"""
path = "/diagnose"

with patch("barman.__config__") as mock_config:
mock_config.model_names.side_effect = AttributeError("Old Barman")
response = client.get(path)

assert response.status_code == 200
assert response.data == b'{"global":{"config":{}}}\n'

def test_diagnose_not_allowed(self, client):
"""Test ``/diagnose`` endpoint.

Expand Down
Loading