github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/resource_openstack_blockstorage_volume_v1_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/v1/volumes"
    12  )
    13  
    14  func TestAccBlockStorageV1Volume_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: testAccCheckBlockStorageV1VolumeDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccBlockStorageV1Volume_basic,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckBlockStorageV1VolumeExists("openstack_blockstorage_volume_v1.volume_1", &volume),
    26  					testAccCheckBlockStorageV1VolumeMetadata(&volume, "foo", "bar"),
    27  					resource.TestCheckResourceAttr(
    28  						"openstack_blockstorage_volume_v1.volume_1", "name", "volume_1"),
    29  				),
    30  			},
    31  			resource.TestStep{
    32  				Config: testAccBlockStorageV1Volume_update,
    33  				Check: resource.ComposeTestCheckFunc(
    34  					testAccCheckBlockStorageV1VolumeExists("openstack_blockstorage_volume_v1.volume_1", &volume),
    35  					testAccCheckBlockStorageV1VolumeMetadata(&volume, "foo", "bar"),
    36  					resource.TestCheckResourceAttr(
    37  						"openstack_blockstorage_volume_v1.volume_1", "name", "volume_1-updated"),
    38  				),
    39  			},
    40  		},
    41  	})
    42  }
    43  
    44  func TestAccBlockStorageV1Volume_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: testAccCheckBlockStorageV1VolumeDestroy,
    51  		Steps: []resource.TestStep{
    52  			resource.TestStep{
    53  				Config: testAccBlockStorageV1Volume_image,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccCheckBlockStorageV1VolumeExists("openstack_blockstorage_volume_v1.volume_1", &volume),
    56  					resource.TestCheckResourceAttr(
    57  						"openstack_blockstorage_volume_v1.volume_1", "name", "volume_1"),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func TestAccBlockStorageV1Volume_timeout(t *testing.T) {
    65  	var volume volumes.Volume
    66  
    67  	resource.Test(t, resource.TestCase{
    68  		PreCheck:     func() { testAccPreCheck(t) },
    69  		Providers:    testAccProviders,
    70  		CheckDestroy: testAccCheckBlockStorageV1VolumeDestroy,
    71  		Steps: []resource.TestStep{
    72  			resource.TestStep{
    73  				Config: testAccBlockStorageV1Volume_timeout,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckBlockStorageV1VolumeExists("openstack_blockstorage_volume_v1.volume_1", &volume),
    76  				),
    77  			},
    78  		},
    79  	})
    80  }
    81  
    82  func testAccCheckBlockStorageV1VolumeDestroy(s *terraform.State) error {
    83  	config := testAccProvider.Meta().(*Config)
    84  	blockStorageClient, err := config.blockStorageV1Client(OS_REGION_NAME)
    85  	if err != nil {
    86  		return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
    87  	}
    88  
    89  	for _, rs := range s.RootModule().Resources {
    90  		if rs.Type != "openstack_blockstorage_volume_v1" {
    91  			continue
    92  		}
    93  
    94  		_, err := volumes.Get(blockStorageClient, rs.Primary.ID).Extract()
    95  		if err == nil {
    96  			return fmt.Errorf("Volume still exists")
    97  		}
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  func testAccCheckBlockStorageV1VolumeExists(n string, volume *volumes.Volume) resource.TestCheckFunc {
   104  	return func(s *terraform.State) error {
   105  		rs, ok := s.RootModule().Resources[n]
   106  		if !ok {
   107  			return fmt.Errorf("Not found: %s", n)
   108  		}
   109  
   110  		if rs.Primary.ID == "" {
   111  			return fmt.Errorf("No ID is set")
   112  		}
   113  
   114  		config := testAccProvider.Meta().(*Config)
   115  		blockStorageClient, err := config.blockStorageV1Client(OS_REGION_NAME)
   116  		if err != nil {
   117  			return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
   118  		}
   119  
   120  		found, err := volumes.Get(blockStorageClient, rs.Primary.ID).Extract()
   121  		if err != nil {
   122  			return err
   123  		}
   124  
   125  		if found.ID != rs.Primary.ID {
   126  			return fmt.Errorf("Volume not found")
   127  		}
   128  
   129  		*volume = *found
   130  
   131  		return nil
   132  	}
   133  }
   134  
   135  func testAccCheckBlockStorageV1VolumeDoesNotExist(t *testing.T, n string, volume *volumes.Volume) resource.TestCheckFunc {
   136  	return func(s *terraform.State) error {
   137  		config := testAccProvider.Meta().(*Config)
   138  		blockStorageClient, err := config.blockStorageV1Client(OS_REGION_NAME)
   139  		if err != nil {
   140  			return fmt.Errorf("Error creating OpenStack block storage client: %s", err)
   141  		}
   142  
   143  		_, err = volumes.Get(blockStorageClient, volume.ID).Extract()
   144  		if err != nil {
   145  			if _, ok := err.(gophercloud.ErrDefault404); ok {
   146  				return nil
   147  			}
   148  
   149  			return err
   150  		}
   151  
   152  		return fmt.Errorf("Volume still exists")
   153  	}
   154  }
   155  
   156  func testAccCheckBlockStorageV1VolumeMetadata(
   157  	volume *volumes.Volume, k string, v string) resource.TestCheckFunc {
   158  	return func(s *terraform.State) error {
   159  		if volume.Metadata == nil {
   160  			return fmt.Errorf("No metadata")
   161  		}
   162  
   163  		for key, value := range volume.Metadata {
   164  			if k != key {
   165  				continue
   166  			}
   167  
   168  			if v == value {
   169  				return nil
   170  			}
   171  
   172  			return fmt.Errorf("Bad value for %s: %s", k, value)
   173  		}
   174  
   175  		return fmt.Errorf("Metadata not found: %s", k)
   176  	}
   177  }
   178  
   179  const testAccBlockStorageV1Volume_basic = `
   180  resource "openstack_blockstorage_volume_v1" "volume_1" {
   181    name = "volume_1"
   182    description = "first test volume"
   183    availability_zone = "nova"
   184    metadata {
   185      foo = "bar"
   186    }
   187    size = 1
   188  }
   189  `
   190  
   191  const testAccBlockStorageV1Volume_update = `
   192  resource "openstack_blockstorage_volume_v1" "volume_1" {
   193    name = "volume_1-updated"
   194    description = "first test volume"
   195    metadata {
   196      foo = "bar"
   197    }
   198    size = 1
   199  }
   200  `
   201  
   202  var testAccBlockStorageV1Volume_image = fmt.Sprintf(`
   203  resource "openstack_blockstorage_volume_v1" "volume_1" {
   204    name = "volume_1"
   205    size = 5
   206    image_id = "%s"
   207  }
   208  `, OS_IMAGE_ID)
   209  
   210  const testAccBlockStorageV1Volume_timeout = `
   211  resource "openstack_blockstorage_volume_v1" "volume_1" {
   212    name = "volume_1"
   213    description = "first test volume"
   214    size = 1
   215  
   216    timeouts {
   217      create = "5m"
   218      delete = "5m"
   219    }
   220  }
   221  `