Example #1
0
    def test_invoke_local_script(self):
        self.qa_repository.myscript = Mock(qa_extraparams=[], script_url='ls -l %s')
        self.qa_repository.myscript.content_type_out = 'text/plain'
        from StringIO import StringIO
        mock_file = StringIO(self.doc_content)
        mock_file.name = 'test_file'
        mock_file.__exit__ = Mock()
        mock_file.__enter__ = Mock()
        file_data = Mock(data_file=Mock(open=Mock(return_value=mock_file)))
        self.qa_repository.unrestrictedTraverse = Mock(return_value=file_data)
        with patch('Products.Reportek.QARepository.RepUtils',
                Mock(temporary_named_copy=Mock(return_value=mock_file))):
            with patch.object(self.qa_repository, 'REQUEST', create=True) as request:
                request.SERVER_URL = 'http://example.com'
                file_url = 'http://example.com/envelope/foo.txt'
                with patch('Products.Reportek.QARepository.subprocess') as mock_sp:
                    mock_proc = Mock(stdout=StringIO('test output'))
                    mock_sp.Popen.return_value = mock_proc
                    ret = self.qa_repository._runQAScript(file_url, 'loc_myscript')
                (file_id, [content_type, result]) = ret
                self.assertEqual(len(mock_sp.Popen.mock_calls), 1)

        self.assertEqual('test output', result.data)
        self.assertEqual(
                ['ls', '-l', 'test_file'],
                mock_sp.Popen.mock_calls[0][1][0])

        self.assertEqual(
                False,
                mock_sp.Popen.mock_calls[0][2]['shell'])
Example #2
0
    def setUp(self):

        ConfigManager._instances.clear()

        self.patcher1 = patch('netki.common.config.os.listdir')
        self.patcher2 = patch('netki.common.config.os.path.isfile')
        self.patcher3 = patch('netki.common.config.open', create=True)
        self.patcher4 = patch('netki.common.config.ConfigManager.find_config_file')

        self.mockListdir = self.patcher1.start()
        self.mockIsFile = self.patcher2.start()
        self.mockOpen = self.patcher3.start()
        self.mockFindConfigFile = self.patcher4.start()

        self.mockListdir.side_effect = (['etc'], ['app.test.config'])
        self.mockIsFile.return_value = True

        def empty_func(*args):
            pass

        def return_file_data():
            return StringIO('''
[section]
string_value=string
int_value=1
float_value=42.42
bool_true=true
bool_false=false
        ''')

        mockFile = StringIO()
        mockFile.__enter__ = return_file_data
        mockFile.__exit__ = empty_func
        self.mockOpen.return_value = mockFile

        self.mockFindConfigFile.return_value = 'CONFIGFILE'