github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/alicloud/resource_alicloud_disk_attachment_test.go (about) 1 package alicloud 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/denverdino/aliyungo/ecs" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "time" 11 ) 12 13 func TestAccAlicloudDiskAttachment(t *testing.T) { 14 var i ecs.InstanceAttributesType 15 var v ecs.DiskItemType 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { 19 testAccPreCheck(t) 20 }, 21 22 // module name 23 IDRefreshName: "alicloud_disk_attachment.disk-att", 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckDiskAttachmentDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccDiskAttachmentConfig, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckInstanceExists( 31 "alicloud_instance.instance", &i), 32 testAccCheckDiskExists( 33 "alicloud_disk.disk", &v), 34 testAccCheckDiskAttachmentExists( 35 "alicloud_disk_attachment.disk-att", &i, &v), 36 resource.TestCheckResourceAttr( 37 "alicloud_disk_attachment.disk-att", 38 "device_name", 39 "/dev/xvdb"), 40 ), 41 }, 42 }, 43 }) 44 45 } 46 47 func testAccCheckDiskAttachmentExists(n string, instance *ecs.InstanceAttributesType, disk *ecs.DiskItemType) resource.TestCheckFunc { 48 return func(s *terraform.State) error { 49 rs, ok := s.RootModule().Resources[n] 50 if !ok { 51 return fmt.Errorf("Not found: %s", n) 52 } 53 54 if rs.Primary.ID == "" { 55 return fmt.Errorf("No Disk ID is set") 56 } 57 58 client := testAccProvider.Meta().(*AliyunClient) 59 conn := client.ecsconn 60 61 request := &ecs.DescribeDisksArgs{ 62 RegionId: client.Region, 63 DiskIds: []string{rs.Primary.Attributes["disk_id"]}, 64 } 65 66 return resource.Retry(3*time.Minute, func() *resource.RetryError { 67 response, _, err := conn.DescribeDisks(request) 68 if response != nil { 69 for _, d := range response { 70 if d.Status != ecs.DiskStatusInUse { 71 return resource.RetryableError(fmt.Errorf("Disk is in attaching - trying again while it attaches")) 72 } else if d.InstanceId == instance.InstanceId { 73 // pass 74 *disk = d 75 return nil 76 } 77 } 78 } 79 if err != nil { 80 return resource.NonRetryableError(err) 81 } 82 83 return resource.NonRetryableError(fmt.Errorf("Error finding instance/disk")) 84 }) 85 } 86 } 87 88 func testAccCheckDiskAttachmentDestroy(s *terraform.State) error { 89 90 for _, rs := range s.RootModule().Resources { 91 if rs.Type != "alicloud_disk_attachment" { 92 continue 93 } 94 // Try to find the Disk 95 client := testAccProvider.Meta().(*AliyunClient) 96 conn := client.ecsconn 97 98 request := &ecs.DescribeDisksArgs{ 99 RegionId: client.Region, 100 DiskIds: []string{rs.Primary.ID}, 101 } 102 103 response, _, err := conn.DescribeDisks(request) 104 105 for _, disk := range response { 106 if disk.Status != ecs.DiskStatusAvailable { 107 return fmt.Errorf("Error ECS Disk Attachment still exist") 108 } 109 } 110 111 if err != nil { 112 // Verify the error is what we want 113 return err 114 } 115 } 116 117 return nil 118 } 119 120 const testAccDiskAttachmentConfig = ` 121 resource "alicloud_disk" "disk" { 122 availability_zone = "cn-beijing-a" 123 size = "50" 124 125 tags { 126 Name = "TerraformTest-disk" 127 } 128 } 129 130 resource "alicloud_instance" "instance" { 131 image_id = "ubuntu_140405_64_40G_cloudinit_20161115.vhd" 132 instance_type = "ecs.s1.small" 133 availability_zone = "cn-beijing-a" 134 security_groups = ["${alicloud_security_group.group.id}"] 135 instance_name = "hello" 136 internet_charge_type = "PayByBandwidth" 137 io_optimized = "none" 138 139 tags { 140 Name = "TerraformTest-instance" 141 } 142 } 143 144 resource "alicloud_disk_attachment" "disk-att" { 145 disk_id = "${alicloud_disk.disk.id}" 146 instance_id = "${alicloud_instance.instance.id}" 147 device_name = "/dev/xvdb" 148 } 149 150 resource "alicloud_security_group" "group" { 151 name = "terraform-test-group" 152 description = "New security group" 153 } 154 `