github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_target_https_proxy_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccComputeTargetHttpsProxy_basic(t *testing.T) {
    12  
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckComputeTargetHttpsProxyDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccComputeTargetHttpsProxy_basic1,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckComputeTargetHttpsProxyExists(
    22  						"google_compute_target_https_proxy.foobar"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func TestAccComputeTargetHttpsProxy_update(t *testing.T) {
    30  
    31  	resource.Test(t, resource.TestCase{
    32  		PreCheck:     func() { testAccPreCheck(t) },
    33  		Providers:    testAccProviders,
    34  		CheckDestroy: testAccCheckComputeTargetHttpsProxyDestroy,
    35  		Steps: []resource.TestStep{
    36  			resource.TestStep{
    37  				Config: testAccComputeTargetHttpsProxy_basic1,
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testAccCheckComputeTargetHttpsProxyExists(
    40  						"google_compute_target_https_proxy.foobar"),
    41  				),
    42  			},
    43  
    44  			resource.TestStep{
    45  				Config: testAccComputeTargetHttpsProxy_basic2,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckComputeTargetHttpsProxyExists(
    48  						"google_compute_target_https_proxy.foobar"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testAccCheckComputeTargetHttpsProxyDestroy(s *terraform.State) error {
    56  	config := testAccProvider.Meta().(*Config)
    57  
    58  	for _, rs := range s.RootModule().Resources {
    59  		if rs.Type != "google_compute_target_https_proxy" {
    60  			continue
    61  		}
    62  
    63  		_, err := config.clientCompute.TargetHttpsProxies.Get(
    64  			config.Project, rs.Primary.ID).Do()
    65  		if err == nil {
    66  			return fmt.Errorf("TargetHttpsProxy still exists")
    67  		}
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func testAccCheckComputeTargetHttpsProxyExists(n string) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		rs, ok := s.RootModule().Resources[n]
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", n)
    78  		}
    79  
    80  		if rs.Primary.ID == "" {
    81  			return fmt.Errorf("No ID is set")
    82  		}
    83  
    84  		config := testAccProvider.Meta().(*Config)
    85  
    86  		found, err := config.clientCompute.TargetHttpsProxies.Get(
    87  			config.Project, rs.Primary.ID).Do()
    88  		if err != nil {
    89  			return err
    90  		}
    91  
    92  		if found.Name != rs.Primary.ID {
    93  			return fmt.Errorf("TargetHttpsProxy not found")
    94  		}
    95  
    96  		return nil
    97  	}
    98  }
    99  
   100  const testAccComputeTargetHttpsProxy_basic1 = `
   101  resource "google_compute_target_https_proxy" "foobar" {
   102  	description = "Resource created for Terraform acceptance testing"
   103  	name = "terraform-test"
   104  	url_map = "${google_compute_url_map.foobar.self_link}"
   105  	ssl_certificates = ["${google_compute_ssl_certificate.foobar1.self_link}"]
   106  }
   107  
   108  resource "google_compute_backend_service" "foobar" {
   109  	name = "service"
   110  	health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   111  }
   112  
   113  resource "google_compute_http_health_check" "zero" {
   114  	name = "tf-test-zero"
   115  	request_path = "/"
   116  	check_interval_sec = 1
   117  	timeout_sec = 1
   118  }
   119  
   120  resource "google_compute_url_map" "foobar" {
   121  	name = "myurlmap"
   122  	default_service = "${google_compute_backend_service.foobar.self_link}"
   123  	host_rule {
   124  		hosts = ["mysite.com", "myothersite.com"]
   125  		path_matcher = "boop"
   126  	}
   127  	path_matcher {
   128  		default_service = "${google_compute_backend_service.foobar.self_link}"
   129  		name = "boop"
   130  		path_rule {
   131  			paths = ["/*"]
   132  			service = "${google_compute_backend_service.foobar.self_link}"
   133  		}
   134  	}
   135  	test {
   136  		host = "mysite.com"
   137  		path = "/*"
   138  		service = "${google_compute_backend_service.foobar.self_link}"
   139  	}
   140  }
   141  
   142  resource "google_compute_ssl_certificate" "foobar1" {
   143  	name = "terraform-test1"
   144  	description = "very descriptive"
   145  	private_key = "${file("test-fixtures/ssl_cert/test.key")}"
   146  	certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
   147  }
   148  
   149  resource "google_compute_ssl_certificate" "foobar2" {
   150  	name = "terraform-test2"
   151  	description = "very descriptive"
   152  	private_key = "${file("test-fixtures/ssl_cert/test.key")}"
   153  	certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
   154  }
   155  `
   156  
   157  const testAccComputeTargetHttpsProxy_basic2 = `
   158  resource "google_compute_target_https_proxy" "foobar" {
   159  	description = "Resource created for Terraform acceptance testing"
   160  	name = "terraform-test"
   161  	url_map = "${google_compute_url_map.foobar.self_link}"
   162  	ssl_certificates = ["${google_compute_ssl_certificate.foobar1.self_link}"]
   163  }
   164  
   165  resource "google_compute_backend_service" "foobar" {
   166  	name = "service"
   167  	health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   168  }
   169  
   170  resource "google_compute_http_health_check" "zero" {
   171  	name = "tf-test-zero"
   172  	request_path = "/"
   173  	check_interval_sec = 1
   174  	timeout_sec = 1
   175  }
   176  
   177  resource "google_compute_url_map" "foobar" {
   178  	name = "myurlmap"
   179  	default_service = "${google_compute_backend_service.foobar.self_link}"
   180  	host_rule {
   181  		hosts = ["mysite.com", "myothersite.com"]
   182  		path_matcher = "boop"
   183  	}
   184  	path_matcher {
   185  		default_service = "${google_compute_backend_service.foobar.self_link}"
   186  		name = "boop"
   187  		path_rule {
   188  			paths = ["/*"]
   189  			service = "${google_compute_backend_service.foobar.self_link}"
   190  		}
   191  	}
   192  	test {
   193  		host = "mysite.com"
   194  		path = "/*"
   195  		service = "${google_compute_backend_service.foobar.self_link}"
   196  	}
   197  }
   198  
   199  resource "google_compute_ssl_certificate" "foobar1" {
   200  	name = "terraform-test1"
   201  	description = "very descriptive"
   202  	private_key = "${file("test-fixtures/ssl_cert/test.key")}"
   203  	certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
   204  }
   205  
   206  resource "google_compute_ssl_certificate" "foobar2" {
   207  	name = "terraform-test2"
   208  	description = "very descriptive"
   209  	private_key = "${file("test-fixtures/ssl_cert/test.key")}"
   210  	certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
   211  }
   212  `