def test_invalid_config_missing_function_dir2():
    with pytest.raises(Exception):
        config.Config(
            # invalid function dir
            path.join(EX_CONFIG, 'pleasedontexist_dir'),
            # valid config file
            path.join(EX_CONFIG, 'lambda.json'))
 def test_successfully_updates_kinesis_subscription(self, mocked_session):
     resonse = {
         "Error": {
             "Code": "ResourceConflictException",
             "Message": ""
         }
     }
     err = botocore.exceptions.ClientError(resonse,
                                           "create_event_source_mapping")
     _mocked_lambda = Mock()
     _mocked_lambda.create_event_source_mapping.side_effect = err
     _mocked_lambda.list_event_source_mappings.return_value = {
         'EventSourceMappings': [{
             'UUID': 'myuuid'
         }]
     }
     _mocked_session = Mock()
     _mocked_session.client = Mock()
     _mocked_session.client.return_value = _mocked_lambda
     mocked_session.return_value = _mocked_session
     conf = config.Config(path.dirname(__file__),
                          config_file=path.join(
                              EX_CONFIG, 'lambda-with-subscription.json'))
     subscribers.create_subscriptions(conf, None)
     nt.assert_equals(True,
                      _mocked_lambda.update_event_source_mapping.called)
Exemple #3
0
def test_set_publish():
    cfg = config.Config(EX_CONFIG)
    # Check that we default to false
    assert cfg.publish is False

    cfg.set_publish()
    assert cfg.publish
def test_vpc_config():
    subnet = 'subnet-00000000'
    securitygroup = 'sg-00000000'
    vpc = {'subnets': [subnet], 'security_groups': [securitygroup]}
    cfg = config.Config(EX_CONFIG)

    assert cfg.raw['vpc']['security_groups'][0] == vpc['security_groups'][0]
    assert cfg.raw['vpc']['subnets'][0] == vpc['subnets'][0]
Exemple #5
0
def test_kinesis_subscription():
    ksub = {
        'stream': 'arn:aws:kinesis:eu-west-1:000000000000:stream/services',
        'batch_size': 10
    }
    cfg = config.Config(EX_CONFIG, EX_CONFIG + '/lambda-with-subscription.json')
    assert cfg.raw['subscription']['kinesis']['stream'] == ksub['stream']
    assert cfg.raw['subscription']['kinesis']['batch_size'] == ksub['batch_size']
def test_load_config():
    cfg = config.Config(EX_CONFIG)

    # Do a quick test of some attributes
    attrs = {'name': 'myFunc', 'description': 'myfunc', 'region': 'us-east-1'}

    for key, val in attrs.items():
        assert cfg.raw[key] == val
Exemple #7
0
def _execute(args):
    pth = path.abspath(args.function_dir)

    cfg = config.Config(pth,
                        args.config,
                        role=args.role,
                        variables=args.variables)

    if args.s3_bucket:
        cfg.set_s3(args.s3_bucket, args.s3_key)

    if args.no_virtualenv:
        # specified flag to omit entirely
        venv = False
    elif args.virtualenv:
        # specified a custom virtualenv
        venv = args.virtualenv
    else:
        # build and include virtualenv, the default
        venv = None

    if args.no_build:
        pkg = package.create_package(pth)
    else:
        _print('Building Package')
        requirements = cfg.requirements
        if args.requirements:
            requirements = path.abspath(args.requirements)
        extra_files = cfg.extra_files
        if args.extra_files:
            extra_files = args.extra_files
        pkg = package.build_package(pth, requirements, venv, cfg.ignore,
                                    extra_files)

    if not args.no_clean:
        pkg.clean_workspace()

    if not args.no_upload:
        # Set publish if flagged to do so
        if args.publish:
            cfg.set_publish()

        create_alias = False
        # Set alias if the arg is passed
        if args.alias is not None:
            cfg.set_alias(args.alias, args.alias_description)
            create_alias = True

        _print('Uploading Package')
        upldr = uploader.PackageUploader(cfg, args.profile)
        upldr.upload(pkg)
        # If the alias was set create it
        if create_alias:
            upldr.alias()

        pkg.clean_zipfile()

    _print('Fin')
