Ejemplo n.º 1
0
def test_send_to_s3(tmp_path, request):
    # TODO test s3 bucket versioning
    bucket_name = create_chili_pepper_s3_bucket()
    app_dir = create_app_structure(tmp_path,
                                   bucket_name=bucket_name,
                                   pytest_request_fixture=request)

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

    deployer = Deployer(app=app)

    s3_client = boto3.client("s3")

    deployment_package_path = deployer._create_deployment_package(
        tmp_path, app_dir)
    deployment_package_code_prop = deployer._send_deployment_package_to_s3(
        deployment_package_path)

    actual_get_object_response = s3_client.get_object(
        Bucket=bucket_name,
        Key=deployment_package_code_prop.S3Key,
        VersionId=deployment_package_code_prop.S3ObjectVersion)

    assert actual_get_object_response["VersionId"] == "0"
    assert actual_get_object_response["Body"].read(
    ) == deployment_package_path.read_bytes()
Ejemplo n.º 2
0
def test_deploy(tmp_path, request, runtime):
    bucket_name = create_chili_pepper_s3_bucket()
    app_dir = create_app_structure(tmp_path,
                                   bucket_name=bucket_name,
                                   runtime=runtime,
                                   pytest_request_fixture=request)

    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)

    lambda_client = boto3.client("lambda")
    lambda_list_functions_response = lambda_client.list_functions()

    assert len(lambda_list_functions_response["Functions"]) == 1
    my_function = lambda_list_functions_response["Functions"][0]

    print("Full function dictionary:")
    pprint.pprint(my_function)

    expected_function_attributes = {
        "Runtime": runtime,
        "Handler": "tasks.say_hello",
        "Role": {
            "Fn::GetAtt": ["FunctionRole", "Arn"]
        },  # this is moto's mock not resolving GetAtt
    }

    for expected_attribute, expected_value in expected_function_attributes.items(
    ):
        assert expected_attribute in my_function
        assert my_function[expected_attribute] == expected_value

    iam_resource = boto3.resource("iam")
    all_roles = list(iam_resource.roles.all())

    assert len(all_roles) == 1
    my_role = all_roles[0]
    print("Full role from list_roles:")
    pprint.pprint(my_role)

    assert json.loads(my_role.assume_role_policy_document.replace(
        "'", '"')) == {
            "Statement": [{
                "Action": ["sts:AssumeRole"],
                "Effect": "Allow",
                "Principal": {
                    "Service": ["lambda.amazonaws.com"]
                }
            }]
        }
    # TOOO moto does not handle attaching the lambda execution role policy correctly....
    assert list(my_role.attached_policies.all()) == []
Ejemplo n.º 3
0
def test_zip(tmp_path, request):
    app = ChiliPepper().create_app("test_deployer_app")
    app.conf["aws"]["bucket_name"] = "dummy"
    app.conf["aws"]["runtime"] = "python3.7"

    deployer = Deployer(app=app)
    app_dir = create_app_structure(tmp_path, pytest_request_fixture=request)

    deployer._create_deployment_package(tmp_path, app_dir)

    # There should now be a zip file created.
    zipfile.is_zipfile(str(tmp_path / (app.app_name + ".zip")))
Ejemplo n.º 4
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
Ejemplo n.º 5
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