def test_get_vnc_port_if_running_proxy(self): from core.db.models import Session, Endpoint, Provider provider = Provider(name='noname', url='nourl') endpoint = Endpoint(Mock(), '', provider) endpoint.ip = '127.0.0.1' endpoint.name = 'test_endpoint' endpoint.ports = {'4455': 4455, '9000': 9000, '5900': 5900} session = Session("some_platform") session.id = 1 session.name = "session1" session.status = "running" session.vnc_proxy_port = 55555 session.vnc_proxy_pid = 55555 session.created = session.modified = datetime.now() session.endpoint = endpoint session.stop_vnc_proxy = Mock() with patch('flask.current_app.sessions.get_session', Mock(return_value=session)), patch( 'core.utils.kill_process', Mock(return_value=True)): response = self.vmmaster_client.get('/api/session/%s/vnc_info' % session.id) session._close() body = json.loads(response.data) self.assertEqual(200, response.status_code) self.assertDictEqual({'vnc_proxy_port': 55555}, body['result']) self.assertEqual(200, body['metacode']) self.assertTrue(session.stop_vnc_proxy.called)
def test_when_during_proxy_starting_already_started_from_other_thread( self): from core.db.models import Session, Endpoint, Provider provider = Provider(name='noname', url='nourl') endpoint = Endpoint(Mock(), '', provider) endpoint.ip = '127.0.0.1' endpoint.name = 'test_endpoint' endpoint.ports = {'4455': 4455, '9000': 9000, '5900': 5900} session = Session("some_platform") session.id = 1 session.name = "session1" session.status = "running" type(session).vnc_proxy_port = PropertyMock( side_effect=[None, 55555, 55555]) session.vnc_proxy_pid = 55555 session.created = session.modified = datetime.now() session.endpoint = endpoint session.stop_vnc_proxy = Mock() with patch('flask.current_app.sessions.get_session', Mock(return_value=session)): response = self.vmmaster_client.get( '/api/session/{}/vnc_info'.format(session.id)) session._close() body = json.loads(response.data) self.assertEqual(200, response.status_code) self.assertDictEqual({'vnc_proxy_port': 55555}, body['result']) self.assertEqual(200, body['metacode']) self.assertTrue(session.stop_vnc_proxy.called)
class CommonCommandsTestCase(BaseTestCase): webdriver_server = None vmmaster_agent = None vnc_server = None host = 'localhost' @classmethod def setUpClass(cls): setup_config("data/config_openstack.py") body = { "sessionId": None, "desiredCapabilities": { "platform": "some_platform", "browserName": "firefox", "version": "", "javascriptEnabled": True } } session_request_body = json.dumps(body) session_request_headers = { 'content-length': '%s' % len(session_request_body), 'accept-encoding': 'identity', 'Connection': 'close', 'accept': 'application/json', 'user-agent': 'Python-urllib/2.7', 'host': '127.0.0.1:9000', 'content-type': 'application/json;charset=UTF-8', } cls.request = Mock() cls.request.method = "POST" cls.request.path = "/wd/hub/session" cls.request.headers = dict() cls.request.headers.update(session_request_headers) cls.request.data = session_request_body cls.webdriver_server = ServerMock(cls.host, get_free_port()) cls.webdriver_server.start() cls.vmmaster_agent = ServerMock(cls.host, get_free_port()) cls.vmmaster_agent.start() cls.vnc_server = ServerMock(cls.host, get_free_port()) cls.vnc_server.start() cls.app = Flask(__name__) cls.app.database = None cls.app.sessions = None cls.app.database_task_queue = Mock() cls.app.pool = Mock() def setUp(self): self.ctx = self.app.test_request_context() self.ctx.push() with patch('flask.current_app.database', DatabaseMock()), patch('flask.current_app.sessions', Mock()): from core.db.models import Session, Provider, Endpoint self.session = Session('origin_1') self.session.name = "session1" provider = Provider(name='noname', url='nourl') vm = Endpoint(Mock(), '', provider) vm.name = 'vm1' vm.ip = self.host vm.ports = { 'selenium': self.webdriver_server.port, 'agent': self.vmmaster_agent.port, 'vnc': self.vnc_server.port } self.session.endpoint = vm self.session.run() from vmmaster.webdriver import commands self.commands = commands def tearDown(self): with patch('flask.current_app.sessions', Mock()), patch('flask.current_app.database', Mock()): self.session._close() self.ctx.pop() @classmethod def tearDownClass(cls): cls.webdriver_server.stop() cls.vmmaster_agent.stop() cls.vnc_server.stop() del cls.app
class TestHttpProxy(BaseTestCase): def setUp(self): setup_config('data/config_openstack.py') self.host = "localhost" self.port = config.PORT self.address = (self.host, self.port) self.vmmaster = vmmaster_server_mock(self.port) self.assertTrue(server_is_up(self.address)) self.free_port = get_free_port() self.ctx = self.vmmaster.app.app_context() self.ctx.push() from core.db.models import Session, Provider, Endpoint self.session = Session('some_platform') provider = Provider(name='noname', url='nourl') endpoint = Endpoint(Mock(), '', provider) endpoint.ip = 'localhost' self.session.endpoint = endpoint def tearDown(self): self.session._close() self.ctx.pop() self.vmmaster.app.sessions.kill_all() self.vmmaster.app.cleanup() self.vmmaster.stop_services() self.assertTrue(server_is_down(self.address)) def test_proxy_successful(self): server = ServerMock(self.host, self.free_port) server.start() with patch( 'core.sessions.Sessions.get_session', Mock( return_value=self.session) ): response = requests.get( "http://%s:%s/proxy/session/%s/port/%s/" % (self.host, self.port, self.session.id, self.free_port) ) server.stop() self.assertEqual("ok", response.content) def test_proxy_responses_when_trying_to_connect_failed(self): with patch( 'core.sessions.Sessions.get_session', Mock( return_value=self.session) ): response = requests.get( "http://%s:%s/proxy/session/%s/port/%s/" % (self.host, self.port, self.session.id, self.free_port) ) self.assertEqual( "Request forwarding failed:\n" "Connection was refused by other side: 111: Connection refused.", response.content) def test_proxy_to_session_that_was_closed(self): self.session.succeed() with patch( 'flask.current_app.database.get_session', Mock(return_value=self.session) ): response = requests.get( "http://%s:%s/proxy/session/%s/port/%s/" % (self.host, self.port, self.session.id, self.free_port) ) self.assertIn( "Session {}(Success) already closed earlier".format(self.session.id), response.content ) def test_proxy_with_wrong_path(self): response = requests.get( "http://%s:%s/proxy/asdf/%s/" % (self.host, self.port, self.free_port) ) self.assertEqual( "Couldn't parse request uri, " "make sure you request uri has " "/proxy/session/<session_id>/port/<port_number>/<destination>", response.content)