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