github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 TestAccComputeSslCertificate_no_name(t *testing.T) {
    30  	resource.Test(t, resource.TestCase{
    31  		PreCheck:     func() { testAccPreCheck(t) },
    32  		Providers:    testAccProviders,
    33  		CheckDestroy: testAccCheckComputeSslCertificateDestroy,
    34  		Steps: []resource.TestStep{
    35  			resource.TestStep{
    36  				Config: testAccComputeSslCertificate_no_name,
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckComputeSslCertificateExists(
    39  						"google_compute_ssl_certificate.foobar"),
    40  				),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func TestAccComputeSslCertificate_name_prefix(t *testing.T) {
    47  	resource.Test(t, resource.TestCase{
    48  		PreCheck:     func() { testAccPreCheck(t) },
    49  		Providers:    testAccProviders,
    50  		CheckDestroy: testAccCheckComputeSslCertificateDestroy,
    51  		Steps: []resource.TestStep{
    52  			resource.TestStep{
    53  				Config: testAccComputeSslCertificate_name_prefix,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccCheckComputeSslCertificateExists(
    56  						"google_compute_ssl_certificate.foobar"),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func testAccCheckComputeSslCertificateDestroy(s *terraform.State) error {
    64  	config := testAccProvider.Meta().(*Config)
    65  
    66  	for _, rs := range s.RootModule().Resources {
    67  		if rs.Type != "google_compute_ssl_certificate" {
    68  			continue
    69  		}
    70  
    71  		_, err := config.clientCompute.SslCertificates.Get(
    72  			config.Project, rs.Primary.ID).Do()
    73  		if err == nil {
    74  			return fmt.Errorf("SslCertificate still exists")
    75  		}
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  func testAccCheckComputeSslCertificateExists(n string) resource.TestCheckFunc {
    82  	return func(s *terraform.State) error {
    83  		rs, ok := s.RootModule().Resources[n]
    84  		if !ok {
    85  			return fmt.Errorf("Not found: %s", n)
    86  		}
    87  
    88  		if rs.Primary.ID == "" {
    89  			return fmt.Errorf("No ID is set")
    90  		}
    91  
    92  		config := testAccProvider.Meta().(*Config)
    93  
    94  		found, err := config.clientCompute.SslCertificates.Get(
    95  			config.Project, rs.Primary.ID).Do()
    96  		if err != nil {
    97  			return err
    98  		}
    99  
   100  		if found.Name != rs.Primary.ID {
   101  			return fmt.Errorf("Certificate not found")
   102  		}
   103  
   104  		return nil
   105  	}
   106  }
   107  
   108  var testAccComputeSslCertificate_basic = fmt.Sprintf(`
   109  resource "google_compute_ssl_certificate" "foobar" {
   110  	name = "sslcert-test-%s"
   111  	description = "very descriptive"
   112  	private_key = "${file("test-fixtures/ssl_cert/test.key")}"
   113  	certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
   114  }
   115  `, acctest.RandString(10))
   116  
   117  var testAccComputeSslCertificate_no_name = fmt.Sprintf(`
   118  resource "google_compute_ssl_certificate" "foobar" {
   119  	description = "really descriptive"
   120  	private_key = "${file("test-fixtures/ssl_cert/test.key")}"
   121  	certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
   122  }
   123  `)
   124  
   125  var testAccComputeSslCertificate_name_prefix = fmt.Sprintf(`
   126  resource "google_compute_ssl_certificate" "foobar" {
   127  	name_prefix = "sslcert-test-%s-"
   128  	description = "extremely descriptive"
   129  	private_key = "${file("test-fixtures/ssl_cert/test.key")}"
   130  	certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
   131  }
   132  `, acctest.RandString(10))