github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/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 backend { 86 address = "aws.amazon.com" 87 name = "amazon docs" 88 } 89 90 vcl { 91 name = "my_custom_main_vcl" 92 content = <<EOF 93 sub vcl_recv { 94 #FASTLY recv 95 96 if (req.request != "HEAD" && req.request != "GET" && req.request != "FASTLYPURGE") { 97 return(pass); 98 } 99 100 return(lookup); 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 backend { 121 address = "aws.amazon.com" 122 name = "amazon docs" 123 } 124 125 vcl { 126 name = "my_custom_main_vcl" 127 content = <<EOF 128 sub vcl_recv { 129 #FASTLY recv 130 131 if (req.request != "HEAD" && req.request != "GET" && req.request != "FASTLYPURGE") { 132 return(pass); 133 } 134 135 return(lookup); 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 EOF 148 } 149 150 force_destroy = true 151 }`, name, domain) 152 }