Exemplo n.º 1
0
def test_sample_lambda_nodejs_with_env(awsclient, vendored_folder,
                                       cleanup_lambdas_deprecated, cleanup_roles):
    log.info('running test_sample_lambda_nodejs_with_env')
    lambda_folder = './resources/sample_lambda_nodejs_with_env/'

    temp_string = utils.random_string()
    lambda_name = 'jenkins_test_sample-lambda-nodejs6_10_' + temp_string
    role_name = 'unittest_%s_lambda' % temp_string
    role_arn = create_lambda_role_helper(awsclient, role_name)

    # create the function
    create_lambda_helper(awsclient, lambda_name, role_arn,
                         here(lambda_folder + 'index.js'),
                         lambda_handler='index.handler',
                         folders_from_file=[],
                         runtime='nodejs6.10',
                         environment={"MYVALUE": "FOO"}
                         )

    cleanup_roles.append(role_name)
    cleanup_lambdas_deprecated.append(lambda_name)

    payload = '{"ramuda_action": "getenv"}'  # provided by our test sample
    result = invoke(awsclient, lambda_name, payload)
    env = json.loads(result)
    assert 'MYVALUE' in env
    assert env['MYVALUE'] == 'FOO'
Exemplo n.º 2
0
def test_lambda_add_invoke_permission(awsclient, vendored_folder, temp_bucket,
                                      cleanup_lambdas, cleanup_roles):
    log.info('running test_lambda_add_invoke_permission')
    temp_string = helpers.random_string()
    # print(temp_string)
    lambda_name = 'jenkins_test_%s' % temp_string
    role_name = 'unittest_%s_lambda' % temp_string
    role_arn = create_lambda_role_helper(awsclient, role_name)
    cleanup_roles.append(role_name)
    create_lambda_helper(awsclient,
                         lambda_name,
                         role_arn,
                         './resources/sample_lambda/handler_counter.py',
                         lambda_handler='handler_counter.handle')
    cleanup_lambdas.append(lambda_name)
    bucket_name = temp_bucket

    s3_arn = 'arn:aws:s3:::' + bucket_name
    response = _lambda_add_invoke_permission(awsclient, lambda_name,
                                             's3.amazonaws.com', s3_arn)

    # {"Statement":"{\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:s3:::unittest-lambda-s3-bucket-coedce\"}},\"Action\":[\"lambda:InvokeFunction\"],\"Resource\":\"arn:aws:lambda:eu-west-1:188084614522:function:jenkins_test_coedce:ACTIVE\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"s3.amazonaws.com\"},\"Sid\":\"07c77fac-68ff-11e6-97f8-c4850848610b\"}"}

    assert_not_in('Error', response)
    assert_in('lambda:InvokeFunction', response['Statement'])
Exemplo n.º 3
0
def test_wire_unwire_new_events_s3(awsclient, vendored_folder, temp_bucket,
                                   cleanup_lambdas, cleanup_roles):
    log.info('running test_wire_unwire_new_events_s3')

    # create a lambda function
    temp_string = utils.random_string()
    lambda_name = 'jenkins_test_%s' % temp_string
    role_name = 'unittest_%s_lambda' % temp_string
    role_arn = create_lambda_role_helper(awsclient, role_name)
    cleanup_roles.append(role_name)
    create_lambda_helper(awsclient,
                         lambda_name,
                         role_arn,
                         './resources/sample_lambda/handler_counter.py',
                         lambda_handler='handler_counter.handle')

    events = [{
        "event_source": {
            "arn": "arn:aws:s3:::" + temp_bucket,
            "events": ['s3:ObjectCreated:*'],
            "suffix": ".gz"
        }
    }]
    cleanup_lambdas.append((lambda_name, events))

    # wire the function with the bucket
    exit_code = wire(awsclient, events, lambda_name)
    assert exit_code == 0

    # put a file into the bucket
    awsclient.get_client('s3').put_object(
        ACL='public-read',
        Body=b'this is some content',
        Bucket=temp_bucket,
        Key='test_file.gz',
    )

    # validate function call
    time.sleep(20)  # sleep till the event arrived
    assert int(_get_count(awsclient, lambda_name)) == 1

    # unwire the function
    exit_code = unwire(awsclient, events, lambda_name)
    assert exit_code == 0

    # put in another file
    awsclient.get_client('s3').put_object(
        ACL='public-read',
        Body=b'this is some content',
        Bucket=temp_bucket,
        Key='test_file_2.gz',
    )

    # validate function not called
    time.sleep(10)
    assert int(_get_count(awsclient, lambda_name)) == 1
