github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/heroku/resource_heroku_cert_test.go (about)

     1  package heroku
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/cyberdelia/heroku-go/v3"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccHerokuCert_Basic(t *testing.T) {
    16  	var endpoint heroku.SSLEndpoint
    17  	wd, _ := os.Getwd()
    18  	certificateChainFile := wd + "/test-fixtures/terraform.cert"
    19  	certificateChainBytes, _ := ioutil.ReadFile(certificateChainFile)
    20  	certificateChain := string(certificateChainBytes)
    21  	appName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
    22  	testAccCheckHerokuCertConfig_basic := `
    23      resource "heroku_app" "foobar" {
    24          name = "` + appName + `"
    25          region = "eu"
    26      }
    27  
    28      resource "heroku_addon" "ssl" {
    29          app = "${heroku_app.foobar.name}"
    30          plan = "ssl:endpoint"
    31      }
    32  
    33      resource "heroku_cert" "ssl_certificate" {
    34          app = "${heroku_app.foobar.name}"
    35          depends_on = ["heroku_addon.ssl"]
    36          certificate_chain="${file("` + certificateChainFile + `")}"
    37          private_key="${file("` + wd + `/test-fixtures/terraform.key")}"
    38      }
    39      `
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccCheckHerokuCertDestroy,
    45  		Steps: []resource.TestStep{
    46  			resource.TestStep{
    47  				Config: testAccCheckHerokuCertConfig_basic,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckHerokuCertExists("heroku_cert.ssl_certificate", &endpoint),
    50  					testAccCheckHerokuCertificateChain(&endpoint, certificateChain),
    51  					resource.TestCheckResourceAttr(
    52  						"heroku_cert.ssl_certificate",
    53  						"cname", fmt.Sprintf("%s.herokuapp.com", appName)),
    54  				),
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func testAccCheckHerokuCertDestroy(s *terraform.State) error {
    61  	client := testAccProvider.Meta().(*heroku.Service)
    62  
    63  	for _, rs := range s.RootModule().Resources {
    64  		if rs.Type != "heroku_cert" {
    65  			continue
    66  		}
    67  
    68  		_, err := client.SSLEndpointInfo(rs.Primary.Attributes["app"], rs.Primary.ID)
    69  
    70  		if err == nil {
    71  			return fmt.Errorf("Cerfificate still exists")
    72  		}
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func testAccCheckHerokuCertificateChain(endpoint *heroku.SSLEndpoint, chain string) resource.TestCheckFunc {
    79  	return func(s *terraform.State) error {
    80  
    81  		if endpoint.CertificateChain != chain {
    82  			return fmt.Errorf("Bad certificate chain: %s", endpoint.CertificateChain)
    83  		}
    84  
    85  		return nil
    86  	}
    87  }
    88  
    89  func testAccCheckHerokuCertExists(n string, endpoint *heroku.SSLEndpoint) resource.TestCheckFunc {
    90  	return func(s *terraform.State) error {
    91  		rs, ok := s.RootModule().Resources[n]
    92  
    93  		if !ok {
    94  			return fmt.Errorf("Not found: %s", n)
    95  		}
    96  
    97  		if rs.Primary.ID == "" {
    98  			return fmt.Errorf("No SSL endpoint ID is set")
    99  		}
   100  
   101  		client := testAccProvider.Meta().(*heroku.Service)
   102  
   103  		foundEndpoint, err := client.SSLEndpointInfo(rs.Primary.Attributes["app"], rs.Primary.ID)
   104  
   105  		if err != nil {
   106  			return err
   107  		}
   108  
   109  		if foundEndpoint.ID != rs.Primary.ID {
   110  			return fmt.Errorf("SSL endpoint not found")
   111  		}
   112  
   113  		*endpoint = *foundEndpoint
   114  
   115  		return nil
   116  	}
   117  }