github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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/gophercloud/gophercloud"
    12  	"github.com/gophercloud/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  			if _, ok := err.(gophercloud.ErrDefault404); ok {
   121  				return nil
   122  			}
   123  
   124  			return err
   125  		}
   126  
   127  		return fmt.Errorf("Volume still exists")
   128  	}
   129  }
   130  
   131  func testAccCheckBlockStorageV1VolumeMetadata(
   132  	volume *volumes.Volume, k string, v string) resource.TestCheckFunc {
   133  	return func(s *terraform.State) error {
   134  		if volume.Metadata == nil {
   135  			return fmt.Errorf("No metadata")
   136  		}
   137  
   138  		for key, value := range volume.Metadata {
   139  			if k != key {
   140  				continue
   141  			}
   142  
   143  			if v == value {
   144  				return nil
   145  			}
   146  
   147  			return fmt.Errorf("Bad value for %s: %s", k, value)
   148  		}
   149  
   150  		return fmt.Errorf("Metadata not found: %s", k)
   151  	}
   152  }
   153  
   154  var testAccBlockStorageV1Volume_basic = fmt.Sprintf(`
   155  	resource "openstack_blockstorage_volume_v1" "volume_1" {
   156  		name = "tf-test-volume"
   157  		description = "first test volume"
   158  		metadata{
   159  			foo = "bar"
   160  		}
   161  		size = 1
   162  	}`)
   163  
   164  var testAccBlockStorageV1Volume_update = fmt.Sprintf(`
   165  	resource "openstack_blockstorage_volume_v1" "volume_1" {
   166  		name = "tf-test-volume-updated"
   167  		description = "first test volume"
   168  		metadata{
   169  			foo = "bar"
   170  		}
   171  		size = 1
   172  	}`)