github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_health_check_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"google.golang.org/api/compute/v1"
    12  )
    13  
    14  func TestAccComputeHealthCheck_tcp(t *testing.T) {
    15  	var healthCheck compute.HealthCheck
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckComputeHealthCheckDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccComputeHealthCheck_tcp,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckComputeHealthCheckExists(
    26  						"google_compute_health_check.foobar", &healthCheck),
    27  					testAccCheckComputeHealthCheckThresholds(
    28  						3, 3, &healthCheck),
    29  					testAccCheckComputeHealthCheckTcpPort(80, &healthCheck),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestAccComputeHealthCheck_tcp_update(t *testing.T) {
    37  	var healthCheck compute.HealthCheck
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testAccCheckComputeHealthCheckDestroy,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: testAccComputeHealthCheck_tcp,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckComputeHealthCheckExists(
    48  						"google_compute_health_check.foobar", &healthCheck),
    49  					testAccCheckComputeHealthCheckThresholds(
    50  						3, 3, &healthCheck),
    51  					testAccCheckComputeHealthCheckTcpPort(80, &healthCheck),
    52  				),
    53  			},
    54  			resource.TestStep{
    55  				Config: testAccComputeHealthCheck_tcp_update,
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckComputeHealthCheckExists(
    58  						"google_compute_health_check.foobar", &healthCheck),
    59  					testAccCheckComputeHealthCheckThresholds(
    60  						10, 10, &healthCheck),
    61  					testAccCheckComputeHealthCheckTcpPort(8080, &healthCheck),
    62  				),
    63  			},
    64  		},
    65  	})
    66  }
    67  
    68  func TestAccComputeHealthCheck_ssl(t *testing.T) {
    69  	var healthCheck compute.HealthCheck
    70  
    71  	resource.Test(t, resource.TestCase{
    72  		PreCheck:     func() { testAccPreCheck(t) },
    73  		Providers:    testAccProviders,
    74  		CheckDestroy: testAccCheckComputeHealthCheckDestroy,
    75  		Steps: []resource.TestStep{
    76  			resource.TestStep{
    77  				Config: testAccComputeHealthCheck_ssl,
    78  				Check: resource.ComposeTestCheckFunc(
    79  					testAccCheckComputeHealthCheckExists(
    80  						"google_compute_health_check.foobar", &healthCheck),
    81  					testAccCheckComputeHealthCheckThresholds(
    82  						3, 3, &healthCheck),
    83  				),
    84  			},
    85  		},
    86  	})
    87  }
    88  
    89  func TestAccComputeHealthCheck_http(t *testing.T) {
    90  	var healthCheck compute.HealthCheck
    91  
    92  	resource.Test(t, resource.TestCase{
    93  		PreCheck:     func() { testAccPreCheck(t) },
    94  		Providers:    testAccProviders,
    95  		CheckDestroy: testAccCheckComputeHealthCheckDestroy,
    96  		Steps: []resource.TestStep{
    97  			resource.TestStep{
    98  				Config: testAccComputeHealthCheck_http,
    99  				Check: resource.ComposeTestCheckFunc(
   100  					testAccCheckComputeHealthCheckExists(
   101  						"google_compute_health_check.foobar", &healthCheck),
   102  					testAccCheckComputeHealthCheckThresholds(
   103  						3, 3, &healthCheck),
   104  				),
   105  			},
   106  		},
   107  	})
   108  }
   109  
   110  func TestAccComputeHealthCheck_https(t *testing.T) {
   111  	var healthCheck compute.HealthCheck
   112  
   113  	resource.Test(t, resource.TestCase{
   114  		PreCheck:     func() { testAccPreCheck(t) },
   115  		Providers:    testAccProviders,
   116  		CheckDestroy: testAccCheckComputeHealthCheckDestroy,
   117  		Steps: []resource.TestStep{
   118  			resource.TestStep{
   119  				Config: testAccComputeHealthCheck_https,
   120  				Check: resource.ComposeTestCheckFunc(
   121  					testAccCheckComputeHealthCheckExists(
   122  						"google_compute_health_check.foobar", &healthCheck),
   123  					testAccCheckComputeHealthCheckThresholds(
   124  						3, 3, &healthCheck),
   125  				),
   126  			},
   127  		},
   128  	})
   129  }
   130  
   131  func TestAccComputeHealthCheck_tcpAndSsl_shouldFail(t *testing.T) {
   132  	resource.Test(t, resource.TestCase{
   133  		PreCheck:     func() { testAccPreCheck(t) },
   134  		Providers:    testAccProviders,
   135  		CheckDestroy: testAccCheckComputeHealthCheckDestroy,
   136  		Steps: []resource.TestStep{
   137  			resource.TestStep{
   138  				Config:      testAccComputeHealthCheck_tcpAndSsl_shouldFail,
   139  				ExpectError: regexp.MustCompile("conflicts with tcp_health_check"),
   140  			},
   141  		},
   142  	})
   143  }
   144  
   145  func testAccCheckComputeHealthCheckDestroy(s *terraform.State) error {
   146  	config := testAccProvider.Meta().(*Config)
   147  
   148  	for _, rs := range s.RootModule().Resources {
   149  		if rs.Type != "google_compute_health_check" {
   150  			continue
   151  		}
   152  
   153  		_, err := config.clientCompute.HealthChecks.Get(
   154  			config.Project, rs.Primary.ID).Do()
   155  		if err == nil {
   156  			return fmt.Errorf("HealthCheck %s still exists", rs.Primary.ID)
   157  		}
   158  	}
   159  
   160  	return nil
   161  }
   162  
   163  func testAccCheckComputeHealthCheckExists(n string, healthCheck *compute.HealthCheck) resource.TestCheckFunc {
   164  	return func(s *terraform.State) error {
   165  		rs, ok := s.RootModule().Resources[n]
   166  		if !ok {
   167  			return fmt.Errorf("Not found: %s", n)
   168  		}
   169  
   170  		if rs.Primary.ID == "" {
   171  			return fmt.Errorf("No ID is set")
   172  		}
   173  
   174  		config := testAccProvider.Meta().(*Config)
   175  
   176  		found, err := config.clientCompute.HealthChecks.Get(
   177  			config.Project, rs.Primary.ID).Do()
   178  		if err != nil {
   179  			return err
   180  		}
   181  
   182  		if found.Name != rs.Primary.ID {
   183  			return fmt.Errorf("HealthCheck not found")
   184  		}
   185  
   186  		*healthCheck = *found
   187  
   188  		return nil
   189  	}
   190  }
   191  
   192  func testAccCheckErrorCreating(n string) resource.TestCheckFunc {
   193  	return func(s *terraform.State) error {
   194  		_, ok := s.RootModule().Resources[n]
   195  		if ok {
   196  			return fmt.Errorf("HealthCheck %s created successfully with bad config", n)
   197  		}
   198  		return nil
   199  	}
   200  }
   201  
   202  func testAccCheckComputeHealthCheckThresholds(healthy, unhealthy int64, healthCheck *compute.HealthCheck) resource.TestCheckFunc {
   203  	return func(s *terraform.State) error {
   204  		if healthCheck.HealthyThreshold != healthy {
   205  			return fmt.Errorf("HealthyThreshold doesn't match: expected %d, got %d", healthy, healthCheck.HealthyThreshold)
   206  		}
   207  
   208  		if healthCheck.UnhealthyThreshold != unhealthy {
   209  			return fmt.Errorf("UnhealthyThreshold doesn't match: expected %d, got %d", unhealthy, healthCheck.UnhealthyThreshold)
   210  		}
   211  
   212  		return nil
   213  	}
   214  }
   215  
   216  func testAccCheckComputeHealthCheckTcpPort(port int64, healthCheck *compute.HealthCheck) resource.TestCheckFunc {
   217  	return func(s *terraform.State) error {
   218  		if healthCheck.TcpHealthCheck.Port != port {
   219  			return fmt.Errorf("Port doesn't match: expected %v, got %v", port, healthCheck.TcpHealthCheck.Port)
   220  		}
   221  		return nil
   222  	}
   223  }
   224  
   225  var testAccComputeHealthCheck_tcp = fmt.Sprintf(`
   226  resource "google_compute_health_check" "foobar" {
   227  	check_interval_sec = 3
   228  	description = "Resource created for Terraform acceptance testing"
   229  	healthy_threshold = 3
   230  	name = "health-test-%s"
   231  	timeout_sec = 2
   232  	unhealthy_threshold = 3
   233  	tcp_health_check {
   234  	}
   235  }
   236  `, acctest.RandString(10))
   237  
   238  var testAccComputeHealthCheck_tcp_update = fmt.Sprintf(`
   239  resource "google_compute_health_check" "foobar" {
   240  	check_interval_sec = 3
   241  	description = "Resource updated for Terraform acceptance testing"
   242  	healthy_threshold = 10
   243  	name = "health-test-%s"
   244  	timeout_sec = 2
   245  	unhealthy_threshold = 10
   246  	tcp_health_check {
   247  		port = "8080"
   248  	}
   249  }
   250  `, acctest.RandString(10))
   251  
   252  var testAccComputeHealthCheck_ssl = fmt.Sprintf(`
   253  resource "google_compute_health_check" "foobar" {
   254  	check_interval_sec = 3
   255  	description = "Resource created for Terraform acceptance testing"
   256  	healthy_threshold = 3
   257  	name = "health-test-%s"
   258  	timeout_sec = 2
   259  	unhealthy_threshold = 3
   260  	ssl_health_check {
   261  		port = "443"
   262  	}
   263  }
   264  `, acctest.RandString(10))
   265  
   266  var testAccComputeHealthCheck_http = fmt.Sprintf(`
   267  resource "google_compute_health_check" "foobar" {
   268  	check_interval_sec = 3
   269  	description = "Resource created for Terraform acceptance testing"
   270  	healthy_threshold = 3
   271  	name = "health-test-%s"
   272  	timeout_sec = 2
   273  	unhealthy_threshold = 3
   274  	http_health_check {
   275  		port = "80"
   276  	}
   277  }
   278  `, acctest.RandString(10))
   279  
   280  var testAccComputeHealthCheck_https = fmt.Sprintf(`
   281  resource "google_compute_health_check" "foobar" {
   282  	check_interval_sec = 3
   283  	description = "Resource created for Terraform acceptance testing"
   284  	healthy_threshold = 3
   285  	name = "health-test-%s"
   286  	timeout_sec = 2
   287  	unhealthy_threshold = 3
   288  	https_health_check {
   289  		port = "443"
   290  	}
   291  }
   292  `, acctest.RandString(10))
   293  
   294  var testAccComputeHealthCheck_tcpAndSsl_shouldFail = fmt.Sprintf(`
   295  resource "google_compute_health_check" "foobar" {
   296  	check_interval_sec = 3
   297  	description = "Resource created for Terraform acceptance testing"
   298  	healthy_threshold = 3
   299  	name = "health-test-%s"
   300  	timeout_sec = 2
   301  	unhealthy_threshold = 3
   302  
   303  	tcp_health_check {
   304  	}
   305  	ssl_health_check {
   306  	}
   307  }
   308  `, acctest.RandString(10))