示例#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")
示例#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")
示例#3
0
文件: aws.py 项目: sharmer156/bridgy
    def __init__(self, cache_dir, **kwargs):
        super(AwsInventory, self).__init__(cache_dir, **kwargs)
        self.cache_dir = cache_dir

        # this is an override for the config location (at least useful for testing)
        if 'config_path' in kwargs:
            os.environ['AWS_CONFIG_FILE'] = os.path.join(kwargs['config_path'], "config")
            os.environ['AWS_SHARED_CREDENTIALS_FILE'] = os.path.join(kwargs['config_path'], "credentials")

        if 'profile' in kwargs and kwargs['profile'] != None:
            session = boto3.Session(
                profile_name=kwargs['profile'],
                region_name=kwargs['region']
            )
        elif 'access_key_id' in kwargs and 'secret_access_key' in kwargs and 'session_token' in kwargs and 'region' in kwargs:
            session = boto3.Session(
                aws_access_key_id=kwargs['access_key_id'],
                aws_secret_access_key=kwargs['secret_access_key'],
                aws_session_token=kwargs['session_token'],
                region_name=kwargs['region']
            )
        else:
            # pull from ~/.aws/* configs (or other boto search paths)
            session = boto3.Session()

        self.pill = placebo.attach(session, data_path=cache_dir)

        self.client = session.client('ec2')
def test_lookup_opensearch_no_vpc():
    client_session = session.Session()
    here = path.abspath(path.dirname(__file__))
    pill = placebo.attach(session=client_session,
                          data_path=f"{here}/placebos/lookup_x_opensearch")
    # pill.record()
    pill.playback()
    # Public endpoint domain
    config = lookup_resource(
        {
            "Tags": [{
                "CreatedByComposeX": r"true"
            }, {
                "ComposeXName": r"domain-01"
            }]
        },
        session=client_session,
    )
    print(config)

    # Private (VPC) endpoint domain
    config = lookup_resource(
        {
            "Tags": [{
                "CreatedByComposeX": "true"
            }, {
                "ComposeXName": "domain-02"
            }]
        },
        session=client_session,
    )
    print(config)
示例#5
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
    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()
 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')
示例#8
0
            def __call__(fake, region=None, assume=None):
                new_session = None
                # slightly experimental for test recording, using
                # cross account assumes, note this will record sts
                # assume role api calls creds into test data, they will
                # go stale, but its best to modify before commiting.
                # Disabled by default.
                if 0 and (assume is not False and fake.assume_role):
                    client = session.client('sts')
                    creds = client.assume_role(
                        RoleArn=fake.assume_role,
                        RoleSessionName='CustodianTest')['Credentials']
                    new_session = boto3.Session(
                        aws_access_key_id=creds['AccessKeyId'],
                        aws_secret_access_key=creds['SecretAccessKey'],
                        aws_session_token=creds['SessionToken'],
                        region_name=region or fake.region or default_region)
                elif region and region != default_region:
                    new_session = boto3.Session(region_name=region)

                if new_session:
                    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
示例#9
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)
    def test_assumed_session(self):
        factory = self.replay_flight_data("test_credential_sts")
        session = assumed_session(
            role_arn='arn:aws:iam::644160558196:role/CustodianGuardDuty',
            session_name="custodian-dev",
            session=factory(),
        )

        # attach the placebo flight recorder to the new session.
        pill = placebo.attach(
            session, os.path.join(self.placebo_dir, 'test_credential_sts'))
        if self.recording:
            pill.record()
        else:
            pill.playback()
        self.addCleanup(pill.stop)

        try:
            identity = session.client("sts").get_caller_identity()
        except ClientError as e:
            self.assertEqual(e.response["Error"]["Code"], "ValidationError")

        self.assertEqual(
            identity['Arn'],
            'arn:aws:sts::644160558196:assumed-role/CustodianGuardDuty/custodian-dev'
        )
示例#11
0
文件: conftest.py 项目: Wevolver/HAVE
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
示例#12
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)
示例#13
0
文件: utils.py 项目: zappa/Zappa
    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)
示例#14
0
def placebo_session(request):
    session_kwargs = {
        "region_name": os.environ.get("AWS_DEFAULT_REGION", "eu-west-1")
    }
    profile_name = os.environ.get("PLACEBO_PROFILE", None)
    if profile_name:
        session_kwargs["profile_name"] = profile_name

    session = boto3.Session(**session_kwargs)

    prefix = request.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()

    return session
示例#15
0
文件: utils.py 项目: zombiefied/Zappa
    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)
示例#16
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
示例#17
0
def test_lookup(existing_cluster, nonexisting_cluster):
    """
    Function to test the dynamodb table lookup
    """
    here = path.abspath(path.dirname(__file__))
    session = boto3.session.Session()
    pill = placebo.attach(session, data_path=f"{here}/x_ecs")
    pill.playback()
    template = Template()
    stack = ComposeXStack("test", stack_template=template)
    settings = ComposeXSettings(
        content=existing_cluster,
        session=session,
        **{
            ComposeXSettings.name_arg: "test",
            ComposeXSettings.command_arg: ComposeXSettings.render_arg,
            ComposeXSettings.format_arg: "yaml",
        },
    )
    cluster = add_ecs_cluster(settings, stack)
    assert cluster is False

    template = Template()
    stack = ComposeXStack("test", stack_template=template)
    settings = ComposeXSettings(
        content=existing_cluster,
        session=session,
        **{
            ComposeXSettings.name_arg: "test",
            ComposeXSettings.command_arg: ComposeXSettings.render_arg,
            ComposeXSettings.format_arg: "yaml",
        },
    )
    cluster = add_ecs_cluster(settings, stack)
    assert cluster is True
