github.com/atsaki/terraform@v0.4.3-0.20150919165407-25bba5967654/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1_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/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
    12  )
    13  
    14  func TestAccBlockStorageV1Volume_basic(t *testing.T) {
    15  	var volume volumes.Volume
    16  
    17  	var testAccBlockStorageV1Volume_bootable = fmt.Sprintf(`
    18  		resource "openstack_blockstorage_volume_v1" "volume_1" {
    19  			region = "%s"
    20  			name = "tf-test-volume-bootable"
    21  			size = 5
    22  			image_id = "%s"
    23  		}`,
    24  		os.Getenv("OS_REGION_NAME"), os.Getenv("OS_IMAGE_ID"))
    25  
    26  	resource.Test(t, resource.TestCase{
    27  		PreCheck:     func() { testAccPreCheck(t) },
    28  		Providers:    testAccProviders,
    29  		CheckDestroy: testAccCheckBlockStorageV1VolumeDestroy,
    30  		Steps: []resource.TestStep{
    31  			resource.TestStep{
    32  				Config: testAccBlockStorageV1Volume_basic,
    33  				Check: resource.ComposeTestCheckFunc(
    34  					testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.volume_1", &volume),
    35  					resource.TestCheckResourceAttr("openstack_blockstorage_volume_v1.volume_1", "name", "tf-test-volume"),
    36  					testAccCheckBlockStorageV1VolumeMetadata(&volume, "foo", "bar"),
    37  				),
    38  			},
    39  			resource.TestStep{
    40  				Config: testAccBlockStorageV1Volume_update,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					resource.TestCheckResourceAttr("openstack_blockstorage_volume_v1.volume_1", "name", "tf-test-volume-updated"),
    43  					testAccCheckBlockStorageV1VolumeMetadata(&volume, "foo", "bar"),
    44  				),
    45  			},
    46  			resource.TestStep{
    47  				Config: testAccBlockStorageV1Volume_bootable,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					resource.TestCheckResourceAttr("openstack_blockstorage_volume_v1.volume_1", "name", "tf-test-volume-bootable"),
    50  				),
    51  			},
    52  		},
    53  	})
    54  }
    55  
    56  func testAccCheckBlockStorageV1VolumeDestroy(s *terraform.State) error {
    57  	config := testAccProvider.Meta().(*Config)
    58  	blockStorageClient, err := config.blockStorageV1Client(OS_REGION_NAME)
    59  	if err != nil {
    60  		return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
    61  	}
    62  
    63  	for _, rs := range s.RootModule().Resources {
    64  		if rs.Type != "openstack_blockstorage_volume_v1" {
    65  			continue
    66  		}
    67  
    68  		_, err := volumes.Get(blockStorageClient, rs.Primary.ID).Extract()
    69  		if err == nil {
    70  			return fmt.Errorf("Volume still exists")
    71  		}
    72  	}
    73  
    74  	return nil
    75  }
    76  
    77  func testAccCheckBlockStorageV1VolumeExists(t *testing.T, n string, volume *volumes.Volume) resource.TestCheckFunc {
    78  	return func(s *terraform.State) error {
    79  		rs, ok := s.RootModule().Resources[n]
    80  		if !ok {
    81  			return fmt.Errorf("Not found: %s", n)
    82  		}
    83  
    84  		if rs.Primary.ID == "" {
    85  			return fmt.Errorf("No ID is set")
    86  		}
    87  
    88  		config := testAccProvider.Meta().(*Config)
    89  		blockStorageClient, err := config.blockStorageV1Client(OS_REGION_NAME)
    90  		if err != nil {
    91  			return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
    92  		}
    93  
    94  		found, err := volumes.Get(blockStorageClient, rs.Primary.ID).Extract()
    95  		if err != nil {
    96  			return err
    97  		}
    98  
    99  		if found.ID != rs.Primary.ID {
   100  			return fmt.Errorf("Volume not found")
   101  		}
   102  
   103  		*volume = *found
   104  
   105  		return nil
   106  	}
   107  }
   108  
   109  func testAccCheckBlockStorageV1VolumeMetadata(
   110  	volume *volumes.Volume, k string, v string) resource.TestCheckFunc {
   111  	return func(s *terraform.State) error {
   112  		if volume.Metadata == nil {
   113  			return fmt.Errorf("No metadata")
   114  		}
   115  
   116  		for key, value := range volume.Metadata {
   117  			if k != key {
   118  				continue
   119  			}
   120  
   121  			if v == value {
   122  				return nil
   123  			}
   124  
   125  			return fmt.Errorf("Bad value for %s: %s", k, value)
   126  		}
   127  
   128  		return fmt.Errorf("Metadata not found: %s", k)
   129  	}
   130  }
   131  
   132  var testAccBlockStorageV1Volume_basic = fmt.Sprintf(`
   133  	resource "openstack_blockstorage_volume_v1" "volume_1" {
   134  		region = "%s"
   135  		name = "tf-test-volume"
   136  		description = "first test volume"
   137  		metadata{
   138  			foo = "bar"
   139  		}
   140  		size = 1
   141  	}`,
   142  	OS_REGION_NAME)
   143  
   144  var testAccBlockStorageV1Volume_update = fmt.Sprintf(`
   145  	resource "openstack_blockstorage_volume_v1" "volume_1" {
   146  		region = "%s"
   147  		name = "tf-test-volume-updated"
   148  		description = "first test volume"
   149  		metadata{
   150  			foo = "bar"
   151  		}
   152  		size = 1
   153  	}`,
   154  	OS_REGION_NAME)