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