示例#1
0
    def test_write(self):
        p = daemon.Pidfile('thefile', 'python')
        p.write(34)

        self.os.assert_has_calls([
            mock.call.ftruncate(FAKE_FD, 0),
            mock.call.write(FAKE_FD, b'34'),
            mock.call.fsync(FAKE_FD)
        ])
示例#2
0
    def test_init_open_fail(self):
        self.os.open.side_effect = IOError

        with mock.patch.object(daemon.sys, 'stderr'):
            with testtools.ExpectedException(SystemExit):
                daemon.Pidfile('thefile', 'python')
                sys.assert_has_calls(
                    [mock.call.stderr.write(mock.ANY),
                     mock.call.exit(1)])
示例#3
0
    def _create_pidfile(self):
        pidfile = os.path.join(cfg.CONF.state_path,
                               type(self).__name__.lower() + '.pid')
        self.pidfile = daemon.Pidfile(pidfile, 'python')

        if self._running is None:
            atexit.register(self._delete_pidfile)

        self.pidfile.write(os.getpid())
示例#4
0
    def test_is_running_uuid_false(self):
        mock_open = self.useFixture(
            tools.OpenFixture('/proc/34/cmdline', 'python 1234')).mock_open
        p = daemon.Pidfile('thefile', 'python', uuid='6789')

        with mock.patch.object(p, 'read') as read:
            read.return_value = 34
            self.assertFalse(p.is_running())

        mock_open.assert_called_once_with('/proc/34/cmdline', 'r')
示例#5
0
    def test_is_running(self):
        mock_open = self.useFixture(
            lib_fixtures.OpenFixture('/proc/34/cmdline', 'python')).mock_open
        p = daemon.Pidfile('thefile', 'python')

        with mock.patch.object(p, 'read') as read:
            read.return_value = 34
            self.assertTrue(p.is_running())

        mock_open.assert_called_once_with('/proc/34/cmdline', 'r')
示例#6
0
    def test_is_running_uuid_false(self):
        mock_open = self.useFixture(
            lib_fixtures.OpenFixture('/proc/34/cmdline', '{} 1234'.format(
                sys.executable))).mock_open
        p = daemon.Pidfile('thefile', sys.executable, uuid='6789')

        with mock.patch.object(p, 'read') as read:
            read.return_value = 34
            self.assertFalse(p.is_running())

        mock_open.assert_called_once_with('/proc/34/cmdline', 'r')
示例#7
0
    def test_is_running_uuid_true(self):
        with mock.patch('neutron.agent.linux.utils.execute') as execute:
            execute.return_value = 'python 1234'
            p = daemon.Pidfile('thefile', 'python', uuid='1234')

            with mock.patch.object(p, 'read') as read:
                read.return_value = 34
                self.assertTrue(p.is_running())

            execute.assert_called_once_with(['cat', '/proc/34/cmdline'],
                                            'sudo')
示例#8
0
    def test_is_running_uuid_false(self):
        with mock.patch('six.moves.builtins.open') as mock_open:
            p = daemon.Pidfile('thefile', 'python', uuid='6789')
            mock_open.return_value.__enter__ = lambda s: s
            mock_open.return_value.__exit__ = mock.Mock()
            mock_open.return_value.readline.return_value = 'python 1234'

            with mock.patch.object(p, 'read') as read:
                read.return_value = 34
                self.assertFalse(p.is_running())

            mock_open.assert_called_once_with('/proc/34/cmdline', 'r')
示例#9
0
    def test_is_running(self):
        with mock.patch('__builtin__.open') as mock_open:
            p = daemon.Pidfile('thefile', 'python')
            mock_open.return_value.__enter__ = lambda s: s
            mock_open.return_value.__exit__ = mock.Mock()
            mock_open.return_value.readline.return_value = 'python'

            with mock.patch.object(p, 'read') as read:
                read.return_value = 34
                self.assertTrue(p.is_running())

            mock_open.assert_called_once_with('/proc/34/cmdline', 'r')
示例#10
0
    def _create_pidfile(self):
        pidfile = os.path.join(cfg.CONF.state_path,
                               type(self).__name__.lower() + '.pid')
        self.pidfile = daemon.Pidfile(pidfile, 'python')

        # NOTE(mpeterson): We want self._running to be None before the first
        # run so atexit is only registered once and not several times.
        if self._running is None:
            atexit.unregister(self._delete_pidfile)
            atexit.register(self._delete_pidfile)

        self.pidfile.write(os.getpid())
    def _create_pidfile(self):
        pidfile = os.path.join(cfg.CONF.state_path,
                               type(self).__name__.lower() + '.pid')
        self.pidfile = daemon.Pidfile(pidfile, 'python')

        # NOTE(mpeterson): We want self._running to be None before the first
        # run so atexit is only registered once and not several times.
        # TODO(mpeterson): Once we drop support for PY2 we need to change
        # the logic to use atexit.unregister at stop.
        if self._running is None:
            atexit.register(self._delete_pidfile)

        self.pidfile.write(os.getpid())
示例#12
0
 def test_read(self):
     self.os.read.return_value = '34'
     p = daemon.Pidfile('thefile', 'python')
     self.assertEqual(34, p.read())
示例#13
0
 def test_read(self):
     self.os.read.return_value = '34'
     p = daemon.Pidfile('thefile', sys.executable)
     self.assertEqual(34, p.read())