Skip to content

Commit fc1b071

Browse files
committed
Add basic connection unit test for server/clients
1 parent 91186f0 commit fc1b071

4 files changed

Lines changed: 67 additions & 10 deletions

File tree

hyperion/lib/networking/clientInterface.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def forward_over_ssh(self):
150150

151151

152152
class RemoteSlaveInterface(BaseClient):
153-
def __init__(self, host, port, cc):
153+
def __init__(self, host, port, cc, loop_in_thread=False):
154154
"""Init remote slave interface for communication to the server at `host` on `port` with slave controller `cc`.
155155
156156
:param host: Hostname of the server to connect to
@@ -159,6 +159,9 @@ def __init__(self, host, port, cc):
159159
:type port: int
160160
:param cc: Slave manager to dispatch calls to and forward messages from
161161
:type cc: hyperion.manager.SlaveManager
162+
:param loop_in_thread: Whether to run the loop function in an extra thread. Useful for unit tests and disabled
163+
by default.
164+
:type loop_in_thread: bool
162165
"""
163166
BaseClient.__init__(self, host, port)
164167
self.cc = cc
@@ -210,7 +213,12 @@ def __init__(self, host, port, cc):
210213
'conf_reload': self.cc.reload_config
211214
}
212215
self._send_auth()
213-
self._loop()
216+
217+
if not loop_in_thread:
218+
self._loop()
219+
else:
220+
self.worker = worker = threading.Thread(target=self._loop)
221+
worker.start()
214222

215223
self.logger.debug("Shutdown complete!")
216224

hyperion/lib/networking/server.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _quit(self):
9898

9999

100100
class Server(BaseServer):
101-
def __init__(self, port, cc):
101+
def __init__(self, port, cc, loop_in_thread=False):
102102
BaseServer.__init__(self)
103103
self.port = port
104104
self.cc = cc # type: hyperion.ControlCenter
@@ -143,6 +143,13 @@ def __init__(self, port, cc):
143143

144144
self.sel.register(server, selectors.EVENT_READ, self.accept)
145145

146+
if not loop_in_thread:
147+
self._loop()
148+
else:
149+
self.worker = worker = threading.Thread(target=self._loop)
150+
worker.start()
151+
152+
def _loop(self):
146153
while self.keep_running:
147154
try:
148155
for key, mask in self.sel.select(timeout=1):

hyperion/lib/util/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class HostState(Enum):
3131
SHELL_EXECUTABLE_PATH = '/bin/bash'
3232
"""Path to shell executable"""
3333

34-
DEFAULT_TCP_PORT = "23081"
34+
DEFAULT_TCP_PORT = 23081
3535

3636
TMP_SLAVE_DIR = "/tmp/Hyperion/slave/conf"
3737
TMP_CONF_DIR = "/tmp/Hyperion/conf/"

hyperion/manager_tests.py

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import libtmux
44
import lib.util.exception
55
import os.path
6-
import time
7-
import sys
6+
from lib.networking import server, clientInterface
87
import hyperion.lib.util.exception as exceptions
9-
import hyperion.lib.util.config as config
108
from hyperion.lib.monitoring.threads import *
119

1210
is_py2 = sys.version[0] == '2'
@@ -28,8 +26,8 @@ def tearDown(self):
2826
pass
2927

3028
def test_construction(self):
31-
self.assertEqual(self.cc.config['name'], 'Unit test config')
32-
self.assertTrue(self.cc.server.has_session('Unit test config'))
29+
self.assertEqual(self.cc.config['name'], 'Unit-test-config')
30+
self.assertTrue(self.cc.server.has_session('Unit-test-config'))
3331

3432
def test_host_resolution(self):
3533
host_test_comp = self.cc.get_component_by_id('host_depends_test@localhost')
@@ -52,7 +50,7 @@ def test_component_fetch(self):
5250
self.cc.get_component_by_id('do fail')
5351

5452
lst = self.cc.get_start_all_list()
55-
self.assertEqual(lst[0].comp_id, 'tail@localhost')
53+
self.assertEqual(lst[0].comp_id, 'host_test@resolved-host')
5654

5755
wait = manager.get_component_wait(tail)
5856
self.assertEqual(wait, 0.2)
@@ -206,5 +204,49 @@ def test_stop(self):
206204
)
207205

208206

207+
class ServerClientsTests(unittest.TestCase):
208+
def setUp(self):
209+
sms = server.SlaveManagementServer()
210+
self.cc = manager.ControlCenter('%s/data/test-config.yaml' % manager.BASE_DIR, slave_server=sms)
211+
self.cc.init()
212+
213+
self.cc._establish_master_connection('localhost')
214+
self.cc._copy_config_to_remote('localhost')
215+
216+
config_path = "%s/%s.yaml" % (config.TMP_SLAVE_DIR, self.cc.config['name'])
217+
port = self.cc.slave_server.port
218+
219+
self.server = server.Server(config.DEFAULT_TCP_PORT, self.cc, loop_in_thread=True)
220+
221+
print("Starting slave manager")
222+
self.slc = slc = manager.SlaveManager(config_path)
223+
print("Starting slave interface")
224+
self.si = clientInterface.RemoteSlaveInterface('localhost', port, slc, loop_in_thread=True)
225+
print("Starting client interface")
226+
self.ci_queue = queue.Queue()
227+
self.ci = clientInterface.RemoteControllerInterface('localhost', config.DEFAULT_TCP_PORT)
228+
self.ci.add_subscriber(self.ci_queue)
229+
print("SETUP DONE")
230+
231+
def tearDown(self):
232+
try:
233+
self.ci.cleanup(True)
234+
self.server.worker.join()
235+
236+
while self.cc.server.has_session(self.cc.session_name):
237+
time.sleep(.5)
238+
239+
except SystemExit:
240+
pass
241+
242+
def test_connection(self):
243+
test_ev = events.CheckEvent('tail@localhost', config.CheckState.STOPPED)
244+
self.si.event_queue.put(test_ev)
245+
ci_msg = self.ci_queue.get(timeout=5)
246+
247+
self.assertEqual(ci_msg.comp_id, test_ev.comp_id)
248+
self.assertEqual(ci_msg.check_state, test_ev.check_state)
249+
250+
209251
if __name__ == '__main__':
210252
unittest.main()

0 commit comments

Comments
 (0)