# 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
        # Start them up and assign some tags
        for inst in reservation.instances:
            inst.start()
        conn.create_tags([x.id for x in reservation.instances],
                         {"Name": "loads-br12",
                          "Project": "loads",
                          })

        # Now run the test
        pool = self._callFUT("br12")
        await pool.ready

        self.assertEqual(len(pool._instances[region]), 5)

        coll = await pool.request_instances("run_12", "12423", 5,
                                            inst_type="m1.small",
                                            region=region)
        self.assertEqual(len(coll.instances), 5)
        self.assertEqual(len(pool._instances[region]), 0)

    @gen_test
    async def test_allocate_ignores_already_assigned(self):
        region = "us-west-2"

        # First, add some instances
        conn = boto.ec2.connect_to_region(region)
        reservation = conn.run_instances("ami-1234abcd", 5,
                                         instance_type='m1.small')
        instance = reservation.instances[0]
        # Make the AMI we need available

# 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

        # Start them up and assign some tags
        for inst in reservation.instances:
            inst.start()
        conn.create_tags([x.id for x in reservation.instances],
                         {"Name": "loads-br12",
                          "Project": "loads",
                          })

        # Now run the test
        pool = self._callFUT("br12")
        await pool.ready

        self.assertEqual(len(pool._instances[region]), 5)

        coll = await pool.request_instances("run_12", "12423", 5,
                                            inst_type="m1.small",
                                            region=region)
        self.assertEqual(len(coll.instances), 5)
        self.assertEqual(len(pool._instances[region]), 0)

    @gen_test
    async def test_allocate_ignores_already_assigned(self):
        region = "us-west-2"

        # First, add some instances
        conn = boto.ec2.connect_to_region(region)
        reservation = conn.run_instances("ami-1234abcd", 5,
                                         instance_type='m1.small')
        instance = reservation.instances[0]
        # Make the AMI we need available

        conn.create_image(instance.id, "CoreOS stable")