Exemplo n.º 1
0
    def test_no_pidfile(self, get_pids_dir, makedirs, getpid, remove):
        get_pids_dir.return_value = '/var/pids'
        getpid.return_value = 42
        existence = [
            ('/var/pids', False),
            ('/var/pids/foo', False),
        ]
        def exists(path):
            name, retvalue = existence.pop(0)
            self.assertEqual(name, path)
            return retvalue

        from StringIO import StringIO
        from jove.scripts.utils import only_one
        script = mock.Mock()
        func = only_one('foo')(script)
        out = StringIO()

        with mock.patch('jove.scripts.utils.open', create=True) as mock_open:
            with mock.patch('jove.scripts.utils.os.path.exists', exists):
                mock_open.return_value = mock.MagicMock(spec=file)
                mock_open.return_value.__enter__.return_value = out
                func('args')

        script.assert_called_once_with('args')
        self.assertEqual(existence, [])
        self.assertEqual(out.getvalue(), '42\n')
        makedirs.assert_called_once_with('/var/pids')
        remove.assert_called_once_with('/var/pids/foo')
Exemplo n.º 2
0
    def test_stale_pidfile(self, get_pids_dir, getpid, remove):
        get_pids_dir.return_value = '/var/pids'
        getpid.return_value = 42
        existence = [
            ('/var/pids', True),
            ('/var/pids/foo', True),
            ('/proc', True),
            ('/proc/56', False),
        ]
        def exists(path):
            name, retvalue = existence.pop(0)
            self.assertEqual(name, path)
            return retvalue

        from StringIO import StringIO
        out = StringIO()
        def mock_open(path, mode='r'):
            if mode == 'w':
                mock_file = mock.MagicMock(spec=file)
                mock_file.__enter__.return_value = out
                return mock_file
            return StringIO('56\n')

        from jove.scripts.utils import only_one
        script = mock.Mock()
        func = only_one('foo')(script)

        with mock.patch('jove.scripts.utils.open', mock_open, create=True):
            with mock.patch('jove.scripts.utils.os.path.exists', exists):
                func('args')

        script.assert_called_once_with('args')
        self.assertEqual(existence, [])
        self.assertEqual(out.getvalue(), '42\n')
        remove.assert_called_once_with('/var/pids/foo')
Exemplo n.º 3
0
    def test_running_no_procfs(self, get_pids_dir, exit):
        class Exit(Exception):
            pass
        exit.side_effect = Exit

        get_pids_dir.return_value = '/var/pids'
        existence = [
            ('/var/pids', True),
            ('/var/pids/foo', True),
            ('/proc', False),
        ]
        def exists(path):
            name, retvalue = existence.pop(0)
            self.assertEqual(name, path)
            return retvalue

        from StringIO import StringIO
        def mock_open(path, mode='r'):
            return StringIO('56\n')

        from jove.scripts.utils import only_one
        script = mock.Mock()
        func = only_one('foo')(script)

        with mock.patch('jove.scripts.utils.open', mock_open, create=True):
            with mock.patch('jove.scripts.utils.os.path.exists', exists):
                with self.assertRaises(Exit):
                    func('args')

        self.assertEqual(script.method_calls, [])
        self.assertEqual(existence, [])
        exit.assert_called_once_with(1)