def test_run_master_does_not_invoke_popen_if_resp_is_ok(self): mock_network = self.mock_Network.return_value mock_network.get.return_value = Mock(ok=True) try: service_runner = ServiceRunner('frodo:1') service_runner.run_master() except ServiceRunError: pass assert not self.mock_Popen.called
def test_run_master_invokes_popen(self): self.mock_time.time.side_effect = range(1000) mock_network = self.mock_Network.return_value mock_network.get.return_value = Mock(ok=False) try: service_runner = ServiceRunner('frodo:1') service_runner.run_master() except ServiceRunError: pass assert self.mock_Popen.called
def test_run_master_invokes_popen(self): self.mock_time.time.side_effect = range(1000) mock_network = self.mock_Network.return_value mock_network.get.return_value = Mock(ok=False) try: service_runner = ServiceRunner('frodo:1') service_runner.run_master() except ServiceRunError: pass self.assertEqual(call([ANY, 'master', '--port', '1'], stdout=ANY), self.mock_Popen.call_args)
def start_and_block_until_up(self, port, timeout_sec=_MASTER_SERVICE_TIMEOUT_SEC): """ Start the clusterrunner master service and block until the master responds to web requests. Times out and throws an exception after timeout_sec. :param port: the port that the master service will run on :type port: int :param timeout_sec: number of seconds to wait for the master to respond before timing out :type timeout_sec: int """ # Start the master service daemon master_service_cmd = 'nohup {} master --port {} &'.format( self._executable_path, str(port)) # There are cases when 'clusterrunner deploy' fails, and there is no clusterrunner master service process # to be seen--but the fix is to just re-run the command. for i in range(self._MASTER_SERVICE_START_RETRIES): self._shell_client.exec_command(master_service_cmd, async=True) # Give the service a second to start up time.sleep(1) if self._is_process_running(self._executable_path): break else: self._logger.warning( 'Master service process failed to start on try {}, host {}' .format(i, self.host)) if not self._is_process_running(self._executable_path): self._logger.error( 'Master service process failed to start on host {}.'.format( self.host)) raise SystemExit(1) # Check to see if the master service is responding to http requests master_service_url = '{}:{}'.format(self.host, str(port)) master_service = ServiceRunner(master_service_url, main_executable=self._executable_path) if not master_service.is_up(master_service_url, timeout=timeout_sec): self._logger.error( 'Master service process exists on {}, but service on {} failed to respond.' .format(self.host, master_service_url)) raise SystemExit(1)
def start_and_block_until_up(self, port, timeout_sec=_MASTER_SERVICE_TIMEOUT_SEC): """ Start the clusterrunner master service and block until the master responds to web requests. Times out and throws an exception after timeout_sec. :param port: the port that the master service will run on :type port: int :param timeout_sec: number of seconds to wait for the master to respond before timing out :type timeout_sec: int """ self._shell_client.exec_command('nohup {} master --port {} &'.format(self._executable_path, str(port)), async=True) master_service_url = '{}:{}'.format(self.host, str(port)) master_service = ServiceRunner(master_service_url) if not master_service.is_up(master_service_url, timeout=timeout_sec): self._logger.error('Master service running on {} failed to start.'.format(master_service_url)) raise SystemExit(1)
def _start_local_services(self, master_url): """ In the case that: - the master url is localhost - the slaves list is just localhost Start a master and slave service instance locally. :param master_url: service url (with port number) :type master_url: str """ service_runner = ServiceRunner(master_url) try: service_runner.run_master() service_runner.run_slave() except ServiceRunError as ex: self._logger.error(str(ex)) sys.exit(1)
def start_and_block_until_up(self, port, timeout_sec=_MASTER_SERVICE_TIMEOUT_SEC): """ Start the clusterrunner master service and block until the master responds to web requests. Times out and throws an exception after timeout_sec. :param port: the port that the master service will run on :type port: int :param timeout_sec: number of seconds to wait for the master to respond before timing out :type timeout_sec: int """ # Start the master service daemon master_service_cmd = "nohup {} master --port {} &".format(self._executable_path, str(port)) # There are cases when 'clusterrunner deploy' fails, and there is no clusterrunner master service process # to be seen--but the fix is to just re-run the command. for i in range(self._MASTER_SERVICE_START_RETRIES): self._shell_client.exec_command(master_service_cmd, async=True) # Give the service a second to start up time.sleep(1) if self._is_process_running(self._executable_path): break else: self._logger.warning("Master service process failed to start on try {}, host {}".format(i, self.host)) if not self._is_process_running(self._executable_path): self._logger.error("Master service process failed to start on host {}.".format(self.host)) raise SystemExit(1) # Check to see if the master service is responding to http requests master_service_url = "{}:{}".format(self.host, str(port)) master_service = ServiceRunner(master_service_url, main_executable=self._executable_path) if not master_service.is_up(master_service_url, timeout=timeout_sec): self._logger.error( "Master service process exists on {}, but service on {} failed to respond.".format( self.host, master_service_url ) ) raise SystemExit(1)
def _start_local_services_if_needed(self, master_url): """ In the case that: - the master url is localhost - the slaves list is just localhost Start a master and slave service instance locally, if the master is not already running. :param master_url: service url (with port number) :type master_url: str """ service_runner = ServiceRunner(master_url) if service_runner.is_master_up(): return try: service_runner.run_master() service_runner.run_slave() except ServiceRunError as ex: self._logger.error(str(ex)) sys.exit(1)