github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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_instance" "foo" {
    83  	# cn-beijing
    84  	availability_zone = "cn-beijing-b"
    85  	image_id = "ubuntu_140405_64_40G_cloudinit_20161115.vhd"
    86  
    87  	# series II
    88  	instance_type = "ecs.n1.medium"
    89  	internet_charge_type = "PayByBandwidth"
    90  	internet_max_bandwidth_out = "5"
    91  	system_disk_category = "cloud_efficiency"
    92  	io_optimized = "optimized"
    93  
    94  	security_groups = ["${alicloud_security_group.foo.id}"]
    95  	instance_name = "test_foo"
    96  }
    97  
    98  resource "alicloud_slb" "foo" {
    99  	name = "tf_test_slb_bind"
   100  	internet_charge_type = "paybybandwidth"
   101  	bandwidth = "5"
   102  	internet = "true"
   103  }
   104  
   105  resource "alicloud_slb_attachment" "foo" {
   106  	slb_id = "${alicloud_slb.foo.id}"
   107  	instances = ["${alicloud_instance.foo.id}"]
   108  }
   109  
   110  `