github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/fastly/resource_fastly_service_v1_papertrail_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_papertrail_basic(t *testing.T) { 15 var service gofastly.ServiceDetail 16 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 17 domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10)) 18 19 log1 := gofastly.Papertrail{ 20 Version: 1, 21 Name: "papertrailtesting", 22 Address: "test1.papertrailapp.com", 23 Port: uint(3600), 24 Format: "%h %l %u %t %r %>s", 25 ResponseCondition: "test_response_condition", 26 } 27 28 log2 := gofastly.Papertrail{ 29 Version: 1, 30 Name: "papertrailtesting2", 31 Address: "test2.papertrailapp.com", 32 Port: uint(8080), 33 Format: "%h %l %u %t %r %>s", 34 } 35 36 resource.Test(t, resource.TestCase{ 37 PreCheck: func() { testAccPreCheck(t) }, 38 Providers: testAccProviders, 39 CheckDestroy: testAccCheckServiceV1Destroy, 40 Steps: []resource.TestStep{ 41 resource.TestStep{ 42 Config: testAccServiceV1PapertrailConfig(name, domainName1), 43 Check: resource.ComposeTestCheckFunc( 44 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 45 testAccCheckFastlyServiceV1PapertrailAttributes(&service, []*gofastly.Papertrail{&log1}), 46 resource.TestCheckResourceAttr( 47 "fastly_service_v1.foo", "name", name), 48 resource.TestCheckResourceAttr( 49 "fastly_service_v1.foo", "papertrail.#", "1"), 50 ), 51 }, 52 53 resource.TestStep{ 54 Config: testAccServiceV1PapertrailConfig_update(name, domainName1), 55 Check: resource.ComposeTestCheckFunc( 56 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 57 testAccCheckFastlyServiceV1PapertrailAttributes(&service, []*gofastly.Papertrail{&log1, &log2}), 58 resource.TestCheckResourceAttr( 59 "fastly_service_v1.foo", "name", name), 60 resource.TestCheckResourceAttr( 61 "fastly_service_v1.foo", "papertrail.#", "2"), 62 ), 63 }, 64 }, 65 }) 66 } 67 68 func testAccCheckFastlyServiceV1PapertrailAttributes(service *gofastly.ServiceDetail, papertrails []*gofastly.Papertrail) resource.TestCheckFunc { 69 return func(s *terraform.State) error { 70 71 conn := testAccProvider.Meta().(*FastlyClient).conn 72 papertrailList, err := conn.ListPapertrails(&gofastly.ListPapertrailsInput{ 73 Service: service.ID, 74 Version: service.ActiveVersion.Number, 75 }) 76 77 if err != nil { 78 return fmt.Errorf("[ERR] Error looking up Papertrail for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err) 79 } 80 81 if len(papertrailList) != len(papertrails) { 82 return fmt.Errorf("Papertrail List count mismatch, expected (%d), got (%d)", len(papertrails), len(papertrailList)) 83 } 84 85 var found int 86 for _, p := range papertrails { 87 for _, lp := range papertrailList { 88 if p.Name == lp.Name { 89 // we don't know these things ahead of time, so populate them now 90 p.ServiceID = service.ID 91 p.Version = service.ActiveVersion.Number 92 // We don't track these, so clear them out because we also wont know 93 // these ahead of time 94 lp.CreatedAt = nil 95 lp.UpdatedAt = nil 96 if !reflect.DeepEqual(p, lp) { 97 return fmt.Errorf("Bad match Papertrail match, expected (%#v), got (%#v)", p, lp) 98 } 99 found++ 100 } 101 } 102 } 103 104 if found != len(papertrails) { 105 return fmt.Errorf("Error matching Papertrail rules") 106 } 107 108 return nil 109 } 110 } 111 112 func testAccServiceV1PapertrailConfig(name, domain string) string { 113 return fmt.Sprintf(` 114 resource "fastly_service_v1" "foo" { 115 name = "%s" 116 117 domain { 118 name = "%s" 119 comment = "tf-testing-domain" 120 } 121 122 backend { 123 address = "aws.amazon.com" 124 name = "amazon docs" 125 } 126 127 condition { 128 name = "test_response_condition" 129 type = "RESPONSE" 130 priority = 5 131 statement = "resp.status >= 400 && resp.status < 600" 132 } 133 134 papertrail { 135 name = "papertrailtesting" 136 address = "test1.papertrailapp.com" 137 port = 3600 138 response_condition = "test_response_condition" 139 } 140 141 force_destroy = true 142 }`, name, domain) 143 } 144 145 func testAccServiceV1PapertrailConfig_update(name, domain string) string { 146 return fmt.Sprintf(` 147 resource "fastly_service_v1" "foo" { 148 name = "%s" 149 150 domain { 151 name = "%s" 152 comment = "tf-testing-domain" 153 } 154 155 backend { 156 address = "aws.amazon.com" 157 name = "amazon docs" 158 } 159 160 condition { 161 name = "test_response_condition" 162 type = "RESPONSE" 163 priority = 5 164 statement = "resp.status >= 400 && resp.status < 600" 165 } 166 167 papertrail { 168 name = "papertrailtesting" 169 address = "test1.papertrailapp.com" 170 port = 3600 171 response_condition = "test_response_condition" 172 } 173 174 papertrail { 175 name = "papertrailtesting2" 176 address = "test2.papertrailapp.com" 177 port = 8080 178 } 179 180 force_destroy = true 181 }`, name, domain) 182 }