Esempio n. 1
0
def test_invalid_signature_only_context():
    bucket_name = create_chili_pepper_s3_bucket()

    app = ChiliPepper().create_app("test_deployer_app")
    app.conf["aws"]["bucket_name"] = bucket_name
    app.conf["aws"]["runtime"] = "python3.7"

    with pytest.raises(InvalidFunctionSignature):

        @app.task()
        def say_hello(context):  # pylint: disable=unused-variable
            return "Hello!"
Esempio n. 2
0
def test_task_decorator():
    bucket_name = create_chili_pepper_s3_bucket()

    app = ChiliPepper().create_app("test_deployer_app")
    app.conf["aws"]["bucket_name"] = bucket_name
    app.conf["aws"]["runtime"] = "python3.7"

    @app.task()
    def say_hello(event, context):
        return "Hello!"

    assert hasattr(say_hello, "delay")
    assert app.task_functions == [TaskFunction(say_hello)]
Esempio n. 3
0
def test_task_decorator_environment_variables():
    bucket_name = create_chili_pepper_s3_bucket()

    app = ChiliPepper().create_app("test_deployer_app")
    app.conf["aws"]["bucket_name"] = bucket_name
    app.conf["aws"]["runtime"] = "python3.7"

    env_vars = {"test_app_key": "test_app_value"}

    @app.task(environment_variables=env_vars)
    def say_hello(event, context):
        return "Hello!"

    assert hasattr(say_hello, "delay")
    expected_task_function = TaskFunction(say_hello,
                                          environment_variables=env_vars)
    print(app.task_functions[0].environment_variables)
    print(expected_task_function.environment_variables)
    assert app.task_functions == [expected_task_function]
Esempio n. 4
0
def test_app_env_vars():
    bucket_name = create_chili_pepper_s3_bucket()

    app = ChiliPepper().create_app("test_deployer_app")
    app.conf["aws"]["bucket_name"] = bucket_name
    app.conf["aws"]["runtime"] = "python3.7"

    app.conf["default_environment_variables"] = {
        "test_app_key": "test_app_value"
    }

    @app.task()
    def say_hello(event, context):
        return "Hello!"

    expected_task_function = TaskFunction(
        say_hello,
        environment_variables=app.conf["default_environment_variables"])
    print(app.task_functions[0])
    print(expected_task_function)
    assert app.task_functions == [expected_task_function]
Esempio n. 5
0
def test_delay_function(tmp_path, request):
    # `request` is the pytest request fixture https://docs.pytest.org/en/latest/reference.html#request
    bucket_name = create_chili_pepper_s3_bucket()
    app_dir = create_app_structure(tmp_path,
                                   request,
                                   bucket_name=bucket_name,
                                   include_requirements=True)

    cli = CLI()
    app = cli._load_app("tasks.app", str(app_dir))
    deployer = Deployer(app)
    deployer.deploy(tmp_path, app_dir)

    say_hello_function = [
        f.func for f in app.task_functions if f.func.__name__ == "say_hello"
    ][0]
    say_hello_result = say_hello_function.delay({})

    # moto doesn't handle return :(
    say_hello_result.get()
    assert say_hello_result._invoke_response["StatusCode"] == 202
Esempio n. 6
0
def test_deployed_cf_template(tmp_path, request, runtime,
                              environment_variables, use_custom_kms_key):
    bucket_name = create_chili_pepper_s3_bucket()
    if use_custom_kms_key:
        kms_client = boto3.client("kms")
        create_key_response = kms_client.create_key(Origin="AWS_KMS")
        kms_key_arn = create_key_response["KeyMetadata"]["Arn"]
    else:
        kms_key_arn = None

    app_dir = create_app_structure(tmp_path,
                                   bucket_name=bucket_name,
                                   runtime=runtime,
                                   pytest_request_fixture=request,
                                   environment_variables=environment_variables,
                                   kms_key_arn=kms_key_arn)

    cli = CLI()
    fake_args = argparse.Namespace(app="tasks.app",
                                   app_dir=str(app_dir),
                                   deployment_package_dir=str(tmp_path))
    cli.deploy(args=fake_args)

    cf_client = boto3.client("cloudformation")
    list_stacks_response = cf_client.list_stacks()

    assert len(list_stacks_response["StackSummaries"]) == 1
    stack_summary = list_stacks_response["StackSummaries"][0]
    stack_name = stack_summary["StackName"]

    stack_template = cf_client.get_template(
        StackName=stack_name)["TemplateBody"]
    stack_resources = stack_template["Resources"]

    pprint.pprint(stack_template)

    chili_pepper_function_role = stack_resources["FunctionRole"]
    assert chili_pepper_function_role["Type"] == "AWS::IAM::Role"
    chili_pepper_role_properties = chili_pepper_function_role["Properties"]
    assert chili_pepper_role_properties["ManagedPolicyArns"] == [
        "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
    ]
    assert chili_pepper_role_properties[
        "AssumeRolePolicyDocument"] == OrderedDict([(
            "Statement",
            [
                OrderedDict([
                    ("Action", ["sts:AssumeRole"]), ("Effect", "Allow"),
                    ("Principal",
                     OrderedDict([("Service", ["lambda.amazonaws.com"])]))
                ])
            ],
        )])

    lambda_function = stack_resources["TasksSayHello"]
    assert lambda_function["Type"] == "AWS::Lambda::Function"
    lambda_function_properties = lambda_function["Properties"]
    assert lambda_function_properties["Code"] == OrderedDict([
        ("S3Bucket", bucket_name), ("S3Key", "demo_deployment_package.zip"),
        ("S3ObjectVersion", "0")
    ])
    assert lambda_function_properties["Runtime"] == runtime
    assert lambda_function_properties["Handler"] == "tasks.say_hello"
    assert lambda_function_properties["Environment"] == OrderedDict([
        ("Variables", environment_variables)
    ])
    if use_custom_kms_key:
        assert lambda_function_properties["KmsKeyArn"] == kms_key_arn
    else:
        assert "KmsKeyArn" not in lambda_function_properties

    assert len(stack_resources) == 2