github.com/meteor/terraform@v0.6.15-0.20210412225145-79ec4bc057c6/builtin/providers/heroku/resource_heroku_cert_test.go (about)

     1  package heroku
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"regexp"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/cyberdelia/heroku-go/v3"
    13  	"github.com/hashicorp/terraform/helper/acctest"
    14  	"github.com/hashicorp/terraform/helper/resource"
    15  	"github.com/hashicorp/terraform/terraform"
    16  )
    17  
    18  // We break apart testing for EU and US because at present, Heroku deals with
    19  // each a bit differently and the setup/teardown of separate tests seems to
    20  // help them to perform more consistently.
    21  // https://devcenter.heroku.com/articles/ssl-endpoint#add-certificate-and-intermediaries
    22  func TestAccHerokuCert_EU(t *testing.T) {
    23  	var endpoint heroku.SSLEndpointInfoResult
    24  	appName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
    25  
    26  	wd, _ := os.Getwd()
    27  	certFile := wd + "/test-fixtures/terraform.cert"
    28  	certFile2 := wd + "/test-fixtures/terraform2.cert"
    29  	keyFile := wd + "/test-fixtures/terraform.key"
    30  	keyFile2 := wd + "/test-fixtures/terraform2.key"
    31  
    32  	certificateChainBytes, _ := ioutil.ReadFile(certFile)
    33  	certificateChain := string(certificateChainBytes)
    34  	certificateChain2Bytes, _ := ioutil.ReadFile(certFile2)
    35  	certificateChain2 := string(certificateChain2Bytes)
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckHerokuCertDestroy,
    41  		Steps: []resource.TestStep{
    42  			{
    43  				Config: testAccCheckHerokuCertEUConfig(appName, certFile, keyFile),
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckHerokuCertExists("heroku_cert.ssl_certificate", &endpoint),
    46  					testAccCheckHerokuCertificateChain(&endpoint, certificateChain),
    47  					resource.TestCheckResourceAttr(
    48  						"heroku_cert.ssl_certificate",
    49  						"cname", fmt.Sprintf("%s.herokuapp.com", appName)),
    50  				),
    51  			},
    52  			{
    53  				Config: testAccCheckHerokuCertEUConfig(appName, certFile2, keyFile2),
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccCheckHerokuCertExists("heroku_cert.ssl_certificate", &endpoint),
    56  					testAccCheckHerokuCertificateChain(&endpoint, certificateChain2),
    57  					resource.TestCheckResourceAttr(
    58  						"heroku_cert.ssl_certificate",
    59  						"cname", fmt.Sprintf("%s.herokuapp.com", appName)),
    60  				),
    61  			},
    62  		},
    63  	})
    64  }
    65  
    66  func TestAccHerokuCert_US(t *testing.T) {
    67  	var endpoint heroku.SSLEndpointInfoResult
    68  	appName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
    69  
    70  	wd, _ := os.Getwd()
    71  	certFile := wd + "/test-fixtures/terraform.cert"
    72  	certFile2 := wd + "/test-fixtures/terraform2.cert"
    73  	keyFile := wd + "/test-fixtures/terraform.key"
    74  	keyFile2 := wd + "/test-fixtures/terraform2.key"
    75  
    76  	certificateChainBytes, _ := ioutil.ReadFile(certFile)
    77  	certificateChain := string(certificateChainBytes)
    78  	certificateChain2Bytes, _ := ioutil.ReadFile(certFile2)
    79  	certificateChain2 := string(certificateChain2Bytes)
    80  
    81  	resource.Test(t, resource.TestCase{
    82  		PreCheck:     func() { testAccPreCheck(t) },
    83  		Providers:    testAccProviders,
    84  		CheckDestroy: testAccCheckHerokuCertDestroy,
    85  		Steps: []resource.TestStep{
    86  			{
    87  				Config: testAccCheckHerokuCertUSConfig(appName, certFile2, keyFile2),
    88  				Check: resource.ComposeTestCheckFunc(
    89  					testAccCheckHerokuCertExists("heroku_cert.ssl_certificate", &endpoint),
    90  					testAccCheckHerokuCertificateChain(&endpoint, certificateChain2),
    91  					resource.TestMatchResourceAttr(
    92  						"heroku_cert.ssl_certificate",
    93  						"cname", regexp.MustCompile(`herokussl`)),
    94  				),
    95  			},
    96  			{
    97  				Config: testAccCheckHerokuCertUSConfig(appName, certFile, keyFile),
    98  				Check: resource.ComposeTestCheckFunc(
    99  					testAccCheckHerokuCertExists("heroku_cert.ssl_certificate", &endpoint),
   100  					testAccCheckHerokuCertificateChain(&endpoint, certificateChain),
   101  					resource.TestMatchResourceAttr(
   102  						"heroku_cert.ssl_certificate",
   103  						"cname", regexp.MustCompile(`herokussl`)),
   104  				),
   105  			},
   106  		},
   107  	})
   108  }
   109  
   110  func testAccCheckHerokuCertEUConfig(appName, certFile, keyFile string) string {
   111  	return strings.TrimSpace(fmt.Sprintf(`
   112  resource "heroku_app" "foobar" {
   113    name = "%s"
   114    region = "eu"
   115  }
   116  
   117  resource "heroku_addon" "ssl" {
   118    app = "${heroku_app.foobar.name}"
   119    plan = "ssl:endpoint"
   120  }
   121  
   122  resource "heroku_cert" "ssl_certificate" {
   123    app = "${heroku_app.foobar.name}"
   124    depends_on = ["heroku_addon.ssl"]
   125    certificate_chain="${file("%s")}"
   126    private_key="${file("%s")}"
   127  }`, appName, certFile, keyFile))
   128  }
   129  
   130  func testAccCheckHerokuCertUSConfig(appName, certFile, keyFile string) string {
   131  	return strings.TrimSpace(fmt.Sprintf(`
   132  resource "heroku_app" "foobar" {
   133    name = "%s"
   134    region = "us"
   135  }
   136  
   137  resource "heroku_addon" "ssl" {
   138    app = "${heroku_app.foobar.name}"
   139    plan = "ssl:endpoint"
   140  }
   141  
   142  resource "heroku_cert" "ssl_certificate" {
   143    app = "${heroku_app.foobar.name}"
   144    depends_on = ["heroku_addon.ssl"]
   145    certificate_chain="${file("%s")}"
   146    private_key="${file("%s")}"
   147  }`, appName, certFile, keyFile))
   148  }
   149  
   150  func testAccCheckHerokuCertDestroy(s *terraform.State) error {
   151  	client := testAccProvider.Meta().(*heroku.Service)
   152  
   153  	for _, rs := range s.RootModule().Resources {
   154  		if rs.Type != "heroku_cert" {
   155  			continue
   156  		}
   157  
   158  		_, err := client.SSLEndpointInfo(context.TODO(), rs.Primary.Attributes["app"], rs.Primary.ID)
   159  
   160  		if err == nil {
   161  			return fmt.Errorf("Cerfificate still exists")
   162  		}
   163  	}
   164  
   165  	return nil
   166  }
   167  
   168  func testAccCheckHerokuCertificateChain(endpoint *heroku.SSLEndpointInfoResult, chain string) resource.TestCheckFunc {
   169  	return func(s *terraform.State) error {
   170  
   171  		if endpoint.CertificateChain != chain {
   172  			return fmt.Errorf("Bad certificate chain: %s", endpoint.CertificateChain)
   173  		}
   174  
   175  		return nil
   176  	}
   177  }
   178  
   179  func testAccCheckHerokuCertExists(n string, endpoint *heroku.SSLEndpointInfoResult) resource.TestCheckFunc {
   180  	return func(s *terraform.State) error {
   181  		rs, ok := s.RootModule().Resources[n]
   182  
   183  		if !ok {
   184  			return fmt.Errorf("Not found: %s", n)
   185  		}
   186  
   187  		if rs.Primary.ID == "" {
   188  			return fmt.Errorf("No SSL endpoint ID is set")
   189  		}
   190  
   191  		client := testAccProvider.Meta().(*heroku.Service)
   192  
   193  		foundEndpoint, err := client.SSLEndpointInfo(context.TODO(), rs.Primary.Attributes["app"], rs.Primary.ID)
   194  
   195  		if err != nil {
   196  			return err
   197  		}
   198  
   199  		if foundEndpoint.ID != rs.Primary.ID {
   200  			return fmt.Errorf("SSL endpoint not found")
   201  		}
   202  
   203  		*endpoint = *foundEndpoint
   204  
   205  		return nil
   206  	}
   207  }