# Path: core/config.py
# def full_path(path_to_config):
# def _import(filename):
# def create_if_not_exist(directory):
#     def update(self, new_values):
# def setup_config(path_to_config):
# class Config(object):
# 
# Path: core/exceptions.py
# class SessionException(Exception):
#     pass
# 
# Path: tests/helpers.py
# class TimeoutException(Exception):
#     class Response(object):
# class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
# class ServerMock(object):
# class BaseTestCase(unittest.TestCase):
# class DatabaseMock(Mock):
# def call_in_thread_mock(f):
#     def wrapper(*args, **kwargs):
# def wait_for(condition, timeout=10):
# def request(host, method, url, headers=None, body=None):
# def request_with_drop(address, desired_caps, method=None):
# def new_session_request(client, desired_caps):
# def open_url_request(client, session, data):
# def delete_session_request(client, session):
# def get_session_request(client, session):
# def run_script(client, session, script):
# def vmmaster_label(client, session, label=None):
# def server_is_up(address, wait=5):
# def server_is_down(address, wait=5):
# def fake_home_dir():
# def get_free_port():
#     def body(self):
#     def send_reply(self, code, headers, body):
#     def do_POST(self):
#     def do_GET(self):
#     def log_error(self, format, *args):
#     def log_message(self, format, *args):
#     def log_request(self, code='-', size='-'):
#     def __init__(self, host, port):
#     def start(self):
#     def stop(self):
#     def shortDescription(self):
# def set_primary_key(_self):
#     def __init__(self, *args, **kwargs):
#     def get_active_sessions(self, provider_id=None):
#     def get_session(self, session_id):
#     def get_endpoint(self, endpoint):
#     def get_endpoints(self, *args, **kwargs):
#     def get_last_session_step(session_id):
#     def register_provider(self, name, url, platforms):
# def custom_wait(self):
# def vmmaster_server_mock(port):
# def request_mock(**kwargs):
# def wait_deferred(d, timeout=5):


import json
import time
from uuid import uuid4
from flask import Flask
from mock import Mock, patch
from multiprocessing.pool import ThreadPool
from nose.twistedtools import reactor
from core.config import setup_config, config
from core.exceptions import SessionException
from tests.helpers import server_is_up, server_is_down, \
    new_session_request, get_session_request, delete_session_request, \
    vmmaster_label, run_script, BaseTestCase, \
    DatabaseMock, custom_wait, request_mock, wait_for, open_url_request, app_context_mock
            from vmmaster.app import create_app
            from core.db.models import Session
            from core.db.models import Session
            from core.db.models import Session
            from core.db.models import Session
        from core.db.models import Session
        from core.db.models import Session
        from core.db.models import Session
        from core.db.models import Session
        from core.db.models import Session
        from core.db.models import Session
        from core.db.models import Session
        from flask import Flask
        from core.sessions import SessionWorker
            from vmmaster.server import VMMasterServer
            from vmmaster.webdriver.helpers import get_session
            'core.sessions.Sessions.get_session', Mock(return_value=session)
        ):
            response = vmmaster_label(self.vmmaster_client, session.id, "step-label")

        self.assertEqual(200, response.status_code)
        self.assertEqual(json.dumps({"value": "step-label"}), response.data)
        session._close()

    @patch('vmmaster.webdriver.helpers.swap_session', Mock())
    def test_vmmaster_no_such_platform(self):
        self.app.get_matched_platforms = Mock(return_value=(None, None))
        desired_caps = {
            'desiredCapabilities': {
                'platform': 'no_platform'
            }
        }
        response = new_session_request(self.vmmaster_client, desired_caps)
        error = json.loads(response.data).get('value').get('message')

        self.assertIn('Cannot match platform for DesiredCapabilities: {u\'platform\': u\'no_platform\'}', error)


class TestSessionWorker(BaseTestCase):
    def setUp(self):
        setup_config('data/config_openstack.py')

        self.app = Flask(__name__)
        self.app.sessions = Mock()
        self.app.sessions.app = self.app


