22# See LICENSE file for licensing details.
33
44import unittest
5- from unittest .mock import Mock , call , patch
5+ from unittest .mock import Mock , patch
66
77from lightkube import codecs
88from lightkube .resources .core_v1 import Pod
9- from ops .model import ActiveStatus , BlockedStatus
9+ from ops .model import ActiveStatus
1010from ops .testing import Harness
1111from tenacity import RetryError
1212
@@ -130,41 +130,19 @@ def test_fail_to_get_primary(self, _get_primary):
130130 _get_primary .assert_called_once ()
131131 mock_event .set_results .assert_not_called ()
132132
133- @patch ("ops.model.Container.restart" )
134- def test_restart_postgresql_service (self , _restart ):
135- self .charm ._restart_postgresql_service ()
136- _restart .assert_called_once_with (self ._postgresql_service )
137- self .assertEqual (
138- self .harness .model .unit .status ,
139- ActiveStatus (),
140- )
141-
142133 @patch_network_get (private_address = "1.1.1.1" )
143134 @patch ("charm.Patroni.get_primary" )
144- @patch ("charm.PostgresqlOperatorCharm._restart_postgresql_service" )
145- @patch ("charm.Patroni.change_master_start_timeout" )
146- @patch ("charm.Patroni.get_postgresql_state" )
147135 def test_on_update_status (
148136 self ,
149- _get_postgresql_state ,
150- _change_master_start_timeout ,
151- _restart_postgresql_service ,
152137 _get_primary ,
153138 ):
154- _get_postgresql_state .side_effect = ["running" , "running" , "restarting" , "stopping" ]
155139 _get_primary .side_effect = [
156140 "postgresql-k8s/1" ,
157141 self .charm .unit .name ,
158- self .charm .unit .name ,
159- self .charm .unit .name ,
160142 ]
161143
162- # Test running status.
163- self .charm .on .update_status .emit ()
164- _change_master_start_timeout .assert_not_called ()
165- _restart_postgresql_service .assert_not_called ()
166-
167144 # Check primary message not being set (current unit is not the primary).
145+ self .charm .on .update_status .emit ()
168146 _get_primary .assert_called_once ()
169147 self .assertNotEqual (
170148 self .harness .model .unit .status ,
@@ -178,41 +156,9 @@ def test_on_update_status(
178156 ActiveStatus ("Primary" ),
179157 )
180158
181- # Test restarting status.
182- self .charm .on .update_status .emit ()
183- _change_master_start_timeout .assert_not_called ()
184- _restart_postgresql_service .assert_called_once ()
185-
186- # Create a manager mock to check the correct order of the calls.
187- manager = Mock ()
188- manager .attach_mock (_change_master_start_timeout , "c" )
189- manager .attach_mock (_restart_postgresql_service , "r" )
190-
191- # Test stopping status.
192- _restart_postgresql_service .reset_mock ()
193- self .charm .on .update_status .emit ()
194- expected_calls = [call .c (0 ), call .r (), call .c (None )]
195- self .assertEqual (manager .mock_calls , expected_calls )
196-
197- @patch_network_get (private_address = "1.1.1.1" )
198- @patch ("charm.Patroni.get_primary" )
199- @patch ("charm.PostgresqlOperatorCharm._restart_postgresql_service" )
200- @patch ("charm.Patroni.get_postgresql_state" )
201- def test_on_update_status_with_error_on_postgresql_status_check (
202- self , _get_postgresql_state , _restart_postgresql_service , _
203- ):
204- _get_postgresql_state .side_effect = [RetryError ("fake error" )]
205- self .charm .on .update_status .emit ()
206- _restart_postgresql_service .assert_not_called ()
207- self .assertEqual (
208- self .harness .model .unit .status ,
209- BlockedStatus ("failed to check PostgreSQL state with error RetryError[fake error]" ),
210- )
211-
212159 @patch_network_get (private_address = "1.1.1.1" )
213160 @patch ("charm.Patroni.get_primary" )
214- @patch ("charm.Patroni.get_postgresql_state" )
215- def test_on_update_status_with_error_on_get_primary (self , _ , _get_primary ):
161+ def test_on_update_status_with_error_on_get_primary (self , _get_primary ):
216162 _get_primary .side_effect = [RetryError ("fake error" )]
217163
218164 with self .assertLogs ("charm" , "ERROR" ) as logs :
0 commit comments