Exemple #1
0
def get_session():
    profiler.register_get_session_call()

    dc = commands.get_desired_capabilities(request)
    matched_platform, provider_id = current_app.get_matched_platforms(dc)
    if not matched_platform:
        raise SessionException("Cannot match platform for DesiredCapabilities: {}".format(dc))

    session = Session(platform=matched_platform, dc=dc, provider_id=provider_id)
    request.session = session
    log.info("New session {}({}) on provider {} with dc: {}".format(session.id, session.name, provider_id, dc))
    yield session

    start_time = time.time()
    while not session.endpoint_id:
        time.sleep(constants.GET_SESSION_SLEEP_TIME)
        if time.time() - start_time >= config.GET_VM_TIMEOUT:
            raise CreationException("Timeout getting endpoint for {}".format(session))
        session.refresh()
        yield session

    session.run()
    yield session
Exemple #2
0
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