Skip to content

Commit

Permalink
Merge pull request #3026 from terraform-providers/t-apig-domain-fix
Browse files Browse the repository at this point in the history
test/aws_api_gateway_*: Fix invalid domain
  • Loading branch information
radeksimko authored Jan 17, 2018
2 parents e1ae713 + d820a44 commit 825a9bf
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 143 deletions.
40 changes: 19 additions & 21 deletions aws/resource_aws_api_gateway_base_path_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
Expand All @@ -15,11 +16,11 @@ func TestAccAWSAPIGatewayBasePath_basic(t *testing.T) {
var conf apigateway.BasePathMapping

// Our test cert is for a wildcard on this domain
name := fmt.Sprintf("%s.tf-acc.invalid", resource.UniqueId())
name := fmt.Sprintf("tf-acc-%s.terraformtest.com", acctest.RandString(8))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Providers: testAccProvidersWithTLS,
CheckDestroy: testAccCheckAWSAPIGatewayBasePathDestroy(name),
Steps: []resource.TestStep{
{
Expand All @@ -37,11 +38,11 @@ func TestAccAWSAPIGatewayEmptyBasePath_basic(t *testing.T) {
var conf apigateway.BasePathMapping

// Our test cert is for a wildcard on this domain
name := fmt.Sprintf("%s.tf-acc.invalid", resource.UniqueId())
name := fmt.Sprintf("tf-acc-%s.terraformtest.com", acctest.RandString(8))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Providers: testAccProvidersWithTLS,
CheckDestroy: testAccCheckAWSAPIGatewayBasePathDestroy(name),
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -142,12 +143,13 @@ func testAccCheckAWSAPIGatewayBasePathDestroy(name string) resource.TestCheckFun
}
}

func testAccAWSAPIGatewayBasePathConfig(name string) string {
func testAccAWSAPIGatewayBasePathConfig(domainName string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_rest_api" "test" {
name = "tf-acc-apigateway-base-path-mapping"
description = "Terraform Acceptance Tests"
}
# API gateway won't let us deploy an empty API
resource "aws_api_gateway_resource" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
Expand Down Expand Up @@ -180,17 +182,15 @@ resource "aws_api_gateway_base_path_mapping" "test" {
resource "aws_api_gateway_domain_name" "test" {
domain_name = "%s"
certificate_name = "tf-apigateway-base-path-mapping-test"
certificate_body = <<EOF
%vEOF
certificate_chain = <<EOF
%vEOF
certificate_private_key = <<EOF
%vEOF
certificate_body = "${tls_locally_signed_cert.leaf.cert_pem}"
certificate_chain = "${tls_self_signed_cert.ca.cert_pem}"
certificate_private_key = "${tls_private_key.test.private_key_pem}"
}
`, name, testAccAWSAPIGatewayCertBody, testAccAWSAPIGatewayCertChain, testAccAWSAPIGatewayCertPrivateKey)
%s
`, domainName, testAccAWSAPIGatewayCerts(domainName))
}

func testAccAWSAPIGatewayEmptyBasePathConfig(name string) string {
func testAccAWSAPIGatewayEmptyBasePathConfig(domainName string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_rest_api" "test" {
name = "tf-acc-apigateway-base-path-mapping"
Expand Down Expand Up @@ -222,12 +222,10 @@ resource "aws_api_gateway_base_path_mapping" "test" {
resource "aws_api_gateway_domain_name" "test" {
domain_name = "%s"
certificate_name = "tf-apigateway-base-path-mapping-test"
certificate_body = <<EOF
%vEOF
certificate_chain = <<EOF
%vEOF
certificate_private_key = <<EOF
%vEOF
}
`, name, testAccAWSAPIGatewayCertBody, testAccAWSAPIGatewayCertChain, testAccAWSAPIGatewayCertPrivateKey)
certificate_body = "${tls_locally_signed_cert.leaf.cert_pem}"
certificate_chain = "${tls_self_signed_cert.ca.cert_pem}"
certificate_private_key = "${tls_private_key.test.private_key_pem}"
}
%s
`, domainName, testAccAWSAPIGatewayCerts(domainName))
}
192 changes: 70 additions & 122 deletions aws/resource_aws_api_gateway_domain_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,52 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSAPIGatewayDomainName_basic(t *testing.T) {
var conf apigateway.DomainName

// Our test cert is for a wildcard on this domain
uniqueId := resource.UniqueId()
name := fmt.Sprintf("%s.tf-acc.invalid", uniqueId)
nameModified := fmt.Sprintf("test-acc.%s.tf-acc.invalid", uniqueId)
rString := acctest.RandString(8)
name := fmt.Sprintf("tf-acc-%s.terraformtest.com", rString)
nameModified := fmt.Sprintf("tf-acc-%s-modified.terraformtest.com", rString)
commonName := "*.terraformtest.com"
certRe := regexp.MustCompile("^-----BEGIN CERTIFICATE-----\n")
keyRe := regexp.MustCompile("^-----BEGIN RSA PRIVATE KEY-----\n")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Providers: testAccProvidersWithTLS,
CheckDestroy: testAccCheckAWSAPIGatewayDomainNameDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAPIGatewayDomainNameConfigCreate(name),
Config: testAccAWSAPIGatewayDomainNameConfig(name, commonName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayDomainNameExists("aws_api_gateway_domain_name.test", &conf),
resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_body", testAccAWSAPIGatewayCertBody),
resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_chain", testAccAWSAPIGatewayCertChain),
resource.TestMatchResourceAttr("aws_api_gateway_domain_name.test", "certificate_body", certRe),
resource.TestMatchResourceAttr("aws_api_gateway_domain_name.test", "certificate_chain", certRe),
resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_name", "tf-acc-apigateway-domain-name"),
resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_private_key", testAccAWSAPIGatewayCertPrivateKey),
resource.TestMatchResourceAttr("aws_api_gateway_domain_name.test", "certificate_private_key", keyRe),
resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "domain_name", name),
resource.TestCheckResourceAttrSet("aws_api_gateway_domain_name.test", "certificate_upload_date"),
),
},
{
Config: testAccAWSAPIGatewayDomainNameConfigUpdate(name),
Config: testAccAWSAPIGatewayDomainNameConfig(nameModified, commonName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayDomainNameExists("aws_api_gateway_domain_name.test", &conf),
resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_body", testAccAWSAPIGatewayCertBody),
resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_chain", testAccAWSAPIGatewayCertChain),
resource.TestMatchResourceAttr("aws_api_gateway_domain_name.test", "certificate_body", certRe),
resource.TestMatchResourceAttr("aws_api_gateway_domain_name.test", "certificate_chain", certRe),
resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_name", "tf-acc-apigateway-domain-name"),
resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "certificate_private_key", testAccAWSAPIGatewayCertPrivateKey),
resource.TestMatchResourceAttr("aws_api_gateway_domain_name.test", "certificate_private_key", keyRe),
resource.TestCheckResourceAttr("aws_api_gateway_domain_name.test", "domain_name", nameModified),
resource.TestCheckResourceAttrSet("aws_api_gateway_domain_name.test", "certificate_upload_date"),
),
Expand Down Expand Up @@ -114,121 +118,65 @@ func testAccCheckAWSAPIGatewayDomainNameDestroy(s *terraform.State) error {
return nil
}

// Expires on August 20, 2026
const testAccAWSAPIGatewayCertBody = `-----BEGIN CERTIFICATE-----
MIIEfjCCA2agAwIBAgIRAPHiUbRWC/Ff/reiyzqh3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fdaGfmp6WLhO0pSnl4c06bXnvwqbD
DhhT6UvMs6m/uSufwtfCGO48yYiY1z6DqBjuHCMCgYBWJyrltHbSu8D4cgucFRiB
PPJ5DDCkIhmgYYWA7R7CvEB/OxyppvFj+RtYMYqNg8xWRH1DA7rhOw/5x4ZB8lGc
cRbrZbBp033YdkdV3r9IAoz5aoNgoaSq+Yk0KIeU2FYqRXl2FltqYL+aQgJmjR5Z
fxz8Xvy9qtlfuWcDM/e24QKBgQDGtH8mk+lCfUkfRuh4UJCaHnGSif5grS2R9ZZA
n18rpbThd9qS6reXYgUm/5Hs8KRBzqX5cS4qY4rlw2XRIPMxfU6lWbFh96KToPFx
MD1+L5JxpbRFpGnsYvYdCmHxy03r03wojRAcH7JU6o9U7j936hDTLjqmq7LRhid5
goFwlQKBgQCGxTWqQu9FJjtmjdM/ARi7CcwWT40NI8ybyebjDtElekILFMUwO+/w
M78Me3d5g/oyeS6qCfyK1f7vBb/WhxLBBegaqMGUWbRoKYD0LH5U3zkqZ9ehnDFB
vhcEnoxPD9HmdFkRduJscyzVNJ50lMuQ3uk7zwiYuS/Z7shH81xzMQ==
-----END RSA PRIVATE KEY-----
`

func testAccAWSAPIGatewayDomainNameConfigCreate(name string) string {
func testAccAWSAPIGatewayCerts(commonName string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_domain_name" "test" {
domain_name = "%s"
certificate_body = <<EOF
%vEOF
certificate_chain = <<EOF
%vEOF
certificate_name = "tf-acc-apigateway-domain-name"
certificate_private_key = <<EOF
%vEOF
resource "tls_private_key" "test" {
algorithm = "RSA"
}
`, name, testAccAWSAPIGatewayCertBody, testAccAWSAPIGatewayCertChain, testAccAWSAPIGatewayCertPrivateKey)
resource "tls_self_signed_cert" "ca" {
key_algorithm = "RSA"
private_key_pem = "${tls_private_key.test.private_key_pem}"
is_ca_certificate = true
validity_period_hours = 12
subject {
common_name = "ACME Root CA"
organization = "ACME Example Holdings"
}
allowed_uses = [
"key_encipherment",
"digital_signature",
"server_auth",
]
}
func testAccAWSAPIGatewayDomainNameConfigUpdate(name string) string {
resource "tls_cert_request" "test" {
key_algorithm = "RSA"
private_key_pem = "${tls_private_key.test.private_key_pem}"
subject {
common_name = "%s"
organization = "ACME Example Holdings, Inc"
}
}
resource "tls_locally_signed_cert" "leaf" {
cert_request_pem = "${tls_cert_request.test.cert_request_pem}"
ca_key_algorithm = "RSA"
ca_private_key_pem = "${tls_private_key.test.private_key_pem}"
ca_cert_pem = "${tls_self_signed_cert.ca.cert_pem}"
validity_period_hours = 12
allowed_uses = [
"key_encipherment",
"digital_signature",
"server_auth",
]
}
`, commonName)
}

func testAccAWSAPIGatewayDomainNameConfig(domainName, commonName string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_domain_name" "test" {
domain_name = "test-acc.%s"
certificate_body = <<EOF
%vEOF
certificate_chain = <<EOF
%vEOF
domain_name = "%s"
certificate_body = "${tls_locally_signed_cert.leaf.cert_pem}"
certificate_chain = "${tls_self_signed_cert.ca.cert_pem}"
certificate_name = "tf-acc-apigateway-domain-name"
certificate_private_key = <<EOF
%vEOF
certificate_private_key = "${tls_private_key.test.private_key_pem}"
}
`, name, testAccAWSAPIGatewayCertBody, testAccAWSAPIGatewayCertChain, testAccAWSAPIGatewayCertPrivateKey)
%s
`, domainName, testAccAWSAPIGatewayCerts(commonName))
}

0 comments on commit 825a9bf

Please sign in to comment.