def test_client_code_failure():
    serialized = json.dumps(response)

    responses.add(
        responses.PUT,
        base_event['ResponseURL'],
        json=serialized,
        content_type='application/json',
        match_querystring=True
    )

    rsrc = cfn_resource.Resource()

    @rsrc.delete
    def flaky_function(*args):
        raise KeyError('Oopsie')

    rsrc(base_event.copy(), FakeLambdaContext())

    body = responses.calls[0].request.body
    reply = json.loads(body)

    assert reply['Status'] == cfn_resource.FAILED
    assert reply['StackId'] == base_event['StackId']
    assert reply['Reason'] == "Exception was raised while handling custom resource"
def test_wraps_func():
    rsrc = cfn_resource.Resource(wrap_with_nothing)
    @rsrc.delete
    def delete(event, context):
        return {'Status': cfn_resource.FAILED}
    resp = rsrc(base_event.copy(), FakeLambdaContext())
    assert resp['Status'] == 'FAILED'
def test_sends_put_request(urlmock):
    rsrc = cfn_resource.Resource()

    resp = rsrc(base_event.copy(), FakeLambdaContext())

    args = urlmock.call_args
    sent_req = args[0][0]

    assert sent_req.get_method() == 'PUT'
def test_no_override():
    rsrc = cfn_resource.Resource(wrap_with_nothing)

    event = base_event.copy()
    event['RequestType'] = 'Create'

    @rsrc.create
    def create(event, context):
        return {'Data': {'called-from': 1}}

    assert create(event, FakeLambdaContext()) == rsrc(event, FakeLambdaContext())
def test_succeeds_default():
    event = base_event.copy()
    event['PhysicalResourceId'] = 'my-existing-thing'
    event['RequestType'] = 'Update'

    rsrc = cfn_resource.Resource(wrap_with_nothing)
    resp = rsrc(event, FakeLambdaContext())
    assert resp == {
        'Status': 'SUCCESS',
        'PhysicalResourceId': 'my-existing-thing',
        'Reason': 'Life is good, man',
        'Data': {},
    }
def test_wraps_func_noresponse():

    rsrc = cfn_resource.Resource()

    event = base_event.copy()
    event['RequestType'] = 'Create'

    @rsrc.create
    def create(event, context):
        raise cfn_resource.NoResponse()

    resp = rsrc(event, FakeLambdaContext())

    assert resp is None
def test_sends_put_request():
    serialized = json.dumps(response)

    responses.add(
        responses.PUT,
        base_event['ResponseURL'],
        json=serialized,
        content_type='application/json',
        match_querystring=True
    )

    rsrc = cfn_resource.Resource()
    rsrc(base_event.copy(), FakeLambdaContext())

    assert responses.calls[0].request.method == 'PUT'
def test_double_register():
    rsrc = cfn_resource.Resource(wrap_with_nothing)

    event = base_event.copy()
    event['RequestType'] = 'Update'

    @rsrc.update
    def update(event, context):
        return {'Data': {'called-from': 1}}

    @rsrc.update
    def update_two(event, context):
        return {'Data': {'called-from': 2}}

    resp = rsrc(event, FakeLambdaContext())
    assert resp['Data'] == {'called-from': 2}
def test_client_code_failure(urlmock):
    rsrc = cfn_resource.Resource()

    @rsrc.delete
    def flaky_function(*args):
        raise KeyError('Oopsie')

    resp = rsrc(base_event.copy(), FakeLambdaContext())

    args = urlmock.call_args
    sent_req = args[0][0]

    reply = json.loads(sent_req.data)

    assert reply['Status'] == cfn_resource.FAILED
    assert reply['StackId'] == base_event['StackId']
    assert reply['Reason'] == "Exception was raised while handling custom resource"
def test_client_code_failure(cassette):
    rsrc = cfn_resource.Resource()

    @rsrc.delete
    def flaky_function(*args):
        raise KeyError('Oopsie')

    resp = rsrc(base_event.copy(), FakeLambdaContext())
    assert resp is None
    print(cassette)

    assert json.loads(cassette.requests[0].body)

    reply = json.loads(cassette.requests[0].body)

    assert cassette.requests[0].method == 'PUT'
    assert reply['Status'] == cfn_resource.FAILED
    assert reply['StackId'] == base_event['StackId']
    assert reply['Reason'] == "Exception was raised while handling custom resource"
예제 #11
0
#                                                                            #
#  or in the 'license' file accompanying this file. This file is distributed #
#  on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,        #
#  express or implied. See the License for the specific language governing   #
#  permissions and limitations under the License.                            #
##############################################################################

from __future__ import print_function

import boto3
# Unable to import module? You need to zip CRRdeployagent.py with
# cfn_resource.py!!
import cfn_resource
import json

handler = cfn_resource.Resource()

source_buckets = []

try:
    sts = boto3.client('sts')
    ec2 = boto3.client('ec2')
except Exception as e:
    print(e)
    print('Error creating sts and ec2 clients')
    raise e

local_account = sts.get_caller_identity()['Account']

# Create a hash of regions
REGIONSEL = {}
def test_sends_put_request(cassette):
    rsrc = cfn_resource.Resource()

    resp = rsrc(base_event.copy(), FakeLambdaContext())

    assert cassette.requests[0].method == 'PUT'