def test(self):
        # given
        self.prepare_default_testbed(['command_stub'])
        self.create_command_wrapper(
            'command_wrapper', 'command_stub', ['-arg1', '-arg2', '-arg3'], 'stdin')

        # when
        with self.fixture() as when:
            when.calling('command_stub').at_least_with_arguments('-arg1', '-arg2', '-arg3').and_input('stdin') \
                .then_answer('Hello world.', 'Hello error!', 2)

        stub_configurations_filename = join(
            self.base_dir, 'shtub', 'stub-configurations')
        actual_stub_configurations = deserialize_stub_configurations(
            stub_configurations_filename)

        # then
        self.assertEqual(1, len(actual_stub_configurations))
        actual_stub_configuration = actual_stub_configurations[0]

        self.assertEqual(['-arg1', '-arg2', '-arg3'],
                         actual_stub_configuration.command_input.arguments)
        self.assertEqual(
            'stdin', actual_stub_configuration.command_input.stdin)
        self.assertEqual(
            'command_stub', actual_stub_configuration.command_input.command)

        actual_answer = actual_stub_configuration.next_answer()

        self.assertEqual('Hello world.', actual_answer.stdout)
        self.assertEqual('Hello error!', actual_answer.stderr)
        self.assertEqual(2, actual_answer.return_code)
Example #2
0
    def test_should_deserialize_stub_configurations(self, mock_open, mock_json):
        fake_file = self.return_file_when_calling(mock_open)
        json_string = "[{'current_answer': 0, 'answers': [{'return_code': 15, 'stderr': 'stderr', 'stdout': 'stdout', 'milliseconds_to_wait': None}], 'command_input': {'stdin': 'stdin', 'command': 'command', 'arguments': ['-arg1', '-arg2', '-arg3']}}]"
        fake_file.read.return_value = json_string
        mock_json.return_value = [
            {'command_input': {'command': 'command',
                               'arguments': ['-arg1', '-arg2', '-arg3'],
                               'stdin': 'stdin'},
             'current_answer': 0,
             'answers': [{'stdout': 'stdout',
                          'stderr': 'stderr',
                          'return_code': 15,
                          'milliseconds_to_wait': None}]
             }]

        actual_stub_configurations = deserialize_stub_configurations(
            'stub_configuration.json')

        self.assertEqual(
            call('stub_configuration.json', mode='r'), mock_open.call_args)
        self.assertEqual(call(), fake_file.read.call_args)
        self.assertEqual(call(json_string), mock_json.call_args)

        expected_stub_configurations = [
            StubConfiguration('command', ['-arg1', '-arg2', '-arg3'], 'stdin', [Answer('stdout', 'stderr', 15)], 0)]

        self.assertEqual(
            expected_stub_configurations, actual_stub_configurations)
Example #3
0
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 test(self):
        self.prepare_default_testbed(['command_stub1', 'command_stub2'])
        self.create_command_wrapper(
            'command_wrapper1', 'command_stub1', ['-arg1', '-arg2', '-arg3'], 'stdin1')
        self.create_command_wrapper(
            'command_wrapper2', 'command_stub2', ['-arg6', '-arg7', '-arg8'], 'stdin2')

        with self.fixture() as when:
            when.calling('command_stub1').at_least_with_arguments('-arg1', '-arg2', '-arg3').and_input('stdin1') \
                .then_answer('Hello world 1', 'Hello error 1', 0)
            when.calling('command_stub2').at_least_with_arguments('-arg6', '-arg7', '-arg8').and_input('stdin2') \
                .then_answer('Hello world 2', 'Hello error 2', 0)

        actual_return_code1 = self.execute_command('command_wrapper1')
        actual_return_code2 = self.execute_command('command_wrapper2')

        self.assertEqual(0, actual_return_code1)
        self.assertEqual(0, actual_return_code2)

        stub_configurations_filename = join(
            self.base_dir, 'shtub', 'stub-configurations')
        actual_stub_configurations = deserialize_stub_configurations(
            stub_configurations_filename)

        self.assertEqual(2, len(actual_stub_configurations))

        actual_first_stub_configuration = actual_stub_configurations[0]

        self.assertEqual(
            'stdin1', actual_first_stub_configuration.command_input.stdin)
        self.assertEqual(['-arg1', '-arg2', '-arg3'],
                         actual_first_stub_configuration.command_input.arguments)
        self.assertEqual(
            'command_stub1', actual_first_stub_configuration.command_input.command)

        actual_first_answer = actual_first_stub_configuration.next_answer()

        self.assertEqual('Hello world 1', actual_first_answer.stdout)
        self.assertEqual('Hello error 1', actual_first_answer.stderr)
        self.assertEqual(0, actual_first_answer.return_code)

        actual_second_stub_configuration = actual_stub_configurations[1]

        self.assertEqual(['-arg6', '-arg7', '-arg8'],
                         actual_second_stub_configuration.command_input.arguments)
        self.assertEqual(
            'stdin2', actual_second_stub_configuration.command_input.stdin)
        self.assertEqual(
            'command_stub2', actual_second_stub_configuration.command_input.command)

        actual_second_answer = actual_second_stub_configuration.next_answer()

        self.assertEqual('Hello world 2', actual_second_answer.stdout)
        self.assertEqual('Hello error 2', actual_second_answer.stderr)
        self.assertEqual(0, actual_second_answer.return_code)