Exemplo n.º 4
0
def test_deprecated_schedule_event_source(awsclient, vendored_folder,
                                          cleanup_lambdas_deprecated,
                                          cleanup_roles):
    log.info('running test_schedule_event_source')
    # include reading config from settings file
    config = {
        "lambda": {
            "events": {
                "timeSchedules": [{
                    "ruleName": "unittest-dev-lambda-schedule",
                    "ruleDescription": "run every 1 minute",
                    "scheduleExpression": "rate(1 minute)"
                }]
            }
        }
    }

    # for time_event in time_event_sources:
    time_event = config['lambda'].get('events', []).get('timeSchedules', [])[0]
    rule_name = time_event.get('ruleName')
    rule_description = time_event.get('ruleDescription')
    schedule_expression = time_event.get('scheduleExpression')

    # now, I need a lambda function that registers the calls!!
    temp_string = utils.random_string()
    lambda_name = 'jenkins_test_%s' % temp_string
    role_name = 'unittest_%s_lambda' % temp_string
    role_arn = create_lambda_role_helper(awsclient, role_name)
    cleanup_roles.append(role_name)
    create_lambda_helper(awsclient,
                         lambda_name,
                         role_arn,
                         './resources/sample_lambda/handler_counter.py',
                         lambda_handler='handler_counter.handle')
    cleanup_lambdas_deprecated.append(lambda_name)

    # lookup lambda arn
    lambda_client = awsclient.get_client('lambda')
    alias_name = 'ACTIVE'
    lambda_arn = lambda_client.get_alias(FunctionName=lambda_name,
                                         Name=alias_name)['AliasArn']
    # create scheduled event source
    rule_arn = _lambda_add_time_schedule_event_source(awsclient, rule_name,
                                                      rule_description,
                                                      schedule_expression,
                                                      lambda_arn)
    _lambda_add_invoke_permission(awsclient, lambda_name,
                                  'events.amazonaws.com', rule_arn)

    time.sleep(180)  # wait for at least 2 invocations

    count = _get_count(awsclient, lambda_name)
    assert int(count) >= 2
Exemplo n.º 5
0
def test_rollback(awsclient, vendored_folder, temp_lambda):
    log.info('running test_rollback')

    lambda_name = temp_lambda[0]
    role_arn = temp_lambda[2]
    alias_version = _get_alias_version(awsclient, lambda_name, 'ACTIVE')
    assert_equal(alias_version, '1')

    # update the function
    create_lambda_helper(awsclient, lambda_name, role_arn,
                         './resources/sample_lambda/handler_v2.py')

    # now we use function_version 2!
    alias_version = _get_alias_version(awsclient, lambda_name, 'ACTIVE')
    assert_equal(alias_version, '$LATEST')

    exit_code = rollback(awsclient, lambda_name, alias_name='ACTIVE')
    assert_equal(exit_code, 0)

    # we rolled back to function_version 1
    alias_version = _get_alias_version(awsclient, lambda_name, 'ACTIVE')
    assert_equal(alias_version, '1')

    # try to rollback when previous version does not exist
    exit_code = rollback(awsclient, lambda_name, alias_name='ACTIVE')
    assert_equal(exit_code, 1)

    # version did not change
    alias_version = _get_alias_version(awsclient, lambda_name, 'ACTIVE')
    assert_equal(alias_version, '1')

    # roll back to the latest version
    exit_code = rollback(awsclient,
                         lambda_name,
                         alias_name='ACTIVE',
                         version='$LATEST')
    assert_equal(exit_code, 0)

    # latest version of lambda is used
    alias_version = _get_alias_version(awsclient, lambda_name, 'ACTIVE')
    assert_equal(alias_version, '$LATEST')

    # TODO: create more versions >5
    # TODO: do multiple rollbacks >5
    # TODO: verify version + active after rollback
    # TODO: verify invocations meet the right lamda_function version

    # here we have the test for ramuda_utils.list_lambda_versions
    response = list_lambda_versions(awsclient, lambda_name)
    assert_equal(response['Versions'][0]['Version'], '$LATEST')
    assert_equal(response['Versions'][1]['Version'], '1')
    assert_equal(response['Versions'][2]['Version'], '2')
