This is an automated email from the git hooks/post-receive script. x2go pushed a commit to branch master in repository x2gobroker. commit ebf77180885f59b9febd46afe4c4eb39a3edd894 Author: Mike Gabriel <mike.gabriel@das-netzwerkteam.de> Date: Thu Nov 6 05:54:59 2014 +0100 Add tests for broker agent queries. --- debian/changelog | 1 + x2gobroker/tests/test_broker_agent.py | 255 +++++++++++++++++++++++++++++++++ 2 files changed, 256 insertions(+) diff --git a/debian/changelog b/debian/changelog index 37fe2a8..2c1aeee 100644 --- a/debian/changelog +++ b/debian/changelog @@ -189,6 +189,7 @@ x2gobroker (0.0.3.0-0x2go1) UNRELEASED; urgency=low for authentication requests. - Get several issues around select_session fixed via tests in the broker's backend base.py. + - Add tests for broker agent queries. * debian/control: + Provide separate bin:package for SSH brokerage: x2gobroker-ssh. + Replace LDAP support with session brokerage support in LONG_DESCRIPTION. diff --git a/x2gobroker/tests/test_broker_agent.py b/x2gobroker/tests/test_broker_agent.py new file mode 100644 index 0000000..6eefa02 --- /dev/null +++ b/x2gobroker/tests/test_broker_agent.py @@ -0,0 +1,255 @@ +# -*- coding: utf-8 -*- + +# Copyright (C) 2012-2014 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de> +# +# X2Go Session Broker is free software; you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# X2Go Session Broker is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program; if not, write to the +# Free Software Foundation, Inc., +# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. + +import unittest +import tempfile +import copy + +# Python X2GoBroker modules +import x2gobroker.brokers.inifile_broker as inifile +import x2gobroker.defaults + +class TestX2GoBrokerAgent(unittest.TestCase): + + # TEST INTERPRETATION OF REPLIES FROM (FAKED) BROKER AGENT + + def test_broker_agent_replies(self): + _save_icmp_ping = x2gobroker.agent.icmp_ping + _save_local_broker_agent_call = x2gobroker.agent._call_local_broker_agent + _save_remote_broker_agent_call = x2gobroker.agent._call_remote_broker_agent + + def _call_testsuite_broker_agent(username, task, cmdline_args=[], remote_agent=None): + + if task == 'listsessions': + list_sessions = [] + if username == 'foo1R': + list_sessions = ['30342|foo1R-50-1414759661_stDMATE_dp24|50|host1|R|2014-10-31T13:47:41|c02c7bbe58677a2726f7e456cb398ae4|127.0.0.1|30001|30002|2014-10-31T13:47:43|foo1R|34|30003|-1|-1', + ] + elif username == 'foo1S': + list_sessions = ['30342|foo1S-50-1414759661_stDMATE_dp24|50|host1|S|2014-10-31T13:47:41|c02c7bbe58677a2726f7e456cb398ae4|127.0.0.1|30001|30002|2014-10-31T13:47:43|foo1S|34|30003|-1|-1', + ] + elif username == 'foo1N': + list_sessions = ['30342|foo1N-50-1414759661_stDMATE_dp24|50|host2|S|2014-10-31T13:47:41|c02c7bbe58677a2726f7e456cb398ae4|127.0.0.1|30001|30002|2014-10-31T13:47:43|foo1N|34|30003|-1|-1', + ] + elif username == 'foo2RS': + # the session on host2 is older than the session on host1!!! + list_sessions = ['30342|foo2RS-50-1414759789_stDMATE_dp24|50|host2|S|2014-10-31T13:49:51|c02c7bbe58677a2726f7e456cb398ae4|127.0.0.1|30001|30002|2014-10-31T13:49:51|foo2RS|89|30003|-1|-1', + '23412|foo2RS-50-1414759661_stDMATE_dp24|50|host1|R|2014-10-31T13:47:43|fasd7asd58677a2726f7e456cb398ae4|127.0.0.1|30001|30002|2014-10-31T13:47:43|foo2RS|34|30003|-1|-1', + ] + elif username == 'foo3RS': + # the session on host2 is older than the session on host1!!! + list_sessions = ['30342|foo3RS-50-1414759789_stDMATE_dp24|50|host2|S|2014-10-31T13:49:51|c02c7bbe58677a2726f7e456cb398ae4|127.0.0.1|30001|30002|2014-10-31T13:49:51|foo3RS|89|30003|-1|-1', + '23412|foo3RS-50-1414759661_stDMATE_dp24|50|host1|R|2014-10-31T13:47:43|fasd7asd58677a2726f7e456cb398ae4|127.0.0.1|30001|30002|2014-10-31T13:47:43|foo3RS|34|30003|-1|-1', + ] + return True, list_sessions + + elif task == 'suspendsession': + return True + + return False, [] + + def _fake_icmp_ping(hostname): + return True + + x2gobroker.agent._call_local_broker_agent = _call_testsuite_broker_agent + x2gobroker.agent._call_remote_broker_agent = _call_testsuite_broker_agent + x2gobroker.agent.icmp_ping = _fake_icmp_ping + + _session_profiles = """ +[DEFAULT] +command = MATE +user = foo +broker-agent-query-mode = NONE + +[testprofile1] +name = testprofile1 +host = host1 +broker-agent-query-mode = LOCAL + +[testprofile2] +name = testprofile2 +host = host1, host2, host3, host4 +broker-agent-query-mode = SSH + +[testprofile3] +name = testprofile3 +host = host1.mydomain, host2.mydomain, host3.mydomain, host4.mydomain, host5.mydomain, host6.mydomain +broker-agent-query-mode = LOCAL + +[testprofile4] +name = testprofile4 +host = host1.mydomain, host2.yourdomain + +[testprofile5] +name = testprofile5 +host = host1.mydomain (10.0.2.4), host2.mydomain (10.0.2.5) +broker-agent-query-mode = SSH + +[testprofile6] +name = testprofile6 +host = host1.mydomain (10.0.2.4), host2.mydomain (10.0.2.5) +sshport = 23467 +broker-agent-query-mode = SSH + +[testprofile7] +name = testprofile7 +host = docker-vm-1 (docker-server:22001), docker-vm-2 (docker-server:22002) +broker-agent-query-mode = SSH + +[testprofile8] +name = testprofile8 +host = docker-vm-0 (localhost), docker-vm-1 (localhost:22001), docker-vm-2 (localhost:22002) +sshport = 22000 + +""" + tf = tempfile.NamedTemporaryFile() + print >> tf, _session_profiles + tf.seek(0) + inifile_backend = inifile.X2GoBroker(profile_config_file=tf.name) + + # test with 'testprofile1' + + # the faked broker agent should report a running session + _list1 = inifile_backend.list_profiles(username='foo1R') + _profile1 = _list1['testprofile1'] + self.assertTrue( ( _profile1['host'] == ['host1'] ) ) + self.assertTrue( ( _profile1.has_key('status') and _profile1['status'] == 'R' ) ) + # here will be a short pause, as we will try to suspend the faked X2Go Session + _session1 = inifile_backend.select_session('testprofile1', username='foo1R') + # the session broker has detected the running session on host 1 and changed its status to "S" + self.assertEqual ( _session1, {'port': 22, 'server': 'host1', 'session_info': '30342|foo1R-50-1414759661_stDMATE_dp24|50|host1|S|2014-10-31T13:47:41|c02c7bbe58677a2726f7e456cb398ae4|127.0.0.1|30001|30002|2014-10-31T13:47:43|foo1R|34|30003|-1|-1', } ) + + # the faked broker agent should report a suspended session + _list1 = inifile_backend.list_profiles(username='foo1S') + _profile1 = _list1['testprofile1'] + self.assertTrue( ( _profile1['host'] == ['host1'] ) ) + self.assertTrue( ( _profile1.has_key('status') and _profile1['status'] == 'S' ) ) + _session1 = inifile_backend.select_session('testprofile1') + self.assertEqual ( _session1, {'port': 22, 'server': 'host1'} ) + + # the faked broker agent should report a suspended session for host2, nothing for host1 + _list1 = inifile_backend.list_profiles(username='foo1N') + _profile1 = _list1['testprofile1'] + self.assertTrue( ( _profile1['host'] == ['host1'] ) ) + self.assertFalse( ( _profile1.has_key('status') ) ) + _session1 = inifile_backend.select_session('testprofile1') + self.assertEqual ( _session1, {'port': 22, 'server': 'host1'} ) + + # test "testprofile2", always resume suspended sessions first + + # the faked broker agent should report a suspended session for host1, a running session for host2 + # we resume host1 (the broker attempts resumption of suspended sessions, then the take-over of running sessions + _list2 = inifile_backend.list_profiles(username='foo2RS') + _profile2 = _list2['testprofile2'] + _profile2['host'].sort() + self.assertTrue( ( _profile2['host'] == ['host2'] ) ) + self.assertTrue( ( _profile2.has_key('status') and _profile2['status'] == 'S' ) ) + _session2 = inifile_backend.select_session('testprofile2', 'foo2RS') + self.assertEqual ( _session2, {'port': 22, 'server': 'host2', 'session_info': '30342|foo2RS-50-1414759789_stDMATE_dp24|50|host2|S|2014-10-31T13:49:51|c02c7bbe58677a2726f7e456cb398ae4|127.0.0.1|30001|30002|2014-10-31T13:49:51|foo2RS|89|30003|-1|-1', } ) + + # test "testprofile3", given host names with a subdomain, but subdomains not in listsessions output + + # the faked broker agent should report a suspended session for host1, a running session for host2 + # we resume host1 (the broker attempts resumption of suspended sessions, then the take-over of running sessions + _list3 = inifile_backend.list_profiles(username='foo3RS') + _profile3 = _list3['testprofile3'] + _profile3['host'].sort() + self.assertTrue( ( _profile3['host'] == ['host2'] ) ) + self.assertTrue( ( _profile3.has_key('status') and _profile3['status'] == 'S' ) ) + _session3 = inifile_backend.select_session('testprofile3', 'foo3RS') + self.assertEqual ( _session3, {'port': 22, 'server': 'host2.mydomain', 'session_info': '30342|foo3RS-50-1414759789_stDMATE_dp24|50|host2|S|2014-10-31T13:49:51|c02c7bbe58677a2726f7e456cb398ae4|127.0.0.1|30001|30002|2014-10-31T13:49:51|foo3RS|89|30003|-1|-1', } ) + _session3 = inifile_backend.select_session('testprofile3', 'foo3RS') + +# # test "testprofile4" +# _profile4 = inifile_backend.get_profile('testprofile4') +# _profile4['host'].sort() +# self.assertTrue( ( _profile4['host'] == ['host1.mydomain', 'host2.yourdomain'] ) ) +# for key in _profile4.keys(): +# self.assertFalse( (key.startswith('host') and key != 'host' ) ) +# _session4 = inifile_backend.select_session('testprofile4') +# self.assertTrue ( _session4['port'] == 22 ) +# self.assertTrue ( _session4['server'] in ('host1.mydomain', 'host2.yourdomain') ) + + # test "testprofile5", test if canonical hostnames and real hostnames differ + + _list5 = inifile_backend.list_profiles(username='foo5N') + _profile5 = _list5['testprofile5'] + _profile5['host'].sort() + self.assertTrue( _profile5['host'][0] in ('host1.mydomain', 'host2.mydomain') ) + self.assertTrue( not _profile5.has_key('status') ) + _remoteagent5 = inifile_backend.get_remote_agent('testprofile5') + self.assertTrue( _remoteagent5 == {u'hostname': '10.0.2.4', u'port': 22} or _remoteagent5 == {u'hostname': '10.0.2.5', u'port': 22} ) + _session5 = inifile_backend.select_session('testprofile5', 'foo5N') + self.assertTrue( _session5 == {'port': 22, 'server': '10.0.2.4', } or _session5 == {'port': 22, 'server': '10.0.2.5', } ) + + # test "testprofile6", test if canonical hostnames and real hostnames differ and the default + # SSH port has been adapted + + _list6 = inifile_backend.list_profiles(username='foo6N') + _profile6 = _list6['testprofile6'] + _profile6['host'].sort() + self.assertTrue( _profile6['host'][0] in ('host1.mydomain', 'host2.mydomain') ) + self.assertTrue( not _profile6.has_key('status') ) + _remoteagent6 = inifile_backend.get_remote_agent('testprofile6') + self.assertTrue( _remoteagent6 == {u'hostname': '10.0.2.4', u'port': 23467} or _remoteagent6 == {u'hostname': '10.0.2.5', u'port': 23467} ) + _session6 = inifile_backend.select_session('testprofile6', 'foo6N') + self.assertTrue( _session6 == {'port': 23467, 'server': '10.0.2.4', } or _session6 == {'port': 23467, 'server': '10.0.2.5', } ) + + _list7 = inifile_backend.list_profiles(username='foo7N') + _profile7 = _list7['testprofile7'] + _profile7['host'].sort() + self.assertTrue( _profile7['host'][0] in ('docker-vm-1', 'docker-vm-2') ) + self.assertTrue( not _profile7.has_key('status') ) + _remoteagent7 = inifile_backend.get_remote_agent('testprofile7') + self.assertTrue( _remoteagent7 == {u'hostname': 'docker-server', u'port': 22001} or _remoteagent7 == {u'hostname': 'docker-server', u'port': 22002} ) + _session7 = inifile_backend.select_session('testprofile7', 'foo7N') + self.assertTrue( _session7 == {'port': 22001, 'server': 'docker-server', } or _session7 == {'port': 22001, 'server': 'docker-server', } ) + +# # test "testprofile8" +# _profile8 = inifile_backend.get_profile('testprofile8') +# _profile8['host'].sort() +# self.assertTrue( ( _profile8['host'] == ['docker-vm-0', 'docker-vm-1', 'docker-vm-2'] ) ) +# self.assertTrue( _profile8.has_key('host=docker-vm-0') ) +# self.assertTrue( _profile8.has_key('host=docker-vm-1') ) +# self.assertTrue( _profile8.has_key('host=docker-vm-2') ) +# self.assertTrue( _profile8['host=docker-vm-0'] == 'localhost' ) +# self.assertTrue( _profile8['host=docker-vm-1'] == 'localhost' ) +# self.assertTrue( _profile8['host=docker-vm-2'] == 'localhost' ) +# self.assertTrue( _profile8['sshport'] == 22000 ) +# self.assertFalse( _profile8.has_key('sshport=docker-vm-0') ) +# self.assertTrue( _profile8['sshport=docker-vm-1'] == 22001 ) +# self.assertTrue( _profile8['sshport=docker-vm-2'] == 22002 ) +# i = 0 +# while i < 10: +# _session8 = inifile_backend.select_session('testprofile8', username='foo') +# self.assertTrue ( _session8['port'] in (22000, 22001, 22002) ) +# self.assertTrue ( _session8['server'] == 'localhost' ) +# i += 1 + + x2gobroker.agent._call_local_broker_agent = _save_local_broker_agent_call + x2gobroker.agent._call_remote_broker_agent = _save_remote_broker_agent_call + x2gobroker.agent.icmp_ping = _save_icmp_ping + + +def test_suite(): + from unittest import TestSuite, makeSuite + suite = TestSuite() + suite.addTest(makeSuite(TestX2GoBrokerAgent)) + return suite + -- Alioth's /srv/git/_hooks_/post-receive-email on /srv/git/code.x2go.org/x2gobroker.git