"use strict";
const assert_1 = require("@aws-cdk/assert");
const codecommit = require("@aws-cdk/aws-codecommit");
const s3 = require("@aws-cdk/aws-s3");
const cdk = require("@aws-cdk/cdk");
const codebuild = require("../lib");
module.exports = {
    'default properties': {
        'with CodePipeline source'(test) {
            const stack = new cdk.Stack();
            const source = new codebuild.CodePipelineSource();
            new codebuild.Project(stack, 'MyProject', {
                source
            });
            assert_1.expect(stack).toMatch({
                "Resources": {
                    "MyProjectRole9BBE5233": {
                        "Type": "AWS::IAM::Role",
                        "Properties": {
                            "AssumeRolePolicyDocument": {
                                "Statement": [
                                    {
                                        "Action": "sts:AssumeRole",
                                        "Effect": "Allow",
                                        "Principal": {
                                            "Service": "codebuild.amazonaws.com"
                                        }
                                    }
                                ],
                                "Version": "2012-10-17"
                            }
                        }
                    },
                    "MyProjectRoleDefaultPolicyB19B7C29": {
                        "Type": "AWS::IAM::Policy",
                        "Properties": {
                            "PolicyDocument": {
                                "Statement": [
                                    {
                                        "Action": [
                                            "logs:CreateLogGroup",
                                            "logs:CreateLogStream",
                                            "logs:PutLogEvents"
                                        ],
                                        "Effect": "Allow",
                                        "Resource": [
                                            {
                                                "Fn::Join": [
                                                    "",
                                                    [
                                                        "arn",
                                                        ":",
                                                        {
                                                            "Ref": "AWS::Partition"
                                                        },
                                                        ":",
                                                        "logs",
                                                        ":",
                                                        {
                                                            "Ref": "AWS::Region"
                                                        },
                                                        ":",
                                                        {
                                                            "Ref": "AWS::AccountId"
                                                        },
                                                        ":",
                                                        "log-group",
                                                        ":",
                                                        "/aws/codebuild/",
                                                        {
                                                            "Ref": "MyProject39F7B0AE"
                                                        }
                                                    ]
                                                ]
                                            },
                                            {
                                                "Fn::Join": [
                                                    "",
                                                    [
                                                        {
                                                            "Fn::Join": [
                                                                "",
                                                                [
                                                                    "arn",
                                                                    ":",
                                                                    {
                                                                        "Ref": "AWS::Partition"
                                                                    },
                                                                    ":",
                                                                    "logs",
                                                                    ":",
                                                                    {
                                                                        "Ref": "AWS::Region"
                                                                    },
                                                                    ":",
                                                                    {
                                                                        "Ref": "AWS::AccountId"
                                                                    },
                                                                    ":",
                                                                    "log-group",
                                                                    ":",
                                                                    "/aws/codebuild/",
                                                                    {
                                                                        "Ref": "MyProject39F7B0AE"
                                                                    }
                                                                ]
                                                            ]
                                                        },
                                                        ":*"
                                                    ]
                                                ]
                                            }
                                        ]
                                    }
                                ],
                                "Version": "2012-10-17"
                            },
                            "PolicyName": "MyProjectRoleDefaultPolicyB19B7C29",
                            "Roles": [
                                {
                                    "Ref": "MyProjectRole9BBE5233"
                                }
                            ]
                        }
                    },
                    "MyProject39F7B0AE": {
                        "Type": "AWS::CodeBuild::Project",
                        "Properties": {
                            "Source": {
                                "Type": "CODEPIPELINE"
                            },
                            "Artifacts": {
                                "Type": "CODEPIPELINE"
                            },
                            "ServiceRole": {
                                "Fn::GetAtt": [
                                    "MyProjectRole9BBE5233",
                                    "Arn"
                                ]
                            },
                            "Environment": {
                                "Type": "LINUX_CONTAINER",
                                "PrivilegedMode": false,
                                "Image": "aws/codebuild/ubuntu-base:14.04",
                                "ComputeType": "BUILD_GENERAL1_SMALL"
                            }
                        }
                    }
                }
            });
            test.done();
        },
        'with CodeCommit source'(test) {
            const stack = new cdk.Stack();
            const repo = new codecommit.Repository(stack, 'MyRepo', { repositoryName: 'hello-cdk' });
            const source = new codebuild.CodeCommitSource(repo);
            new codebuild.Project(stack, 'MyProject', {
                source
            });
            assert_1.expect(stack).toMatch({
                "Resources": {
                    "MyRepoF4F48043": {
                        "Type": "AWS::CodeCommit::Repository",
                        "Properties": {
                            "RepositoryName": "hello-cdk",
                            "Triggers": []
                        }
                    },
                    "MyProjectRole9BBE5233": {
                        "Type": "AWS::IAM::Role",
                        "Properties": {
                            "AssumeRolePolicyDocument": {
                                "Statement": [
                                    {
                                        "Action": "sts:AssumeRole",
                                        "Effect": "Allow",
                                        "Principal": {
                                            "Service": "codebuild.amazonaws.com"
                                        }
                                    }
                                ],
                                "Version": "2012-10-17"
                            }
                        }
                    },
                    "MyProjectRoleDefaultPolicyB19B7C29": {
                        "Type": "AWS::IAM::Policy",
                        "Properties": {
                            "PolicyDocument": {
                                "Statement": [
                                    {
                                        "Action": "codecommit:GitPull",
                                        "Effect": "Allow",
                                        "Resource": {
                                            "Fn::GetAtt": [
                                                "MyRepoF4F48043",
                                                "Arn"
                                            ]
                                        }
                                    },
                                    {
                                        "Action": [
                                            "logs:CreateLogGroup",
                                            "logs:CreateLogStream",
                                            "logs:PutLogEvents"
                                        ],
                                        "Effect": "Allow",
                                        "Resource": [
                                            {
                                                "Fn::Join": [
                                                    "",
                                                    [
                                                        "arn",
                                                        ":",
                                                        {
                                                            "Ref": "AWS::Partition"
                                                        },
                                                        ":",
                                                        "logs",
                                                        ":",
                                                        {
                                                            "Ref": "AWS::Region"
                                                        },
                                                        ":",
                                                        {
                                                            "Ref": "AWS::AccountId"
                                                        },
                                                        ":",
                                                        "log-group",
                                                        ":",
                                                        "/aws/codebuild/",
                                                        {
                                                            "Ref": "MyProject39F7B0AE"
                                                        }
                                                    ]
                                                ]
                                            },
                                            {
                                                "Fn::Join": [
                                                    "",
                                                    [
                                                        {
                                                            "Fn::Join": [
                                                                "",
                                                                [
                                                                    "arn",
                                                                    ":",
                                                                    {
                                                                        "Ref": "AWS::Partition"
                                                                    },
                                                                    ":",
                                                                    "logs",
                                                                    ":",
                                                                    {
                                                                        "Ref": "AWS::Region"
                                                                    },
                                                                    ":",
                                                                    {
                                                                        "Ref": "AWS::AccountId"
                                                                    },
                                                                    ":",
                                                                    "log-group",
                                                                    ":",
                                                                    "/aws/codebuild/",
                                                                    {
                                                                        "Ref": "MyProject39F7B0AE"
                                                                    }
                                                                ]
                                                            ]
                                                        },
                                                        ":*"
                                                    ]
                                                ]
                                            }
                                        ]
                                    }
                                ],
                                "Version": "2012-10-17"
                            },
                            "PolicyName": "MyProjectRoleDefaultPolicyB19B7C29",
                            "Roles": [
                                {
                                    "Ref": "MyProjectRole9BBE5233"
                                }
                            ]
                        }
                    },
                    "MyProject39F7B0AE": {
                        "Type": "AWS::CodeBuild::Project",
                        "Properties": {
                            "Source": {
                                "Type": "CODECOMMIT",
                                "Location": {
                                    "Fn::GetAtt": [
                                        "MyRepoF4F48043",
                                        "CloneUrlHttp"
                                    ]
                                }
                            },
                            "Artifacts": {
                                "Type": "NO_ARTIFACTS"
                            },
                            "ServiceRole": {
                                "Fn::GetAtt": [
                                    "MyProjectRole9BBE5233",
                                    "Arn"
                                ]
                            },
                            "Environment": {
                                "Type": "LINUX_CONTAINER",
                                "PrivilegedMode": false,
                                "Image": "aws/codebuild/ubuntu-base:14.04",
                                "ComputeType": "BUILD_GENERAL1_SMALL"
                            }
                        }
                    }
                }
            });
            test.done();
        },
        'with S3Bucket source'(test) {
            const stack = new cdk.Stack();
            const bucket = new s3.Bucket(stack, 'MyBucket');
            new codebuild.Project(stack, 'MyProject', {
                source: new codebuild.S3BucketSource(bucket, 'path/to/source.zip'),
                environment: {
                    buildImage: codebuild.WindowsBuildImage.WIN_SERVER_CORE_2016_BASE,
                },
            });
            assert_1.expect(stack).toMatch({
                "Resources": {
                    "MyBucketF68F3FF0": {
                        "Type": "AWS::S3::Bucket"
                    },
                    "MyProjectRole9BBE5233": {
                        "Type": "AWS::IAM::Role",
                        "Properties": {
                            "AssumeRolePolicyDocument": {
                                "Statement": [
                                    {
                                        "Action": "sts:AssumeRole",
                                        "Effect": "Allow",
                                        "Principal": {
                                            "Service": "codebuild.amazonaws.com"
                                        }
                                    }
                                ],
                                "Version": "2012-10-17"
                            }
                        }
                    },
                    "MyProjectRoleDefaultPolicyB19B7C29": {
                        "Type": "AWS::IAM::Policy",
                        "Properties": {
                            "PolicyDocument": {
                                "Statement": [
                                    {
                                        "Action": [
                                            "s3:GetObject*",
                                            "s3:GetBucket*",
                                            "s3:List*"
                                        ],
                                        "Effect": "Allow",
                                        "Resource": [
                                            {
                                                "Fn::GetAtt": [
                                                    "MyBucketF68F3FF0",
                                                    "Arn"
                                                ]
                                            },
                                            {
                                                "Fn::Join": [
                                                    "",
                                                    [
                                                        {
                                                            "Fn::GetAtt": [
                                                                "MyBucketF68F3FF0",
                                                                "Arn"
                                                            ]
                                                        },
                                                        "/",
                                                        "*"
                                                    ]
                                                ]
                                            }
                                        ]
                                    },
                                    {
                                        "Action": [
                                            "logs:CreateLogGroup",
                                            "logs:CreateLogStream",
                                            "logs:PutLogEvents"
                                        ],
                                        "Effect": "Allow",
                                        "Resource": [
                                            {
                                                "Fn::Join": [
                                                    "",
                                                    [
                                                        "arn",
                                                        ":",
                                                        {
                                                            "Ref": "AWS::Partition"
                                                        },
                                                        ":",
                                                        "logs",
                                                        ":",
                                                        {
                                                            "Ref": "AWS::Region"
                                                        },
                                                        ":",
                                                        {
                                                            "Ref": "AWS::AccountId"
                                                        },
                                                        ":",
                                                        "log-group",
                                                        ":",
                                                        "/aws/codebuild/",
                                                        {
                                                            "Ref": "MyProject39F7B0AE"
                                                        }
                                                    ]
                                                ]
                                            },
                                            {
                                                "Fn::Join": [
                                                    "",
                                                    [
                                                        {
                                                            "Fn::Join": [
                                                                "",
                                                                [
                                                                    "arn",
                                                                    ":",
                                                                    {
                                                                        "Ref": "AWS::Partition"
                                                                    },
                                                                    ":",
                                                                    "logs",
                                                                    ":",
                                                                    {
                                                                        "Ref": "AWS::Region"
                                                                    },
                                                                    ":",
                                                                    {
                                                                        "Ref": "AWS::AccountId"
                                                                    },
                                                                    ":",
                                                                    "log-group",
                                                                    ":",
                                                                    "/aws/codebuild/",
                                                                    {
                                                                        "Ref": "MyProject39F7B0AE"
                                                                    }
                                                                ]
                                                            ]
                                                        },
                                                        ":*"
                                                    ]
                                                ]
                                            }
                                        ]
                                    }
                                ],
                                "Version": "2012-10-17"
                            },
                            "PolicyName": "MyProjectRoleDefaultPolicyB19B7C29",
                            "Roles": [
                                {
                                    "Ref": "MyProjectRole9BBE5233"
                                }
                            ]
                        }
                    },
                    "MyProject39F7B0AE": {
                        "Type": "AWS::CodeBuild::Project",
                        "Properties": {
                            "Source": {
                                "Type": "S3",
                                "Location": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            {
                                                "Ref": "MyBucketF68F3FF0"
                                            },
                                            "/",
                                            "path/to/source.zip"
                                        ]
                                    ]
                                }
                            },
                            "Artifacts": {
                                "Type": "NO_ARTIFACTS"
                            },
                            "ServiceRole": {
                                "Fn::GetAtt": [
                                    "MyProjectRole9BBE5233",
                                    "Arn"
                                ]
                            },
                            "Environment": {
                                "Type": "WINDOWS_CONTAINER",
                                "PrivilegedMode": false,
                                "Image": "aws/codebuild/windows-base:1.0",
                                "ComputeType": "BUILD_GENERAL1_MEDIUM"
                            }
                        }
                    }
                }
            });
            test.done();
        }
    },
    'artifacts': {
        'CodePipeline': {
            'both source and artifacs are set to CodePipeline'(test) {
                const stack = new cdk.Stack();
                new codebuild.Project(stack, 'MyProject', {
                    source: new codebuild.CodePipelineSource(),
                    artifacts: new codebuild.CodePipelineBuildArtifacts()
                });
                assert_1.expect(stack).to(assert_1.haveResource('AWS::CodeBuild::Project', {
                    "Source": {
                        "Type": "CODEPIPELINE"
                    },
                    "Artifacts": {
                        "Type": "CODEPIPELINE"
                    },
                    "ServiceRole": {
                        "Fn::GetAtt": [
                            "MyProjectRole9BBE5233",
                            "Arn"
                        ]
                    },
                    "Environment": {
                        "Type": "LINUX_CONTAINER",
                        "PrivilegedMode": false,
                        "Image": "aws/codebuild/ubuntu-base:14.04",
                        "ComputeType": "BUILD_GENERAL1_SMALL"
                    }
                }));
                test.done();
            },
            'if sourcde is set to CodePipeline, and artifacts are not set, they are defaulted to CodePipeline'(test) {
                const stack = new cdk.Stack();
                new codebuild.Project(stack, 'MyProject', {
                    source: new codebuild.CodePipelineSource()
                });
                assert_1.expect(stack).to(assert_1.haveResource('AWS::CodeBuild::Project', {
                    "Source": {
                        "Type": "CODEPIPELINE"
                    },
                    "Artifacts": {
                        "Type": "CODEPIPELINE"
                    },
                    "ServiceRole": {
                        "Fn::GetAtt": [
                            "MyProjectRole9BBE5233",
                            "Arn"
                        ]
                    },
                    "Environment": {
                        "Type": "LINUX_CONTAINER",
                        "PrivilegedMode": false,
                        "Image": "aws/codebuild/ubuntu-base:14.04",
                        "ComputeType": "BUILD_GENERAL1_SMALL"
                    }
                }));
                test.done();
            },
            'fails if one of source/artifacts is set to CodePipeline and the other isn\'t'(test) {
                const stack = new cdk.Stack();
                test.throws(() => new codebuild.Project(stack, 'MyProject', {
                    source: new codebuild.CodePipelineSource(),
                    artifacts: new codebuild.NoBuildArtifacts()
                }), /Both source and artifacts must be set to CodePipeline/);
                test.throws(() => new codebuild.Project(stack, 'YourProject', {
                    source: new codebuild.CodeCommitSource(new codecommit.Repository(stack, 'MyRepo', { repositoryName: 'boo' })),
                    artifacts: new codebuild.CodePipelineBuildArtifacts()
                }), /Both source and artifacts must be set to CodePipeline/);
                test.done();
            }
        }
    },
    'events'(test) {
        const stack = new cdk.Stack();
        const project = new codebuild.Project(stack, 'MyProject', {
            source: new codebuild.CodePipelineSource()
        });
        project.onBuildFailed('OnBuildFailed');
        project.onBuildSucceeded('OnBuildSucceeded');
        project.onPhaseChange('OnPhaseChange');
        project.onStateChange('OnStateChange');
        project.onBuildStarted('OnBuildStarted');
        assert_1.expect(stack).to(assert_1.haveResource('AWS::Events::Rule', {
            "EventPattern": {
                "source": [
                    "aws.codebuild"
                ],
                "detail-type": [
                    "CodeBuild Build State Change"
                ],
                "detail": {
                    "project-name": [
                        {
                            "Ref": "MyProject39F7B0AE"
                        }
                    ],
                    "build-status": [
                        "FAILED"
                    ]
                }
            },
            "State": "ENABLED"
        }));
        assert_1.expect(stack).to(assert_1.haveResource('AWS::Events::Rule', {
            "EventPattern": {
                "source": [
                    "aws.codebuild"
                ],
                "detail-type": [
                    "CodeBuild Build State Change"
                ],
                "detail": {
                    "project-name": [
                        {
                            "Ref": "MyProject39F7B0AE"
                        }
                    ],
                    "build-status": [
                        "SUCCEEDED"
                    ]
                }
            },
            "State": "ENABLED"
        }));
        assert_1.expect(stack).to(assert_1.haveResource('AWS::Events::Rule', {
            "EventPattern": {
                "source": [
                    "aws.codebuild"
                ],
                "detail-type": [
                    "CodeBuild Build Phase Change"
                ],
                "detail": {
                    "project-name": [
                        {
                            "Ref": "MyProject39F7B0AE"
                        }
                    ]
                }
            },
            "State": "ENABLED"
        }));
        assert_1.expect(stack).to(assert_1.haveResource('AWS::Events::Rule', {
            "EventPattern": {
                "source": [
                    "aws.codebuild"
                ],
                "detail-type": [
                    "CodeBuild Build State Change"
                ],
                "detail": {
                    "project-name": [
                        {
                            "Ref": "MyProject39F7B0AE"
                        }
                    ]
                }
            },
            "State": "ENABLED"
        }));
        assert_1.expect(stack).to(assert_1.haveResource('AWS::Events::Rule', {
            "EventPattern": {
                "source": [
                    "aws.codebuild"
                ],
                "detail-type": [
                    "CodeBuild Build State Change"
                ],
                "detail": {
                    "project-name": [
                        {
                            "Ref": "MyProject39F7B0AE"
                        }
                    ],
                    "build-status": [
                        "IN_PROGRESS"
                    ]
                }
            },
            "State": "ENABLED"
        }));
        test.done();
    },
    'environment variables can be overridden at the project level'(test) {
        const stack = new cdk.Stack();
        new codebuild.Project(stack, 'Project', {
            source: new codebuild.CodePipelineSource(),
            environment: {
                environmentVariables: {
                    FOO: { value: '1234' },
                    BAR: { value: new cdk.FnConcat('111', '222'), type: codebuild.BuildEnvironmentVariableType.ParameterStore }
                }
            },
            environmentVariables: {
                GOO: { value: 'ABC' },
                FOO: { value: 'OVERRIDE!' }
            }
        });
        assert_1.expect(stack).to(assert_1.haveResource('AWS::CodeBuild::Project', {
            "Source": {
                "Type": "CODEPIPELINE"
            },
            "Artifacts": {
                "Type": "CODEPIPELINE"
            },
            "ServiceRole": {
                "Fn::GetAtt": [
                    "ProjectRole4CCB274E",
                    "Arn"
                ]
            },
            "Environment": {
                "Type": "LINUX_CONTAINER",
                "EnvironmentVariables": [
                    {
                        "Type": "PLAINTEXT",
                        "Value": "OVERRIDE!",
                        "Name": "FOO"
                    },
                    {
                        "Type": "PARAMETER_STORE",
                        "Value": {
                            "Fn::Join": [
                                "",
                                [
                                    "111",
                                    "222"
                                ]
                            ]
                        },
                        "Name": "BAR"
                    },
                    {
                        "Type": "PLAINTEXT",
                        "Value": "ABC",
                        "Name": "GOO"
                    }
                ],
                "PrivilegedMode": false,
                "Image": "aws/codebuild/ubuntu-base:14.04",
                "ComputeType": "BUILD_GENERAL1_SMALL"
            }
        }));
        test.done();
    },
    '.metricXxx() methods can be used to obtain Metrics for CodeBuild projects'(test) {
        const stack = new cdk.Stack();
        const project = new codebuild.Project(stack, 'MyBuildProject', { source: new codebuild.CodePipelineSource() });
        const metricBuilds = project.metricBuilds();
        test.same(metricBuilds.dimensions.ProjectName, project.projectName);
        test.deepEqual(metricBuilds.namespace, 'AWS/CodeBuild');
        test.deepEqual(metricBuilds.statistic, 'sum', 'default stat is SUM');
        test.deepEqual(metricBuilds.metricName, 'Builds');
        const metricDuration = project.metricDuration({ label: 'hello' });
        test.deepEqual(metricDuration.metricName, 'Duration');
        test.deepEqual(metricDuration.label, 'hello');
        test.deepEqual(project.metricFailedBuilds().metricName, 'FailedBuilds');
        test.deepEqual(project.metricSucceededBuilds().metricName, 'SucceededBuilds');
        test.done();
    },
    'using ComputeType.Small with a Windows image fails validation'(test) {
        const stack = new cdk.Stack();
        const invalidEnvironment = {
            buildImage: codebuild.WindowsBuildImage.WIN_SERVER_CORE_2016_BASE,
            computeType: codebuild.ComputeType.Small,
        };
        test.throws(() => {
            new codebuild.Project(stack, 'MyProject', {
                source: new codebuild.CodePipelineSource(),
                environment: invalidEnvironment,
            });
        }, /Windows images do not support the Small ComputeType/);
        test.done();
    }
};
//# sourceMappingURL=data:application/json;base64, 