Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service/codepipeline: Update acceptance test with ARN testing check function #13041

Merged
merged 3 commits into from
Apr 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 126 additions & 117 deletions aws/resource_aws_codepipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestAccAWSCodePipeline_basic(t *testing.T) {
})
}

func TestAccAWSCodePipeline_emptyArtifacts(t *testing.T) {
func TestAccAWSCodePipeline_emptyStageArtifacts(t *testing.T) {
var p codepipeline.PipelineDeclaration
name := acctest.RandString(10)
resourceName := "aws_codepipeline.test"
Expand All @@ -121,11 +121,20 @@ func TestAccAWSCodePipeline_emptyArtifacts(t *testing.T) {
CheckDestroy: testAccCheckAWSCodePipelineDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodePipelineConfig_emptyArtifacts(name),
Config: testAccAWSCodePipelineConfig_emptyStageArtifacts(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodePipelineExists(resourceName, &p),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "codepipeline", regexp.MustCompile(fmt.Sprintf("test-pipeline-%s", name))),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "codepipeline", regexp.MustCompile(fmt.Sprintf("test-pipeline-%s$", name))),
resource.TestCheckResourceAttr(resourceName, "artifact_store.#", "1"),
resource.TestCheckResourceAttr(resourceName, "stage.#", "2"),
resource.TestCheckResourceAttr(resourceName, "stage.1.name", "Build"),
resource.TestCheckResourceAttr(resourceName, "stage.1.action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "stage.1.action.0.name", "Build"),
resource.TestCheckResourceAttr(resourceName, "stage.1.action.0.category", "Build"),
resource.TestCheckResourceAttr(resourceName, "stage.1.action.0.owner", "AWS"),
resource.TestCheckResourceAttr(resourceName, "stage.1.action.0.provider", "CodeBuild"),
resource.TestCheckResourceAttr(resourceName, "stage.1.action.0.input_artifacts.#", "1"),
resource.TestCheckResourceAttr(resourceName, "stage.1.action.0.output_artifacts.#", "0"),
),
ExpectNonEmptyPlan: true,
},
Expand Down Expand Up @@ -715,7 +724,7 @@ resource "aws_codepipeline" "test" {
`, rName))
}

func testAccAWSCodePipelineConfig_emptyArtifacts(rName string) string {
func testAccAWSCodePipelineConfig_emptyStageArtifacts(rName string) string {
return composeConfig(
testAccAWSCodePipelineS3DefaultBucket(rName),
testAccAWSCodePipelineServiceIAMRole(rName),
Expand Down Expand Up @@ -1264,6 +1273,119 @@ resource "aws_s3_bucket" "%[1]s" {
`, bucket, rName, provider)
}

func testAccAWSCodePipelineConfigWithNamespace(rName string) string {
return fmt.Sprintf(`
resource "aws_codepipeline" "test" {
name = "test-pipeline-%[1]s"
role_arn = "${aws_iam_role.codepipeline_role.arn}"

artifact_store {
location = "${aws_s3_bucket.foo.bucket}"
type = "S3"

encryption_key {
id = "1234"
type = "KMS"
}
}

stage {
name = "Source"

action {
name = "Source"
category = "Source"
owner = "ThirdParty"
provider = "GitHub"
version = "1"
output_artifacts = ["test"]
namespace = "SourceVariables"

configuration = {
Owner = "lifesum-terraform"
Repo = "test"
Branch = "master"
}
}
}

stage {
name = "Build"

action {
name = "Build"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["test"]
version = "1"

configuration = {
ProjectName = "test"
}
}
}
}

resource "aws_s3_bucket" "foo" {
bucket = "tf-test-pipeline-%[1]s"
acl = "private"
}

resource "aws_iam_role" "codepipeline_role" {
name = "codepipeline-role-%[1]s"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "codepipeline.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

resource "aws_iam_role_policy" "codepipeline_policy" {
name = "codepipeline_policy"
role = "${aws_iam_role.codepipeline_role.id}"

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect":"Allow",
"Action": [
"s3:GetObject",
"s3:GetObjectVersion",
"s3:GetBucketVersioning"
],
"Resource": [
"${aws_s3_bucket.foo.arn}",
"${aws_s3_bucket.foo.arn}/*"
]
},
{
"Effect": "Allow",
"Action": [
"codebuild:BatchGetBuilds",
"codebuild:StartBuild"
],
"Resource": "*"
}
]
}
EOF
}
`, rName)
}

func TestResourceAWSCodePipelineExpandArtifactStoresValidation(t *testing.T) {
cases := []struct {
Name string
Expand Down Expand Up @@ -1383,116 +1505,3 @@ func TestResourceAWSCodePipelineExpandArtifactStoresValidation(t *testing.T) {
}
}
}

func testAccAWSCodePipelineConfigWithNamespace(rName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "foo" {
bucket = "tf-test-pipeline-%s"
acl = "private"
}

resource "aws_iam_role" "codepipeline_role" {
name = "codepipeline-role-%s"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "codepipeline.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

resource "aws_iam_role_policy" "codepipeline_policy" {
name = "codepipeline_policy"
role = "${aws_iam_role.codepipeline_role.id}"

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect":"Allow",
"Action": [
"s3:GetObject",
"s3:GetObjectVersion",
"s3:GetBucketVersioning"
],
"Resource": [
"${aws_s3_bucket.foo.arn}",
"${aws_s3_bucket.foo.arn}/*"
]
},
{
"Effect": "Allow",
"Action": [
"codebuild:BatchGetBuilds",
"codebuild:StartBuild"
],
"Resource": "*"
}
]
}
EOF
}

resource "aws_codepipeline" "test" {
name = "test-pipeline-%s"
role_arn = "${aws_iam_role.codepipeline_role.arn}"

artifact_store {
location = "${aws_s3_bucket.foo.bucket}"
type = "S3"

encryption_key {
id = "1234"
type = "KMS"
}
}

stage {
name = "Source"

action {
name = "Source"
category = "Source"
owner = "ThirdParty"
provider = "GitHub"
version = "1"
output_artifacts = ["test"]
namespace = "SourceVariables"

configuration = {
Owner = "lifesum-terraform"
Repo = "test"
Branch = "master"
}
}
}

stage {
name = "Build"

action {
name = "Build"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["test"]
version = "1"

configuration = {
ProjectName = "test"
}
}
}
}
`, rName, rName, rName)
}