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

        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!', 0)

        actual_return_code = self.execute_command('command_wrapper')

        self.assertEqual(0, actual_return_code)

        path = join(self.base_dir, 'shtub', 'executions')

        actual_executions = deserialize_executions(path)

        self.assertEqual(1, len(actual_executions))

        actual_execution = actual_executions[0]

        self.assertEqual(
            'command_stub', actual_execution.command_input.command)
        self.assertEqual(
            ['-arg1', '-arg2', '-arg3'], actual_execution.command_input.arguments)
        self.assertEqual('stdin', actual_execution.command_input.stdin)
Esempio n. 2
0
    def test(self):
        self.prepare_default_testbed(['command_stub'])
        self.create_command_wrapper('command_wrapper', 'command_stub',
                                    ['-arg1', '-arg2', '-arg3'], 'stdin')

        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!', 0)

        actual_return_code = self.execute_command('command_wrapper')

        self.assertEqual(0, actual_return_code)

        path = join(self.base_dir, 'shtub', 'executions')

        actual_executions = deserialize_executions(path)

        self.assertEqual(1, len(actual_executions))

        actual_execution = actual_executions[0]

        self.assertEqual('command_stub',
                         actual_execution.command_input.command)
        self.assertEqual(['-arg1', '-arg2', '-arg3'],
                         actual_execution.command_input.arguments)
        self.assertEqual('stdin', actual_execution.command_input.stdin)
Esempio n. 3
0
    def __enter__ (self):
        """
            since this class is designed to be integrated in a "with" block it
            will load the actual executions and return itself.
        """
        filename = os.path.join(self.base_dir, EXECUTIONS_FILENAME)

        if not os.path.exists(filename):
            raise VerificationException('No executions found. Stubbed commands have never been called.')

        self.executions = deserialize_executions(filename)

        for execution in self.executions:
            if not execution.expected:
                raise VerificationException('Unexpected %s: did not fulfill any stub configuration.' % str(execution))

        return self
Esempio n. 4
0
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)
Esempio n. 5
0
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)
Esempio n. 6
0
    def test_should_deserialize_stub_configuration_with_no_answers(self, mock_open, mock_json):
        fake_file = self.return_file_when_calling(mock_open)
        json_string = "[{'expected': false, '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'},
                                   'expected': False}]

        actual_stub_configuration = deserialize_executions(
            '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_configuration = [
            Execution('command', ['-arg1', '-arg2', '-arg3'], 'stdin')]
        self.assertEqual(
            expected_stub_configuration, actual_stub_configuration)
Esempio n. 7
0
    def __enter__(self):
        """
            since this class is designed to be integrated in a "with" block it
            will load the actual executions and return itself.
        """
        filename = os.path.join(self.base_dir, EXECUTIONS_FILENAME)

        if not os.path.exists(filename):
            raise VerificationException(
                'No executions found. Stubbed commands have never been called.'
            )

        self.executions = deserialize_executions(filename)

        for execution in self.executions:
            if not execution.expected:
                raise VerificationException(
                    'Unexpected %s: did not fulfill any stub configuration.' %
                    str(execution))

        return self
    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)

        path = join(self.base_dir, 'shtub', 'executions')

        actual_calls = deserialize_executions(path)

        self.assertEqual(2, len(actual_calls))

        actual_first_call = actual_calls[0]

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

        actual_second_call = actual_calls[1]

        self.assertEqual(
            'command_stub2', actual_second_call.command_input.command)
        self.assertEqual(['-arg6', '-arg7', '-arg8'],
                         actual_second_call.command_input.arguments)
        self.assertEqual('stdin2', actual_second_call.command_input.stdin)
Esempio n. 9
0
    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)

        path = join(self.base_dir, 'shtub', 'executions')

        actual_calls = deserialize_executions(path)

        self.assertEqual(2, len(actual_calls))

        actual_first_call = actual_calls[0]

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

        actual_second_call = actual_calls[1]

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