Exemplo n.º 1
0
 def test_ec2(self):
     session = boto3.Session()
     placebo.attach(session)
     ec2_client = session.client("ec2")
     ec2_client.meta.placebo.add_response("ec2", "DescribeAddresses", addresses_result_one)
     ec2_client.meta.placebo.start()
     result = ec2_client.describe_addresses()
     self.assertEqual(result["Addresses"][0]["PublicIp"], "192.168.0.1")
     result = ec2_client.describe_addresses()
     self.assertEqual(result["Addresses"][0]["PublicIp"], "192.168.0.1")
Exemplo n.º 2
0
 def test_ec2_multiple_responses(self):
     session = boto3.Session()
     placebo.attach(session)
     ec2_client = session.client("ec2")
     ec2_client.meta.placebo.add_response("ec2", "DescribeKeyPairs", kp_result_one)
     ec2_client.meta.placebo.add_response("ec2", "DescribeKeyPairs", kp_result_two)
     ec2_client.meta.placebo.start()
     result = ec2_client.describe_key_pairs()
     self.assertEqual(result["KeyPairs"][0]["KeyName"], "foo")
     result = ec2_client.describe_key_pairs()
     self.assertEqual(result["KeyPairs"][0]["KeyName"], "bar")
     result = ec2_client.describe_key_pairs()
     self.assertEqual(result["KeyPairs"][0]["KeyName"], "bar")
Exemplo n.º 3
0
    def wrapper(*args, **kwargs):
        session_kwargs = {"region_name": os.environ.get("AWS_DEFAULT_REGION", "us-east-1")}
        profile_name = os.environ.get("PLACEBO_PROFILE", None)
        if profile_name:
            session_kwargs["profile_name"] = profile_name

        session = boto3.Session(**session_kwargs)

        self = args[0]
        prefix = self.__class__.__name__ + "." + function.__name__
        record_dir = os.path.join(PLACEBO_DIR, prefix)

        if not os.path.exists(record_dir):
            os.makedirs(record_dir)

        pill = placebo.attach(session, data_path=record_dir)

        if os.environ.get("PLACEBO_MODE") == "record":
            pill.record()
        else:
            pill.playback()

        kwargs["session"] = session

        return function(*args, **kwargs)
Exemplo n.º 4
0
    def record_flight_data(self, test_case, zdata=False):
        if not zdata:
            test_dir = os.path.join(self.placebo_dir, test_case)
            if os.path.exists(test_dir):
                shutil.rmtree(test_dir)
            os.makedirs(test_dir)

        session = boto3.Session()
        default_region = session.region_name
        if not zdata:
            pill = placebo.attach(session, test_dir, debug=True)
        else:
            pill = attach(session, self.archive_path, test_case, debug=True)

        pill.record()
        self.addCleanup(pill.stop)
        self.addCleanup(self.cleanUp)

        def factory(region=None, assume=None):
            if region and region != default_region:
                new_session = boto3.Session(region_name=region)
                assert not zdata
                new_pill = placebo.attach(new_session, test_dir, debug=True)
                new_pill.record()
                self.addCleanup(new_pill.stop)
                return new_session
            return session

        return factory
Exemplo n.º 5
0
    def replay_flight_data(self, test_case, zdata=False, region=None):
        """
        The `region` argument is to allow functional tests to override the
        default region. It is unused when replaying stored data.
        """

        if os.environ.get('C7N_FUNCTIONAL') == 'yes':
            self.recording = True
            return lambda region=region, assume=None: boto3.Session(
                region_name=region)

        if not zdata:
            test_dir = os.path.join(self.placebo_dir, test_case)
            if not os.path.exists(test_dir):
                raise RuntimeError(
                    "Invalid Test Dir for flight data %s" % test_dir)

        session = boto3.Session()
        if not zdata:
            pill = placebo.attach(session, test_dir)
        else:
            pill = attach(session, self.archive_path, test_case, False)

        pill.playback()
        self.addCleanup(pill.stop)
        self.addCleanup(self.cleanUp)
        return lambda region=None, assume=None: session
