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

     1  package fastly
     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  	gofastly "github.com/sethvargo/go-fastly"
    11  )
    12  
    13  func TestAccFastlyServiceV1_VCL_basic(t *testing.T) {
    14  	var service gofastly.ServiceDetail
    15  	name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    16  	domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10))
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckServiceV1Destroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccServiceV1VCLConfig(name, domainName1),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    27  					testAccCheckFastlyServiceV1VCLAttributes(&service, name, 1),
    28  					resource.TestCheckResourceAttr(
    29  						"fastly_service_v1.foo", "name", name),
    30  					resource.TestCheckResourceAttr(
    31  						"fastly_service_v1.foo", "vcl.#", "1"),
    32  				),
    33  			},
    34  
    35  			resource.TestStep{
    36  				Config: testAccServiceV1VCLConfig_update(name, domainName1),
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    39  					testAccCheckFastlyServiceV1VCLAttributes(&service, name, 2),
    40  					resource.TestCheckResourceAttr(
    41  						"fastly_service_v1.foo", "name", name),
    42  					resource.TestCheckResourceAttr(
    43  						"fastly_service_v1.foo", "vcl.#", "2"),
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func testAccCheckFastlyServiceV1VCLAttributes(service *gofastly.ServiceDetail, name string, vclCount int) resource.TestCheckFunc {
    51  	return func(s *terraform.State) error {
    52  
    53  		if service.Name != name {
    54  			return fmt.Errorf("Bad name, expected (%s), got (%s)", name, service.Name)
    55  		}
    56  
    57  		conn := testAccProvider.Meta().(*FastlyClient).conn
    58  		vclList, err := conn.ListVCLs(&gofastly.ListVCLsInput{
    59  			Service: service.ID,
    60  			Version: service.ActiveVersion.Number,
    61  		})
    62  
    63  		if err != nil {
    64  			return fmt.Errorf("[ERR] Error looking up VCL for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err)
    65  		}
    66  
    67  		if len(vclList) != vclCount {
    68  			return fmt.Errorf("VCL count mismatch, expected (%d), got (%d)", vclCount, len(vclList))
    69  		}
    70  
    71  		return nil
    72  	}
    73  }
    74  
    75  func testAccServiceV1VCLConfig(name, domain string) string {
    76  	return fmt.Sprintf(`
    77  resource "fastly_service_v1" "foo" {
    78    name = "%s"
    79  
    80    domain {
    81      name    = "%s"
    82      comment = "tf-testing-domain"
    83    }
    84  
    85    vcl {
    86      name    = "my_custom_main_vcl"
    87      content = <<EOF
    88  sub vcl_recv {
    89  #FASTLY recv
    90  
    91      if (req.request != "HEAD" && req.request != "GET" && req.request != "FASTLYPURGE") {
    92        return(pass);
    93      }
    94  
    95      return(lookup);
    96  }
    97  
    98  backend amazondocs {
    99    .host = "127.0.0.1";
   100    .port = "80";
   101  }
   102  EOF
   103      main    = true
   104    }
   105  
   106    force_destroy = true
   107  }`, name, domain)
   108  }
   109  
   110  func testAccServiceV1VCLConfig_update(name, domain string) string {
   111  	return fmt.Sprintf(`
   112  resource "fastly_service_v1" "foo" {
   113    name = "%s"
   114  
   115    domain {
   116      name    = "%s"
   117      comment = "tf-testing-domain"
   118    }
   119  
   120    vcl {
   121      name    = "my_custom_main_vcl"
   122      content = <<EOF
   123  sub vcl_recv {
   124  #FASTLY recv
   125  
   126      if (req.request != "HEAD" && req.request != "GET" && req.request != "FASTLYPURGE") {
   127        return(pass);
   128      }
   129  
   130      return(lookup);
   131  }
   132  
   133  backend amazondocs {
   134    .host = "127.0.0.1";
   135    .port = "80";
   136  }
   137  EOF
   138      main    = true
   139    }
   140  
   141          vcl {
   142                  name = "other_vcl"
   143                  content = <<EOF
   144  sub vcl_error {
   145  #FASTLY error
   146  }
   147  
   148  backend amazondocs {
   149    .host = "127.0.0.1";
   150    .port = "80";
   151  }
   152  EOF
   153          }
   154  
   155    force_destroy = true
   156  }`, name, domain)
   157  }