github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 testAccCheckBlockStorageV1VolumeDestroy(s *terraform.State) error {
    65  	config := testAccProvider.Meta().(*Config)
    66  	blockStorageClient, err := config.blockStorageV1Client(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_v1" {
    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 testAccCheckBlockStorageV1VolumeExists(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.blockStorageV1Client(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 testAccCheckBlockStorageV1VolumeDoesNotExist(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.blockStorageV1Client(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  
   131  			return err
   132  		}
   133  
   134  		return fmt.Errorf("Volume still exists")
   135  	}
   136  }
   137  
   138  func testAccCheckBlockStorageV1VolumeMetadata(
   139  	volume *volumes.Volume, k string, v string) resource.TestCheckFunc {
   140  	return func(s *terraform.State) error {
   141  		if volume.Metadata == nil {
   142  			return fmt.Errorf("No metadata")
   143  		}
   144  
   145  		for key, value := range volume.Metadata {
   146  			if k != key {
   147  				continue
   148  			}
   149  
   150  			if v == value {
   151  				return nil
   152  			}
   153  
   154  			return fmt.Errorf("Bad value for %s: %s", k, value)
   155  		}
   156  
   157  		return fmt.Errorf("Metadata not found: %s", k)
   158  	}
   159  }
   160  
   161  const testAccBlockStorageV1Volume_basic = `
   162  resource "openstack_blockstorage_volume_v1" "volume_1" {
   163    name = "volume_1"
   164    description = "first test volume"
   165    metadata{
   166      foo = "bar"
   167    }
   168    size = 1
   169  }
   170  `
   171  
   172  const testAccBlockStorageV1Volume_update = `
   173  resource "openstack_blockstorage_volume_v1" "volume_1" {
   174    name = "volume_1-updated"
   175    description = "first test volume"
   176    metadata{
   177      foo = "bar"
   178    }
   179    size = 1
   180  }
   181  `
   182  
   183  var testAccBlockStorageV1Volume_image = fmt.Sprintf(`
   184  resource "openstack_blockstorage_volume_v1" "volume_1" {
   185    name = "volume_1"
   186    size = 5
   187    image_id = "%s"
   188  }
   189  `, OS_IMAGE_ID)