Exemplo n.º 6
0
    def wrapper(*args, **kwargs):
        session_kwargs = {
            'region_name': os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')
        }
        profile_name = os.environ.get('PLACEBO_PROFILE', None)
        if profile_name:
            session_kwargs['profile_name'] = profile_name

        session = boto3.Session(**session_kwargs)

        self = args[0]
        prefix = self.__class__.__name__ + '.' + function.__name__
        base_dir = os.environ.get(
            "PLACEBO_DIR", os.path.join(os.getcwd(), "placebo"))
        record_dir = os.path.join(base_dir, prefix)

        if not os.path.exists(record_dir):
            os.makedirs(record_dir)

        pill = placebo.attach(session, data_path=record_dir)

        if os.environ.get('PLACEBO_MODE') == 'record':
            pill.record()
        else:
            pill.playback()

        kwargs['session'] = session

        return function(*args, **kwargs)
Exemplo n.º 7
0
def aws_session():
    """Provide an aws session.

        Note:
            Inspired from the placebo.utils.placebo_session decoractor.

            Like the placebo_session decoractor some environment variables
            can be set:
                - MULTIPLE_TEST_PLACEBO_MODE : to switch the placebo mode,
                    either 'record', 'playback', or 'off', 'playback' is
                    thedefault. In 'off' mode placebo isn't used.
    """
    session = boto3.Session()

    mode = os.environ.get('MULTIPLE_TEST_PLACEBO_MODE', 'playback')

    if mode not in ('off', 'record', 'playback'):
        raise ValueError('unknow placebo mode %r', mode)

    if mode != 'off':
        directory_name = os.path.dirname(os.path.realpath(__file__))
        record_directory = os.path.join(directory_name, 'aws-data', 'records')

        if not os.path.exists(record_directory):
            os.makedirs(record_directory)

        pill = placebo.attach(session, data_path=record_directory)

        if mode == 'record':
            pill.record()
        elif mode == 'playback':
            pill.playback()

    return session
 def setup_class(self):
     self.data_path = os.path.join(os.path.dirname(__file__), 'responses')
     self.session = boto3.Session()
     self.pill = placebo.attach(self.session, data_path=self.data_path)
     self.pill.playback()
     lambda_function.client = self.session.client('lambda', region_name='us-east-1')
     lambda_function.cloudwatch = self.session.client('cloudwatch', region_name='us-east-1')
Exemplo n.º 9
0
    def __init__(self, config_file, environment=None,
                 debug=False, recording_path=None):
        if debug:
            self.set_logger('kappa', logging.DEBUG)
        else:
            self.set_logger('kappa', logging.INFO)
        self._load_cache()
        self.config = yaml.load(config_file)
        self.environment = environment

        if self.environment not in self.config.get('environments', {}):
            message = 'Invalid environment {0} specified'.format(
                self.environment)
            LOG.error(message)
            sys.exit(1)

        profile = self.config['environments'][self.environment]['profile']
        region = self.config['environments'][self.environment]['region']
        self.session = kappa.awsclient.create_session(profile, region)
        if recording_path:
            self.pill = placebo.attach(self.session, recording_path)
            self.pill.record()
        self.policy = kappa.policy.Policy(
            self, self.config['environments'][self.environment])
        self.role = kappa.role.Role(
            self, self.config['environments'][self.environment])
        self.function = kappa.function.Function(
            self, self.config['lambda'])
        if 'restapi' in self.config:
            self.restapi = kappa.restapi.RestApi(
                self, self.config['restapi'])
        else:
            self.restapi = None
        self.event_sources = []
        self._create_event_sources()
