github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/resource_compute_url_map_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 TestAccComputeUrlMap_basic(t *testing.T) {
    13  	bsName := fmt.Sprintf("urlmap-test-%s", acctest.RandString(10))
    14  	hcName := fmt.Sprintf("urlmap-test-%s", acctest.RandString(10))
    15  	umName := fmt.Sprintf("urlmap-test-%s", acctest.RandString(10))
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckComputeUrlMapDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccComputeUrlMap_basic1(bsName, hcName, umName),
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckComputeUrlMapExists(
    25  						"google_compute_url_map.foobar"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccComputeUrlMap_update_path_matcher(t *testing.T) {
    33  	bsName := fmt.Sprintf("urlmap-test-%s", acctest.RandString(10))
    34  	hcName := fmt.Sprintf("urlmap-test-%s", acctest.RandString(10))
    35  	umName := fmt.Sprintf("urlmap-test-%s", acctest.RandString(10))
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckComputeUrlMapDestroy,
    40  		Steps: []resource.TestStep{
    41  			resource.TestStep{
    42  				Config: testAccComputeUrlMap_basic1(bsName, hcName, umName),
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckComputeUrlMapExists(
    45  						"google_compute_url_map.foobar"),
    46  				),
    47  			},
    48  
    49  			resource.TestStep{
    50  				Config: testAccComputeUrlMap_basic2(bsName, hcName, umName),
    51  				Check: resource.ComposeTestCheckFunc(
    52  					testAccCheckComputeUrlMapExists(
    53  						"google_compute_url_map.foobar"),
    54  				),
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func TestAccComputeUrlMap_advanced(t *testing.T) {
    61  	resource.Test(t, resource.TestCase{
    62  		PreCheck:     func() { testAccPreCheck(t) },
    63  		Providers:    testAccProviders,
    64  		CheckDestroy: testAccCheckComputeUrlMapDestroy,
    65  		Steps: []resource.TestStep{
    66  			resource.TestStep{
    67  				Config: testAccComputeUrlMap_advanced1,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckComputeUrlMapExists(
    70  						"google_compute_url_map.foobar"),
    71  				),
    72  			},
    73  
    74  			resource.TestStep{
    75  				Config: testAccComputeUrlMap_advanced2,
    76  				Check: resource.ComposeTestCheckFunc(
    77  					testAccCheckComputeUrlMapExists(
    78  						"google_compute_url_map.foobar"),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  func testAccCheckComputeUrlMapDestroy(s *terraform.State) error {
    86  	config := testAccProvider.Meta().(*Config)
    87  
    88  	for _, rs := range s.RootModule().Resources {
    89  		if rs.Type != "google_compute_url_map" {
    90  			continue
    91  		}
    92  
    93  		_, err := config.clientCompute.UrlMaps.Get(
    94  			config.Project, rs.Primary.ID).Do()
    95  		if err == nil {
    96  			return fmt.Errorf("Url map still exists")
    97  		}
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  func testAccCheckComputeUrlMapExists(n string) resource.TestCheckFunc {
   104  	return func(s *terraform.State) error {
   105  		rs, ok := s.RootModule().Resources[n]
   106  		if !ok {
   107  			return fmt.Errorf("Not found: %s", n)
   108  		}
   109  
   110  		if rs.Primary.ID == "" {
   111  			return fmt.Errorf("No ID is set")
   112  		}
   113  
   114  		config := testAccProvider.Meta().(*Config)
   115  
   116  		found, err := config.clientCompute.UrlMaps.Get(
   117  			config.Project, rs.Primary.ID).Do()
   118  		if err != nil {
   119  			return err
   120  		}
   121  
   122  		if found.Name != rs.Primary.ID {
   123  			return fmt.Errorf("Url map not found")
   124  		}
   125  		return nil
   126  	}
   127  }
   128  
   129  func testAccComputeUrlMap_basic1(bsName, hcName, umName string) string {
   130  	return fmt.Sprintf(`
   131  resource "google_compute_backend_service" "foobar" {
   132      name = "urlmap-test-%s"
   133      health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   134  }
   135  
   136  resource "google_compute_http_health_check" "zero" {
   137      name = "urlmap-test-%s"
   138      request_path = "/"
   139      check_interval_sec = 1
   140      timeout_sec = 1
   141  }
   142  
   143  resource "google_compute_url_map" "foobar" {
   144      name = "urlmap-test-%s"
   145  	default_service = "${google_compute_backend_service.foobar.self_link}"
   146  
   147      host_rule {
   148          hosts = ["mysite.com", "myothersite.com"]
   149          path_matcher = "boop"
   150      }
   151  
   152      path_matcher {
   153          default_service = "${google_compute_backend_service.foobar.self_link}"
   154          name = "boop"
   155          path_rule {
   156              paths = ["/*"]
   157              service = "${google_compute_backend_service.foobar.self_link}"
   158          }
   159      }
   160  
   161  	test {
   162  		host = "mysite.com"
   163  		path = "/*"
   164  		service = "${google_compute_backend_service.foobar.self_link}"
   165  	}
   166  }
   167  `, bsName, hcName, umName)
   168  }
   169  
   170  func testAccComputeUrlMap_basic2(bsName, hcName, umName string) string {
   171  	return fmt.Sprintf(`
   172  resource "google_compute_backend_service" "foobar" {
   173      name = "urlmap-test-%s"
   174      health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   175  }
   176  
   177  resource "google_compute_http_health_check" "zero" {
   178      name = "urlmap-test-%s"
   179      request_path = "/"
   180      check_interval_sec = 1
   181      timeout_sec = 1
   182  }
   183  
   184  resource "google_compute_url_map" "foobar" {
   185      name = "urlmap-test-%s"
   186  	default_service = "${google_compute_backend_service.foobar.self_link}"
   187  
   188      host_rule {
   189          hosts = ["mysite.com", "myothersite.com"]
   190          path_matcher = "blip"
   191      }
   192  
   193      path_matcher {
   194          default_service = "${google_compute_backend_service.foobar.self_link}"
   195          name = "blip"
   196          path_rule {
   197              paths = ["/*", "/home"]
   198              service = "${google_compute_backend_service.foobar.self_link}"
   199          }
   200      }
   201  
   202  	test {
   203  		host = "mysite.com"
   204  		path = "/test"
   205  		service = "${google_compute_backend_service.foobar.self_link}"
   206  	}
   207  }
   208  `, bsName, hcName, umName)
   209  }
   210  
   211  var testAccComputeUrlMap_advanced1 = fmt.Sprintf(`
   212  resource "google_compute_backend_service" "foobar" {
   213      name = "urlmap-test-%s"
   214      health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   215  }
   216  
   217  resource "google_compute_http_health_check" "zero" {
   218      name = "urlmap-test-%s"
   219      request_path = "/"
   220      check_interval_sec = 1
   221      timeout_sec = 1
   222  }
   223  
   224  resource "google_compute_url_map" "foobar" {
   225      name = "urlmap-test-%s"
   226  	default_service = "${google_compute_backend_service.foobar.self_link}"
   227  
   228      host_rule {
   229          hosts = ["mysite.com", "myothersite.com"]
   230          path_matcher = "blop"
   231      }
   232  
   233      host_rule {
   234          hosts = ["myfavoritesite.com"]
   235          path_matcher = "blip"
   236      }
   237  
   238      path_matcher {
   239          default_service = "${google_compute_backend_service.foobar.self_link}"
   240          name = "blop"
   241          path_rule {
   242              paths = ["/*", "/home"]
   243              service = "${google_compute_backend_service.foobar.self_link}"
   244          }
   245      }
   246  
   247      path_matcher {
   248          default_service = "${google_compute_backend_service.foobar.self_link}"
   249          name = "blip"
   250          path_rule {
   251              paths = ["/*", "/home"]
   252              service = "${google_compute_backend_service.foobar.self_link}"
   253          }
   254      }
   255  }
   256  `, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
   257  
   258  var testAccComputeUrlMap_advanced2 = fmt.Sprintf(`
   259  resource "google_compute_backend_service" "foobar" {
   260      name = "urlmap-test-%s"
   261      health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   262  }
   263  
   264  resource "google_compute_http_health_check" "zero" {
   265      name = "urlmap-test-%s"
   266      request_path = "/"
   267      check_interval_sec = 1
   268      timeout_sec = 1
   269  }
   270  
   271  resource "google_compute_url_map" "foobar" {
   272      name = "urlmap-test-%s"
   273  	default_service = "${google_compute_backend_service.foobar.self_link}"
   274  
   275      host_rule {
   276          hosts = ["mysite.com", "myothersite.com"]
   277          path_matcher = "blep"
   278      }
   279  
   280      host_rule {
   281          hosts = ["myfavoritesite.com"]
   282          path_matcher = "blip"
   283      }
   284  
   285      host_rule {
   286          hosts = ["myleastfavoritesite.com"]
   287          path_matcher = "blub"
   288      }
   289  
   290      path_matcher {
   291          default_service = "${google_compute_backend_service.foobar.self_link}"
   292          name = "blep"
   293          path_rule {
   294              paths = ["/home"]
   295              service = "${google_compute_backend_service.foobar.self_link}"
   296          }
   297  
   298          path_rule {
   299              paths = ["/login"]
   300              service = "${google_compute_backend_service.foobar.self_link}"
   301          }
   302      }
   303  
   304      path_matcher {
   305          default_service = "${google_compute_backend_service.foobar.self_link}"
   306          name = "blub"
   307          path_rule {
   308              paths = ["/*", "/blub"]
   309              service = "${google_compute_backend_service.foobar.self_link}"
   310          }
   311      }
   312  
   313      path_matcher {
   314          default_service = "${google_compute_backend_service.foobar.self_link}"
   315          name = "blip"
   316          path_rule {
   317              paths = ["/*", "/home"]
   318              service = "${google_compute_backend_service.foobar.self_link}"
   319          }
   320      }
   321  }
   322  `, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))