github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/openstack/resource_openstack_blockstorage_volume_v2_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  
    11  	"github.com/gophercloud/gophercloud"
    12  	"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
    13  )
    14  
    15  func TestAccBlockStorageV2Volume_basic(t *testing.T) {
    16  	var volume volumes.Volume
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckBlockStorageV2VolumeDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccBlockStorageV2Volume_basic,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckBlockStorageV2VolumeExists(t, "openstack_blockstorage_volume_v2.volume_1", &volume),
    27  					resource.TestCheckResourceAttr("openstack_blockstorage_volume_v2.volume_1", "name", "volume_1"),
    28  					testAccCheckBlockStorageV2VolumeMetadata(&volume, "foo", "bar"),
    29  				),
    30  			},
    31  			resource.TestStep{
    32  				Config: testAccBlockStorageV2Volume_update,
    33  				Check: resource.ComposeTestCheckFunc(
    34  					testAccCheckBlockStorageV2VolumeExists(t, "openstack_blockstorage_volume_v2.volume_1", &volume),
    35  					resource.TestCheckResourceAttr("openstack_blockstorage_volume_v2.volume_1", "name", "volume_1-updated"),
    36  					testAccCheckBlockStorageV2VolumeMetadata(&volume, "foo", "bar"),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func TestAccBlockStorageV2Volume_bootable(t *testing.T) {
    44  	var volume volumes.Volume
    45  
    46  	var testAccBlockStorageV2Volume_bootable = fmt.Sprintf(`
    47  		resource "openstack_blockstorage_volume_v2" "volume_1" {
    48  			name = "volume_1-bootable"
    49  			size = 5
    50  			image_id = "%s"
    51  		}`,
    52  		os.Getenv("OS_IMAGE_ID"))
    53  
    54  	resource.Test(t, resource.TestCase{
    55  		PreCheck:     func() { testAccPreCheck(t) },
    56  		Providers:    testAccProviders,
    57  		CheckDestroy: testAccCheckBlockStorageV2VolumeDestroy,
    58  		Steps: []resource.TestStep{
    59  			resource.TestStep{
    60  				Config: testAccBlockStorageV2Volume_bootable,
    61  				Check: resource.ComposeTestCheckFunc(
    62  					testAccCheckBlockStorageV2VolumeExists(t, "openstack_blockstorage_volume_v2.volume_1", &volume),
    63  					resource.TestCheckResourceAttr("openstack_blockstorage_volume_v2.volume_1", "name", "volume_1-bootable"),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func testAccCheckBlockStorageV2VolumeDestroy(s *terraform.State) error {
    71  	config := testAccProvider.Meta().(*Config)
    72  	blockStorageClient, err := config.blockStorageV2Client(OS_REGION_NAME)
    73  	if err != nil {
    74  		return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
    75  	}
    76  
    77  	for _, rs := range s.RootModule().Resources {
    78  		if rs.Type != "openstack_blockstorage_volume_v2" {
    79  			continue
    80  		}
    81  
    82  		_, err := volumes.Get(blockStorageClient, rs.Primary.ID).Extract()
    83  		if err == nil {
    84  			return fmt.Errorf("Volume still exists")
    85  		}
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  func testAccCheckBlockStorageV2VolumeExists(t *testing.T, n string, volume *volumes.Volume) resource.TestCheckFunc {
    92  	return func(s *terraform.State) error {
    93  		rs, ok := s.RootModule().Resources[n]
    94  		if !ok {
    95  			return fmt.Errorf("Not found: %s", n)
    96  		}
    97  
    98  		if rs.Primary.ID == "" {
    99  			return fmt.Errorf("No ID is set")
   100  		}
   101  
   102  		config := testAccProvider.Meta().(*Config)
   103  		blockStorageClient, err := config.blockStorageV2Client(OS_REGION_NAME)
   104  		if err != nil {
   105  			return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
   106  		}
   107  
   108  		found, err := volumes.Get(blockStorageClient, rs.Primary.ID).Extract()
   109  		if err != nil {
   110  			return err
   111  		}
   112  
   113  		if found.ID != rs.Primary.ID {
   114  			return fmt.Errorf("Volume not found")
   115  		}
   116  
   117  		*volume = *found
   118  
   119  		return nil
   120  	}
   121  }
   122  
   123  func testAccCheckBlockStorageV2VolumeDoesNotExist(t *testing.T, n string, volume *volumes.Volume) resource.TestCheckFunc {
   124  	return func(s *terraform.State) error {
   125  		config := testAccProvider.Meta().(*Config)
   126  		blockStorageClient, err := config.blockStorageV2Client(OS_REGION_NAME)
   127  		if err != nil {
   128  			return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
   129  		}
   130  
   131  		_, err = volumes.Get(blockStorageClient, volume.ID).Extract()
   132  		if err != nil {
   133  			if _, ok := err.(gophercloud.ErrDefault404); ok {
   134  				return nil
   135  			}
   136  			return err
   137  		}
   138  
   139  		return fmt.Errorf("Volume still exists")
   140  	}
   141  }
   142  
   143  func testAccCheckBlockStorageV2VolumeMetadata(
   144  	volume *volumes.Volume, k string, v string) resource.TestCheckFunc {
   145  	return func(s *terraform.State) error {
   146  		if volume.Metadata == nil {
   147  			return fmt.Errorf("No metadata")
   148  		}
   149  
   150  		for key, value := range volume.Metadata {
   151  			if k != key {
   152  				continue
   153  			}
   154  
   155  			if v == value {
   156  				return nil
   157  			}
   158  
   159  			return fmt.Errorf("Bad value for %s: %s", k, value)
   160  		}
   161  
   162  		return fmt.Errorf("Metadata not found: %s", k)
   163  	}
   164  }
   165  
   166  var testAccBlockStorageV2Volume_basic = fmt.Sprintf(`
   167  	resource "openstack_blockstorage_volume_v2" "volume_1" {
   168  		name = "volume_1"
   169  		description = "first test volume"
   170  		metadata {
   171  			foo = "bar"
   172  		}
   173  		size = 1
   174  	}`)
   175  
   176  var testAccBlockStorageV2Volume_update = fmt.Sprintf(`
   177  	resource "openstack_blockstorage_volume_v2" "volume_1" {
   178  		name = "volume_1-updated"
   179  		description = "first test volume"
   180  		metadata {
   181  			foo = "bar"
   182  		}
   183  		size = 1
   184  	}`)