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