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