From 5d2abce4a0e8e0f94e02c75cbea88132aaf19322 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sun, 23 Jun 2019 11:11:20 +0200 Subject: CtlClient infrastructure --- pyapd/ctl.py | 23 +++++++++++++++++++++-- tests/test_ctl.py | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/pyapd/ctl.py b/pyapd/ctl.py index fa86ccd..b7021ec 100644 --- a/pyapd/ctl.py +++ b/pyapd/ctl.py @@ -7,8 +7,7 @@ import tornado.netutil import tornado.ioloop import tornado.iostream -from . import objects -from . import app as apd_app +from . import objects, config class Commands(): @@ -71,3 +70,23 @@ class CtlServer(): res = "syntax error" # TODO: wrap res in a sensible json return res + + +class CtlClient(): + def __init__(self, conf: config.Config): + self.config = conf + + def send_command(self, cmd): + print("Sending command", cmd) + s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + s.connect(self.config.ctl_socket) + s.send(json.dumps(cmd).encode()) + + def ping(self): + return {"command": "ping"} + + def add_object(self, obj: str): + return { + "command": "add_object", + "obj": obj, + } diff --git a/tests/test_ctl.py b/tests/test_ctl.py index f58ce5a..ed6a5e4 100644 --- a/tests/test_ctl.py +++ b/tests/test_ctl.py @@ -1,5 +1,6 @@ import os import tempfile +import unittest from tornado.testing import AsyncTestCase, gen_test @@ -40,3 +41,23 @@ class TestCtlServerCommands(AsyncTestCase): )) self.assertIn('ok', res) self.assertEqual(len(self.ctl_server.app.store.objects), 1) + + +class TestCtlClient(unittest.TestCase): + def setUp(self): + self.client = ctl.CtlClient( + config.Config('tests/data/test_config.yaml') + ) + + def test_cmd_ping(self): + cmd = self.client.ping() + self.assertEqual(cmd, {"command": "ping"}) + + def test_cmd_add_object(self): + obj = { + "id": "http://example.org/123456", + "type": "object", + } + cmd = self.client.add_object(obj) + self.assertEqual(cmd['command'], 'add_object') + self.assertEqual(cmd['obj'], obj) -- cgit v1.2.3