github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/fastly/resource_fastly_service_v1_sumologic_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 TestResourceFastlyFlattenSumologic(t *testing.T) { 15 cases := []struct { 16 remote []*gofastly.Sumologic 17 local []map[string]interface{} 18 }{ 19 { 20 remote: []*gofastly.Sumologic{ 21 &gofastly.Sumologic{ 22 Name: "sumo collector", 23 URL: "https://sumologic.com/collector/1", 24 Format: "log format", 25 FormatVersion: 2, 26 MessageType: "classic", 27 ResponseCondition: "condition 1", 28 }, 29 }, 30 local: []map[string]interface{}{ 31 map[string]interface{}{ 32 "name": "sumo collector", 33 "url": "https://sumologic.com/collector/1", 34 "format": "log format", 35 "format_version": 2, 36 "message_type": "classic", 37 "response_condition": "condition 1", 38 }, 39 }, 40 }, 41 } 42 43 for _, c := range cases { 44 out := flattenSumologics(c.remote) 45 if !reflect.DeepEqual(out, c.local) { 46 t.Fatalf("Error matching:\nexpected: %#v\ngot: %#v", c.local, out) 47 } 48 } 49 } 50 51 func TestAccFastlyServiceV1_sumologic(t *testing.T) { 52 var service gofastly.ServiceDetail 53 name := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 54 sumologicName := fmt.Sprintf("sumologic %s", acctest.RandString(3)) 55 56 resource.Test(t, resource.TestCase{ 57 PreCheck: func() { testAccPreCheck(t) }, 58 Providers: testAccProviders, 59 CheckDestroy: testAccCheckServiceV1Destroy, 60 Steps: []resource.TestStep{ 61 resource.TestStep{ 62 Config: testAccServiceV1Config_sumologic(name, sumologicName), 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckServiceV1Exists("fastly_service_v1.foo", &service), 65 testAccCheckFastlyServiceV1Attributes_sumologic(&service, name, sumologicName), 66 ), 67 }, 68 }, 69 }) 70 } 71 72 func testAccCheckFastlyServiceV1Attributes_sumologic(service *gofastly.ServiceDetail, name, sumologic string) resource.TestCheckFunc { 73 return func(s *terraform.State) error { 74 75 if service.Name != name { 76 return fmt.Errorf("Bad name, expected (%s), got (%s)", name, service.Name) 77 } 78 79 conn := testAccProvider.Meta().(*FastlyClient).conn 80 sumologicList, err := conn.ListSumologics(&gofastly.ListSumologicsInput{ 81 Service: service.ID, 82 Version: service.ActiveVersion.Number, 83 }) 84 85 if err != nil { 86 return fmt.Errorf("[ERR] Error looking up Sumologics for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err) 87 } 88 89 if len(sumologicList) != 1 { 90 return fmt.Errorf("Sumologic missing, expected: 1, got: %d", len(sumologicList)) 91 } 92 93 if sumologicList[0].Name != sumologic { 94 return fmt.Errorf("Sumologic name mismatch, expected: %s, got: %#v", sumologic, sumologicList[0].Name) 95 } 96 97 return nil 98 } 99 } 100 101 func testAccServiceV1Config_sumologic(name, sumologic string) string { 102 backendName := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3)) 103 104 return fmt.Sprintf(` 105 resource "fastly_service_v1" "foo" { 106 name = "%s" 107 108 domain { 109 name = "test.notadomain.com" 110 comment = "tf-testing-domain" 111 } 112 113 backend { 114 address = "%s" 115 name = "tf -test backend" 116 } 117 118 sumologic { 119 name = "%s" 120 url = "https://sumologic.com/collector/1" 121 format_version = 2 122 } 123 124 force_destroy = true 125 }`, name, backendName, sumologic) 126 }