def dispatch(command_input): """ currently this will handle the given command_input by testing whether it fulfills a stub configuration. If so it will save a execution (with the expected flag set to true) and send the next answer as defined in the stub configuration object. """ stub_configurations = deserialize_stub_configurations( CONFIGURED_STUBS_FILENAME) logging.info('Got %s', command_input) execution = Execution( command_input.command, command_input.arguments, command_input.stdin) for stub_configuration in stub_configurations: if command_input.fulfills(stub_configuration.command_input): logging.info('Execution fulfills %s', stub_configuration) execution.mark_as_expected() record_execution(execution) answer = stub_configuration.next_answer() serialize_as_dictionaries( CONFIGURED_STUBS_FILENAME, stub_configurations) unlock(lock_handle) if answer.milliseconds_to_wait: time.sleep(answer.milliseconds_to_wait / 1000) send_answer(answer) return unlock(lock_handle) logging.error( 'Given command_input does not fulfill requirements of any stub configuration.') sys.exit(255)
def dispatch(command_input): """ currently this will handle the given command_input by testing whether it fulfills a stub configuration. If so it will save a execution (with the expected flag set to true) and send the next answer as defined in the stub configuration object. """ stub_configurations = deserialize_stub_configurations( CONFIGURED_STUBS_FILENAME) logging.info('Got %s', command_input) execution = Execution(command_input.command, command_input.arguments, command_input.stdin) for stub_configuration in stub_configurations: if command_input.fulfills(stub_configuration.command_input): logging.info('Execution fulfills %s', stub_configuration) execution.mark_as_expected() record_execution(execution) answer = stub_configuration.next_answer() serialize_as_dictionaries(CONFIGURED_STUBS_FILENAME, stub_configurations) unlock(lock_handle) if answer.milliseconds_to_wait: time.sleep(answer.milliseconds_to_wait / 1000) send_answer(answer) return unlock(lock_handle) logging.error( 'Given command_input does not fulfill requirements of any stub configuration.' ) sys.exit(255)
def __exit__(self, exception_type, exception_value, traceback): """ since this class is designed to be used in a "with" statement this will save the list of stub_configurations in the base directory. @return: False, when exception_type, exception_value or traceback given, otherwise None """ if exception_type or exception_value or traceback: return False filename = os.path.join(self.base_directory, CONFIGURED_STUBS_FILENAME) serialize_as_dictionaries(filename, self.stub_configurations)
def record_execution(execution): """ loads the list of recent executions from the EXECUTIONS_FILENAME file, appends the given execution to the list, then writes the list back to the file again. To assure only one process is reading and writing the file a file lock is used. """ lock_file_handle = lock() executions = [] if os.path.exists(EXECUTIONS_FILENAME): executions = deserialize_executions(EXECUTIONS_FILENAME) executions.append(execution) serialize_as_dictionaries(EXECUTIONS_FILENAME, executions) logging.info('Recorded %s executions.', len(executions)) unlock(lock_file_handle)
def test_should_serialize_as_dictionaries(self, mock_open, mock_json, mock_unlock, mock_lock): fake_file = self.return_file_when_calling(mock_open) mock_json.return_value = '[{"some": "json"}]' stub_configuration = [ Execution('command', ['-arg1', '-arg2', '-arg3'], 'stdin', expected=True)] serialize_as_dictionaries( 'stub_configuration.json', stub_configuration) expected_dictionary = {'command_input': {'command': 'command', 'arguments': ['-arg1', '-arg2', '-arg3'], 'stdin': 'stdin'}, 'expected': True} self.assertEqual( call([expected_dictionary], sort_keys=True, indent=4), mock_json.call_args) self.assertEqual( call('stub_configuration.json', mode='w'), mock_open.call_args) self.assertEqual(call('[{"some": "json"}]'), fake_file.write.call_args)