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