Python twisted.internet.error.ProcessDone() Examples

The following are 30 code examples of twisted.internet.error.ProcessDone(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.internet.error , or try the search function .
Example #1
Source File: test_iutils.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_outputWithErrorIgnored(self):
        """
        The L{Deferred} returned by L{getProcessOutput} is fired with an
        L{IOError} L{Failure} if the child process writes to stderr.
        """
        # make sure stderr raises an error normally
        scriptFile = self.makeSourceFile([
            'import sys',
            'sys.stderr.write("hello world\\n")'
            ])

        d = utils.getProcessOutput(self.exe, ['-u', scriptFile])
        d = self.assertFailure(d, IOError)
        def cbFailed(err):
            return self.assertFailure(err.processEnded, error.ProcessDone)
        d.addCallback(cbFailed)
        return d 
Example #2
Source File: test_stdio.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_consumer(self):
        """
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IConsumer} provider.
        """
        p = StandardIOTestProcessProtocol()
        d = p.onCompletion

        junkPath = self._junkPath()

        self._spawnProcess(p, b'stdio_test_consumer', junkPath)

        def processEnded(reason):
            with open(junkPath, 'rb') as f:
                self.assertEqual(p.data[1], f.read())
            reason.trap(error.ProcessDone)
        return self._requireFailure(d, processEnded) 
Example #3
Source File: test_scriptexecution.py    From landscape-client with GNU General Public License v2.0 6 votes vote down vote up
def test_user(self):
        """A user can be specified in the message."""
        username = pwd.getpwuid(os.getuid())[0]
        uid, gid, home = get_user_info(username)

        def spawnProcess(protocol, filename, args, env, path, uid, gid):
            protocol.childDataReceived(1, "hi!\n")
            protocol.processEnded(Failure(ProcessDone(0)))
            self._verify_script(filename, sys.executable, "print 'hi'")

        process_factory = mock.Mock()
        process_factory.spawnProcess = mock.Mock(side_effect=spawnProcess)
        self.manager.add(
            ScriptExecutionPlugin(process_factory=process_factory))

        result = self._send_script(sys.executable, "print 'hi'", user=username)

        def check(_):
            process_factory.spawnProcess.assert_called_with(
                mock.ANY, mock.ANY, args=mock.ANY, uid=None, gid=None,
                path=mock.ANY, env=encoded_default_environment())

        return result.addCallback(check) 
Example #4
Source File: test_scriptexecution.py    From landscape-client with GNU General Public License v2.0 6 votes vote down vote up
def test_command_output_ends_with_truncation(self):
        """After truncation, no further output is recorded."""
        factory = StubProcessFactory()
        self.plugin.process_factory = factory
        self.manager.config.script_output_limit = 1
        result = self.plugin.run_script("/bin/sh", "")

        # Ultimately we assert that the resulting output is limited to
        # 1024 bytes and indicates its truncation.
        result.addCallback(self.assertEqual,
                           ("x" * (1024 - 21)) + "\n**OUTPUT TRUNCATED**")
        protocol = factory.spawns[0][0]

        # Push 1024 bytes of output, so we trigger truncation.
        protocol.childDataReceived(1, b"x" * 1024)
        # Push 1024 bytes more
        protocol.childDataReceived(1, b"x" * 1024)

        for fd in (0, 1, 2):
            protocol.childConnectionLost(fd)
        protocol.processEnded(Failure(ProcessDone(0)))

        return result 
Example #5
Source File: test_shutdownmanager.py    From landscape-client with GNU General Public License v2.0 6 votes vote down vote up
def test_restart_stops_exchanger(self):
        """
        After a successful shutdown, the broker stops processing new messages.
        """
        message = {"type": "shutdown", "reboot": False, "operation-id": 100}
        self.plugin.perform_shutdown(message)

        [arguments] = self.process_factory.spawns
        protocol = arguments[0]
        protocol.processEnded(Failure(ProcessDone(status=0)))
        self.broker_service.reactor.advance(100)
        self.manager.reactor.advance(100)

        # New messages will not be exchanged after a reboot process is in
        # process.
        self.manager.broker.exchanger.schedule_exchange()
        payloads = self.manager.broker.exchanger._transport.payloads
        self.assertEqual(0, len(payloads))
        return protocol.result 
Example #6
Source File: test_scriptexecution.py    From landscape-client with GNU General Public License v2.0 6 votes vote down vote up
def test_cancel_doesnt_blow_after_success(self):
        """
        When the process ends successfully and is immediately followed by the
        timeout, the output should still be in the failure and nothing bad will
        happen!
        [regression test: killing of the already-dead process would blow up.]
        """
        factory = StubProcessFactory()
        self.plugin.process_factory = factory
        result = self.plugin.run_script("/bin/sh", "", time_limit=500)
        protocol = factory.spawns[0][0]
        protocol.makeConnection(DummyProcess())
        protocol.childDataReceived(1, b"hi")
        protocol.processEnded(Failure(ProcessDone(0)))
        self.manager.reactor.advance(501)

        def got_result(output):
            self.assertEqual(output, "hi")

        result.addCallback(got_result)
        return result 
Example #7
Source File: test_stdio.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_readConnectionLost(self):
        """
        When stdin is closed and the protocol connected to it implements
        L{IHalfCloseableProtocol}, the protocol's C{readConnectionLost} method
        is called.
        """
        errorLogFile = self.mktemp()
        log.msg("Child process logging to " + errorLogFile)
        p = StandardIOTestProcessProtocol()
        p.onDataReceived = defer.Deferred()

        def cbBytes(ignored):
            d = p.onCompletion
            p.transport.closeStdin()
            return d
        p.onDataReceived.addCallback(cbBytes)

        def processEnded(reason):
            reason.trap(error.ProcessDone)
        d = self._requireFailure(p.onDataReceived, processEnded)

        self._spawnProcess(
            p, b'stdio_test_halfclose', errorLogFile)
        return d 
Example #8
Source File: test_stdio.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_loseConnection(self):
        """
        Verify that a protocol connected to L{StandardIO} can disconnect
        itself using C{transport.loseConnection}.
        """
        errorLogFile = self.mktemp()
        log.msg("Child process logging to " + errorLogFile)
        p = StandardIOTestProcessProtocol()
        d = p.onCompletion
        self._spawnProcess(p, b'stdio_test_loseconn', errorLogFile)

        def processEnded(reason):
            # Copy the child's log to ours so it's more visible.
            with open(errorLogFile, 'r') as f:
                for line in f:
                    log.msg("Child logged: " + line.rstrip())

            self.failIfIn(1, p.data)
            reason.trap(error.ProcessDone)
        return self._requireFailure(d, processEnded) 
Example #9
Source File: test_filesystems_zfs.py    From flocker with Apache License 2.0 6 votes vote down vote up
def test_list_result_ignores_other_pools(self):
        """
        ``ZFSSnapshots.list`` skips snapshots of other pools.

        In particular, we are likely to see snapshot names of sub-pools in
        the output.
        """
        reactor = FakeProcessReactor()
        snapshots = ZFSSnapshots(reactor, Filesystem(b"mypool", None))

        d = snapshots.list()
        process_protocol = reactor.processes[0].processProtocol
        process_protocol.childDataReceived(1, b"mypool/child@name\n")
        process_protocol.childDataReceived(1, b"mypool@name2\n")
        reactor.processes[0].processProtocol.processEnded(
            Failure(ProcessDone(0)))
        self.assertEqual(self.successResultOf(d), [b"name2"]) 
Example #10
Source File: test_scriptexecution.py    From landscape-client with GNU General Public License v2.0 6 votes vote down vote up
def test_limit_time_accumulates_data(self):
        """
        Data from processes that time out should still be accumulated and
        available from the exception object that is raised.
        """
        factory = StubProcessFactory()
        self.plugin.process_factory = factory
        result = self.plugin.run_script("/bin/sh", "", time_limit=500)
        protocol = factory.spawns[0][0]
        protocol.makeConnection(DummyProcess())
        protocol.childDataReceived(1, b"hi\n")
        self.manager.reactor.advance(501)
        protocol.processEnded(Failure(ProcessDone(0)))

        def got_error(f):
            self.assertTrue(f.check(ProcessTimeLimitReachedError))
            self.assertEqual(f.value.data, "hi\n")

        result.addErrback(got_error)
        return result 
Example #11
Source File: test_util.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_stdin(self):
        """
        Making sure getPassword accepts a password from standard input by
        running a child process which uses getPassword to read in a string
        which it then writes it out again.  Write a string to the child
        process and then read one and make sure it is the right string.
        """
        p = PasswordTestingProcessProtocol()
        p.finished = Deferred()
        reactor.spawnProcess(
            p, pyExe,
            [pyExe,
             b'-c',
             (b'import sys\n'
              b'from twisted.python.util import getPassword\n'
              b'sys.stdout.write(getPassword())\n'
              b'sys.stdout.flush()\n')],
            env={b'PYTHONPATH': os.pathsep.join(sys.path).encode("utf8")})

        def processFinished(result):
            (reason, output) = result
            reason.trap(ProcessDone)
            self.assertIn((1, b'secret'), output)

        return p.finished.addCallback(processFinished) 
Example #12
Source File: test_stdio.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_consumer(self):
        """
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IConsumer} provider.
        """
        p = StandardIOTestProcessProtocol()
        d = p.onCompletion

        junkPath = self._junkPath()

        self._spawnProcess(p, b'stdio_test_consumer', junkPath)

        def processEnded(reason):
            with open(junkPath, 'rb') as f:
                self.assertEqual(p.data[1], f.read())
            reason.trap(error.ProcessDone)
        return self._requireFailure(d, processEnded) 
Example #13
Source File: test_util.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_stdin(self):
        """
        Making sure getPassword accepts a password from standard input by
        running a child process which uses getPassword to read in a string
        which it then writes it out again.  Write a string to the child
        process and then read one and make sure it is the right string.
        """
        p = PasswordTestingProcessProtocol()
        p.finished = Deferred()
        reactor.spawnProcess(
            p, pyExe,
            [pyExe,
             b'-c',
             (b'import sys\n'
              b'from twisted.python.util import getPassword\n'
              b'sys.stdout.write(getPassword())\n'
              b'sys.stdout.flush()\n')],
            env={b'PYTHONPATH': os.pathsep.join(sys.path).encode("utf8")})

        def processFinished(result):
            (reason, output) = result
            reason.trap(ProcessDone)
            self.assertIn((1, b'secret'), output)

        return p.finished.addCallback(processFinished) 
Example #14
Source File: process.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def processExited(self, failure):
        err = failure.trap(
            internet_error.ProcessDone, internet_error.ProcessTerminated)

        if err == internet_error.ProcessDone:
            pass

        elif err == internet_error.ProcessTerminated:
            self.failed = True
            self.errmsg = failure.value.exitCode
            if self.errmsg:
                self.log.debug('Process Exited, status %d' % (self.errmsg,))
            else:
                self.log.warn('%r' % failure.value)
        if IS_MAC:
            # TODO: need to exit properly!
            self.errmsg = None
        self.proto = None
        self._turn_state_off() 
Example #15
Source File: test_stdio.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_loseConnection(self):
        """
        Verify that a protocol connected to L{StandardIO} can disconnect
        itself using C{transport.loseConnection}.
        """
        errorLogFile = self.mktemp()
        log.msg("Child process logging to " + errorLogFile)
        p = StandardIOTestProcessProtocol()
        d = p.onCompletion
        self._spawnProcess(p, b'stdio_test_loseconn', errorLogFile)

        def processEnded(reason):
            # Copy the child's log to ours so it's more visible.
            with open(errorLogFile, 'r') as f:
                for line in f:
                    log.msg("Child logged: " + line.rstrip())

            self.failIfIn(1, p.data)
            reason.trap(error.ProcessDone)
        return self._requireFailure(d, processEnded) 
Example #16
Source File: test_stdio.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def test_readConnectionLost(self):
        """
        When stdin is closed and the protocol connected to it implements
        L{IHalfCloseableProtocol}, the protocol's C{readConnectionLost} method
        is called.
        """
        errorLogFile = self.mktemp()
        log.msg("Child process logging to " + errorLogFile)
        p = StandardIOTestProcessProtocol()
        p.onDataReceived = defer.Deferred()

        def cbBytes(ignored):
            d = p.onCompletion
            p.transport.closeStdin()
            return d
        p.onDataReceived.addCallback(cbBytes)

        def processEnded(reason):
            reason.trap(error.ProcessDone)
        d = self._requireFailure(p.onDataReceived, processEnded)

        self._spawnProcess(
            p, 'stdio_test_halfclose.py', errorLogFile)
        return d 
Example #17
Source File: test_stdio.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_readConnectionLost(self):
        """
        When stdin is closed and the protocol connected to it implements
        L{IHalfCloseableProtocol}, the protocol's C{readConnectionLost} method
        is called.
        """
        errorLogFile = self.mktemp()
        log.msg("Child process logging to " + errorLogFile)
        p = StandardIOTestProcessProtocol()
        p.onDataReceived = defer.Deferred()

        def cbBytes(ignored):
            d = p.onCompletion
            p.transport.closeStdin()
            return d
        p.onDataReceived.addCallback(cbBytes)

        def processEnded(reason):
            reason.trap(error.ProcessDone)
        d = self._requireFailure(p.onDataReceived, processEnded)

        self._spawnProcess(
            p, b'stdio_test_halfclose', errorLogFile)
        return d 
Example #18
Source File: test_session.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def test_processEndedWithExitCode(self):
        """
        When processEnded is called, if there is an exit code in the reason
        it should be sent in an exit-status method.  The connection should be
        closed.
        """
        self.pp.processEnded(Failure(ProcessDone(None)))
        self.assertRequestsEqual(
            [('exit-status', struct.pack('>I', 0) , False)])
        self.assertSessionClosed() 
Example #19
Source File: test_session.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def test_connectionLost(self):
        """
        When connectionLost() is called, it should call loseConnection()
        on the session channel.
        """
        self.pp.connectionLost(failure.Failure(
                ProcessDone(0))) 
Example #20
Source File: test_process.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def processEnded(self, reason):
        self.finished = 1
        if not reason.check(error.ProcessDone):
            self.failure = "process didn't terminate normally: " + str(reason)
        self.onEnded.callback(self) 
Example #21
Source File: test_stdio.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_write(self):
        """
        Verify that the C{write} method of the transport of a protocol
        connected to L{StandardIO} sends bytes to standard out.
        """
        p = StandardIOTestProcessProtocol()
        d = p.onCompletion

        self._spawnProcess(p, b'stdio_test_write')

        def processEnded(reason):
            self.assertEqual(p.data[1], b'ok!')
            reason.trap(error.ProcessDone)
        return self._requireFailure(d, processEnded) 
Example #22
Source File: test_scriptexecution.py    From landscape-client with GNU General Public License v2.0 5 votes vote down vote up
def test_user_with_attachments(self):
        uid = os.getuid()
        info = pwd.getpwuid(uid)
        username = info.pw_name
        gid = info.pw_gid

        patch_chown = mock.patch("os.chown")
        mock_chown = patch_chown.start()

        factory = StubProcessFactory()
        self.plugin.process_factory = factory

        result = self.plugin.run_script("/bin/sh", "echo hi", user=username,
                                        attachments={u"file 1": "some data"})

        self.assertEqual(len(factory.spawns), 1)
        spawn = factory.spawns[0]
        self.assertIn("LANDSCAPE_ATTACHMENTS", spawn[3])
        attachment_dir = spawn[3]["LANDSCAPE_ATTACHMENTS"].decode('ascii')
        self.assertEqual(stat.S_IMODE(os.stat(attachment_dir).st_mode), 0o700)
        filename = os.path.join(attachment_dir, "file 1")
        self.assertEqual(stat.S_IMODE(os.stat(filename).st_mode), 0o600)

        protocol = spawn[0]
        protocol.childDataReceived(1, b"foobar")
        for fd in (0, 1, 2):
            protocol.childConnectionLost(fd)
        protocol.processEnded(Failure(ProcessDone(0)))

        def check(data):
            self.assertEqual(data, "foobar")
            self.assertFalse(os.path.exists(attachment_dir))
            mock_chown.assert_has_calls(
                [mock.call(mock.ANY, uid, gid) for x in range(3)])

        def cleanup(result):
            patch_chown.stop()
            return result

        return result.addCallback(check).addBoth(cleanup) 
Example #23
Source File: test_scriptexecution.py    From landscape-client with GNU General Public License v2.0 5 votes vote down vote up
def _run_script(self, username, uid, gid, path):
        expected_uid = uid if uid != os.getuid() else None
        expected_gid = gid if gid != os.getgid() else None

        factory = StubProcessFactory()
        self.plugin.process_factory = factory

        # ignore the call to chown!
        patch_chown = mock.patch("os.chown")
        mock_chown = patch_chown.start()

        result = self.plugin.run_script("/bin/sh", "echo hi", user=username)

        self.assertEqual(len(factory.spawns), 1)
        spawn = factory.spawns[0]
        self.assertEqual(spawn[4], path)
        self.assertEqual(spawn[5], expected_uid)
        self.assertEqual(spawn[6], expected_gid)

        protocol = spawn[0]
        protocol.childDataReceived(1, b"foobar")
        for fd in (0, 1, 2):
            protocol.childConnectionLost(fd)
        protocol.processEnded(Failure(ProcessDone(0)))

        def check(result):
            mock_chown.assert_called_with()
            self.assertEqual(result, "foobar")

        def cleanup(result):
            patch_chown.stop()
            return result

        return result.addErrback(check).addBoth(cleanup) 
Example #24
Source File: test_customgraph.py    From landscape-client with GNU General Public License v2.0 5 votes vote down vote up
def test_run_timeout(self):
        filename = self.makeFile("some content")
        self.store.add_graph(123, filename, None)
        factory = StubProcessFactory()
        self.graph_manager.process_factory = factory
        result = self.graph_manager.run()

        self.assertEqual(len(factory.spawns), 1)
        spawn = factory.spawns[0]
        protocol = spawn[0]
        protocol.makeConnection(DummyProcess())
        self.assertEqual(spawn[1], filename)

        self.manager.reactor.advance(110)
        protocol.processEnded(Failure(ProcessDone(0)))

        def check(ignore):
            self.graph_manager.exchange()
            self.assertMessages(
                self.broker_service.message_store.get_pending_messages(),
                [{"data": {
                    123: {
                        "error": u"Process exceeded the 10 seconds limit",
                        "script-hash": b"9893532233caff98cd083a116b013c0b",
                        "values": []},
                    },
                  "type": "custom-graph"}])

        return result.addCallback(check) 
Example #25
Source File: test_customgraph.py    From landscape-client with GNU General Public License v2.0 5 votes vote down vote up
def _exit_process_protocol(self, protocol, stdout):
        protocol.childDataReceived(1, stdout)
        for fd in (0, 1, 2):
            protocol.childConnectionLost(fd)
        protocol.processEnded(Failure(ProcessDone(0))) 
Example #26
Source File: shutdownmanager.py    From landscape-client with GNU General Public License v2.0 5 votes vote down vote up
def processEnded(self, reason):
        """Fire back the C{result} L{Deferred}.

        C{result}'s callback will be fired with the string of data received
        from the subprocess, or if the subprocess failed C{result}'s errback
        will be fired with the string of data received from the subprocess.
        """
        if self._waiting:
            if reason.check(ProcessDone):
                self._succeed()
            else:
                self.result.errback(ShutdownFailedError(self.get_data()))
                self._waiting = False 
Example #27
Source File: stream.py    From ccs-calendarserver with Apache License 2.0 5 votes vote down vote up
def run(self):
        """Run the process.

        Returns Deferred which will eventually have errback for non-clean (exit code > 0)
        exit, with ProcessTerminated, or callback with None on exit code 0.
        """
        # XXX what happens if spawn fails?
        reactor.spawnProcess(self._protocol, self._program, self._args, env=self._env)
        del self._env
        return self._protocol.resultDeferred.addErrback(lambda _: _.trap(ti_error.ProcessDone))


#
# generatorToStream
# 
Example #28
Source File: test_stdio.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_producer(self):
        """
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IProducer} provider.
        """
        p = StandardIOTestProcessProtocol()
        d = p.onCompletion

        written = []
        toWrite = list(range(100))

        def connectionMade(ign):
            if toWrite:
                written.append(intToBytes(toWrite.pop()) + b"\n")
                proc.write(written[-1])
                reactor.callLater(0.01, connectionMade, None)

        proc = self._spawnProcess(p, b'stdio_test_producer')

        p.onConnection.addCallback(connectionMade)

        def processEnded(reason):
            self.assertEqual(p.data[1], b''.join(written))
            self.assertFalse(
                toWrite,
                "Connection lost with %d writes left to go." % (len(toWrite),))
            reason.trap(error.ProcessDone)
        return self._requireFailure(d, processEnded) 
Example #29
Source File: test_stdio.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_writeSequence(self):
        """
        Verify that the C{writeSequence} method of the transport of a
        protocol connected to L{StandardIO} sends bytes to standard out.
        """
        p = StandardIOTestProcessProtocol()
        d = p.onCompletion

        self._spawnProcess(p, b'stdio_test_writeseq')

        def processEnded(reason):
            self.assertEqual(p.data[1], b'ok!')
            reason.trap(error.ProcessDone)
        return self._requireFailure(d, processEnded) 
Example #30
Source File: test_process.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_normalTermination(self):
        cmd = self.getCommand('true')

        d = defer.Deferred()
        p = TrivialProcessProtocol(d)
        reactor.spawnProcess(p, cmd, [b'true'], env=None,
                             usePTY=self.usePTY)
        def check(ignored):
            p.reason.trap(error.ProcessDone)
            self.assertEqual(p.reason.value.exitCode, 0)
            self.assertIsNone(p.reason.value.signal)
        d.addCallback(check)
        return d