@@ -602,49 +602,98 @@ def test_execute_command(harness):
602
602
603
603
604
604
def test_format_backup_list (harness ):
605
- # Test when there are no backups.
606
- tc .assertEqual (
607
- harness .charm .backup ._format_backup_list ([]),
608
- """backup-id | backup-type | backup-status
609
- ----------------------------------------------------""" ,
610
- )
605
+ with patch (
606
+ "charms.data_platform_libs.v0.s3.S3Requirer.get_s3_connection_info"
607
+ ) as _get_s3_connection_info :
608
+ # Test when there are no backups.
609
+ _get_s3_connection_info .return_value = {
610
+ "bucket" : " /test-bucket/ " ,
611
+ "access-key" : " test-access-key " ,
612
+ "secret-key" : " test-secret-key " ,
613
+ "path" : " test-path/ " ,
614
+ }
615
+ assert (
616
+ harness .charm .backup ._format_backup_list ([])
617
+ == """Storage bucket name: test-bucket
618
+ Backups base path: /test-path/backup/
611
619
612
- # Test when there are backups.
613
- backup_list = [
614
- ("2023-01-01T09:00:00Z" , "full" , "failed: fake error" ),
615
- ("2023-01-01T10:00:00Z" , "full" , "finished" ),
616
- ]
617
- tc .assertEqual (
618
- harness .charm .backup ._format_backup_list (backup_list ),
619
- """backup-id | backup-type | backup-status
620
- ----------------------------------------------------
621
- 2023-01-01T09:00:00Z | full | failed: fake error
622
- 2023-01-01T10:00:00Z | full | finished""" ,
623
- )
620
+ backup-id | type | status | reference-backup-id | LSN start/stop | start-time | finish-time | backup-path
621
+ -----------------------------------------------------------------------------------------------------------------------------------------------------------"""
622
+ )
623
+
624
+ # Test when there are backups.
625
+ backup_list = [
626
+ (
627
+ "2023-01-01T09:00:00Z" ,
628
+ "full" ,
629
+ "failed: fake error" ,
630
+ "None" ,
631
+ "0/3000000 / 0/5000000" ,
632
+ "2023-01-01T09:00:00Z" ,
633
+ "2023-01-01T09:00:05Z" ,
634
+ "a/b/c" ,
635
+ ),
636
+ (
637
+ "2023-01-01T10:00:00Z" ,
638
+ "full" ,
639
+ "finished" ,
640
+ "None" ,
641
+ "0/5000000 / 0/7000000" ,
642
+ "2023-01-01T10:00:00Z" ,
643
+ "2023-01-01T010:00:07Z" ,
644
+ "a/b/d" ,
645
+ ),
646
+ ]
647
+ assert (
648
+ harness .charm .backup ._format_backup_list (backup_list )
649
+ == """Storage bucket name: test-bucket
650
+ Backups base path: /test-path/backup/
651
+
652
+ backup-id | type | status | reference-backup-id | LSN start/stop | start-time | finish-time | backup-path
653
+ -----------------------------------------------------------------------------------------------------------------------------------------------------------
654
+ 2023-01-01T09:00:00Z | full | failed: fake error | None | 0/3000000 / 0/5000000 | 2023-01-01T09:00:00Z | 2023-01-01T09:00:05Z | a/b/c
655
+ 2023-01-01T10:00:00Z | full | finished | None | 0/5000000 / 0/7000000 | 2023-01-01T10:00:00Z | 2023-01-01T010:00:07Z | a/b/d"""
656
+ )
624
657
625
658
626
659
def test_generate_backup_list_output (harness ):
627
- with patch ("charm.PostgreSQLBackups._execute_command" ) as _execute_command :
660
+ with (
661
+ patch (
662
+ "charms.data_platform_libs.v0.s3.S3Requirer.get_s3_connection_info"
663
+ ) as _get_s3_connection_info ,
664
+ patch ("charm.PostgreSQLBackups._execute_command" ) as _execute_command ,
665
+ ):
666
+ _get_s3_connection_info .return_value = {
667
+ "bucket" : " /test-bucket/ " ,
668
+ "access-key" : " test-access-key " ,
669
+ "secret-key" : " test-secret-key " ,
670
+ "path" : " test-path/ " ,
671
+ }
628
672
# Test when no backups are returned.
629
673
_execute_command .return_value = (0 , '[{"backup":[]}]' , "" )
630
- tc .assertEqual (
631
- harness .charm .backup ._generate_backup_list_output (),
632
- """backup-id | backup-type | backup-status
633
- ----------------------------------------------------""" ,
674
+ assert (
675
+ harness .charm .backup ._generate_backup_list_output ()
676
+ == """Storage bucket name: test-bucket
677
+ Backups base path: /test-path/backup/
678
+
679
+ backup-id | type | status | reference-backup-id | LSN start/stop | start-time | finish-time | backup-path
680
+ -----------------------------------------------------------------------------------------------------------------------------------------------------------"""
634
681
)
635
682
636
683
# Test when backups are returned.
637
684
_execute_command .return_value = (
638
685
0 ,
639
- '[{"backup":[{"label":"20230101-090000F","error":"fake error"},{"label ":"20230101-100000F ","error":null }]}]' ,
686
+ '[{"backup":[{"label":"20230101-090000F","error":"fake error","reference":null,"lsn":{"start ":"0/3000000 ","stop":"0/5000000"},"timestamp":{"start":1719866711,"stop":1719866714} }]}]' ,
640
687
"" ,
641
688
)
642
- tc .assertEqual (
643
- harness .charm .backup ._generate_backup_list_output (),
644
- """backup-id | backup-type | backup-status
645
- ----------------------------------------------------
646
- 2023-01-01T09:00:00Z | full | failed: fake error
647
- 2023-01-01T10:00:00Z | full | finished""" ,
689
+ assert (
690
+ harness .charm .backup ._generate_backup_list_output ()
691
+ == """Storage bucket name: test-bucket
692
+ Backups base path: /test-path/backup/
693
+
694
+ backup-id | type | status | reference-backup-id | LSN start/stop | start-time | finish-time | backup-path
695
+ -----------------------------------------------------------------------------------------------------------------------------------------------------------
696
+ 2023-01-01T09:00:00Z | full | failed: fake error | None | 0/3000000 / 0/5000000 | 2024-07-01T20:45:11Z | 2024-07-01T20:45:14Z | /None.postgresql/20230101-090000F"""
648
697
)
649
698
650
699
@@ -1230,8 +1279,18 @@ def test_on_create_backup_action(harness):
1230
1279
mock_event .fail .assert_called_once ()
1231
1280
mock_event .set_results .assert_not_called ()
1232
1281
1282
+ # Test when the backup is of type diff/incr when there's no previous full backup.
1283
+ mock_event .reset_mock ()
1284
+ mock_event .params = {"type" : "differential" }
1285
+ _upload_content_to_s3 .return_value = True
1286
+ _is_primary .return_value = True
1287
+ harness .charm .backup ._on_create_backup_action (mock_event )
1288
+ mock_event .fail .assert_called_once ()
1289
+ mock_event .set_results .assert_not_called ()
1290
+
1233
1291
# Test when the backup fails.
1234
1292
mock_event .reset_mock ()
1293
+ mock_event .params = {"type" : "full" }
1235
1294
_upload_content_to_s3 .return_value = True
1236
1295
_is_primary .return_value = True
1237
1296
_execute_command .return_value = (1 , "" , "fake error" )
0 commit comments