# 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
        )
        self.helpers.telegraf.stop = zero_out

        # Ensure instances all report as done after everything
        # has been started
        async def return_true(*args, **kwargs):
            return not all([s.ec2_collection.started for s in rm._set_links])
        self.helpers.docker.is_running = return_true

        result = await rm._run()
        self.assertEqual(rm.state, TERMINATING)

        result = await rm._shutdown()
        self.assertEqual(rm.state, COMPLETED)
        self.assertEqual(result, None)

    @gen_test(timeout=20)
    async def test_abort(self):
        rm = await self._createFUT()
        self.assertEqual(rm.state, INITIALIZING)
        await rm._initialize()
        self.assertEqual(rm.state, RUNNING)
        rm.sleep_time = 0.5

        # Zero out extra calls
        async def zero_out(*args, **kwargs):
            return None
        self.helpers.ssh.reload_sysctl = zero_out
        self.helpers.dns.start = zero_out
        self.helpers.watcher.start = zero_out
        self.helpers.influxdb.start = zero_out

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

        self.helpers.telegraf.stop = zero_out

        # Ensure instances all report as done after everything
        # has been started
        async def return_true(*args, **kwargs):
            return not all([s.ec2_collection.started for s in rm._set_links])
        self.helpers.docker.is_running = return_true

        result = await rm._run()
        self.assertEqual(rm.state, TERMINATING)

        result = await rm._shutdown()
        self.assertEqual(rm.state, COMPLETED)
        self.assertEqual(result, None)

    @gen_test(timeout=20)
    async def test_abort(self):
        rm = await self._createFUT()
        self.assertEqual(rm.state, INITIALIZING)
        await rm._initialize()
        self.assertEqual(rm.state, RUNNING)
        rm.sleep_time = 0.5

        # Zero out extra calls
        async def zero_out(*args, **kwargs):
            return None
        self.helpers.ssh.reload_sysctl = zero_out
        self.helpers.dns.start = zero_out
        self.helpers.watcher.start = zero_out
        self.helpers.influxdb.start = zero_out

        self.helpers.telegraf.start = zero_out