Skip to content

Commit 113ef1c

Browse files
authored
Merge pull request #4 from dura0ok/PBCKP-2912
PBCKP-2912: add sanity check in set_archiving(instance should exists)
2 parents 1ab5e9f + 99e989c commit 113ef1c

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

pg_probackup2/app.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ def show(
571571
backup_list.append(backup)
572572

573573
if backup_id is not None:
574-
assert False, "Failed to find backup with ID: {0}".format(backup_id)
574+
raise RuntimeError("Failed to find backup with ID: {0}".format(backup_id))
575575

576576
return backup_list
577577
else:
@@ -629,7 +629,7 @@ def show(
629629
specific_record[name.strip()] = var
630630

631631
if not specific_record:
632-
assert False, "Failed to find backup with ID: {0}".format(backup_id)
632+
raise RuntimeError("Failed to find backup with ID: {0}".format(backup_id))
633633

634634
return specific_record
635635

@@ -778,6 +778,13 @@ def set_archiving(
778778
log_level=False, archive_timeout=False,
779779
custom_archive_command=None):
780780

781+
# check instance existing
782+
instances_json = self.show(instance=None, as_json=True, expect_error=False, as_text=True)
783+
instances_data = json.loads(instances_json)
784+
if not any(inst_data.get('instance') == instance for inst_data in instances_data):
785+
raise RuntimeError("Instance '{0}' does not exist. " \
786+
"Please add the instance first using add_instance() or init_pb_node().".format(instance))
787+
781788
# parse postgresql.auto.conf
782789
options = {}
783790
if replica:

pg_probackup2/tests/test_basic.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import pytest
66

77
import testgres
8-
from ...pg_probackup2.app import ProbackupApp
9-
from ...pg_probackup2.init_helpers import Init, init_params
10-
from ..storage.fs_backup import FSTestBackupDir
8+
from pg_probackup2.app import ProbackupApp
9+
from pg_probackup2.init_helpers import Init, init_params
10+
from pg_probackup2.storage.fs_backup import FSTestBackupDir
1111

1212

1313
class ProbackupTest:
@@ -102,3 +102,22 @@ def test_full_backup(self):
102102

103103
# Check if the backup is valid
104104
assert f"INFO: Backup {backup_id} is valid" in out
105+
106+
def test_set_archiving_nonexistent_instance(self):
107+
self.pb.init()
108+
109+
with pytest.raises(AssertionError) as exc_info:
110+
self.pb.set_archiving('nonexistent_instance', None)
111+
112+
assert "Instance 'nonexistent_instance' does not exist" in str(exc_info.value)
113+
114+
def test_set_archiving_existing_instance(self):
115+
node = self.pg_node.make_simple('node', pg_options={"fsync": "off", "synchronous_commit": "off"})
116+
117+
with node:
118+
self.pb.init()
119+
node.slow_start()
120+
self.pb.add_instance('node', node)
121+
self.pb.set_archiving('node', node)
122+
123+
assert "does not exist" not in str(self.pb.test_class.output or "")

0 commit comments

Comments
 (0)