github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"google.golang.org/api/compute/v1"
    10  )
    11  
    12  func TestAccComputeBackendService_basic(t *testing.T) {
    13  	var svc compute.BackendService
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckComputeBackendServiceDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccComputeBackendService_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckComputeBackendServiceExists(
    24  						"google_compute_backend_service.foobar", &svc),
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccComputeBackendService_basicModified,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckComputeBackendServiceExists(
    31  						"google_compute_backend_service.foobar", &svc),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func TestAccComputeBackendService_withBackend(t *testing.T) {
    39  	var svc compute.BackendService
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccCheckComputeBackendServiceDestroy,
    45  		Steps: []resource.TestStep{
    46  			resource.TestStep{
    47  				Config: testAccComputeBackendService_withBackend,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckComputeBackendServiceExists(
    50  						"google_compute_backend_service.lipsum", &svc),
    51  				),
    52  			},
    53  		},
    54  	})
    55  
    56  	if svc.TimeoutSec != 10 {
    57  		t.Errorf("Expected TimeoutSec == 10, got %d", svc.TimeoutSec)
    58  	}
    59  	if svc.Protocol != "HTTP" {
    60  		t.Errorf("Expected Protocol to be HTTP, got %q", svc.Protocol)
    61  	}
    62  	if len(svc.Backends) != 1 {
    63  		t.Errorf("Expected 1 backend, got %d", len(svc.Backends))
    64  	}
    65  }
    66  
    67  func testAccCheckComputeBackendServiceDestroy(s *terraform.State) error {
    68  	config := testAccProvider.Meta().(*Config)
    69  
    70  	for _, rs := range s.RootModule().Resources {
    71  		if rs.Type != "google_compute_backend_service" {
    72  			continue
    73  		}
    74  
    75  		_, err := config.clientCompute.BackendServices.Get(
    76  			config.Project, rs.Primary.ID).Do()
    77  		if err == nil {
    78  			return fmt.Errorf("Backend service still exists")
    79  		}
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  func testAccCheckComputeBackendServiceExists(n string, svc *compute.BackendService) resource.TestCheckFunc {
    86  	return func(s *terraform.State) error {
    87  		rs, ok := s.RootModule().Resources[n]
    88  		if !ok {
    89  			return fmt.Errorf("Not found: %s", n)
    90  		}
    91  
    92  		if rs.Primary.ID == "" {
    93  			return fmt.Errorf("No ID is set")
    94  		}
    95  
    96  		config := testAccProvider.Meta().(*Config)
    97  
    98  		found, err := config.clientCompute.BackendServices.Get(
    99  			config.Project, rs.Primary.ID).Do()
   100  		if err != nil {
   101  			return err
   102  		}
   103  
   104  		if found.Name != rs.Primary.ID {
   105  			return fmt.Errorf("Backend service not found")
   106  		}
   107  
   108  		*svc = *found
   109  
   110  		return nil
   111  	}
   112  }
   113  
   114  const testAccComputeBackendService_basic = `
   115  resource "google_compute_backend_service" "foobar" {
   116      name = "blablah"
   117      health_checks = ["${google_compute_http_health_check.zero.self_link}"]
   118  }
   119  
   120  resource "google_compute_http_health_check" "zero" {
   121      name = "tf-test-zero"
   122      request_path = "/"
   123      check_interval_sec = 1
   124      timeout_sec = 1
   125  }
   126  `
   127  
   128  const testAccComputeBackendService_basicModified = `
   129  resource "google_compute_backend_service" "foobar" {
   130      name = "blablah"
   131      health_checks = ["${google_compute_http_health_check.one.self_link}"]
   132  }
   133  
   134  resource "google_compute_http_health_check" "zero" {
   135      name = "tf-test-zero"
   136      request_path = "/"
   137      check_interval_sec = 1
   138      timeout_sec = 1
   139  }
   140  
   141  resource "google_compute_http_health_check" "one" {
   142      name = "tf-test-one"
   143      request_path = "/one"
   144      check_interval_sec = 30
   145      timeout_sec = 30
   146  }
   147  `
   148  
   149  const testAccComputeBackendService_withBackend = `
   150  resource "google_compute_backend_service" "lipsum" {
   151      name = "hello-world-bs"
   152      description = "Hello World 1234"
   153      port_name = "http"
   154      protocol = "HTTP"
   155      timeout_sec = 10
   156  
   157      backend {
   158          group = "${google_compute_instance_group_manager.foobar.instance_group}"
   159      }
   160  
   161      health_checks = ["${google_compute_http_health_check.default.self_link}"]
   162  }
   163  
   164  resource "google_compute_instance_group_manager" "foobar" {
   165      name = "terraform-test"
   166      instance_template = "${google_compute_instance_template.foobar.self_link}"
   167      base_instance_name = "foobar"
   168      zone = "us-central1-f"
   169      target_size = 1
   170  }
   171  
   172  resource "google_compute_instance_template" "foobar" {
   173      name = "terraform-test"
   174      machine_type = "n1-standard-1"
   175  
   176      network_interface {
   177          network = "default"
   178      }
   179  
   180      disk {
   181          source_image = "debian-7-wheezy-v20140814"
   182          auto_delete = true
   183          boot = true
   184      }
   185  }
   186  
   187  resource "google_compute_http_health_check" "default" {
   188      name = "test2"
   189      request_path = "/"
   190      check_interval_sec = 1
   191      timeout_sec = 1
   192  }
   193  `