11from unittest import mock
22
33from cterasdk import toxmlstr
4- from cterasdk import exceptions
54from cterasdk .core import setup
65from cterasdk .core .enum import ServerMode , SlaveAuthenticaionMethod , SetupWizardStage , SetupWizardStatus
76from cterasdk .common import Object
@@ -20,7 +19,6 @@ def setUp(self):
2019 self ._admin_last_name = 'Account'
2120 self ._admin_password = 'password'
2221 self ._domain = 'ctera.me'
23- self ._replication_candidates = ['objs/6//Server/server' , 'objs/7//Server/server1' , 'objs/8//Server/server3' ]
2422 self ._replicate_from = 'server1'
2523
2624 def test_init_master (self ):
@@ -91,7 +89,6 @@ def _test_init_application_server_success(self, authentication_method):
9189 self ._init_setup ()
9290 self ._global_admin .ctera .get = mock .MagicMock (side_effect = [
9391 TestCoreSetup ._generate_status_response (SetupWizardStage .Server , SetupWizardStatus .NA , '' ),
94- TestCoreSetup ._generate_status_response (SetupWizardStage .Replication , SetupWizardStatus .NA , '' ),
9592 TestCoreSetup ._generate_status_response (SetupWizardStage .Finish , SetupWizardStatus .NA , '' )
9693 ])
9794 execute_side_effect = TestCoreSetup ._create_init_slave_execute_function (authentication_method )
@@ -103,21 +100,16 @@ def _test_init_application_server_success(self, authentication_method):
103100 setup_status_url = '/setup/status'
104101 self ._global_admin .ctera .get .assert_has_calls (
105102 [
106- mock .call (setup_status_url ),
107103 mock .call (setup_status_url ),
108104 mock .call (setup_status_url )
109105 ]
110106 )
111107 self ._global_admin .ctera .execute .assert_has_calls (
112108 [
113109 mock .call ('/setup/authenticaionMethod' ,
114- 'askMasterForSlaveAuthenticaionMethod' , self ._master_ipaddr ),
115- mock .call ('/public/servers' , 'setReplication' , mock .ANY )
110+ 'askMasterForSlaveAuthenticaionMethod' , self ._master_ipaddr )
116111 ]
117112 )
118- expected_param = TestCoreSetup ._get_init_replication_param ()
119- actual_param = self ._global_admin .ctera .execute .call_args_list [1 ][0 ][2 ] # Access setReplication call param
120- self ._assert_equal_objects (actual_param , expected_param )
121113
122114 params = TestCoreSetup ._get_init_server_params (ServerMode .Slave , authentication_method , self ._master_ipaddr , self ._master_secret )
123115 expected_params = TestCoreSetup ._get_form_data (params )
@@ -127,70 +119,6 @@ def _test_init_application_server_success(self, authentication_method):
127119 self ._assert_equal_objects (actual_param [key ], expected_params [key ])
128120 mock_startup_wait .assert_called_once ()
129121
130- def test_init_replication_server_success_password (self ):
131- self ._test_init_replication_server_success (SlaveAuthenticaionMethod .Password )
132-
133- def test_init_replication_server_success_pk (self ):
134- self ._test_init_replication_server_success (SlaveAuthenticaionMethod .PrivateKey )
135-
136- def _test_init_replication_server_success (self , authentication_method ):
137- self .patch_call ("time.sleep" )
138- self ._init_setup ()
139- self ._global_admin .ctera .get = mock .MagicMock (side_effect = [
140- TestCoreSetup ._generate_status_response (SetupWizardStage .Server , SetupWizardStatus .NA , '' ),
141- TestCoreSetup ._generate_status_response (SetupWizardStage .Replication , SetupWizardStatus .NA , '' ),
142- TestCoreSetup ._generate_status_response (SetupWizardStage .Finish , SetupWizardStatus .NA , '' )
143- ])
144- candidates = self ._replication_candidates
145- execute_side_effect = TestCoreSetup ._create_init_slave_execute_function (authentication_method , candidates )
146- self ._global_admin .ctera .execute = mock .MagicMock (side_effect = execute_side_effect )
147- mock_startup_wait = self .patch_call ("cterasdk.core.startup.Startup.wait" )
148-
149- setup .Setup (self ._global_admin ).init_replication_server (self ._master_ipaddr , self ._master_secret , self ._replicate_from )
150-
151- setup_status_url = '/setup/status'
152- self ._global_admin .ctera .get .assert_has_calls (
153- [
154- mock .call (setup_status_url ),
155- mock .call (setup_status_url ),
156- mock .call (setup_status_url )
157- ]
158- )
159- self ._global_admin .ctera .execute .assert_has_calls (
160- [
161- mock .call ('/setup/authenticaionMethod' ,
162- 'askMasterForSlaveAuthenticaionMethod' , self ._master_ipaddr ),
163- mock .call ('/public/servers' , 'getReplicaitonCandidates' , None ),
164- mock .call ('/public/servers' , 'setReplication' , mock .ANY )
165- ]
166- )
167- expected_param = TestCoreSetup ._get_init_replication_param (self ._replication_candidates [1 ])
168- actual_param = self ._global_admin .ctera .execute .call_args_list [2 ][0 ][2 ] # Access setReplication call param
169- self ._assert_equal_objects (actual_param , expected_param )
170-
171- params = TestCoreSetup ._get_init_server_params (ServerMode .Slave , authentication_method , self ._master_ipaddr , self ._master_secret )
172- expected_params = TestCoreSetup ._get_form_data (params )
173- actual_param = TestCoreSetup ._format_actual_parameters_to_dict (self ._global_admin .ctera .multipart .call_args [0 ][1 ])
174- self ._global_admin .ctera .multipart .assert_called_once_with ('/setup' , mock .ANY )
175- for key in actual_param .keys (): # pylint: disable=consider-using-dict-items, consider-iterating-dictionary
176- self ._assert_equal_objects (actual_param [key ], expected_params [key ])
177- mock_startup_wait .assert_called_once ()
178-
179- def test_no_replication_target (self ):
180- self ._init_setup ()
181- get_function = mock .MagicMock (side_effect = [
182- TestCoreSetup ._generate_status_response (SetupWizardStage .Replication , SetupWizardStatus .NA , '' )
183- ])
184- candidates = ['objs/8//Server/server4' ]
185- execute_side_effect_function = TestCoreSetup ._create_init_slave_execute_function (SlaveAuthenticaionMethod .Password , candidates )
186- execute_function = mock .MagicMock (side_effect = execute_side_effect_function )
187- self ._global_admin .ctera .get = get_function
188- self ._global_admin .ctera .execute = execute_function
189-
190- with self .assertRaises (exceptions .CTERAException ) as error :
191- setup .Setup (self ._global_admin ).init_replication_server (self ._master_ipaddr , self ._master_secret , self ._replicate_from )
192- self .assertEqual ('Could not find database replication target.' , error .exception .message )
193-
194122 def _get_init_portal_param (self ):
195123 params = Object ()
196124 params ._classname = 'InitParams' # pylint: disable=protected-access
@@ -225,15 +153,11 @@ def default_settings():
225153 return settings
226154
227155 @staticmethod
228- def _create_init_slave_execute_function (authentication_method , replication_candidates = None ):
156+ def _create_init_slave_execute_function (authentication_method ):
229157 def _get_init_slave_execute_function (path , name , param , use_file_url = False ):
230158 # pylint: disable=unused-argument
231159 if name == 'askMasterForSlaveAuthenticaionMethod' :
232160 return authentication_method
233- if name == 'getReplicaitonCandidates' :
234- return replication_candidates
235- if name == 'setReplication' :
236- return 'Success'
237161 return None
238162 return _get_init_slave_execute_function
239163
@@ -267,14 +191,3 @@ def _get_init_server_params(mode, authentication_method=None, master_ipaddr=None
267191 elif authentication_method == SlaveAuthenticaionMethod .PrivateKey :
268192 params .slaveSettings .masterKey = master_secret
269193 return params
270-
271- @staticmethod
272- def _get_init_replication_param (replicate_from = None ):
273- params = Object ()
274- params ._classname = 'SetReplicationParam' # pylint: disable=protected-access
275- if replicate_from :
276- params .enabledReplicationParam = Object ()
277- params .enabledReplicationParam ._classname = 'EnabledReplicationParam' # pylint: disable=protected-access
278- params .enabledReplicationParam .replicationOf = replicate_from
279- params .enabledReplicationParam .restartDB = True
280- return params
0 commit comments