Exemple #8
0
 def test_successfully_adds_kinesis_subscription(self, mocked_session):
     _mocked_lambda = Mock()
     _mocked_session = Mock()
     _mocked_session.client = Mock()
     _mocked_session.client.return_value = _mocked_lambda
     mocked_session.return_value = _mocked_session
     conf = config.Config(path.dirname(__file__),
                          config_file=path.join(
                              EX_CONFIG, 'lambda-with-subscription.json'))
     subscribers.create_subscriptions(conf, None)
     nt.assert_equals(True,
                      _mocked_lambda.create_event_source_mapping.called)
def test_kinesis_subscription_with_starting_position_at_timestamp():
    ksub = {
        'stream': 'arn:aws:kinesis:eu-west-1:000000000000:stream/services',
        'batch_size': 10,
        'starting_position_timestamp': '2017-11-01T11:00:00Z'
    }
    cfg = config.Config(EX_CONFIG,
                        EX_CONFIG + '/lambda-with-subscription_at_ts.json')
    assert cfg.raw['subscription']['kinesis']['stream'] == ksub['stream']
    assert cfg.raw['subscription']['kinesis']['batch_size'] == ksub[
        'batch_size']
    assert cfg.raw['subscription']['kinesis'][
        'starting_position_timestamp'] == ksub['starting_position_timestamp']
Exemple #10
0
def test_s3_upload():
    mock_bucket = 'mybucket'
    conn = boto3.resource('s3')
    conn.create_bucket(Bucket=mock_bucket)

    conf = config.Config(path.dirname(__file__),
                         config_file=path.join(EX_CONFIG, 'lambda.json'))
    conf.set_s3(mock_bucket)
    upldr = uploader.PackageUploader(conf, None)

    upldr._upload_s3(path.join(path.dirname(__file__), 'dummyfile'))

    # fetch the contents back out, be sure we truly uploaded the dummyfile
    retrieved_bucket = conn.Object(
        mock_bucket,
        conf.s3_package_name()
        ).get()['Body']
    found_contents = str(retrieved_bucket.read()).rstrip()
    if python_version() < '3.0.0':
        assert found_contents == 'dummy data'
    else:
        assert found_contents == "b'dummy data\\n'"
Exemple #11
0
def test_default_runtime():
    cfg = config.Config(EX_CONFIG)
    assert cfg.runtime == 'python2.7'

    cfg.set_runtime('java8')
    assert cfg.runtime == 'java8'
Exemple #12
0
def test_invalid_config_missing_function_dir():
    # try invalid file
    with pytest.raises(Exception):
        config.Config(path.join(EX_CONFIG, 'pleasedontexist_dir'))
Exemple #13
0
def test_invalid_config_missing_file():
    # try invalid file
    with pytest.raises(Exception):
        config.Config(EX_CONFIG, path.join(EX_CONFIG, 'pleasedontexist.json'))
Exemple #14
0
def test_invalid_config_as_dir():
    # pass the function directory as the lambda configuration --
    # this should not work!
    with pytest.raises(Exception):
        config.Config(EX_CONFIG, EX_CONFIG)
Exemple #15
0
def test___getattr__():
    cfg = config.Config(EX_CONFIG, path.join(EX_CONFIG, 'lambda.json'))
    assert cfg.s3_bucket is None
    assert cfg.name == 'myFunc'
Exemple #16
0
def test_no_vpc():
    cfg = config.Config(EX_CONFIG, EX_CONFIG + '/lambda-no-vpc.json')

    assert cfg.raw['vpc'] is None
Exemple #17
0
def test_role_override():
    role = 'arn:aws:iam::00000000000:role/myfunc_role'
    cfg = config.Config(EX_CONFIG, role=role)

    assert cfg.role is role
Exemple #18
0
def test_kinesis_subscription_with_starting_position_at_timestamp_fails_when_timestamp_not_provided(
):
    with pytest.raises(Exception):
        cfg = config.Config(
            EX_CONFIG,
            EX_CONFIG + '/lambda-with-subscription_at_ts_not_provided.json')