Esempio n. 1
0
    def test_start_running(self):
        self.pidfile.return_value.is_running.return_value = True
        d = daemon.Daemon('pidfile')

        with mock.patch.object(daemon.sys, 'stderr'):
            with mock.patch.object(d, 'daemonize') as daemonize:
                with testtools.ExpectedException(SystemExit):
                    d.start()
                self.assertFalse(daemonize.called)
Esempio n. 2
0
    def test_start(self):
        self.pidfile.return_value.is_running.return_value = False
        d = daemon.Daemon('pidfile')

        with mock.patch.object(d, 'daemonize') as daemonize:
            with mock.patch.object(d, 'run') as run:
                d.start()
                run.assert_called_once_with()
                daemonize.assert_called_once_with()
Esempio n. 3
0
    def test_daemonize(self):
        d = daemon.Daemon('pidfile')
        with mock.patch.object(d, '_fork') as fork:
            with mock.patch.object(daemon, 'atexit') as atexit:
                with mock.patch.object(daemon, 'sys') as sys:
                    sys.stdin.fileno.return_value = 0
                    sys.stdout.fileno.return_value = 1
                    sys.stderr.fileno.return_value = 2
                    d.daemonize()
                    atexit.register.assert_called_once_with(d.delete_pid)

            fork.assert_has_calls([mock.call(), mock.call()])

        self.os.assert_has_calls([
            mock.call.chdir('/'),
            mock.call.setsid(),
            mock.call.umask(0),
            mock.call.dup2(mock.ANY, 0),
            mock.call.dup2(mock.ANY, 1),
            mock.call.dup2(mock.ANY, 2),
            mock.call.getpid()
        ])
Esempio n. 4
0
 def test_delete_pid(self):
     self.pidfile.return_value.__str__.return_value = 'pidfile'
     d = daemon.Daemon('pidfile')
     d.delete_pid()
     self.os.remove.assert_called_once_with('pidfile')
Esempio n. 5
0
 def test_fork_error(self):
     self.os.fork.side_effect = lambda: OSError(1)
     with mock.patch.object(daemon.sys, 'stderr'):
         with testtools.ExpectedException(SystemExit):
             d = daemon.Daemon('pidfile', 'stdin')
             d._fork()
Esempio n. 6
0
 def test_fork_child(self):
     self.os.fork.return_value = 0
     d = daemon.Daemon('pidfile')
     self.assertIsNone(d._fork())
Esempio n. 7
0
 def test_fork_parent(self):
     self.os.fork.return_value = 1
     with testtools.ExpectedException(SystemExit):
         d = daemon.Daemon('pidfile')
         d._fork()
Esempio n. 8
0
 def test_init(self):
     d = daemon.Daemon('pidfile')
     self.assertEqual(d.procname, 'python')
Esempio n. 9
0
 def test_fork_error(self):
     self.os.fork.side_effect = lambda: OSError(1)
     with mock.patch.object(daemon.sys, 'stderr') as stderr:
         with self.assertRaises(SystemExit):
             d = daemon.Daemon('pidfile', 'stdin')
             d._fork()
Esempio n. 10
0
 def test_fork_parent(self):
     self.os.fork.return_value = 1
     with self.assertRaises(SystemExit):
         d = daemon.Daemon('pidfile')
         d._fork()