Exemplo n.º 6
0
def test_update_lambda(awsclient, vendored_folder, cleanup_lambdas,
                       cleanup_roles):
    log.info('running test_update_lambda')
    temp_string = helpers.random_string()
    lambda_name = 'jenkins_test_%s' % temp_string
    role_name = 'unittest_%s_lambda' % temp_string
    # create the function
    role_arn = create_lambda_role_helper(awsclient, role_name)
    cleanup_roles.append(role_name)
    create_lambda_helper(awsclient, lambda_name, role_arn,
                         './resources/sample_lambda/handler.py')
    # update the function
    create_lambda_helper(awsclient, lambda_name, role_arn,
                         './resources/sample_lambda/handler_v2.py')
    cleanup_lambdas.append(lambda_name)
Exemplo n.º 7
0
def temp_lambda(awsclient):
    # provide a lambda function and cleanup after test suite
    temp_string = utils.random_string()
    lambda_name = 'jenkins_test_%s' % temp_string
    role_name = 'unittest_%s_lambda' % temp_string
    # create the function
    role_arn = create_lambda_role_helper(awsclient, role_name)
    create_lambda_helper(awsclient, lambda_name, role_arn,
                         # './resources/sample_lambda/handler.py',
                         here('./resources/sample_lambda/handler.py'),
                         lambda_handler='handler.handle')
    yield lambda_name, role_name, role_arn
    # cleanup
    delete_lambda_deprecated(awsclient, lambda_name, delete_logs=True)
    delete_role_helper(awsclient, role_name)
Exemplo n.º 8
0
def test_event_source_lifecycle_cloudwatch_pattern(awsclient, vendored_folder,
                                                   cleanup_lambdas_deprecated,
                                                   cleanup_roles):
    log.info('running test_event_source_lifecycle_cloudwatch_pattern')

    lambda_folder = './resources/sample_lambda_event_pattern/'

    temp_string = utils.random_string()
    lambda_name = 'jenkins_test_sample-lambda-event-pattern_' + temp_string
    role_name = 'unittest_%s_lambda' % temp_string
    role_arn = create_lambda_role_helper(awsclient, role_name)

    # create the function
    create_lambda_helper(awsclient,
                         lambda_name,
                         role_arn,
                         here(lambda_folder + 'handler.py'),
                         lambda_handler='handler.handler',
                         folders_from_file=[],
                         runtime='python2.7')

    cleanup_roles.append(role_name)
    cleanup_lambdas_deprecated.append(lambda_name)

    # lookup lambda arn
    # us-east-1 is the only region that implements lambda@edge
    lambda_client = awsclient.get_client('lambda')
    alias_name = 'ACTIVE'
    lambda_arn = lambda_client.get_alias(FunctionName=lambda_name,
                                         Name=alias_name)['AliasArn']

    # define event source
    evt_source = {
        "name": "ssm_parameter_changed",
        "input_path": "$.detail",
        "pattern": {
            "source": ["aws.ssm"],
            "detail-type": ["Parameter Store Change"]
        }
    }

    # event source lifecycle
    _add_event_source(awsclient, evt_source, lambda_arn)
    status = _get_event_source_status(awsclient, evt_source, lambda_arn)
    assert status['EventSourceArn']
    _remove_event_source(awsclient, evt_source, lambda_arn)
Exemplo n.º 9
0
def test_ping(awsclient, vendored_folder, temp_lambda):
    log.info('running test_ping')

    lambda_name = temp_lambda[0]
    role_arn = temp_lambda[2]

    # test the ping
    response = ping(awsclient, lambda_name)
    assert response == '"alive"'

    # update the function
    create_lambda_helper(awsclient, lambda_name, role_arn,
                         './resources/sample_lambda/handler_no_ping.py',
                         lambda_handler='handler_no_ping.handle')

    # test has no ping
    response = ping(awsclient, lambda_name)
    assert response == '{"ramuda_action": "ping"}'