# Path: core/config.py
# def full_path(path_to_config):
# def _import(filename):
# def create_if_not_exist(directory):
#     def update(self, new_values):
# def setup_config(path_to_config):
# class Config(object):
# 
# Path: core/exceptions.py
# class SessionException(Exception):
#     pass
# 
# Path: tests/helpers.py
# class TimeoutException(Exception):
#     class Response(object):
# class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
# class ServerMock(object):
# class BaseTestCase(unittest.TestCase):
# class DatabaseMock(Mock):
# def call_in_thread_mock(f):
#     def wrapper(*args, **kwargs):
# def wait_for(condition, timeout=10):
# def request(host, method, url, headers=None, body=None):
# def request_with_drop(address, desired_caps, method=None):
# def new_session_request(client, desired_caps):
# def open_url_request(client, session, data):
# def delete_session_request(client, session):
# def get_session_request(client, session):
# def run_script(client, session, script):
# def vmmaster_label(client, session, label=None):
# def server_is_up(address, wait=5):
# def server_is_down(address, wait=5):
# def fake_home_dir():
# def get_free_port():
#     def body(self):
#     def send_reply(self, code, headers, body):
#     def do_POST(self):
#     def do_GET(self):
#     def log_error(self, format, *args):
#     def log_message(self, format, *args):
#     def log_request(self, code='-', size='-'):
#     def __init__(self, host, port):
#     def start(self):
#     def stop(self):
#     def shortDescription(self):
# def set_primary_key(_self):
#     def __init__(self, *args, **kwargs):
#     def get_active_sessions(self, provider_id=None):
#     def get_session(self, session_id):
#     def get_endpoint(self, endpoint):
#     def get_endpoints(self, *args, **kwargs):
#     def get_last_session_step(session_id):
#     def register_provider(self, name, url, platforms):
# def custom_wait(self):
# def vmmaster_server_mock(port):
# def request_mock(**kwargs):
# def wait_deferred(d, timeout=5):


# Path: tests/unit/test_server.py
import json
import time
from uuid import uuid4
from flask import Flask
from mock import Mock, patch
from multiprocessing.pool import ThreadPool
from nose.twistedtools import reactor
from core.config import setup_config, config
from core.exceptions import SessionException
from tests.helpers import server_is_up, server_is_down, \
    new_session_request, get_session_request, delete_session_request, \
    vmmaster_label, run_script, BaseTestCase, \
    DatabaseMock, custom_wait, request_mock, wait_for, open_url_request, app_context_mock
            from vmmaster.app import create_app
            from core.db.models import Session
            from core.db.models import Session
            from core.db.models import Session
            from core.db.models import Session
        from core.db.models import Session
        from core.db.models import Session
        from core.db.models import Session
        from core.db.models import Session
        from core.db.models import Session
        from core.db.models import Session
        from core.db.models import Session
        from flask import Flask
        from core.sessions import SessionWorker
            from vmmaster.server import VMMasterServer
            from vmmaster.webdriver.helpers import get_session

            'core.sessions.Sessions.get_session', Mock(return_value=session)
        ):
            response = vmmaster_label(self.vmmaster_client, session.id, "step-label")

        self.assertEqual(200, response.status_code)
        self.assertEqual(json.dumps({"value": "step-label"}), response.data)
        session._close()

    @patch('vmmaster.webdriver.helpers.swap_session', Mock())
    def test_vmmaster_no_such_platform(self):
        self.app.get_matched_platforms = Mock(return_value=(None, None))
        desired_caps = {
            'desiredCapabilities': {
                'platform': 'no_platform'
            }
        }
        response = new_session_request(self.vmmaster_client, desired_caps)
        error = json.loads(response.data).get('value').get('message')

        self.assertIn('Cannot match platform for DesiredCapabilities: {u\'platform\': u\'no_platform\'}', error)


class TestSessionWorker(BaseTestCase):
    def setUp(self):
        setup_config('data/config_openstack.py')

        self.app = Flask(__name__)
        self.app.sessions = Mock()
        self.app.sessions.app = self.app


        self.worker = SessionWorker(self.app.sessions, app_context_mock)