Exemplo n.º 10
0
 def factory(region=None, assume=None):
     if region and region != default_region:
         new_session = boto3.Session(region_name=region)
         assert not zdata
         new_pill = placebo.attach(new_session, test_dir, debug=True)
         new_pill.record()
         self.addCleanup(new_pill.stop)
         return new_session
     return session
Exemplo n.º 11
0
def upload_session(s3, test_app, event_loop):
    """Return a session with placebo attached to it."""
    pill = placebo.attach(s3._session, data_path=placebo_fixture("upload"))

    event_loop.run_until_complete(s3._connect(test_app))

    pill.playback()

    return pill
Exemplo n.º 12
0
def error_session(request, s3, test_app, event_loop):
    """Return a session that errs with placebo attached to it."""
    pill = placebo.attach(s3._session, data_path=placebo_fixture(request.param))

    event_loop.run_until_complete(s3._connect(test_app))

    pill.playback()

    return pill
    def setUp(self):
        session = Session(region_name='us-west-1')
        current_dir = os.path.dirname(os.path.realpath(__file__))
        test_dir = '{0}/test_data'.format(current_dir)
        pill = placebo.attach(session, test_dir)
        pill.playback()

        test_config_dir = 'tests/sit/configs'
        self.ec2_helper_placebo = EC2Helper(configs_directory=test_config_dir, session=session)
Exemplo n.º 14
0
 def test_deploy(self):
     pill = placebo.attach(self.session, self.data_path)
     pill.playback()
     os.chdir(self.prj_path)
     cfg_filepath = os.path.join(self.prj_path, 'kappa.yml')
     cfg_fp = open(cfg_filepath)
     ctx = kappa.context.Context(cfg_fp, 'dev')
     ctx.deploy()
     ctx.deploy()
    def setUp(self):
        session = Session(region_name='us-west-1')
        current_dir = os.path.dirname(os.path.realpath(__file__))
        test_dir = '{0}/test_data'.format(current_dir)
        pill = placebo.attach(session, test_dir)
        pill.playback()

        self.configs_directory = 'tests/sit/configs'
        self.cf_helper_placebo = CFHelper(configs_directory=self.configs_directory, session=session)
        self.sit_loader_placebo = SITLoader(configs_directory=self.configs_directory, session=session)
 def setUp(self):
     self.session = Session(region_name='us-west-1')
     pill = placebo.attach(self.session, 'tests/sit/test_data')
     pill.playback()
     redis_client = FakeRedis()
     jid = 123456
     redis_client.lpush('test-1-php:state.highstate', jid)
     redis_client.lpush('test-1-lb:state.highstate', jid)
     redis_client.set('test-1-php:{0}'.format(jid), '{"result": "false","return": {"test": "true"}}')
     redis_client.set('test-1-lb:{0}'.format(jid), '{"result": "false","return": {"test": "true"}}')
     self.redis_client = RedisClient()
     self.redis_client.redis_instance = redis_client
Exemplo n.º 17
0
 def setUp(self):
     self.environ = {}
     self.environ_patch = mock.patch('os.environ', self.environ)
     self.environ_patch.start()
     credential_path = os.path.join(os.path.dirname(__file__), 'cfg',
                                    'aws_credentials')
     self.environ['AWS_SHARED_CREDENTIALS_FILE'] = credential_path
     self.data_path = os.path.join(os.path.dirname(__file__), 'responses')
     self.data_path = os.path.join(self.data_path, 'saved')
     self.session = boto3.Session(profile_name='foobar',
                                  region_name='us-west-2')
     self.pill = placebo.attach(self.session, self.data_path)
Exemplo n.º 18
0
    def test_deploy_no_profile(self):
        self.environ['AWS_ACCESS_KEY_ID'] = 'foo'
        self.environ['AWS_SECRET_ACCESS_KEY'] = 'bar'
        self.session = kappa.awsclient.create_session(None, 'us-west-2')

        pill = placebo.attach(self.session, self.data_path)
        pill.playback()
        os.chdir(self.prj_path)
        cfg_filepath = os.path.join(self.prj_path, 'kappa-no-profile.yml')
        cfg_fp = open(cfg_filepath)
        ctx = kappa.context.Context(cfg_fp, 'dev')
        ctx.deploy()
        ctx.deploy()
