@@ -31,6 +31,8 @@ def setUp(self):
3131 "app_name" : self .harness .model .app .name ,
3232 }
3333
34+ self .rel_id = self .harness .add_relation (self ._peer_relation , self .charm .app .name )
35+
3436 @patch_network_get (private_address = "1.1.1.1" )
3537 @patch ("charm.Patroni.render_postgresql_conf_file" )
3638 def test_on_install (
@@ -46,7 +48,6 @@ def test_on_install(
4648 @patch ("charm.PostgresqlOperatorCharm._create_resources" )
4749 def test_on_leader_elected (self , _ , __ , _render_postgresql_conf_file , ___ ):
4850 # Assert that there is no password in the peer relation.
49- self .harness .add_relation (self ._peer_relation , self .charm .app .name )
5051 self .assertIsNone (self .charm ._peers .data [self .charm .app ].get ("postgres-password" , None ))
5152 self .assertIsNone (self .charm ._peers .data [self .charm .app ].get ("replication-password" , None ))
5253
@@ -86,7 +87,6 @@ def test_on_postgresql_pebble_ready(
8687 # Check that the initial plan is empty.
8788 plan = self .harness .get_container_pebble_plan (self ._postgresql_container )
8889 self .assertEqual (plan .to_dict (), {})
89- self .harness .add_relation (self ._peer_relation , self .charm .app .name )
9090
9191 # Get the current and the expected layer from the pebble plan and the _postgresql_layer
9292 # method, respectively.
@@ -202,7 +202,6 @@ def test_patch_pod_labels(self, _client):
202202 @patch ("charm.PostgresqlOperatorCharm._create_resources" )
203203 def test_postgresql_layer (self , _ , __ , ___ , ____ ):
204204 # Test with the already generated password.
205- self .harness .add_relation (self ._peer_relation , self .charm .app .name )
206205 self .harness .set_leader ()
207206 plan = self .charm ._postgresql_layer ().to_dict ()
208207 expected = {
@@ -238,11 +237,52 @@ def test_postgresql_layer(self, _, __, ___, ____):
238237 @patch ("charm.PostgresqlOperatorCharm._create_resources" )
239238 def test_get_operator_password (self , _ , __ , ___ , ____ ):
240239 # Test for a None password.
241- self .harness .add_relation (self ._peer_relation , self .charm .app .name )
242240 self .assertIsNone (self .charm ._get_operator_password ())
243241
244242 # Then test for a non empty password after leader election and peer data set.
245243 self .harness .set_leader ()
246244 password = self .charm ._get_operator_password ()
247245 self .assertIsNotNone (password )
248246 self .assertNotEqual (password , "" )
247+
248+ @patch ("charm.Patroni.reload_patroni_configuration" )
249+ @patch ("charm.Patroni.render_postgresql_conf_file" )
250+ @patch ("charm.PostgresqlOperatorCharm._create_resources" )
251+ def test_get_secret (self , _ , __ , ___ ):
252+ self .harness .set_leader ()
253+
254+ # Test application scope.
255+ assert self .charm ._get_secret ("app" , "password" ) is None
256+ self .harness .update_relation_data (
257+ self .rel_id , self .charm .app .name , {"password" : "test-password" }
258+ )
259+ assert self .charm ._get_secret ("app" , "password" ) == "test-password"
260+
261+ # Test unit scope.
262+ assert self .charm ._get_secret ("unit" , "password" ) is None
263+ self .harness .update_relation_data (
264+ self .rel_id , self .charm .unit .name , {"password" : "test-password" }
265+ )
266+ assert self .charm ._get_secret ("unit" , "password" ) == "test-password"
267+
268+ @patch ("charm.Patroni.reload_patroni_configuration" )
269+ @patch ("charm.Patroni.render_postgresql_conf_file" )
270+ @patch ("charm.PostgresqlOperatorCharm._create_resources" )
271+ def test_set_secret (self , _ , __ , ___ ):
272+ self .harness .set_leader ()
273+
274+ # Test application scope.
275+ assert "password" not in self .harness .get_relation_data (self .rel_id , self .charm .app .name )
276+ self .charm ._set_secret ("app" , "password" , "test-password" )
277+ assert (
278+ self .harness .get_relation_data (self .rel_id , self .charm .app .name )["password" ]
279+ == "test-password"
280+ )
281+
282+ # Test unit scope.
283+ assert "password" not in self .harness .get_relation_data (self .rel_id , self .charm .unit .name )
284+ self .charm ._set_secret ("unit" , "password" , "test-password" )
285+ assert (
286+ self .harness .get_relation_data (self .rel_id , self .charm .unit .name )["password" ]
287+ == "test-password"
288+ )
0 commit comments