@@ -154,9 +154,15 @@ def test_can_unit_perform_backup(
154
154
@patch ("charm.Patroni.reload_patroni_configuration" )
155
155
@patch ("charm.Patroni.member_started" , new_callable = PropertyMock )
156
156
@patch ("charm.PostgresqlOperatorCharm.update_config" )
157
+ @patch ("charm.Patroni.get_postgresql_version" , return_value = "14.10" )
157
158
@patch ("charm.PostgreSQLBackups._execute_command" )
158
159
def test_can_use_s3_repository (
159
- self , _execute_command , _update_config , _member_started , _reload_patroni_configuration
160
+ self ,
161
+ _execute_command ,
162
+ _get_postgresql_version ,
163
+ _update_config ,
164
+ _member_started ,
165
+ _reload_patroni_configuration ,
160
166
):
161
167
# Define the stanza name inside the unit relation data.
162
168
with self .harness .hooks_disabled ():
@@ -178,13 +184,13 @@ def test_can_use_s3_repository(
178
184
(False , FAILED_TO_INITIALIZE_STANZA_ERROR_MESSAGE ),
179
185
)
180
186
181
- # Test when the unit is a replica and there is a backup from another cluster
182
- # in the S3 repository.
183
- _execute_command .return_value = (
187
+ # Test when the unit is a replica.
188
+ pgbackrest_info_same_cluster_backup_output = (
184
189
0 ,
185
- f'[{{"name ": "another-model. { self .charm .cluster_name } "}}]' ,
190
+ f'[{{"db ": [{{"system-id": "12345"}}], "name": " { self .charm .backup . stanza_name } "}}]' ,
186
191
"" ,
187
192
)
193
+ _execute_command .return_value = pgbackrest_info_same_cluster_backup_output
188
194
self .assertEqual (
189
195
self .charm .backup .can_use_s3_repository (),
190
196
(True , None ),
@@ -196,10 +202,80 @@ def test_can_use_s3_repository(
196
202
{"stanza" : self .charm .backup .stanza_name },
197
203
)
198
204
199
- # Test when the unit is the leader and the workload is running.
205
+ # Test when the unit is the leader and the workload is running,
206
+ # but an exception happens when retrieving the cluster system id.
200
207
_member_started .return_value = True
208
+ _execute_command .side_effect = [
209
+ pgbackrest_info_same_cluster_backup_output ,
210
+ (1 , "" , "fake error" ),
211
+ ]
201
212
with self .harness .hooks_disabled ():
202
213
self .harness .set_leader ()
214
+ with self .assertRaises (Exception ):
215
+ self .assertEqual (
216
+ self .charm .backup .can_use_s3_repository (),
217
+ (False , ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE ),
218
+ )
219
+ _update_config .assert_not_called ()
220
+ _member_started .assert_not_called ()
221
+ _reload_patroni_configuration .assert_not_called ()
222
+
223
+ # Test when the cluster system id can be retrieved, but it's different from the stanza system id.
224
+ pgbackrest_info_other_cluster_system_id_backup_output = (
225
+ 0 ,
226
+ f'[{{"db": [{{"system-id": "12345"}}], "name": "{ self .charm .backup .stanza_name } "}}]' ,
227
+ "" ,
228
+ )
229
+ other_instance_system_identifier_output = (
230
+ 0 ,
231
+ "Database system identifier: 67890" ,
232
+ "" ,
233
+ )
234
+ _execute_command .side_effect = [
235
+ pgbackrest_info_other_cluster_system_id_backup_output ,
236
+ other_instance_system_identifier_output ,
237
+ ]
238
+ with self .harness .hooks_disabled ():
239
+ self .harness .update_relation_data (
240
+ self .peer_rel_id ,
241
+ self .charm .app .name ,
242
+ {"stanza" : self .charm .backup .stanza_name },
243
+ )
244
+ self .assertEqual (
245
+ self .charm .backup .can_use_s3_repository (),
246
+ (False , ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE ),
247
+ )
248
+ _update_config .assert_called_once ()
249
+ _member_started .assert_called_once ()
250
+ _reload_patroni_configuration .assert_called_once ()
251
+
252
+ # Assert that the stanza name is not present in the unit relation data anymore.
253
+ self .assertEqual (self .harness .get_relation_data (self .peer_rel_id , self .charm .app ), {})
254
+
255
+ # Test when the cluster system id can be retrieved, but it's different from the stanza system id.
256
+ _update_config .reset_mock ()
257
+ _member_started .reset_mock ()
258
+ _reload_patroni_configuration .reset_mock ()
259
+ pgbackrest_info_other_cluster_name_backup_output = (
260
+ 0 ,
261
+ f'[{{"db": [{{"system-id": "12345"}}], "name": "another-model.{ self .charm .cluster_name } "}}]' ,
262
+ "" ,
263
+ )
264
+ same_instance_system_identifier_output = (
265
+ 0 ,
266
+ "Database system identifier: 12345" ,
267
+ "" ,
268
+ )
269
+ _execute_command .side_effect = [
270
+ pgbackrest_info_other_cluster_name_backup_output ,
271
+ same_instance_system_identifier_output ,
272
+ ]
273
+ with self .harness .hooks_disabled ():
274
+ self .harness .update_relation_data (
275
+ self .peer_rel_id ,
276
+ self .charm .app .name ,
277
+ {"stanza" : self .charm .backup .stanza_name },
278
+ )
203
279
self .assertEqual (
204
280
self .charm .backup .can_use_s3_repository (),
205
281
(False , ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE ),
@@ -222,6 +298,10 @@ def test_can_use_s3_repository(
222
298
self .charm .app .name ,
223
299
{"stanza" : self .charm .backup .stanza_name },
224
300
)
301
+ _execute_command .side_effect = [
302
+ pgbackrest_info_same_cluster_backup_output ,
303
+ other_instance_system_identifier_output ,
304
+ ]
225
305
self .assertEqual (
226
306
self .charm .backup .can_use_s3_repository (),
227
307
(False , ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE ),
@@ -240,7 +320,10 @@ def test_can_use_s3_repository(
240
320
self .charm .app .name ,
241
321
{"stanza" : self .charm .backup .stanza_name },
242
322
)
243
- _execute_command .return_value = (0 , f'[{{"name": "{ self .charm .backup .stanza_name } "}}]' , "" )
323
+ _execute_command .side_effect = [
324
+ pgbackrest_info_same_cluster_backup_output ,
325
+ same_instance_system_identifier_output ,
326
+ ]
244
327
self .assertEqual (
245
328
self .charm .backup .can_use_s3_repository (),
246
329
(True , None ),
0 commit comments