示例#18
0
    def record_flight_data(self, test_case, zdata=False, augment=False):
        self.recording = True
        test_dir = os.path.join(self.placebo_dir, test_case)
        if not (zdata or augment):
            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
示例#19
0
def pill(tmpdir, session):
    '''
    Get placebo for simulate boto3 calls
    '''
    pill = placebo.attach(session, tmpdir)
    pill.playback()
    yield pill
    pill.stop()
 def _setUp(self, subfolder):
     session = boto3.Session()
     pill = placebo.attach(session,
                           data_path=os.path.join(self.PLACEBO_PATH,
                                                  subfolder))
     pill.playback()
     self.ssm_client = session.client('ssm')
     SSMParameter.set_ssm_client(self.ssm_client)
示例#21
0
def step_impl(context):
    """
    Function to test the deployment.
    """
    pill = placebo.attach(session=context.settings.session,
                          data_path=f"{here()}/cfn_cannot_update")
    pill.playback()
    context.stack_id = deploy(context.settings, context.root_stack)
示例#22
0
def test_iam_role_arn():
    case_path = "settings/role_arn"
    here = path.abspath(path.dirname(__file__))
    session = boto3.session.Session()
    pill = placebo.attach(session, data_path=f"{here}/{case_path}")
    pill.playback()

    settings = ComposeXSettings(
        content=get_basic_content(),
        session=session,
        **{
            ComposeXSettings.name_arg:
            "test",
            ComposeXSettings.command_arg:
            ComposeXSettings.render_arg,
            ComposeXSettings.input_file_arg:
            path.abspath(f"{here}/../../uses-cases/blog.yml"),
            ComposeXSettings.format_arg:
            "yaml",
            ComposeXSettings.arn_arg:
            "arn:aws:iam::012345678912:role/testx",
        },
    )
    print(settings.secrets_mappings)
    with raises(ValueError):
        ComposeXSettings(
            content=get_basic_content(),
            session=session,
            **{
                ComposeXSettings.name_arg:
                "test",
                ComposeXSettings.command_arg:
                ComposeXSettings.render_arg,
                ComposeXSettings.input_file_arg:
                path.abspath(f"{here}/../../uses-cases/blog.yml"),
                ComposeXSettings.format_arg:
                "yaml",
                ComposeXSettings.arn_arg:
                "arn:aws:iam::012345678912:roleX/testx",
            },
        )
    with raises(ClientError):
        ComposeXSettings(
            content=get_basic_content(),
            session=session,
            **{
                ComposeXSettings.name_arg:
                "test",
                ComposeXSettings.command_arg:
                ComposeXSettings.render_arg,
                ComposeXSettings.input_file_arg:
                path.abspath(f"{here}/../../uses-cases/blog.yml"),
                ComposeXSettings.format_arg:
                "yaml",
                ComposeXSettings.arn_arg:
                "arn:aws:iam::012345678912:role/test",
            },
        )
 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')
示例#24
0
def test_ecs_settings():
    session = boto3.session.Session()
    here = path.abspath(path.dirname(__file__))
    pill = placebo.attach(session=session, data_path=f"{here}/x_ecs_settings")
    pill.playback()
    set_ecs_settings(session)
    create_bucket("ecs-composex-eu-west-1", session)
    create_bucket("ecs-composex-eu-west-2", session)
    create_bucket("cfn-templates", session)
    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)
示例#26
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
示例#27
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
示例#28
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
示例#29
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()
示例#30
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
示例#31
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
示例#32
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()

        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)
示例#34
0
def boto_session(request):
    data_path = getattr(request.module, "data_path", "./testdata/placebo")

    session = boto3.session.Session()
    pill = placebo.attach(session, data_path=data_path)

    if os.getenv('PLACEBO_MODE', "playback").lower() == 'record':
        pill.record()
    else:
        pill.playback()
    yield session
示例#35
0
    def record(self, directory):
        """
        Use Placebo to record the session

        :param directory:
        :return:
        """
        if not os.path.exists(directory):
            os.makedirs(directory)
        self._recorder = placebo.attach(self.session(), data_path=directory)
        self._recorder.record()
示例#36
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)
    def test_with_placebo(self):
        """ Test that set_ssm_client works fine with Placebo """
        session = boto3.Session()
        pill = placebo.attach(session, data_path=self.PLACEBO_PATH)
        pill.playback()

        client = session.client('ssm')

        SSMParameter.set_ssm_client(client)

        param = SSMParameter("my_param")
        self.assertEqual(param.value, self.PARAM_VALUE)
    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)
示例#39
0
 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
 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
示例#41
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)
示例#42
0
    def record_flight_data(self, test_case, zdata=False, augment=False):
        self.recording = True
        test_dir = os.path.join(self.placebo_dir, test_case)
        if not (zdata or augment):
            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)
        else:
            pill = attach(session, self.archive_path, test_case, debug=True)

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

        class FakeFactory(object):
            def __call__(fake, region=None, assume=None):
                new_session = None
                # slightly experimental for test recording, using
                # cross account assumes, note this will record sts
                # assume role api calls creds into test data, they will
                # go stale, but its best to modify before commiting.
                # Disabled by default.
                if 0 and (assume is not False and fake.assume_role):
                    client = session.client('sts')
                    creds = client.assume_role(
                        RoleArn=fake.assume_role,
                        RoleSessionName='CustodianTest')['Credentials']
                    new_session = boto3.Session(
                        aws_access_key_id=creds['AccessKeyId'],
                        aws_secret_access_key=creds['SecretAccessKey'],
                        aws_session_token=creds['SessionToken'],
                        region_name=region or fake.region or default_region)
                elif region and region != default_region:
                    new_session = boto3.Session(region_name=region)

                if new_session:
                    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 FakeFactory()
示例#43
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()
示例#44
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
示例#45
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
示例#46
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)