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

     1  package fastly
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	gofastly "github.com/sethvargo/go-fastly"
    12  )
    13  
    14  func TestAccFastlyServiceV1_healthcheck_basic(t *testing.T) {
    15  	var service gofastly.ServiceDetail
    16  	name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    17  	domainName := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10))
    18  
    19  	log1 := gofastly.HealthCheck{
    20  		Version:          1,
    21  		Name:             "example-healthcheck1",
    22  		Host:             "example1.com",
    23  		Path:             "/test1.txt",
    24  		CheckInterval:    4000,
    25  		ExpectedResponse: 200,
    26  		HTTPVersion:      "1.1",
    27  		Initial:          2,
    28  		Method:           "HEAD",
    29  		Threshold:        3,
    30  		Timeout:          5000,
    31  		Window:           5,
    32  	}
    33  
    34  	log2 := gofastly.HealthCheck{
    35  		Version:          1,
    36  		Name:             "example-healthcheck2",
    37  		Host:             "example2.com",
    38  		Path:             "/test2.txt",
    39  		CheckInterval:    4500,
    40  		ExpectedResponse: 404,
    41  		HTTPVersion:      "1.0",
    42  		Initial:          1,
    43  		Method:           "POST",
    44  		Threshold:        4,
    45  		Timeout:          4000,
    46  		Window:           10,
    47  	}
    48  
    49  	resource.Test(t, resource.TestCase{
    50  		PreCheck:     func() { testAccPreCheck(t) },
    51  		Providers:    testAccProviders,
    52  		CheckDestroy: testAccCheckServiceV1Destroy,
    53  		Steps: []resource.TestStep{
    54  			resource.TestStep{
    55  				Config: testAccServiceV1HealthCheckConfig(name, domainName),
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    58  					testAccCheckFastlyServiceV1HealthCheckAttributes(&service, []*gofastly.HealthCheck{&log1}),
    59  					resource.TestCheckResourceAttr(
    60  						"fastly_service_v1.foo", "name", name),
    61  					resource.TestCheckResourceAttr(
    62  						"fastly_service_v1.foo", "healthcheck.#", "1"),
    63  				),
    64  			},
    65  
    66  			resource.TestStep{
    67  				Config: testAccServiceV1HealthCheckConfig_update(name, domainName),
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    70  					testAccCheckFastlyServiceV1HealthCheckAttributes(&service, []*gofastly.HealthCheck{&log1, &log2}),
    71  					resource.TestCheckResourceAttr(
    72  						"fastly_service_v1.foo", "name", name),
    73  					resource.TestCheckResourceAttr(
    74  						"fastly_service_v1.foo", "healthcheck.#", "2"),
    75  				),
    76  			},
    77  		},
    78  	})
    79  }
    80  
    81  func testAccCheckFastlyServiceV1HealthCheckAttributes(service *gofastly.ServiceDetail, healthchecks []*gofastly.HealthCheck) resource.TestCheckFunc {
    82  	return func(s *terraform.State) error {
    83  
    84  		conn := testAccProvider.Meta().(*FastlyClient).conn
    85  		healthcheckList, err := conn.ListHealthChecks(&gofastly.ListHealthChecksInput{
    86  			Service: service.ID,
    87  			Version: service.ActiveVersion.Number,
    88  		})
    89  
    90  		if err != nil {
    91  			return fmt.Errorf("[ERR] Error looking up Healthcheck for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err)
    92  		}
    93  
    94  		if len(healthcheckList) != len(healthchecks) {
    95  			return fmt.Errorf("Healthcheck List count mismatch, expected (%d), got (%d)", len(healthchecks), len(healthcheckList))
    96  		}
    97  
    98  		var found int
    99  		for _, h := range healthchecks {
   100  			for _, lh := range healthcheckList {
   101  				if h.Name == lh.Name {
   102  					// we don't know these things ahead of time, so populate them now
   103  					h.ServiceID = service.ID
   104  					h.Version = service.ActiveVersion.Number
   105  					if !reflect.DeepEqual(h, lh) {
   106  						return fmt.Errorf("Bad match Healthcheck match, expected (%#v), got (%#v)", h, lh)
   107  					}
   108  					found++
   109  				}
   110  			}
   111  		}
   112  
   113  		if found != len(healthchecks) {
   114  			return fmt.Errorf("Error matching Healthcheck rules")
   115  		}
   116  
   117  		return nil
   118  	}
   119  }
   120  
   121  func testAccServiceV1HealthCheckConfig(name, domain string) string {
   122  	return fmt.Sprintf(`
   123  resource "fastly_service_v1" "foo" {
   124    name = "%s"
   125  
   126    domain {
   127      name    = "%s"
   128      comment = "tf-testing-domain"
   129    }
   130  
   131    backend {
   132      address = "aws.amazon.com"
   133      name    = "amazon docs"
   134    }
   135  
   136    healthcheck {
   137  		name              = "example-healthcheck1"
   138  		host              = "example1.com"
   139  		path              = "/test1.txt"
   140  		check_interval    = 4000
   141  		expected_response = 200
   142  		http_version      = "1.1"
   143  		initial           = 2
   144  		method            = "HEAD"
   145  		threshold         = 3
   146  		timeout           = 5000
   147  		window            = 5
   148    }
   149  
   150    force_destroy = true
   151  }`, name, domain)
   152  }
   153  
   154  func testAccServiceV1HealthCheckConfig_update(name, domain string) string {
   155  	return fmt.Sprintf(`
   156  resource "fastly_service_v1" "foo" {
   157    name = "%s"
   158  
   159    domain {
   160      name    = "%s"
   161      comment = "tf-testing-domain"
   162    }
   163  
   164    backend {
   165      address = "aws.amazon.com"
   166      name    = "amazon docs"
   167    }
   168  
   169  	healthcheck {
   170  		name              = "example-healthcheck1"
   171  		host              = "example1.com"
   172  		path              = "/test1.txt"
   173  		check_interval    = 4000
   174  		expected_response = 200
   175  		http_version      = "1.1"
   176  		initial           = 2
   177  		method            = "HEAD"
   178  		threshold         = 3
   179  		timeout           = 5000
   180  		window            = 5
   181    }
   182  
   183  	healthcheck {
   184  		name              = "example-healthcheck2"
   185  		host              = "example2.com"
   186  		path              = "/test2.txt"
   187  		check_interval    = 4500
   188  		expected_response = 404
   189  		http_version      = "1.0"
   190  		initial           = 1
   191  		method            = "POST"
   192  		threshold         = 4
   193  		timeout           = 4000
   194  		window            = 10
   195    }
   196  
   197    force_destroy = true
   198  }`, name, domain)
   199  }