def test_default_send_stringifies_headers(self, header_values, requests, store_service): communicator = ClientCommunicator(None) requests.put.return_value = Mock(status_code=200) communicator._default_send('bogus', None) header_values.assert_called_with(None)
def _start_child_process(self): """ Start a child process with the command. :return: """ communicator = ClientCommunicator( self.master_uri, self.quarantine, verify_certs=self.verify_certs, get_files_auth=self.rapid_config.get_files_basic_auth) # Cleanup first self.workspace = OSUtil.path_join( self.workspace, str(self.work_request.action_instance_id)) self.clean_workspace() env = self.get_environment() try: command = self.get_command(communicator) command.extend(self.get_arguments()) # Check and download required files that will execute in command. try: self._download_required_files([self.work_request.executable], communicator, self.logger) except Exception as exception: self.logger.exception(exception) raise exception stdout = None stderr = None if self.logger: stdout = subprocess.PIPE stderr = subprocess.STDOUT self.child_process = subprocess.Popen(command, cwd=self.workspace, env=env, stderr=stderr, stdout=stdout) self.pid = self.child_process.pid StoreService.save_executor(self) # wait for the process to finish self.reading_thread = threading.Thread( target=self._read_process_output, args=(self.logger, self.child_process)) self.reading_thread.daemon = True self.reading_thread.start() self.child_process.wait() self._finalize_run(communicator) except (BaseException, Exception) as exception: self.logger.exception(exception) communicator.send_done(self.work_request.action_instance_id, 'FAILED', None, None, None, self.logger) finally: self.clean_workspace()
def test_trial(self): config = ClientConfiguration( '/Users/mbright/code/cihub/vagrantconf/configs/rapid.cfg') client_communicator = ClientCommunicator(None) print( client_communicator._get_real_file_name('/mnt/d/rapid/workspace', '/ci/build.sh'))
def test_default_send_throws_exception(self, requests, store_service): communicator = ClientCommunicator(None) requests.put.return_value = Mock(status_code=404) with self.assertRaises(Exception) as exception: communicator._default_send('bogus', None) self.assertEqual('Status Code Failure: 404', str(exception.exception))
def test_default_send_includes_the_master_api_key(self, requests, store_service): mock_app = Mock(rapid_config=Mock(verify_certs=False)) store_service.get_master_key.return_value = 'bogus_key' communicator = ClientCommunicator(None, flask_app=mock_app) requests.put.return_value = Mock(status_code=200) communicator._default_send('bogus', None) store_service.get_master_key.assert_called_with(mock_app) requests.put.assert_called_with( 'bogus', data=None, json=None, headers={'X-Rapidci-Api-Key': 'bogus_key'}, verify=False)
def test_init(self): mock_app = Mock() client_communicator = ClientCommunicator("bogus", "/tmp/trial", mock_app) eq_("bogus", client_communicator.server_uri) eq_("/tmp/trial", client_communicator.quarantine_directory) eq_(mock_app, client_communicator.flask_app)
def test_get_register_headers(self, time): time.time.return_value = 1 config = MasterConfiguration() config.register_api_key = 'Testing!@$%^' # per requests > 2.11.0 - All header values must be strings test = { 'Content-Type': 'application/json', 'X-RAPIDCI-PORT': str(config.port), 'X-RAPIDCI-REGISTER-KEY': config.register_api_key, 'X-RAPIDCI-TIME': '1000', 'X-RAPIDCI-CLIENT-KEY': config.api_key } eq_(test, ClientCommunicator._get_register_headers(config))
def test_register_post_data(self, socket): config = MasterConfiguration() config.grain_restrict = False config.grains = "one;two" socket.gethostname.return_value = "bogus" test = { "grains": "one;two", "grain_restrict": False, "hostname": 'bogus' } eq_(test, json.loads(ClientCommunicator._get_register_post_data(config)))
def test_default_send_supports_post(self, requests, store_service): communicator = ClientCommunicator(None) requests.post.return_value = Mock(status_code=200) communicator._default_send('bogus', None, 'post') requests.post.assert_called()
def __get_quarantined_items(self): items = [] quarantine_directory = self.app.rapid_config.quarantine_directory if quarantine_directory: communicator = None if not os.path.isdir(quarantine_directory): os.makedirs(quarantine_directory) for item in os.listdir(quarantine_directory): if item in ['.', '..']: continue try: items.append({ 'action_instance_id': int(item), 'pid': 'quarantined' }) if communicator is None: communicator = ClientCommunicator( self.app.rapid_config.master_uri, self.app.rapid_config.quarantine_directory, verify_certs=self.app.rapid_config.verify_certs, get_files_auth=self.app.rapid_config. get_files_basic_auth) try: delete_file = False with open("{}/{}".format(quarantine_directory, item), 'r') as tmp_file: data = pickle.loads(tmp_file.read()) try: status = data['status'] parameters = data[ 'parameters'] if 'parameters' in data else None stats = data[ 'stats'] if 'stats' in data else None results = data[ 'results'] if 'results' in data else None communicator.send_done( int(item), status, parameters, stats, results, logger, headers={'X-Rapid-Quarantine': 'true'}) delete_file = True except Exception: import traceback traceback.print_exc() if delete_file: try: os.remove("{}/{}".format( quarantine_directory, item)) except Exception: logger.error("Couldn't remove.") except Exception: import traceback traceback.print_exc() except Exception: pass return items
def test_get_real_file_name_not_empty_override(self, patch_env): patch_env.return_value = 'foobie_bubcus' communicator = ClientCommunicator(None) self.assertEqual('foofoobie_bubcusbar', communicator._get_real_file_name('foo', 'bar'))
def test_get_real_file_name_empty_override_uses_os_path(self, patch_os): patch_os.path_join.return_value = '/testing/this/thing' communicator = ClientCommunicator(None) self.assertEqual('/testing/this/thing', communicator._get_real_file_name('foo', 'bar'))
def test_default_send_defaults_to_put(self, requests, store_service): communicator = ClientCommunicator(None, flask_app=Mock()) requests.put.return_value = Mock(status_code=200) communicator._default_send('bogus', None) requests.put.assert_called()