github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/resource_compute_target_http_proxy_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 TestAccComputeTargetHttpProxy_basic(t *testing.T) {
    13  	target := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
    14  	backend := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
    15  	hc := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
    16  	urlmap1 := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
    17  	urlmap2 := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckComputeTargetHttpProxyDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccComputeTargetHttpProxy_basic1(target, backend, hc, urlmap1, urlmap2),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckComputeTargetHttpProxyExists(
    28  						"google_compute_target_http_proxy.foobar"),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccComputeTargetHttpProxy_update(t *testing.T) {
    36  	target := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
    37  	backend := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
    38  	hc := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
    39  	urlmap1 := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
    40  	urlmap2 := fmt.Sprintf("thttp-test-%s", acctest.RandString(10))
    41  
    42  	resource.Test(t, resource.TestCase{
    43  		PreCheck:     func() { testAccPreCheck(t) },
    44  		Providers:    testAccProviders,
    45  		CheckDestroy: testAccCheckComputeTargetHttpProxyDestroy,
    46  		Steps: []resource.TestStep{
    47  			resource.TestStep{
    48  				Config: testAccComputeTargetHttpProxy_basic1(target, backend, hc, urlmap1, urlmap2),
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckComputeTargetHttpProxyExists(
    51  						"google_compute_target_http_proxy.foobar"),
    52  				),
    53  			},
    54  
    55  			resource.TestStep{
    56  				Config: testAccComputeTargetHttpProxy_basic2(target, backend, hc, urlmap1, urlmap2),
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckComputeTargetHttpProxyExists(
    59  						"google_compute_target_http_proxy.foobar"),
    60  				),
    61  			},
    62  		},
    63  	})
    64  }
    65  
    66  func testAccCheckComputeTargetHttpProxyDestroy(s *terraform.State) error {
    67  	config := testAccProvider.Meta().(*Config)
    68  
    69  	for _, rs := range s.RootModule().Resources {
    70  		if rs.Type != "google_compute_target_http_proxy" {
    71  			continue
    72  		}
    73  
    74  		_, err := config.clientCompute.TargetHttpProxies.Get(
    75  			config.Project, rs.Primary.ID).Do()
    76  		if err == nil {
    77  			return fmt.Errorf("TargetHttpProxy still exists")
    78  		}
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func testAccCheckComputeTargetHttpProxyExists(n string) resource.TestCheckFunc {
    85  	return func(s *terraform.State) error {
    86  		rs, ok := s.RootModule().Resources[n]
    87  		if !ok {
    88  			return fmt.Errorf("Not found: %s", n)
    89  		}
    90  
    91  		if rs.Primary.ID == "" {
    92  			return fmt.Errorf("No ID is set")
    93  		}
    94  
    95  		config := testAccProvider.Meta().(*Config)
    96  
    97  		found, err := config.clientCompute.TargetHttpProxies.Get(
    98  			config.Project, rs.Primary.ID).Do()
    99  		if err != nil {
   100  			return err
   101  		}
   102  
   103  		if found.Name != rs.Primary.ID {
   104  			return fmt.Errorf("TargetHttpProxy not found")
   105  		}
   106  
   107  		return nil
   108  	}
   109  }
   110  
   111  func testAccComputeTargetHttpProxy_basic1(target, backend, hc, urlmap1, urlmap2 string) string {
   112  	return fmt.Sprintf(`
   113  	resource "google_compute_target_http_proxy" "foobar" {
   114  		description = "Resource created for Terraform acceptance testing"
   115  		name = "%s"
   116  		url_map = "${google_compute_url_map.foobar1.self_link}"
   117  	}
   118  
   119  	resource "google_compute_backend_service" "foobar" {
   120  		name = "%s"
   121  		health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   122  	}
   123  
   124  	resource "google_compute_http_health_check" "zero" {
   125  		name = "%s"
   126  		request_path = "/"
   127  		check_interval_sec = 1
   128  		timeout_sec = 1
   129  	}
   130  
   131  	resource "google_compute_url_map" "foobar1" {
   132  		name = "%s"
   133  		default_service = "${google_compute_backend_service.foobar.self_link}"
   134  		host_rule {
   135  			hosts = ["mysite.com", "myothersite.com"]
   136  			path_matcher = "boop"
   137  		}
   138  		path_matcher {
   139  			default_service = "${google_compute_backend_service.foobar.self_link}"
   140  			name = "boop"
   141  			path_rule {
   142  				paths = ["/*"]
   143  				service = "${google_compute_backend_service.foobar.self_link}"
   144  			}
   145  		}
   146  		test {
   147  			host = "mysite.com"
   148  			path = "/*"
   149  			service = "${google_compute_backend_service.foobar.self_link}"
   150  		}
   151  	}
   152  
   153  	resource "google_compute_url_map" "foobar2" {
   154  		name = "%s"
   155  		default_service = "${google_compute_backend_service.foobar.self_link}"
   156  		host_rule {
   157  			hosts = ["mysite.com", "myothersite.com"]
   158  			path_matcher = "boop"
   159  		}
   160  		path_matcher {
   161  			default_service = "${google_compute_backend_service.foobar.self_link}"
   162  			name = "boop"
   163  			path_rule {
   164  				paths = ["/*"]
   165  				service = "${google_compute_backend_service.foobar.self_link}"
   166  			}
   167  		}
   168  		test {
   169  			host = "mysite.com"
   170  			path = "/*"
   171  			service = "${google_compute_backend_service.foobar.self_link}"
   172  		}
   173  	}
   174  	`, target, backend, hc, urlmap1, urlmap2)
   175  }
   176  
   177  func testAccComputeTargetHttpProxy_basic2(target, backend, hc, urlmap1, urlmap2 string) string {
   178  	return fmt.Sprintf(`
   179  	resource "google_compute_target_http_proxy" "foobar" {
   180  		description = "Resource created for Terraform acceptance testing"
   181  		name = "%s"
   182  		url_map = "${google_compute_url_map.foobar2.self_link}"
   183  	}
   184  
   185  	resource "google_compute_backend_service" "foobar" {
   186  		name = "%s"
   187  		health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   188  	}
   189  
   190  	resource "google_compute_http_health_check" "zero" {
   191  		name = "%s"
   192  		request_path = "/"
   193  		check_interval_sec = 1
   194  		timeout_sec = 1
   195  	}
   196  
   197  	resource "google_compute_url_map" "foobar1" {
   198  		name = "%s"
   199  		default_service = "${google_compute_backend_service.foobar.self_link}"
   200  		host_rule {
   201  			hosts = ["mysite.com", "myothersite.com"]
   202  			path_matcher = "boop"
   203  		}
   204  		path_matcher {
   205  			default_service = "${google_compute_backend_service.foobar.self_link}"
   206  			name = "boop"
   207  			path_rule {
   208  				paths = ["/*"]
   209  				service = "${google_compute_backend_service.foobar.self_link}"
   210  			}
   211  		}
   212  		test {
   213  			host = "mysite.com"
   214  			path = "/*"
   215  			service = "${google_compute_backend_service.foobar.self_link}"
   216  		}
   217  	}
   218  
   219  	resource "google_compute_url_map" "foobar2" {
   220  		name = "%s"
   221  		default_service = "${google_compute_backend_service.foobar.self_link}"
   222  		host_rule {
   223  			hosts = ["mysite.com", "myothersite.com"]
   224  			path_matcher = "boop"
   225  		}
   226  		path_matcher {
   227  			default_service = "${google_compute_backend_service.foobar.self_link}"
   228  			name = "boop"
   229  			path_rule {
   230  				paths = ["/*"]
   231  				service = "${google_compute_backend_service.foobar.self_link}"
   232  			}
   233  		}
   234  		test {
   235  			host = "mysite.com"
   236  			path = "/*"
   237  			service = "${google_compute_backend_service.foobar.self_link}"
   238  		}
   239  	}
   240  	`, target, backend, hc, urlmap1, urlmap2)
   241  }