github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 TestAccComputeV2VolumeAttach_timeout(t *testing.T) {
    51  	var va volumeattach.VolumeAttachment
    52  
    53  	resource.Test(t, resource.TestCase{
    54  		PreCheck:     func() { testAccPreCheck(t) },
    55  		Providers:    testAccProviders,
    56  		CheckDestroy: testAccCheckComputeV2VolumeAttachDestroy,
    57  		Steps: []resource.TestStep{
    58  			resource.TestStep{
    59  				Config: testAccComputeV2VolumeAttach_timeout,
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccCheckComputeV2VolumeAttachExists("openstack_compute_volume_attach_v2.va_1", &va),
    62  				),
    63  			},
    64  		},
    65  	})
    66  }
    67  
    68  func testAccCheckComputeV2VolumeAttachDestroy(s *terraform.State) error {
    69  	config := testAccProvider.Meta().(*Config)
    70  	computeClient, err := config.computeV2Client(OS_REGION_NAME)
    71  	if err != nil {
    72  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
    73  	}
    74  
    75  	for _, rs := range s.RootModule().Resources {
    76  		if rs.Type != "openstack_compute_volume_attach_v2" {
    77  			continue
    78  		}
    79  
    80  		instanceId, volumeId, err := parseComputeVolumeAttachmentId(rs.Primary.ID)
    81  		if err != nil {
    82  			return err
    83  		}
    84  
    85  		_, err = volumeattach.Get(computeClient, instanceId, volumeId).Extract()
    86  		if err == nil {
    87  			return fmt.Errorf("Volume attachment still exists")
    88  		}
    89  	}
    90  
    91  	return nil
    92  }
    93  
    94  func testAccCheckComputeV2VolumeAttachExists(n string, va *volumeattach.VolumeAttachment) resource.TestCheckFunc {
    95  	return func(s *terraform.State) error {
    96  		rs, ok := s.RootModule().Resources[n]
    97  		if !ok {
    98  			return fmt.Errorf("Not found: %s", n)
    99  		}
   100  
   101  		if rs.Primary.ID == "" {
   102  			return fmt.Errorf("No ID is set")
   103  		}
   104  
   105  		config := testAccProvider.Meta().(*Config)
   106  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   107  		if err != nil {
   108  			return fmt.Errorf("Error creating OpenStack compute client: %s", err)
   109  		}
   110  
   111  		instanceId, volumeId, err := parseComputeVolumeAttachmentId(rs.Primary.ID)
   112  		if err != nil {
   113  			return err
   114  		}
   115  
   116  		found, err := volumeattach.Get(computeClient, instanceId, volumeId).Extract()
   117  		if err != nil {
   118  			return err
   119  		}
   120  
   121  		if found.ServerID != instanceId || found.VolumeID != volumeId {
   122  			return fmt.Errorf("VolumeAttach not found")
   123  		}
   124  
   125  		*va = *found
   126  
   127  		return nil
   128  	}
   129  }
   130  
   131  func testAccCheckComputeV2VolumeAttachDevice(
   132  	va *volumeattach.VolumeAttachment, device string) resource.TestCheckFunc {
   133  	return func(s *terraform.State) error {
   134  		if va.Device != device {
   135  			return fmt.Errorf("Requested device of volume attachment (%s) does not match: %s",
   136  				device, va.Device)
   137  		}
   138  
   139  		return nil
   140  	}
   141  }
   142  
   143  const testAccComputeV2VolumeAttach_basic = `
   144  resource "openstack_blockstorage_volume_v2" "volume_1" {
   145    name = "volume_1"
   146    size = 1
   147  }
   148  
   149  resource "openstack_compute_instance_v2" "instance_1" {
   150    name = "instance_1"
   151    security_groups = ["default"]
   152  }
   153  
   154  resource "openstack_compute_volume_attach_v2" "va_1" {
   155    instance_id = "${openstack_compute_instance_v2.instance_1.id}"
   156    volume_id = "${openstack_blockstorage_volume_v2.volume_1.id}"
   157  }
   158  `
   159  
   160  const testAccComputeV2VolumeAttach_device = `
   161  resource "openstack_blockstorage_volume_v2" "volume_1" {
   162    name = "volume_1"
   163    size = 1
   164  }
   165  
   166  resource "openstack_compute_instance_v2" "instance_1" {
   167    name = "instance_1"
   168    security_groups = ["default"]
   169  }
   170  
   171  resource "openstack_compute_volume_attach_v2" "va_1" {
   172    instance_id = "${openstack_compute_instance_v2.instance_1.id}"
   173    volume_id = "${openstack_blockstorage_volume_v2.volume_1.id}"
   174    device = "/dev/vdc"
   175  }
   176  `
   177  
   178  const testAccComputeV2VolumeAttach_timeout = `
   179  resource "openstack_blockstorage_volume_v2" "volume_1" {
   180    name = "volume_1"
   181    size = 1
   182  }
   183  
   184  resource "openstack_compute_instance_v2" "instance_1" {
   185    name = "instance_1"
   186    security_groups = ["default"]
   187  }
   188  
   189  resource "openstack_compute_volume_attach_v2" "va_1" {
   190    instance_id = "${openstack_compute_instance_v2.instance_1.id}"
   191    volume_id = "${openstack_blockstorage_volume_v2.volume_1.id}"
   192  
   193    timeouts {
   194      create = "5m"
   195      delete = "5m"
   196    }
   197  }
   198  `