github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 TestAccBlockStorageV2Volume_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: testAccCheckBlockStorageV2VolumeDestroy, 71 Steps: []resource.TestStep{ 72 resource.TestStep{ 73 Config: testAccBlockStorageV2Volume_timeout, 74 Check: resource.ComposeTestCheckFunc( 75 testAccCheckBlockStorageV2VolumeExists("openstack_blockstorage_volume_v2.volume_1", &volume), 76 ), 77 }, 78 }, 79 }) 80 } 81 82 func testAccCheckBlockStorageV2VolumeDestroy(s *terraform.State) error { 83 config := testAccProvider.Meta().(*Config) 84 blockStorageClient, err := config.blockStorageV2Client(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_v2" { 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 testAccCheckBlockStorageV2VolumeExists(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.blockStorageV2Client(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 testAccCheckBlockStorageV2VolumeDoesNotExist(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.blockStorageV2Client(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 return err 149 } 150 151 return fmt.Errorf("Volume still exists") 152 } 153 } 154 155 func testAccCheckBlockStorageV2VolumeMetadata( 156 volume *volumes.Volume, k string, v string) resource.TestCheckFunc { 157 return func(s *terraform.State) error { 158 if volume.Metadata == nil { 159 return fmt.Errorf("No metadata") 160 } 161 162 for key, value := range volume.Metadata { 163 if k != key { 164 continue 165 } 166 167 if v == value { 168 return nil 169 } 170 171 return fmt.Errorf("Bad value for %s: %s", k, value) 172 } 173 174 return fmt.Errorf("Metadata not found: %s", k) 175 } 176 } 177 178 const testAccBlockStorageV2Volume_basic = ` 179 resource "openstack_blockstorage_volume_v2" "volume_1" { 180 name = "volume_1" 181 description = "first test volume" 182 metadata { 183 foo = "bar" 184 } 185 size = 1 186 } 187 ` 188 189 const testAccBlockStorageV2Volume_update = ` 190 resource "openstack_blockstorage_volume_v2" "volume_1" { 191 name = "volume_1-updated" 192 description = "first test volume" 193 metadata { 194 foo = "bar" 195 } 196 size = 1 197 } 198 ` 199 200 var testAccBlockStorageV2Volume_image = fmt.Sprintf(` 201 resource "openstack_blockstorage_volume_v2" "volume_1" { 202 name = "volume_1" 203 size = 5 204 image_id = "%s" 205 } 206 `, OS_IMAGE_ID) 207 208 const testAccBlockStorageV2Volume_timeout = ` 209 resource "openstack_blockstorage_volume_v2" "volume_1" { 210 name = "volume_1" 211 description = "first test volume" 212 size = 1 213 214 timeouts { 215 create = "5m" 216 delete = "5m" 217 } 218 } 219 `