# 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: core/exceptions.py
# class CreationException(Exception):
#     pass


from lode_runner import dataprovider
from mock import Mock, patch, PropertyMock, MagicMock
from tests.helpers import BaseTestCase, app_context_mock
from core.exceptions import CreationException
        from core.config import setup_config
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        - session status was changed to "preparing"
        - get_vm returns none
        - session status was changed to "waiting"
        """
        session = Mock(closed=False, endpoint_id=None, is_preparing=PropertyMock(return_value=True))
        endpoint = Mock()
        pool = Mock(get_vm=Mock(return_value=endpoint))
        endpoint_preparer = EndpointPreparer(
            pool=pool, sessions=Mock(), artifact_collector=Mock(), app_context=app_context_mock
        )
        type(endpoint_preparer).running = PropertyMock(return_value=True)

        endpoint_preparer.prepare_endpoint(session, get_endpoint_attempts=2)

        self.assertTrue(session.refresh.called)
        self.assertTrue(session.set_status.called)
        self.assertEqual(session.set_status.call_count, 2)

    def test_if_got_exception_during_getting_endpoint(self):
        """
        - session.endpoint_id wasn't set and session wasn't closed
        - call OpenstackClone.prepare_endpoint with 2 attempts

        Expected:
        - session status was changed to "preparing"
        - session was refreshed
        - exception was raised in get_vm method
        - session status was changed to "waiting"
        """
        session = MagicMock(closed=False, endpoint_id=None, is_preparing=PropertyMock(return_value=True))

# 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: core/exceptions.py
# class CreationException(Exception):
#     pass


# Path: tests/unit/test_endpoint_preparer.py
from lode_runner import dataprovider
from mock import Mock, patch, PropertyMock, MagicMock
from tests.helpers import BaseTestCase, app_context_mock
from core.exceptions import CreationException
        from core.config import setup_config
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer
        from vmpool.endpoint import EndpointPreparer

        - session status was changed to "preparing"
        - get_vm returns none
        - session status was changed to "waiting"
        """
        session = Mock(closed=False, endpoint_id=None, is_preparing=PropertyMock(return_value=True))
        endpoint = Mock()
        pool = Mock(get_vm=Mock(return_value=endpoint))
        endpoint_preparer = EndpointPreparer(
            pool=pool, sessions=Mock(), artifact_collector=Mock(), app_context=app_context_mock
        )
        type(endpoint_preparer).running = PropertyMock(return_value=True)

        endpoint_preparer.prepare_endpoint(session, get_endpoint_attempts=2)

        self.assertTrue(session.refresh.called)
        self.assertTrue(session.set_status.called)
        self.assertEqual(session.set_status.call_count, 2)

    def test_if_got_exception_during_getting_endpoint(self):
        """
        - session.endpoint_id wasn't set and session wasn't closed
        - call OpenstackClone.prepare_endpoint with 2 attempts

        Expected:
        - session status was changed to "preparing"
        - session was refreshed
        - exception was raised in get_vm method
        - session status was changed to "waiting"
        """
        session = MagicMock(closed=False, endpoint_id=None, is_preparing=PropertyMock(return_value=True))

        pool = Mock(get_vm=Mock(side_effect=CreationException))