github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/openstack/resource_openstack_compute_volume_attach_v2_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 10 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach" 11 ) 12 13 func TestAccComputeV2VolumeAttach_basic(t *testing.T) { 14 var va volumeattach.VolumeAttachment 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckComputeV2VolumeAttachDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccComputeV2VolumeAttach_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckComputeV2VolumeAttachExists("openstack_compute_volume_attach_v2.va_1", &va), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func TestAccComputeV2VolumeAttach_device(t *testing.T) { 32 var va volumeattach.VolumeAttachment 33 34 resource.Test(t, resource.TestCase{ 35 PreCheck: func() { testAccPreCheck(t) }, 36 Providers: testAccProviders, 37 CheckDestroy: testAccCheckComputeV2VolumeAttachDestroy, 38 Steps: []resource.TestStep{ 39 resource.TestStep{ 40 Config: testAccComputeV2VolumeAttach_device, 41 Check: resource.ComposeTestCheckFunc( 42 testAccCheckComputeV2VolumeAttachExists("openstack_compute_volume_attach_v2.va_1", &va), 43 testAccCheckComputeV2VolumeAttachDevice(&va, "/dev/vdc"), 44 ), 45 }, 46 }, 47 }) 48 } 49 50 func testAccCheckComputeV2VolumeAttachDestroy(s *terraform.State) error { 51 config := testAccProvider.Meta().(*Config) 52 computeClient, err := config.computeV2Client(OS_REGION_NAME) 53 if err != nil { 54 return fmt.Errorf("Error creating OpenStack compute client: %s", err) 55 } 56 57 for _, rs := range s.RootModule().Resources { 58 if rs.Type != "openstack_compute_volume_attach_v2" { 59 continue 60 } 61 62 instanceId, volumeId, err := parseComputeVolumeAttachmentId(rs.Primary.ID) 63 if err != nil { 64 return err 65 } 66 67 _, err = volumeattach.Get(computeClient, instanceId, volumeId).Extract() 68 if err == nil { 69 return fmt.Errorf("Volume attachment still exists") 70 } 71 } 72 73 return nil 74 } 75 76 func testAccCheckComputeV2VolumeAttachExists(n string, va *volumeattach.VolumeAttachment) resource.TestCheckFunc { 77 return func(s *terraform.State) error { 78 rs, ok := s.RootModule().Resources[n] 79 if !ok { 80 return fmt.Errorf("Not found: %s", n) 81 } 82 83 if rs.Primary.ID == "" { 84 return fmt.Errorf("No ID is set") 85 } 86 87 config := testAccProvider.Meta().(*Config) 88 computeClient, err := config.computeV2Client(OS_REGION_NAME) 89 if err != nil { 90 return fmt.Errorf("Error creating OpenStack compute client: %s", err) 91 } 92 93 instanceId, volumeId, err := parseComputeVolumeAttachmentId(rs.Primary.ID) 94 if err != nil { 95 return err 96 } 97 98 found, err := volumeattach.Get(computeClient, instanceId, volumeId).Extract() 99 if err != nil { 100 return err 101 } 102 103 if found.ServerID != instanceId || found.VolumeID != volumeId { 104 return fmt.Errorf("VolumeAttach not found") 105 } 106 107 *va = *found 108 109 return nil 110 } 111 } 112 113 func testAccCheckComputeV2VolumeAttachDevice( 114 va *volumeattach.VolumeAttachment, device string) resource.TestCheckFunc { 115 return func(s *terraform.State) error { 116 if va.Device != device { 117 return fmt.Errorf("Requested device of volume attachment (%s) does not match: %s", 118 device, va.Device) 119 } 120 121 return nil 122 } 123 } 124 125 const testAccComputeV2VolumeAttach_basic = ` 126 resource "openstack_blockstorage_volume_v2" "volume_1" { 127 name = "volume_1" 128 size = 1 129 } 130 131 resource "openstack_compute_instance_v2" "instance_1" { 132 name = "instance_1" 133 security_groups = ["default"] 134 } 135 136 resource "openstack_compute_volume_attach_v2" "va_1" { 137 instance_id = "${openstack_compute_instance_v2.instance_1.id}" 138 volume_id = "${openstack_blockstorage_volume_v2.volume_1.id}" 139 } 140 ` 141 142 const testAccComputeV2VolumeAttach_device = ` 143 resource "openstack_blockstorage_volume_v2" "volume_1" { 144 name = "volume_1" 145 size = 1 146 } 147 148 resource "openstack_compute_instance_v2" "instance_1" { 149 name = "instance_1" 150 security_groups = ["default"] 151 } 152 153 resource "openstack_compute_volume_attach_v2" "va_1" { 154 instance_id = "${openstack_compute_instance_v2.instance_1.id}" 155 volume_id = "${openstack_blockstorage_volume_v2.volume_1.id}" 156 device = "/dev/vdc" 157 } 158 `