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)
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 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_get_vnc_proxy_port_if_session_is_waiting(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.name = "session1" session.id = 1 session.status = "waiting" session.vnc_proxy_port = None session.vnc_proxy_pid = None session.created = session.modified = datetime.now() session.endpoint = endpoint with patch('flask.current_app.sessions.get_session', Mock(return_value=session)): response = self.vmmaster_client.get( '/api/session/{}/vnc_info'.format(session.id)) body = json.loads(response.data) self.assertEqual(200, response.status_code) self.assertDictEqual({}, body['result']) self.assertEqual(500, body['metacode'])
def test_500_code_run_script_during_add_tasks(self): """ - add tasks Expected: selenium log was saved and endpoint was deleted """ from vmpool.artifact_collector import ArtifactCollector with patch( 'core.db.Database', DatabaseMock() ): from core.db.models import Session, Endpoint, Provider session = Session("origin_1") session.id = self.session_id provider = Provider(name='noname', url='nourl') endpoint = Endpoint(Mock(), '', provider) endpoint.ip = '127.0.0.1' endpoint.name = 'test_endpoint' endpoint.ports = {'selenium': '4455', 'agent': '9000', 'vnc': '5900'} session.endpoint = endpoint art_collector = ArtifactCollector(database=Mock()) in_queue = art_collector.save_artifact(session, 'selenium_server.log', '/var/log/selenium_server.log') self.assertTrue(in_queue) self.assertTrue(wait_for( lambda: len(art_collector.get_queue()) == 0)) art_collector.stop()
def test_unavailable_run_script_during_add_tasks(self): """ - add tasks Expected: selenium log was saved and endpoint was deleted """ from vmpool.artifact_collector import ArtifactCollector with patch( 'core.db.Database', DatabaseMock() ): from core.db.models import Session, Endpoint, Provider session = Session("origin_1") session.id = self.session_id log_path = os.sep.join([self.artifact_dir, 'selenium_server.log']) provider = Provider(name='noname', url='nourl') endpoint = Endpoint(Mock(), '', provider) endpoint.ip = '127.0.0.1' endpoint.name = 'test_endpoint' endpoint.ports = {'selenium': '4455', 'agent': '9000', 'vnc': '5900'} session.endpoint = endpoint self.app.sessions.get_session = Mock(return_value=session) art_collector = ArtifactCollector(database=Mock()) in_queue = art_collector.save_artifact(session, 'selenium_server.log', '/var/log/selenium_server.log') self.assertTrue(in_queue) self.assertTrue(wait_for( lambda: len(art_collector.get_queue()) == 0)) with open(log_path, 'r') as f: text = f.read() self.assertIn('Connection refused', text) art_collector.stop()
def setUp(self): from flask import Flask from core.config import setup_config setup_config('data/config_openstack.py') self.app = Flask(__name__) self.app.database = DatabaseMock() self.pool = Mock(id=1, provider=Provider(name='fake', url='no//url')) self.mocked_origin = Mock(short_name="platform_1", id=1, status="active", get=Mock(return_value="snapshot"), min_disk=20, min_ram=2, instance_type_flavorid=1) self.ctx = self.app.app_context() self.ctx.push() with patch("core.utils.openstack_utils.nova_client", Mock(return_value=Mock())), patch("flask.current_app", self.app): from core.db.models import OpenstackClone self.clone = OpenstackClone(self.mocked_origin, "preloaded", self.pool)
def test_endpoints_cleanup(self): """ - endpoint1 linked with session - endpoint2 not linked with session - both endpoints mark as 'deleted' expected: endpoint1 deleted, endpoint2 not deleted """ class FakeOrigin(str): short_name = 'fake_short_name' from core.db.models import Session, Endpoint, Provider provider = Provider('name', 'url') endpoint1 = Endpoint(origin=FakeOrigin('fake'), prefix='prefix', provider=provider) endpoint2 = Endpoint(origin=FakeOrigin('fake'), prefix='prefix', provider=provider) endpoint1.deleted, endpoint2.deleted = True, True endpoint1.save(), endpoint2.save() session = Session(platform='some_platform', name='__test_keep_forever_sessions_1') session.refresh() session.endpoint = endpoint1 session.save() endpoints_to_delete = [ e.id for e in self.cleanup.endpoints_to_delete() ] self.assertNotIn(endpoint1.id, endpoints_to_delete) self.assertIn(endpoint2.id, endpoints_to_delete)
def register_provider(self, name, url, platforms, max_limit, dbsession=None): provider = dbsession.query(Provider).filter_by(url=url).first() if provider: provider.active = True provider.name = name provider.config = platforms provider.max_limit = max_limit else: provider = Provider(name=name, url=url, config=platforms, max_limit=max_limit) self.add(provider) return provider
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 register_provider(self, name, url, platforms): from core.db.models import Provider return Provider(name, url, platforms)