Ticket #8011: lua.patch

File lua.patch, 2.7 KB (added by dma, 8 years ago)
  • new file nettests/experimental/lua.py

    diff --git a/nettests/experimental/lua.py b/nettests/experimental/lua.py
    new file mode 100644
    index 0000000..4c740d9
    - +  
     1from ooni import nettest
     2from ooni.utils import log
     3from twisted.internet import defer, protocol, reactor
     4from twisted.python import usage
     5
     6import os
     7
     8
     9def which(program):
     10    def is_exe(fpath):
     11        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
     12
     13    fpath, fname = os.path.split(program)
     14    if fpath:
     15        if is_exe(program):
     16            return program
     17    else:
     18        for path in os.environ["PATH"].split(os.pathsep):
     19            path = path.strip('"')
     20            exe_file = os.path.join(path, program)
     21            if is_exe(exe_file):
     22                return exe_file
     23    return None
     24
     25
     26class UsageOptions(usage.Options):
     27    optParameters = [['script', 's', '', 'The Lua script to run']]
     28
     29
     30class LuaProcessProtocol(protocol.ProcessProtocol):
     31    def __init__(self, test_case):
     32        self.test_case = test_case
     33        self.deferred = defer.Deferred()
     34
     35    def connectionMade(self):
     36        log.debug("connectionMade")
     37        self.transport.closeStdin()
     38        self.test_case.report['lua_output'] = ""
     39
     40    def outReceived(self, data):
     41        log.debug('outReceived: %s' % data)
     42        self.test_case.report['lua_output'] += data
     43
     44    def errReceived(self, data):
     45        log.err('Lua error: %s' % data)
     46        self.transport.signalProcess('KILL')
     47
     48    def processEnded(self, status):
     49        rc = status.value.exitCode
     50        log.debug('processEnded: %s, %s' % \
     51                  (rc, self.test_case.report['lua_output']))
     52        if rc == 0:
     53            self.deferred.callback(self)
     54        else:
     55            self.deferred.errback(rc)
     56
     57
     58# TODO: Maybe the Lua script requires a back-end.
     59class LuaT(nettest.NetTestCase):
     60    name = "Lua test"
     61    version = "0.1"
     62    authors = "Dominic Hamon"
     63
     64    usageOptions = UsageOptions
     65    requiredOptions = ['script']
     66
     67    def test_run_script(self):
     68        """
     69        We run the Lua script specified in the usage options and take whatever
     70        is printed to stdout as the results of the test.
     71        """
     72        processProtocol = LuaProcessProtocol(self)
     73
     74        if not which('lua'):
     75            log.err('Unable to find lua executable in PATH.')
     76            return
     77
     78        reactor.spawnProcess(processProtocol,
     79                             'lua',
     80                             args=['lua', self.localOptions['script']],
     81                             env={'HOME': os.environ['HOME']},
     82                             usePTY=True)
     83       
     84        if not reactor.running:
     85            reactor.run()
     86        return processProtocol.deferred