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