# Path: loadsbroker/tests/util.py
# def clear_boto_context():
#     endpoints = os.environ.get('BOTO_ENDPOINTS')
#     if endpoints is not None:
#         del os.environ['BOTO_ENDPOINTS']
#     s = StringIO()
#     boto.config.write(s)
#     s.seek(0)
#     boto.config.clear()
#     return s.read(), endpoints
# 
# def load_boto_context(config, endpoints=None):
#     s = StringIO()
#     s.write(config)
#     boto.config.clear()
#     boto.config.read(s)
#     if endpoints is not None:
#         os.environ['BOTO_ENDPOINTS'] = endpoints
# 
# def create_image(region="us-west-2"):
#     conn = boto.ec2.connect_to_region(region)
#     reservation = conn.run_instances('ami-1234abcd')
#     instance = reservation.instances[0]
#     conn.create_image(instance.id, "Core OS stable")


import unittest
import boto
        import loadsbroker.aws
        import loadsbroker.aws
        import loadsbroker.aws
        import loadsbroker.aws
        import loadsbroker.aws
        import loadsbroker.aws
        import loadsbroker.aws
from datetime import datetime, timedelta
from tornado.testing import AsyncTestCase, gen_test
from moto import mock_ec2
from freezegun import freeze_time
from loadsbroker.tests.util import (clear_boto_context, load_boto_context,
                                    create_image)
        from loadsbroker.aws import available_instance
        from loadsbroker.aws import EC2Collection
        from loadsbroker.aws import EC2Pool
class Test_ec2_pool(AsyncTestCase):
    def setUp(self):
        super().setUp()
        # Nuke the backend
        nuke_backend()

    def _callFUT(self, broker_id, **kwargs):
        kwargs["io_loop"] = self.io_loop
        kwargs["use_filters"] = False
        return EC2Pool(broker_id, **kwargs)

    @gen_test
    async def test_empty_pool(self):
        pool = self._callFUT("br12")
        # Wait for initialization to finish
        await pool.ready
        self.assertEqual(pool._recovered, {})
        for _, val in pool._instances.items():
            self.assertEqual(val, [])

    @gen_test(timeout=10)
    async def test_recovered_instances(self):
        # First, add some instances
        first_region = loadsbroker.aws.AWS_REGIONS[0]
        conn = boto.ec2.connect_to_region(first_region)
        reservation = conn.run_instances("ami-1234abcd", 5,
                                         instance_type='m1.small')

        # Start them up and assign some tags
        for inst in reservation.instances:

# Path: loadsbroker/tests/util.py
# def clear_boto_context():
#     endpoints = os.environ.get('BOTO_ENDPOINTS')
#     if endpoints is not None:
#         del os.environ['BOTO_ENDPOINTS']
#     s = StringIO()
#     boto.config.write(s)
#     s.seek(0)
#     boto.config.clear()
#     return s.read(), endpoints
# 
# def load_boto_context(config, endpoints=None):
#     s = StringIO()
#     s.write(config)
#     boto.config.clear()
#     boto.config.read(s)
#     if endpoints is not None:
#         os.environ['BOTO_ENDPOINTS'] = endpoints
# 
# def create_image(region="us-west-2"):
#     conn = boto.ec2.connect_to_region(region)
#     reservation = conn.run_instances('ami-1234abcd')
#     instance = reservation.instances[0]
#     conn.create_image(instance.id, "Core OS stable")


# Path: loadsbroker/tests/test_units/test_aws.py
import unittest
import boto
        import loadsbroker.aws
        import loadsbroker.aws
        import loadsbroker.aws
        import loadsbroker.aws
        import loadsbroker.aws
        import loadsbroker.aws
        import loadsbroker.aws
from datetime import datetime, timedelta
from tornado.testing import AsyncTestCase, gen_test
from moto import mock_ec2
from freezegun import freeze_time
from loadsbroker.tests.util import (clear_boto_context, load_boto_context,
                                    create_image)
        from loadsbroker.aws import available_instance
        from loadsbroker.aws import EC2Collection
        from loadsbroker.aws import EC2Pool

class Test_ec2_pool(AsyncTestCase):
    def setUp(self):
        super().setUp()
        # Nuke the backend
        nuke_backend()

    def _callFUT(self, broker_id, **kwargs):
        kwargs["io_loop"] = self.io_loop
        kwargs["use_filters"] = False
        return EC2Pool(broker_id, **kwargs)

    @gen_test
    async def test_empty_pool(self):
        pool = self._callFUT("br12")
        # Wait for initialization to finish
        await pool.ready
        self.assertEqual(pool._recovered, {})
        for _, val in pool._instances.items():
            self.assertEqual(val, [])

    @gen_test(timeout=10)
    async def test_recovered_instances(self):
        # First, add some instances
        first_region = loadsbroker.aws.AWS_REGIONS[0]
        conn = boto.ec2.connect_to_region(first_region)
        reservation = conn.run_instances("ami-1234abcd", 5,
                                         instance_type='m1.small')

        # Start them up and assign some tags
        for inst in reservation.instances:

            inst.start()