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

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccComputeSslCertificate_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckComputeSslCertificateDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccComputeSslCertificate_basic,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckComputeSslCertificateExists(
    22  						"google_compute_ssl_certificate.foobar"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func testAccCheckComputeSslCertificateDestroy(s *terraform.State) error {
    30  	config := testAccProvider.Meta().(*Config)
    31  
    32  	for _, rs := range s.RootModule().Resources {
    33  		if rs.Type != "google_compute_ssl_certificate" {
    34  			continue
    35  		}
    36  
    37  		_, err := config.clientCompute.SslCertificates.Get(
    38  			config.Project, rs.Primary.ID).Do()
    39  		if err == nil {
    40  			return fmt.Errorf("SslCertificate still exists")
    41  		}
    42  	}
    43  
    44  	return nil
    45  }
    46  
    47  func testAccCheckComputeSslCertificateExists(n string) resource.TestCheckFunc {
    48  	return func(s *terraform.State) error {
    49  		rs, ok := s.RootModule().Resources[n]
    50  		if !ok {
    51  			return fmt.Errorf("Not found: %s", n)
    52  		}
    53  
    54  		if rs.Primary.ID == "" {
    55  			return fmt.Errorf("No ID is set")
    56  		}
    57  
    58  		config := testAccProvider.Meta().(*Config)
    59  
    60  		found, err := config.clientCompute.SslCertificates.Get(
    61  			config.Project, rs.Primary.ID).Do()
    62  		if err != nil {
    63  			return err
    64  		}
    65  
    66  		if found.Name != rs.Primary.ID {
    67  			return fmt.Errorf("Certificate not found")
    68  		}
    69  
    70  		return nil
    71  	}
    72  }
    73  
    74  var testAccComputeSslCertificate_basic = fmt.Sprintf(`
    75  resource "google_compute_ssl_certificate" "foobar" {
    76  	name = "sslcert-test-%s"
    77  	description = "very descriptive"
    78  	private_key = "${file("test-fixtures/ssl_cert/test.key")}"
    79  	certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
    80  }
    81  `, acctest.RandString(10))