github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/oneandone/resource_oneandone_loadbalancer_test.go (about) 1 package oneandone 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/1and1/oneandone-cloudserver-sdk-go" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "os" 11 "time" 12 ) 13 14 func TestAccOneandoneLoadbalancer_Basic(t *testing.T) { 15 var lb oneandone.LoadBalancer 16 17 name := "test_loadbalancer" 18 name_updated := "test_loadbalancer_renamed" 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { 22 testAccPreCheck(t) 23 }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckDOneandoneLoadbalancerDestroyCheck, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: fmt.Sprintf(testAccCheckOneandoneLoadbalancer_basic, name), 29 Check: resource.ComposeTestCheckFunc( 30 func(*terraform.State) error { 31 time.Sleep(10 * time.Second) 32 return nil 33 }, 34 testAccCheckOneandoneLoadbalancerExists("oneandone_loadbalancer.lb", &lb), 35 testAccCheckOneandoneLoadbalancerAttributes("oneandone_loadbalancer.lb", name), 36 resource.TestCheckResourceAttr("oneandone_loadbalancer.lb", "name", name), 37 ), 38 }, 39 resource.TestStep{ 40 Config: fmt.Sprintf(testAccCheckOneandoneLoadbalancer_update, name_updated), 41 Check: resource.ComposeTestCheckFunc( 42 func(*terraform.State) error { 43 time.Sleep(10 * time.Second) 44 return nil 45 }, 46 testAccCheckOneandoneLoadbalancerExists("oneandone_loadbalancer.lb", &lb), 47 testAccCheckOneandoneLoadbalancerAttributes("oneandone_loadbalancer.lb", name_updated), 48 resource.TestCheckResourceAttr("oneandone_loadbalancer.lb", "name", name_updated), 49 ), 50 }, 51 }, 52 }) 53 } 54 55 func testAccCheckDOneandoneLoadbalancerDestroyCheck(s *terraform.State) error { 56 for _, rs := range s.RootModule().Resources { 57 if rs.Type != "oneandone_loadbalancer.lb" { 58 continue 59 } 60 61 api := oneandone.New(os.Getenv("ONEANDONE_TOKEN"), oneandone.BaseUrl) 62 63 _, err := api.GetLoadBalancer(rs.Primary.ID) 64 65 if err == nil { 66 return fmt.Errorf("Loadbalancer still exists %s %s", rs.Primary.ID, err.Error()) 67 } 68 } 69 70 return nil 71 } 72 func testAccCheckOneandoneLoadbalancerAttributes(n string, reverse_dns string) resource.TestCheckFunc { 73 return func(s *terraform.State) error { 74 rs, ok := s.RootModule().Resources[n] 75 if !ok { 76 return fmt.Errorf("Not found: %s", n) 77 } 78 if rs.Primary.Attributes["name"] != reverse_dns { 79 return fmt.Errorf("Bad name: expected %s : found %s ", reverse_dns, rs.Primary.Attributes["name"]) 80 } 81 82 return nil 83 } 84 } 85 86 func testAccCheckOneandoneLoadbalancerExists(n string, fw_p *oneandone.LoadBalancer) resource.TestCheckFunc { 87 return func(s *terraform.State) error { 88 rs, ok := s.RootModule().Resources[n] 89 90 if !ok { 91 return fmt.Errorf("Not found: %s", n) 92 } 93 94 if rs.Primary.ID == "" { 95 return fmt.Errorf("No Record ID is set") 96 } 97 98 api := oneandone.New(os.Getenv("ONEANDONE_TOKEN"), oneandone.BaseUrl) 99 100 found_fw, err := api.GetLoadBalancer(rs.Primary.ID) 101 102 if err != nil { 103 return fmt.Errorf("Error occured while fetching Loadbalancer: %s", rs.Primary.ID) 104 } 105 if found_fw.Id != rs.Primary.ID { 106 return fmt.Errorf("Record not found") 107 } 108 fw_p = found_fw 109 110 return nil 111 } 112 } 113 114 const testAccCheckOneandoneLoadbalancer_basic = ` 115 resource "oneandone_loadbalancer" "lb" { 116 name = "%s" 117 method = "ROUND_ROBIN" 118 persistence = true 119 persistence_time = 60 120 health_check_test = "TCP" 121 health_check_interval = 300 122 datacenter = "US" 123 rules = [ 124 { 125 protocol = "TCP" 126 port_balancer = 8080 127 port_server = 8089 128 source_ip = "0.0.0.0" 129 }, 130 { 131 protocol = "TCP" 132 port_balancer = 9090 133 port_server = 9099 134 source_ip = "0.0.0.0" 135 } 136 ] 137 }` 138 139 const testAccCheckOneandoneLoadbalancer_update = ` 140 resource "oneandone_loadbalancer" "lb" { 141 name = "%s" 142 method = "ROUND_ROBIN" 143 persistence = true 144 persistence_time = 60 145 health_check_test = "TCP" 146 health_check_interval = 300 147 datacenter = "US" 148 rules = [ 149 { 150 protocol = "TCP" 151 port_balancer = 8080 152 port_server = 8089 153 source_ip = "0.0.0.0" 154 } 155 ] 156 }`