2017-12-22 02:11:56 +01:00
|
|
|
from .context import platypush, config_file, TestTimeoutException
|
|
|
|
|
2017-12-25 16:20:22 +01:00
|
|
|
import logging
|
2017-12-22 02:52:56 +01:00
|
|
|
import os
|
2017-12-22 02:11:56 +01:00
|
|
|
import sys
|
2017-12-25 16:20:22 +01:00
|
|
|
import time
|
2017-12-22 02:11:56 +01:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
from platypush import Daemon
|
|
|
|
from platypush.config import Config
|
|
|
|
from platypush.pusher import Pusher
|
|
|
|
from platypush.utils import set_timeout, clear_timeout
|
|
|
|
|
|
|
|
class TestLocal(unittest.TestCase):
|
2017-12-22 02:40:06 +01:00
|
|
|
""" Tests the full flow on a local backend by executing a command through
|
|
|
|
the shell.exec plugin and getting the output """
|
|
|
|
|
2017-12-22 02:11:56 +01:00
|
|
|
timeout = 5
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
|
|
|
|
backends = Config.get_backends()
|
|
|
|
self.assertTrue('local' in backends)
|
|
|
|
|
2017-12-22 02:52:56 +01:00
|
|
|
try: os.remove(Config.get_backends()['local']['request_fifo'])
|
|
|
|
except FileNotFoundError as e: pass
|
|
|
|
|
|
|
|
try: os.remove(Config.get_backends()['local']['response_fifo'])
|
|
|
|
except FileNotFoundError as e: pass
|
|
|
|
|
2017-12-22 02:11:56 +01:00
|
|
|
|
|
|
|
def test_local_shell_exec_flow(self):
|
|
|
|
self.start_sender()
|
|
|
|
self.start_receiver()
|
|
|
|
|
|
|
|
def on_response(self):
|
|
|
|
def _f(response):
|
|
|
|
logging.info("Received response: {}".format(response))
|
2017-12-22 02:40:06 +01:00
|
|
|
clear_timeout()
|
2017-12-22 02:11:56 +01:00
|
|
|
self.assertEqual(response.output.strip(), 'ping')
|
|
|
|
return _f
|
|
|
|
|
|
|
|
def on_timeout(self, msg):
|
|
|
|
def _f(): raise TestTimeoutException(msg)
|
|
|
|
return _f
|
|
|
|
|
|
|
|
def start_sender(self):
|
|
|
|
def _run_sender():
|
|
|
|
pusher = Pusher(config_file=config_file, backend='local',
|
|
|
|
on_response=self.on_response())
|
|
|
|
|
|
|
|
logging.info('Sending request')
|
2018-01-02 19:33:33 +01:00
|
|
|
pusher.send_request(target=Config.get('device_id'), action='shell.exec',
|
2017-12-22 02:11:56 +01:00
|
|
|
cmd='echo ping', timeout=None)
|
|
|
|
|
|
|
|
|
|
|
|
# Start the sender thread and wait for a response
|
|
|
|
set_timeout(seconds=self.timeout,
|
|
|
|
on_timeout=self.on_timeout('Receiver response timed out'))
|
|
|
|
|
|
|
|
self.sender = Thread(target=_run_sender)
|
|
|
|
self.sender.start()
|
|
|
|
|
|
|
|
def start_receiver(self):
|
|
|
|
set_timeout(seconds=self.timeout,
|
|
|
|
on_timeout=self.on_timeout('Sender request timed out'))
|
|
|
|
|
|
|
|
self.receiver = Daemon(config_file=config_file, requests_to_process=1)
|
|
|
|
self.receiver.start()
|
|
|
|
|
2017-12-25 16:20:22 +01:00
|
|
|
time.sleep(1)
|
|
|
|
|
2017-12-22 02:11:56 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|