Exemplo n.º 19
0
def main():
    '''
    Query AWS for the state of 10 EC2 instances.
    Record or playback with Placebo.
    '''
    # Command line arguments
    parser = argparse.ArgumentParser(description='A sample Placebo project.')
    parser.add_argument('--region', default='us-east-1', help='AWS region')
    parser.add_argument('--path', default='.', help='JSON recording path')
    parser.add_argument('--prefix',
                        help='Recording prefix (for multiple data sets)')

    placebo_group = parser.add_mutually_exclusive_group()
    placebo_group.add_argument('--record', action='store_true',
                               help='Record AWS calls')
    placebo_group.add_argument('--playback', action='store_true',
                               help='Playback AWS calls')
    parser.epilog = '''Pass AWS credentials via environment variables:
    $AWS_ACCESS_KEY_ID and $AWS_SECRET_ACCESS_KEY, or $AWS_PROFILE'''
    args = parser.parse_args()

    # Establish a Boto3 session
    session = boto3.Session()
    # Create a Placebo pill - use JSON files in the specified location.
    pill = placebo.attach(session, data_path=args.path, prefix=args.prefix)

    # Begin recording or playback
    if args.record:
        pill.record()
    elif args.playback:
        pill.playback()

    ec2 = session.client('ec2', region_name=args.region)

    # Get info about all instances, narrow down to a list of instance IDs
    response = ec2.describe_instances()
    reservations = response['Reservations']
    instance_lists = [reservation['Instances'] for reservation in reservations]
    instances = [instance for ilist in instance_lists for instance in ilist]
    instance_ids = [instance['InstanceId'] for instance in instances]

    # Get the instance state for each instance ID
    for instance_id in instance_ids:
        status = ec2.describe_instance_status(InstanceIds=[instance_id],
                                              IncludeAllInstances=True)
        state = status['InstanceStatuses'][0]['InstanceState']['Name']
        print instance_id + " " + state
Exemplo n.º 20
0
    def replay_flight_data(self, test_case, zdata=False):
        if not zdata:
            test_dir = os.path.join(self.placebo_dir, test_case)
            if not os.path.exists(test_dir):
                raise RuntimeError(
                    "Invalid Test Dir for flight data %s" % test_dir)

        session = boto3.Session()
        if not zdata:
            pill = placebo.attach(session, test_dir)
        else:
            pill = attach(session, self.archive_path, test_case, False)

        pill.playback()
        self.addCleanup(pill.stop)
        self.addCleanup(self.cleanUp)
        return lambda region=None, assume=None: session
Exemplo n.º 21
0
    def record_flight_data(self, test_case, zdata=False):
        if not zdata:
            test_dir = os.path.join(self.placebo_dir, test_case)
            if os.path.exists(test_dir):
                shutil.rmtree(test_dir)
            os.makedirs(test_dir)

        session = boto3.Session()
        if not zdata:
            pill = placebo.attach(session, test_dir, debug=True)
        else:
            pill = attach(session, self.archive_path, test_case, debug=True)
            
        pill.record()
        self.addCleanup(pill.stop)
        self.addCleanup(self.cleanUp)
        # return session factory
        return lambda region=None, assume=None: session
 def setUp(self):
     session = Session(region_name='us-west-1')
     current_dir = os.path.dirname(os.path.realpath(__file__))
     pill = placebo.attach(session, '{0}/test_data'.format(current_dir))
     pill.playback()
     self.rolling_deploy = RollingDeploy('stg', 'server-gms-extender', '0', 'ami-abcd1234', None, './regions.yml', stack_name='test-stack-name', session=session)