Exemplo n.º 10
0
def test_wire_unwire_new_events_sns(awsclient, vendored_folder,
                                    cleanup_lambdas, cleanup_roles,
                                    temp_sns_topic):
    log.info('running test_wire_unwire_new_events_sns')

    # create a lambda function
    temp_string = utils.random_string()
    lambda_name = 'jenkins_test_%s' % temp_string
    role_name = 'unittest_%s_lambda' % temp_string
    role_arn = create_lambda_role_helper(awsclient, role_name)
    cleanup_roles.append(role_name)
    create_lambda_helper(awsclient,
                         lambda_name,
                         role_arn,
                         './resources/sample_lambda/handler_counter.py',
                         lambda_handler='handler_counter.handle')

    # schedule expressions:
    # http://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html
    events = [{
        "event_source": {
            "arn": temp_sns_topic[1],
            "events": ["sns:Publish"]
        }
    }]

    cleanup_lambdas.append((lambda_name, events))

    # wire the function with the bucket
    exit_code = wire(awsclient, events, lambda_name)
    assert exit_code == 0

    assert int(_get_count(awsclient, lambda_name)) == 0

    # send message via sns
    send_message(awsclient, temp_sns_topic[1], {"foo": "bar"})

    time.sleep(10)  # give a little time for the message to arrive
    assert int(_get_count(awsclient, lambda_name)) == 1

    # unwire the function
    exit_code = unwire(awsclient, events, lambda_name)
    assert exit_code == 0
Exemplo n.º 11
0
def test_wire_unwire_new_events_cloudwatch_schedule(awsclient, vendored_folder,
                                                    cleanup_lambdas,
                                                    cleanup_roles):
    log.info('running test_wire_unwire_new_events_cloudwatch_schedule')

    # create a lambda function
    temp_string = utils.random_string()
    lambda_name = 'jenkins_test_%s' % temp_string
    role_name = 'unittest_%s_lambda' % temp_string
    role_arn = create_lambda_role_helper(awsclient, role_name)
    cleanup_roles.append(role_name)
    create_lambda_helper(awsclient,
                         lambda_name,
                         role_arn,
                         './resources/sample_lambda/handler_counter.py',
                         lambda_handler='handler_counter.handle')

    # schedule expressions:
    # http://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html
    events = [{
        "event_source": {
            "name": "execute_backup",
            "schedule": "rate(1 minute)"
        }
    }]
    cleanup_lambdas.append((lambda_name, events))

    # wire the function with the bucket
    exit_code = wire(awsclient, events, lambda_name)
    assert exit_code == 0

    assert int(_get_count(awsclient, lambda_name)) == 0

    time.sleep(70)  # sleep till scheduled event
    assert int(_get_count(awsclient, lambda_name)) == 1

    # unwire the function
    exit_code = unwire(awsclient, events, lambda_name)
    assert exit_code == 0

    time.sleep(70)
    assert int(_get_count(awsclient, lambda_name)) == 1
Exemplo n.º 12
0
def test_wire_unwire_lambda_with_s3(awsclient, vendored_folder,
                                    cleanup_lambdas, cleanup_roles,
                                    temp_bucket):
    log.info('running test_wire_unwire_lambda_with_s3')

    # create a lambda function
    temp_string = helpers.random_string()
    lambda_name = 'jenkins_test_%s' % temp_string
    role_name = 'unittest_%s_lambda' % temp_string
    role_arn = create_lambda_role_helper(awsclient, role_name)
    cleanup_roles.append(role_name)
    create_lambda_helper(awsclient,
                         lambda_name,
                         role_arn,
                         './resources/sample_lambda/handler_counter.py',
                         lambda_handler='handler_counter.handle')
    cleanup_lambdas.append(lambda_name)

    bucket_name = temp_bucket
    config = {
        "lambda": {
            "events": {
                "s3Sources": [{
                    "bucket": bucket_name,
                    "type": "s3:ObjectCreated:*",
                    "suffix": ".gz"
                }]
            }
        }
    }

    # wire the function with the bucket
    s3_event_sources = config['lambda'].get('events', []).get('s3Sources', [])
    time_event_sources = config['lambda'].get('events',
                                              []).get('timeSchedules', [])
    exit_code = wire(awsclient, lambda_name, s3_event_sources,
                     time_event_sources)
    assert_equal(exit_code, 0)

    # put a file into the bucket
    awsclient.get_client('s3').put_object(
        ACL='public-read',
        Body=b'this is some content',
        Bucket=bucket_name,
        Key='test_file.gz',
    )

    # validate function call
    time.sleep(20)  # sleep till the event arrived
    assert_equal(int(_get_count(awsclient, lambda_name)), 1)

    # unwire the function
    exit_code = unwire(awsclient, lambda_name, s3_event_sources,
                       time_event_sources)
    assert_equal(exit_code, 0)

    # put in another file
    awsclient.get_client('s3').put_object(
        ACL='public-read',
        Body=b'this is some content',
        Bucket=bucket_name,
        Key='test_file_2.gz',
    )

    # validate function not called
    time.sleep(10)
    assert int(_get_count(awsclient, lambda_name)) == 1