github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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_withSessionAffinity(t *testing.T) {
   191  	serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
   192  	checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
   193  	var svc compute.BackendService
   194  
   195  	resource.Test(t, resource.TestCase{
   196  		PreCheck:     func() { testAccPreCheck(t) },
   197  		Providers:    testAccProviders,
   198  		CheckDestroy: testAccCheckComputeBackendServiceDestroy,
   199  		Steps: []resource.TestStep{
   200  			resource.TestStep{
   201  				Config: testAccComputeBackendService_withSessionAffinity(
   202  					serviceName, checkName, "CLIENT_IP"),
   203  				Check: resource.ComposeTestCheckFunc(
   204  					testAccCheckComputeBackendServiceExists(
   205  						"google_compute_backend_service.foobar", &svc),
   206  				),
   207  			},
   208  			resource.TestStep{
   209  				Config: testAccComputeBackendService_withSessionAffinity(
   210  					serviceName, checkName, "GENERATED_COOKIE"),
   211  				Check: resource.ComposeTestCheckFunc(
   212  					testAccCheckComputeBackendServiceExists(
   213  						"google_compute_backend_service.foobar", &svc),
   214  				),
   215  			},
   216  		},
   217  	})
   218  
   219  	if svc.SessionAffinity != "GENERATED_COOKIE" {
   220  		t.Errorf("Expected SessionAffinity == \"GENERATED_COOKIE\", got %s", svc.SessionAffinity)
   221  	}
   222  }
   223  
   224  func testAccComputeBackendService_basic(serviceName, checkName string) string {
   225  	return fmt.Sprintf(`
   226  resource "google_compute_backend_service" "foobar" {
   227    name          = "%s"
   228    health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   229  }
   230  
   231  resource "google_compute_http_health_check" "zero" {
   232    name               = "%s"
   233    request_path       = "/"
   234    check_interval_sec = 1
   235    timeout_sec        = 1
   236  }
   237  `, serviceName, checkName)
   238  }
   239  
   240  func testAccComputeBackendService_withCDNEnabled(serviceName, checkName string) string {
   241  	return fmt.Sprintf(`
   242  resource "google_compute_backend_service" "foobar" {
   243    name          = "%s"
   244    health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   245    enable_cdn    = true
   246  }
   247  
   248  resource "google_compute_http_health_check" "zero" {
   249    name               = "%s"
   250    request_path       = "/"
   251    check_interval_sec = 1
   252    timeout_sec        = 1
   253  }
   254  `, serviceName, checkName)
   255  }
   256  
   257  func testAccComputeBackendService_basicModified(serviceName, checkOne, checkTwo string) string {
   258  	return fmt.Sprintf(`
   259  resource "google_compute_backend_service" "foobar" {
   260      name = "%s"
   261      health_checks = ["${google_compute_http_health_check.one.self_link}"]
   262  }
   263  
   264  resource "google_compute_http_health_check" "zero" {
   265      name = "%s"
   266      request_path = "/"
   267      check_interval_sec = 1
   268      timeout_sec = 1
   269  }
   270  
   271  resource "google_compute_http_health_check" "one" {
   272      name = "%s"
   273      request_path = "/one"
   274      check_interval_sec = 30
   275      timeout_sec = 30
   276  }
   277  `, serviceName, checkOne, checkTwo)
   278  }
   279  
   280  func testAccComputeBackendService_withBackend(
   281  	serviceName, igName, itName, checkName string, timeout int64) string {
   282  	return fmt.Sprintf(`
   283  resource "google_compute_backend_service" "lipsum" {
   284    name        = "%s"
   285    description = "Hello World 1234"
   286    port_name   = "http"
   287    protocol    = "HTTP"
   288    timeout_sec = %v
   289  
   290    backend {
   291      group = "${google_compute_instance_group_manager.foobar.instance_group}"
   292    }
   293  
   294    health_checks = ["${google_compute_http_health_check.default.self_link}"]
   295  }
   296  
   297  resource "google_compute_instance_group_manager" "foobar" {
   298    name               = "%s"
   299    instance_template  = "${google_compute_instance_template.foobar.self_link}"
   300    base_instance_name = "foobar"
   301    zone               = "us-central1-f"
   302    target_size        = 1
   303  }
   304  
   305  resource "google_compute_instance_template" "foobar" {
   306    name         = "%s"
   307    machine_type = "n1-standard-1"
   308  
   309    network_interface {
   310      network = "default"
   311    }
   312  
   313    disk {
   314      source_image = "debian-8-jessie-v20160803"
   315      auto_delete  = true
   316      boot         = true
   317    }
   318  }
   319  
   320  resource "google_compute_http_health_check" "default" {
   321    name               = "%s"
   322    request_path       = "/"
   323    check_interval_sec = 1
   324    timeout_sec        = 1
   325  }
   326  `, serviceName, timeout, igName, itName, checkName)
   327  }
   328  
   329  func testAccComputeBackendService_withSessionAffinity(serviceName, checkName, affinityName string) string {
   330  	return fmt.Sprintf(`
   331  resource "google_compute_backend_service" "foobar" {
   332    name             = "%s"
   333    health_checks    = ["${google_compute_http_health_check.zero.self_link}"]
   334    session_affinity = "%s"
   335  }
   336  
   337  resource "google_compute_http_health_check" "zero" {
   338    name               = "%s"
   339    request_path       = "/"
   340    check_interval_sec = 1
   341    timeout_sec        = 1
   342  }
   343  `, serviceName, affinityName, checkName)
   344  }