# 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 os
import boto
        import loadsbroker.aws
from mock import Mock, PropertyMock, patch
from moto import mock_ec2
from tornado.testing import AsyncTestCase, gen_test
from loadsbroker.tests.util import (clear_boto_context, load_boto_context,
                                    create_image)
        from loadsbroker.broker import Broker
        from tornado.concurrent import Future
        from loadsbroker.db import Database
        from loadsbroker.db import setup_database
        from loadsbroker.broker import RunManager, RunHelpers
        from loadsbroker.extensions import (
            Docker, DNSMasq, InfluxDB, SSH, Telegraf, Watcher)
        from loadsbroker.aws import EC2Pool
        from loadsbroker.db import Plan, Run
        from loadsbroker.db import RUNNING, INITIALIZING
        from loadsbroker.db import (
            RUNNING, INITIALIZING, TERMINATING, COMPLETED
        )
        from loadsbroker.db import (
            RUNNING, INITIALIZING, TERMINATING
        )
class Test_run_manager(AsyncTestCase):

    def setUp(self):
        super().setUp()

        self.db = Database(db_uri, echo=True)
        self.db_session = self.db.session()
        setup_database(self.db_session, os.path.join(here_dir, "testdb.json"))

    def tearDown(self):
        super().tearDown()
        loadsbroker.aws.AWS_AMI_IDS = {k: {} for k in
                                       loadsbroker.aws.AWS_REGIONS}
        self.helpers = None
        self.db = None
        self.db_session = None
        if os.path.exists(file_name):
            os.remove(file_name)

    async def _createFUT(self, plan_uuid=None, run_uuid=None):

        if not plan_uuid:
            plan_uuid = self.db_session.query(Plan).limit(1).one().uuid

        region = "us-west-2"
        # Setup the AMI we need available to make instances
        conn = boto.ec2.connect_to_region(region)
        reservation = conn.run_instances('ami-1234abcd',
                                         instance_type='m1.small')
        instance = reservation.instances[0]

# 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_broker.py
import os
import boto
        import loadsbroker.aws
from mock import Mock, PropertyMock, patch
from moto import mock_ec2
from tornado.testing import AsyncTestCase, gen_test
from loadsbroker.tests.util import (clear_boto_context, load_boto_context,
                                    create_image)
        from loadsbroker.broker import Broker
        from tornado.concurrent import Future
        from loadsbroker.db import Database
        from loadsbroker.db import setup_database
        from loadsbroker.broker import RunManager, RunHelpers
        from loadsbroker.extensions import (
            Docker, DNSMasq, InfluxDB, SSH, Telegraf, Watcher)
        from loadsbroker.aws import EC2Pool
        from loadsbroker.db import Plan, Run
        from loadsbroker.db import RUNNING, INITIALIZING
        from loadsbroker.db import (
            RUNNING, INITIALIZING, TERMINATING, COMPLETED
        )
        from loadsbroker.db import (
            RUNNING, INITIALIZING, TERMINATING
        )

class Test_run_manager(AsyncTestCase):

    def setUp(self):
        super().setUp()

        self.db = Database(db_uri, echo=True)
        self.db_session = self.db.session()
        setup_database(self.db_session, os.path.join(here_dir, "testdb.json"))

    def tearDown(self):
        super().tearDown()
        loadsbroker.aws.AWS_AMI_IDS = {k: {} for k in
                                       loadsbroker.aws.AWS_REGIONS}
        self.helpers = None
        self.db = None
        self.db_session = None
        if os.path.exists(file_name):
            os.remove(file_name)

    async def _createFUT(self, plan_uuid=None, run_uuid=None):

        if not plan_uuid:
            plan_uuid = self.db_session.query(Plan).limit(1).one().uuid

        region = "us-west-2"
        # Setup the AMI we need available to make instances
        conn = boto.ec2.connect_to_region(region)
        reservation = conn.run_instances('ami-1234abcd',
                                         instance_type='m1.small')
        instance = reservation.instances[0]

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