github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_slb_attachment_test.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/denverdino/aliyungo/slb"
     6  	"github.com/hashicorp/terraform/helper/resource"
     7  	"github.com/hashicorp/terraform/terraform"
     8  	"log"
     9  	"testing"
    10  )
    11  
    12  func TestAccAlicloudSlbAttachment_basic(t *testing.T) {
    13  	var slb slb.LoadBalancerType
    14  
    15  	testCheckAttr := func() resource.TestCheckFunc {
    16  		return func(*terraform.State) error {
    17  			log.Printf("testCheckAttr slb BackendServers is: %#v", slb.BackendServers)
    18  			return nil
    19  		}
    20  	}
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck: func() {
    24  			testAccPreCheck(t)
    25  		},
    26  
    27  		// module name
    28  		IDRefreshName: "alicloud_slb_attachment.foo",
    29  		Providers:     testAccProviders,
    30  		CheckDestroy:  testAccCheckSlbDestroy,
    31  		Steps: []resource.TestStep{
    32  			//test internet_charge_type is paybybandwidth
    33  			resource.TestStep{
    34  				Config: testAccSlbAttachment,
    35  				Check: resource.ComposeTestCheckFunc(
    36  					testAccCheckSlbExists("alicloud_slb_attachment.foo", &slb),
    37  					testCheckAttr(),
    38  					testAccCheckAttachment("alicloud_instance.foo", &slb),
    39  				),
    40  			},
    41  		},
    42  	})
    43  }
    44  
    45  func testAccCheckAttachment(n string, slb *slb.LoadBalancerType) resource.TestCheckFunc {
    46  	return func(s *terraform.State) error {
    47  		rs, ok := s.RootModule().Resources[n]
    48  		if !ok {
    49  			return fmt.Errorf("Not found: %s", n)
    50  		}
    51  
    52  		if rs.Primary.ID == "" {
    53  			return fmt.Errorf("No ECS ID is set")
    54  		}
    55  
    56  		ecsInstanceId := rs.Primary.ID
    57  
    58  		backendServers := slb.BackendServers.BackendServer
    59  
    60  		if len(backendServers) == 0 {
    61  			return fmt.Errorf("no SLB backendServer: %#v", backendServers)
    62  		}
    63  
    64  		log.Printf("slb bacnendservers: %#v", backendServers)
    65  
    66  		backendServersInstanceId := backendServers[0].ServerId
    67  
    68  		if ecsInstanceId != backendServersInstanceId {
    69  			return fmt.Errorf("SLB attachment check invalid: ECS instance %s is not equal SLB backendServer %s",
    70  				ecsInstanceId, backendServersInstanceId)
    71  		}
    72  		return nil
    73  	}
    74  }
    75  
    76  const testAccSlbAttachment = `
    77  resource "alicloud_security_group" "foo" {
    78  	name = "tf_test_foo"
    79  	description = "foo"
    80  }
    81  
    82  resource "alicloud_security_group_rule" "http-in" {
    83    	type = "ingress"
    84    	ip_protocol = "tcp"
    85    	nic_type = "internet"
    86    	policy = "accept"
    87    	port_range = "80/80"
    88    	priority = 1
    89    	security_group_id = "${alicloud_security_group.foo.id}"
    90    	cidr_ip = "0.0.0.0/0"
    91  }
    92  
    93  resource "alicloud_security_group_rule" "ssh-in" {
    94    	type = "ingress"
    95    	ip_protocol = "tcp"
    96    	nic_type = "internet"
    97    	policy = "accept"
    98    	port_range = "22/22"
    99    	priority = 1
   100    	security_group_id = "${alicloud_security_group.foo.id}"
   101    	cidr_ip = "0.0.0.0/0"
   102  }
   103  
   104  resource "alicloud_instance" "foo" {
   105  	# cn-beijing
   106  	image_id = "ubuntu_140405_64_40G_cloudinit_20161115.vhd"
   107  
   108  	# series II
   109  	instance_type = "ecs.n1.medium"
   110  	internet_charge_type = "PayByBandwidth"
   111  	internet_max_bandwidth_out = "5"
   112  	system_disk_category = "cloud_efficiency"
   113  	io_optimized = "optimized"
   114  
   115  	security_groups = ["${alicloud_security_group.foo.id}"]
   116  	instance_name = "test_foo"
   117  }
   118  
   119  resource "alicloud_slb" "foo" {
   120  	name = "tf_test_slb_bind"
   121  	internet_charge_type = "paybybandwidth"
   122  	bandwidth = "5"
   123  	internet = "true"
   124  }
   125  
   126  resource "alicloud_slb_attachment" "foo" {
   127  	slb_id = "${alicloud_slb.foo.id}"
   128  	instances = ["${alicloud_instance.foo.id}"]
   129  }
   130  
   131  `