@@ -73,6 +73,39 @@ def test_are_backup_settings_ok(harness):
73
73
)
74
74
75
75
76
+ def test_can_initialise_stanza (harness ):
77
+ with patch ("charm.Patroni.member_started" , new_callable = PropertyMock ) as _member_started :
78
+ # Test when Patroni or PostgreSQL hasn't started yet
79
+ # and the unit hasn't joined the peer relation yet.
80
+ _member_started .return_value = False
81
+ tc .assertEqual (
82
+ harness .charm .backup ._can_initialise_stanza ,
83
+ False ,
84
+ )
85
+
86
+ # Test when the unit hasn't configured TLS yet while other unit already has TLS enabled.
87
+ harness .add_relation_unit (
88
+ harness .model .get_relation (PEER ).id , f"{ harness .charm .app .name } /1"
89
+ )
90
+ with harness .hooks_disabled ():
91
+ harness .update_relation_data (
92
+ harness .model .get_relation (PEER ).id ,
93
+ f"{ harness .charm .app .name } /1" ,
94
+ {"tls" : "enabled" },
95
+ )
96
+ tc .assertEqual (
97
+ harness .charm .backup ._can_initialise_stanza ,
98
+ False ,
99
+ )
100
+
101
+ # Test when everything is ok to initialise the stanza.
102
+ _member_started .return_value = True
103
+ tc .assertEqual (
104
+ harness .charm .backup ._can_initialise_stanza ,
105
+ True ,
106
+ )
107
+
108
+
76
109
@patch_network_get (private_address = "1.1.1.1" )
77
110
def test_can_unit_perform_backup (harness ):
78
111
with (
@@ -926,6 +959,9 @@ def test_on_s3_credential_changed(harness):
926
959
patch (
927
960
"charm.PostgresqlOperatorCharm.is_primary" , new_callable = PropertyMock
928
961
) as _is_primary ,
962
+ patch (
963
+ "charm.PostgreSQLBackups._can_initialise_stanza" , new_callable = PropertyMock
964
+ ) as _can_initialise_stanza ,
929
965
patch (
930
966
"charm.PostgreSQLBackups._render_pgbackrest_conf_file"
931
967
) as _render_pgbackrest_conf_file ,
@@ -953,31 +989,42 @@ def test_on_s3_credential_changed(harness):
953
989
{"cluster_initialised" : "True" },
954
990
)
955
991
_render_pgbackrest_conf_file .return_value = False
956
- _is_primary .return_value = False
957
992
harness .charm .backup .s3_client .on .credentials_changed .emit (
958
993
relation = harness .model .get_relation (S3_PARAMETERS_RELATION , s3_rel_id )
959
994
)
960
995
_defer .assert_not_called ()
961
996
_render_pgbackrest_conf_file .assert_called_once ()
997
+ _can_initialise_stanza .assert_not_called ()
962
998
_create_bucket_if_not_exists .assert_not_called ()
963
999
_can_use_s3_repository .assert_not_called ()
964
1000
_initialise_stanza .assert_not_called ()
965
1001
1002
+ # Test when it's not possible to initialise the stanza in this unit.
1003
+ _render_pgbackrest_conf_file .return_value = True
1004
+ _can_initialise_stanza .return_value = False
1005
+ harness .charm .backup .s3_client .on .credentials_changed .emit (
1006
+ relation = harness .model .get_relation (S3_PARAMETERS_RELATION , s3_rel_id )
1007
+ )
1008
+ _defer .assert_called_once ()
1009
+ _can_initialise_stanza .assert_called_once ()
1010
+ _is_primary .assert_not_called ()
1011
+
966
1012
# Test that followers will not initialise the bucket
967
1013
harness .charm .unit .status = ActiveStatus ()
968
1014
_render_pgbackrest_conf_file .reset_mock ()
1015
+ _can_initialise_stanza .return_value = True
1016
+ _is_primary .return_value = False
969
1017
with harness .hooks_disabled ():
970
1018
harness .update_relation_data (
971
1019
peer_rel_id ,
972
1020
harness .charm .app .name ,
973
1021
{"cluster_initialised" : "True" },
974
1022
)
975
- _render_pgbackrest_conf_file .return_value = True
976
-
977
1023
harness .charm .backup .s3_client .on .credentials_changed .emit (
978
1024
relation = harness .model .get_relation (S3_PARAMETERS_RELATION , s3_rel_id )
979
1025
)
980
1026
_render_pgbackrest_conf_file .assert_called_once ()
1027
+ _is_primary .assert_called_once ()
981
1028
_create_bucket_if_not_exists .assert_not_called ()
982
1029
tc .assertIsInstance (harness .charm .unit .status , ActiveStatus )
983
1030
_can_use_s3_repository .assert_not_called ()
0 commit comments