github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_compute_backend_service_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  	"google.golang.org/api/compute/v1"
    11  )
    12  
    13  func TestAccComputeBackendService_basic(t *testing.T) {
    14  	serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    15  	checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    16  	extraCheckName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    17  	var svc compute.BackendService
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckComputeBackendServiceDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccComputeBackendService_basic(serviceName, checkName),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckComputeBackendServiceExists(
    28  						"google_compute_backend_service.foobar", &svc),
    29  				),
    30  			},
    31  			resource.TestStep{
    32  				Config: testAccComputeBackendService_basicModified(
    33  					serviceName, checkName, extraCheckName),
    34  				Check: resource.ComposeTestCheckFunc(
    35  					testAccCheckComputeBackendServiceExists(
    36  						"google_compute_backend_service.foobar", &svc),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func TestAccComputeBackendService_withBackend(t *testing.T) {
    44  	serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    45  	igName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    46  	itName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    47  	checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    48  	var svc compute.BackendService
    49  	resource.Test(t, resource.TestCase{
    50  		PreCheck:     func() { testAccPreCheck(t) },
    51  		Providers:    testAccProviders,
    52  		CheckDestroy: testAccCheckComputeBackendServiceDestroy,
    53  		Steps: []resource.TestStep{
    54  			resource.TestStep{
    55  				Config: testAccComputeBackendService_withBackend(
    56  					serviceName, igName, itName, checkName, 10),
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckComputeBackendServiceExists(
    59  						"google_compute_backend_service.lipsum", &svc),
    60  				),
    61  			},
    62  		},
    63  	})
    64  
    65  	if svc.TimeoutSec != 10 {
    66  		t.Errorf("Expected TimeoutSec == 10, got %d", svc.TimeoutSec)
    67  	}
    68  	if svc.Protocol != "HTTP" {
    69  		t.Errorf("Expected Protocol to be HTTP, got %q", svc.Protocol)
    70  	}
    71  	if len(svc.Backends) != 1 {
    72  		t.Errorf("Expected 1 backend, got %d", len(svc.Backends))
    73  	}
    74  }
    75  
    76  func TestAccComputeBackendService_withBackendAndUpdate(t *testing.T) {
    77  	serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    78  	igName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    79  	itName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    80  	checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    81  	var svc compute.BackendService
    82  	resource.Test(t, resource.TestCase{
    83  		PreCheck:     func() { testAccPreCheck(t) },
    84  		Providers:    testAccProviders,
    85  		CheckDestroy: testAccCheckComputeBackendServiceDestroy,
    86  		Steps: []resource.TestStep{
    87  			resource.TestStep{
    88  				Config: testAccComputeBackendService_withBackend(
    89  					serviceName, igName, itName, checkName, 10),
    90  				Check: resource.ComposeTestCheckFunc(
    91  					testAccCheckComputeBackendServiceExists(
    92  						"google_compute_backend_service.lipsum", &svc),
    93  				),
    94  			},
    95  			resource.TestStep{
    96  				Config: testAccComputeBackendService_withBackend(
    97  					serviceName, igName, itName, checkName, 20),
    98  				Check: resource.ComposeTestCheckFunc(
    99  					testAccCheckComputeBackendServiceExists(
   100  						"google_compute_backend_service.lipsum", &svc),
   101  				),
   102  			},
   103  		},
   104  	})
   105  
   106  	if svc.TimeoutSec != 20 {
   107  		t.Errorf("Expected TimeoutSec == 20, got %d", svc.TimeoutSec)
   108  	}
   109  	if svc.Protocol != "HTTP" {
   110  		t.Errorf("Expected Protocol to be HTTP, got %q", svc.Protocol)
   111  	}
   112  	if len(svc.Backends) != 1 {
   113  		t.Errorf("Expected 1 backend, got %d", len(svc.Backends))
   114  	}
   115  }
   116  
   117  func testAccCheckComputeBackendServiceDestroy(s *terraform.State) error {
   118  	config := testAccProvider.Meta().(*Config)
   119  
   120  	for _, rs := range s.RootModule().Resources {
   121  		if rs.Type != "google_compute_backend_service" {
   122  			continue
   123  		}
   124  
   125  		_, err := config.clientCompute.BackendServices.Get(
   126  			config.Project, rs.Primary.ID).Do()
   127  		if err == nil {
   128  			return fmt.Errorf("Backend service still exists")
   129  		}
   130  	}
   131  
   132  	return nil
   133  }
   134  
   135  func testAccCheckComputeBackendServiceExists(n string, svc *compute.BackendService) resource.TestCheckFunc {
   136  	return func(s *terraform.State) error {
   137  		rs, ok := s.RootModule().Resources[n]
   138  		if !ok {
   139  			return fmt.Errorf("Not found: %s", n)
   140  		}
   141  
   142  		if rs.Primary.ID == "" {
   143  			return fmt.Errorf("No ID is set")
   144  		}
   145  
   146  		config := testAccProvider.Meta().(*Config)
   147  
   148  		found, err := config.clientCompute.BackendServices.Get(
   149  			config.Project, rs.Primary.ID).Do()
   150  		if err != nil {
   151  			return err
   152  		}
   153  
   154  		if found.Name != rs.Primary.ID {
   155  			return fmt.Errorf("Backend service not found")
   156  		}
   157  
   158  		*svc = *found
   159  
   160  		return nil
   161  	}
   162  }
   163  
   164  func TestAccComputeBackendService_withCDNEnabled(t *testing.T) {
   165  	serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
   166  	checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
   167  	var svc compute.BackendService
   168  
   169  	resource.Test(t, resource.TestCase{
   170  		PreCheck:     func() { testAccPreCheck(t) },
   171  		Providers:    testAccProviders,
   172  		CheckDestroy: testAccCheckComputeBackendServiceDestroy,
   173  		Steps: []resource.TestStep{
   174  			resource.TestStep{
   175  				Config: testAccComputeBackendService_withCDNEnabled(
   176  					serviceName, checkName),
   177  				Check: resource.ComposeTestCheckFunc(
   178  					testAccCheckComputeBackendServiceExists(
   179  						"google_compute_backend_service.foobar", &svc),
   180  				),
   181  			},
   182  		},
   183  	})
   184  
   185  	if svc.EnableCDN != true {
   186  		t.Errorf("Expected EnableCDN == true, got %t", svc.EnableCDN)
   187  	}
   188  }
   189  
   190  func testAccComputeBackendService_basic(serviceName, checkName string) string {
   191  	return fmt.Sprintf(`
   192  resource "google_compute_backend_service" "foobar" {
   193    name          = "%s"
   194    health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   195  }
   196  
   197  resource "google_compute_http_health_check" "zero" {
   198    name               = "%s"
   199    request_path       = "/"
   200    check_interval_sec = 1
   201    timeout_sec        = 1
   202  }
   203  `, serviceName, checkName)
   204  }
   205  
   206  func testAccComputeBackendService_withCDNEnabled(serviceName, checkName string) string {
   207  	return fmt.Sprintf(`
   208  resource "google_compute_backend_service" "foobar" {
   209    name          = "%s"
   210    health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   211    enable_cdn    = true
   212  }
   213  
   214  resource "google_compute_http_health_check" "zero" {
   215    name               = "%s"
   216    request_path       = "/"
   217    check_interval_sec = 1
   218    timeout_sec        = 1
   219  }
   220  `, serviceName, checkName)
   221  }
   222  
   223  func testAccComputeBackendService_basicModified(serviceName, checkOne, checkTwo string) string {
   224  	return fmt.Sprintf(`
   225  resource "google_compute_backend_service" "foobar" {
   226      name = "%s"
   227      health_checks = ["${google_compute_http_health_check.one.self_link}"]
   228  }
   229  
   230  resource "google_compute_http_health_check" "zero" {
   231      name = "%s"
   232      request_path = "/"
   233      check_interval_sec = 1
   234      timeout_sec = 1
   235  }
   236  
   237  resource "google_compute_http_health_check" "one" {
   238      name = "%s"
   239      request_path = "/one"
   240      check_interval_sec = 30
   241      timeout_sec = 30
   242  }
   243  `, serviceName, checkOne, checkTwo)
   244  }
   245  
   246  func testAccComputeBackendService_withBackend(
   247  	serviceName, igName, itName, checkName string, timeout int64) string {
   248  	return fmt.Sprintf(`
   249  resource "google_compute_backend_service" "lipsum" {
   250    name        = "%s"
   251    description = "Hello World 1234"
   252    port_name   = "http"
   253    protocol    = "HTTP"
   254    timeout_sec = %v
   255  
   256    backend {
   257      group = "${google_compute_instance_group_manager.foobar.instance_group}"
   258    }
   259  
   260    health_checks = ["${google_compute_http_health_check.default.self_link}"]
   261  }
   262  
   263  resource "google_compute_instance_group_manager" "foobar" {
   264    name               = "%s"
   265    instance_template  = "${google_compute_instance_template.foobar.self_link}"
   266    base_instance_name = "foobar"
   267    zone               = "us-central1-f"
   268    target_size        = 1
   269  }
   270  
   271  resource "google_compute_instance_template" "foobar" {
   272    name         = "%s"
   273    machine_type = "n1-standard-1"
   274  
   275    network_interface {
   276      network = "default"
   277    }
   278  
   279    disk {
   280      source_image = "debian-8-jessie-v20160803"
   281      auto_delete  = true
   282      boot         = true
   283    }
   284  }
   285  
   286  resource "google_compute_http_health_check" "default" {
   287    name               = "%s"
   288    request_path       = "/"
   289    check_interval_sec = 1
   290    timeout_sec        = 1
   291  }
   292  `, serviceName, timeout, igName, itName, checkName)
   293  }