# 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: tests/helpers.py
# def vmmaster_server_mock(port):
#     mocked_image = Mock(
#         id=1, status='active',
#         get=Mock(return_value='snapshot'),
#         min_disk=20,
#         min_ram=2,
#         instance_type_flavorid=1,
#     )
#     type(mocked_image).name = PropertyMock(
#         return_value='test_origin_1')
# 
#     with patch(
#         'core.db.Database', DatabaseMock()
#     ), patch(
#         'core.utils.init.home_dir', Mock(return_value=fake_home_dir())
#     ), patch(
#         'core.logger.setup_logging', Mock(return_value=Mock())
#     ), patch(
#         'core.sessions.SessionWorker', Mock()
#     ), patch.multiple(
#         'vmpool.platforms.OpenstackPlatforms',
#         images=Mock(return_value=[mocked_image]),
#         flavor_params=Mock(return_value={'vcpus': 1, 'ram': 2}),
#         limits=Mock(return_value={
#             'maxTotalCores': 10, 'maxTotalInstances': 10,
#             'maxTotalRAMSize': 100, 'totalCoresUsed': 0,
#             'totalInstancesUsed': 0, 'totalRAMUsed': 0}),
#     ), patch.multiple(
#         'core.utils.openstack_utils',
#         nova_client=Mock(return_value=Mock())
#     ), patch.multiple(
#         'core.db.models.OpenstackClone',
#         _wait_for_activated_service=custom_wait,
#         ping_vm=Mock(return_value=True)
#     ):
#         from vmmaster.server import VMMasterServer
#         return VMMasterServer(reactor, port)
# 
# def server_is_up(address, wait=5):
#     time_start = time.time()
#     while time.time() - time_start < wait:
#         time.sleep(0.1)
#         if get_socket(address[0], address[1]):
#             return True
# 
#     return False
# 
# def server_is_down(address, wait=5):
#     time_start = time.time()
#     while time.time() - time_start < wait:
#         time.sleep(0.1)
#         if not get_socket(address[0], address[1]):
#             return True
# 
#     return False
# 
# class BaseTestCase(unittest.TestCase):
#     def shortDescription(self):
#         return None
# 
# def get_free_port():
#     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#     s.bind(('', 0))
#     port = s.getsockname()[1]
#     s.close()
#     return port
# 
# class ServerMock(object):
#     def __init__(self, host, port):
#         self.host = host
#         self.port = port
#         self._server = BaseHTTPServer.HTTPServer((host, port), Handler)
#         self._server.timeout = 1
#         self._server.allow_reuse_address = True
#         self._thread = threading.Thread(target=self._server.serve_forever)
# 
#     def start(self):
#         self._thread.start()
# 
#     def stop(self):
#         self._server.shutdown()
#         self._server.server_close()
#         self._thread.join()


import requests
from mock import Mock, patch
from core.config import setup_config, config
from tests.helpers import (vmmaster_server_mock, server_is_up, server_is_down,
                           BaseTestCase, get_free_port, ServerMock)
        from core.db.models import Session, Provider, Endpoint




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()

        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()

# 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: tests/helpers.py
# def vmmaster_server_mock(port):
#     mocked_image = Mock(
#         id=1, status='active',
#         get=Mock(return_value='snapshot'),
#         min_disk=20,
#         min_ram=2,
#         instance_type_flavorid=1,
#     )
#     type(mocked_image).name = PropertyMock(
#         return_value='test_origin_1')
# 
#     with patch(
#         'core.db.Database', DatabaseMock()
#     ), patch(
#         'core.utils.init.home_dir', Mock(return_value=fake_home_dir())
#     ), patch(
#         'core.logger.setup_logging', Mock(return_value=Mock())
#     ), patch(
#         'core.sessions.SessionWorker', Mock()
#     ), patch.multiple(
#         'vmpool.platforms.OpenstackPlatforms',
#         images=Mock(return_value=[mocked_image]),
#         flavor_params=Mock(return_value={'vcpus': 1, 'ram': 2}),
#         limits=Mock(return_value={
#             'maxTotalCores': 10, 'maxTotalInstances': 10,
#             'maxTotalRAMSize': 100, 'totalCoresUsed': 0,
#             'totalInstancesUsed': 0, 'totalRAMUsed': 0}),
#     ), patch.multiple(
#         'core.utils.openstack_utils',
#         nova_client=Mock(return_value=Mock())
#     ), patch.multiple(
#         'core.db.models.OpenstackClone',
#         _wait_for_activated_service=custom_wait,
#         ping_vm=Mock(return_value=True)
#     ):
#         from vmmaster.server import VMMasterServer
#         return VMMasterServer(reactor, port)
# 
# def server_is_up(address, wait=5):
#     time_start = time.time()
#     while time.time() - time_start < wait:
#         time.sleep(0.1)
#         if get_socket(address[0], address[1]):
#             return True
# 
#     return False
# 
# def server_is_down(address, wait=5):
#     time_start = time.time()
#     while time.time() - time_start < wait:
#         time.sleep(0.1)
#         if not get_socket(address[0], address[1]):
#             return True
# 
#     return False
# 
# class BaseTestCase(unittest.TestCase):
#     def shortDescription(self):
#         return None
# 
# def get_free_port():
#     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#     s.bind(('', 0))
#     port = s.getsockname()[1]
#     s.close()
#     return port
# 
# class ServerMock(object):
#     def __init__(self, host, port):
#         self.host = host
#         self.port = port
#         self._server = BaseHTTPServer.HTTPServer((host, port), Handler)
#         self._server.timeout = 1
#         self._server.allow_reuse_address = True
#         self._thread = threading.Thread(target=self._server.serve_forever)
# 
#     def start(self):
#         self._thread.start()
# 
#     def stop(self):
#         self._server.shutdown()
#         self._server.server_close()
#         self._thread.join()


# Path: tests/unit/test_http_proxy.py
import requests
from mock import Mock, patch
from core.config import setup_config, config
from tests.helpers import (vmmaster_server_mock, server_is_up, server_is_down,
                           BaseTestCase, get_free_port, ServerMock)
        from core.db.models import Session, Provider, Endpoint





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()

        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))