github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/alicloud/resource_alicloud_slb_test.go (about) 1 package alicloud 2 3 import ( 4 "fmt" 5 "github.com/denverdino/aliyungo/common" 6 "github.com/denverdino/aliyungo/slb" 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "log" 10 "testing" 11 ) 12 13 func TestAccAlicloudSlb_basic(t *testing.T) { 14 var slb slb.LoadBalancerType 15 16 testCheckAttr := func() resource.TestCheckFunc { 17 return func(*terraform.State) error { 18 log.Printf("testCheckAttr slb AddressType is: %s", slb.AddressType) 19 return nil 20 } 21 } 22 23 resource.Test(t, resource.TestCase{ 24 PreCheck: func() { 25 testAccPreCheck(t) 26 }, 27 28 // module name 29 IDRefreshName: "alicloud_slb.bindwidth", 30 31 Providers: testAccProviders, 32 CheckDestroy: testAccCheckSlbDestroy, 33 Steps: []resource.TestStep{ 34 //test internet_charge_type is paybybandwidth 35 resource.TestStep{ 36 Config: testAccSlbBindWidth, 37 Check: resource.ComposeTestCheckFunc( 38 testAccCheckSlbExists("alicloud_slb.bindwidth", &slb), 39 testCheckAttr(), 40 resource.TestCheckResourceAttr( 41 "alicloud_slb.bindwidth", "internet_charge_type", "paybybandwidth"), 42 ), 43 }, 44 }, 45 }) 46 } 47 48 func TestAccAlicloudSlb_traffic(t *testing.T) { 49 var slb slb.LoadBalancerType 50 51 testCheckAttr := func() resource.TestCheckFunc { 52 return func(*terraform.State) error { 53 log.Printf("testCheckAttr slb AddressType is: %s", slb.AddressType) 54 return nil 55 } 56 } 57 58 resource.Test(t, resource.TestCase{ 59 PreCheck: func() { 60 testAccPreCheck(t) 61 }, 62 63 // module name 64 IDRefreshName: "alicloud_slb.traffic", 65 Providers: testAccProviders, 66 CheckDestroy: testAccCheckSlbDestroy, 67 Steps: []resource.TestStep{ 68 //test internet_charge_type is paybytraffic 69 resource.TestStep{ 70 Config: testAccSlbTraffic, 71 Check: resource.ComposeTestCheckFunc( 72 testAccCheckSlbExists("alicloud_slb.traffic", &slb), 73 testCheckAttr(), 74 resource.TestCheckResourceAttr( 75 "alicloud_slb.traffic", "name", "tf_test_slb_classic"), 76 ), 77 }, 78 }, 79 }) 80 } 81 82 func TestAccAlicloudSlb_listener(t *testing.T) { 83 var slb slb.LoadBalancerType 84 85 testListener := func() resource.TestCheckFunc { 86 return func(*terraform.State) error { 87 listenerPorts := slb.ListenerPorts.ListenerPort[0] 88 if listenerPorts != 161 { 89 return fmt.Errorf("bad loadbalancer listener: %#v", listenerPorts) 90 } 91 92 return nil 93 } 94 } 95 96 resource.Test(t, resource.TestCase{ 97 PreCheck: func() { 98 testAccPreCheck(t) 99 }, 100 101 // module name 102 IDRefreshName: "alicloud_slb.listener", 103 Providers: testAccProviders, 104 CheckDestroy: testAccCheckSlbDestroy, 105 Steps: []resource.TestStep{ 106 resource.TestStep{ 107 Config: testAccSlbListener, 108 Check: resource.ComposeTestCheckFunc( 109 testAccCheckSlbExists("alicloud_slb.listener", &slb), 110 resource.TestCheckResourceAttr( 111 "alicloud_slb.listener", "name", "tf_test_slb"), 112 testAccCheckListenersExists("alicloud_slb.listener", &slb, "http"), 113 testListener(), 114 ), 115 }, 116 }, 117 }) 118 } 119 120 func TestAccAlicloudSlb_vpc(t *testing.T) { 121 var slb slb.LoadBalancerType 122 123 resource.Test(t, resource.TestCase{ 124 PreCheck: func() { 125 testAccPreCheck(t) 126 }, 127 128 // module name 129 IDRefreshName: "alicloud_slb.vpc", 130 Providers: testAccProviders, 131 CheckDestroy: testAccCheckSlbDestroy, 132 Steps: []resource.TestStep{ 133 resource.TestStep{ 134 Config: testAccSlb4Vpc, 135 Check: resource.ComposeTestCheckFunc( 136 testAccCheckSlbExists("alicloud_slb.vpc", &slb), 137 resource.TestCheckResourceAttr( 138 "alicloud_slb.vpc", "name", "tf_test_slb_vpc"), 139 ), 140 }, 141 }, 142 }) 143 } 144 145 func testAccCheckSlbExists(n string, slb *slb.LoadBalancerType) resource.TestCheckFunc { 146 return func(s *terraform.State) error { 147 rs, ok := s.RootModule().Resources[n] 148 if !ok { 149 return fmt.Errorf("Not found: %s", n) 150 } 151 152 if rs.Primary.ID == "" { 153 return fmt.Errorf("No SLB ID is set") 154 } 155 156 client := testAccProvider.Meta().(*AliyunClient) 157 instance, err := client.DescribeLoadBalancerAttribute(rs.Primary.ID) 158 159 if err != nil { 160 return err 161 } 162 if instance == nil { 163 return fmt.Errorf("SLB not found") 164 } 165 166 *slb = *instance 167 return nil 168 } 169 } 170 171 func testAccCheckListenersExists(n string, slb *slb.LoadBalancerType, p string) resource.TestCheckFunc { 172 return func(s *terraform.State) error { 173 rs, ok := s.RootModule().Resources[n] 174 if !ok { 175 return fmt.Errorf("Not found: %s", n) 176 } 177 178 if rs.Primary.ID == "" { 179 return fmt.Errorf("No SLB ID is set") 180 } 181 182 client := testAccProvider.Meta().(*AliyunClient) 183 instance, err := client.DescribeLoadBalancerAttribute(rs.Primary.ID) 184 185 if err != nil { 186 return err 187 } 188 if instance == nil { 189 return fmt.Errorf("SLB not found") 190 } 191 192 exist := false 193 for _, listener := range instance.ListenerPortsAndProtocol.ListenerPortAndProtocol { 194 if listener.ListenerProtocol == p { 195 exist = true 196 break 197 } 198 } 199 200 if !exist { 201 return fmt.Errorf("The %s protocol Listener not found.", p) 202 } 203 return nil 204 } 205 } 206 207 func testAccCheckSlbDestroy(s *terraform.State) error { 208 client := testAccProvider.Meta().(*AliyunClient) 209 210 for _, rs := range s.RootModule().Resources { 211 if rs.Type != "alicloud_slb" { 212 continue 213 } 214 215 // Try to find the Slb 216 instance, err := client.DescribeLoadBalancerAttribute(rs.Primary.ID) 217 218 if instance != nil { 219 return fmt.Errorf("SLB still exist") 220 } 221 222 if err != nil { 223 e, _ := err.(*common.Error) 224 // Verify the error is what we want 225 if e.ErrorResponse.Code != LoadBalancerNotFound { 226 return err 227 } 228 229 } 230 231 } 232 233 return nil 234 } 235 236 const testAccSlbBindWidth = ` 237 resource "alicloud_slb" "bindwidth" { 238 name = "tf_test_slb_bindwidth" 239 internet_charge_type = "paybybandwidth" 240 bandwidth = 5 241 internet = true 242 } 243 ` 244 245 const testAccSlbTraffic = ` 246 resource "alicloud_slb" "traffic" { 247 name = "tf_test_slb_classic" 248 } 249 ` 250 251 const testAccSlbListener = ` 252 resource "alicloud_slb" "listener" { 253 name = "tf_test_slb" 254 internet_charge_type = "paybybandwidth" 255 bandwidth = 5 256 internet = true 257 listener = [ 258 { 259 "instance_port" = "2111" 260 "lb_port" = "21" 261 "lb_protocol" = "tcp" 262 "bandwidth" = 1 263 },{ 264 "instance_port" = "8000" 265 "lb_port" = "80" 266 "lb_protocol" = "http" 267 "bandwidth" = 1 268 },{ 269 "instance_port" = "1611" 270 "lb_port" = "161" 271 "lb_protocol" = "udp" 272 "bandwidth" = 1 273 }] 274 } 275 ` 276 277 const testAccSlb4Vpc = ` 278 resource "alicloud_vpc" "foo" { 279 name = "tf_test_foo" 280 cidr_block = "172.16.0.0/12" 281 } 282 283 resource "alicloud_vswitch" "foo" { 284 vpc_id = "${alicloud_vpc.foo.id}" 285 cidr_block = "172.16.0.0/21" 286 availability_zone = "cn-beijing-b" 287 } 288 289 resource "alicloud_slb" "vpc" { 290 name = "tf_test_slb_vpc" 291 //internet_charge_type = "paybybandwidth" 292 vswitch_id = "${alicloud_vswitch.foo.id}" 293 } 294 `