github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_storage_volume_test.go (about) 1 package opc 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/go-oracle-terraform/compute" 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 ) 11 12 func TestAccOPCStorageVolume_Basic(t *testing.T) { 13 volumeResourceName := "opc_compute_storage_volume.test" 14 ri := acctest.RandInt() 15 config := fmt.Sprintf(testAccStorageVolumeBasic, ri) 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeDestroyed), 21 Steps: []resource.TestStep{ 22 { 23 Config: config, 24 Check: resource.ComposeTestCheckFunc( 25 opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeExists), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func TestAccOPCStorageVolume_Complete(t *testing.T) { 33 volumeResourceName := "opc_compute_storage_volume.test" 34 ri := acctest.RandInt() 35 config := fmt.Sprintf(testAccStorageVolumeComplete, ri) 36 37 resource.Test(t, resource.TestCase{ 38 PreCheck: func() { testAccPreCheck(t) }, 39 Providers: testAccProviders, 40 CheckDestroy: opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeDestroyed), 41 Steps: []resource.TestStep{ 42 { 43 Config: config, 44 Check: resource.ComposeTestCheckFunc( 45 opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeExists), 46 ), 47 }, 48 }, 49 }) 50 } 51 52 func TestAccOPCStorageVolume_MaxSize(t *testing.T) { 53 volumeResourceName := "opc_compute_storage_volume.test" 54 ri := acctest.RandInt() 55 config := fmt.Sprintf(testAccStorageVolumeBasicMaxSize, ri) 56 57 resource.Test(t, resource.TestCase{ 58 PreCheck: func() { testAccPreCheck(t) }, 59 Providers: testAccProviders, 60 CheckDestroy: opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeDestroyed), 61 Steps: []resource.TestStep{ 62 { 63 Config: config, 64 Check: resource.ComposeTestCheckFunc( 65 opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeExists), 66 ), 67 }, 68 }, 69 }) 70 } 71 72 func TestAccOPCStorageVolume_Update(t *testing.T) { 73 volumeResourceName := "opc_compute_storage_volume.test" 74 ri := acctest.RandInt() 75 config := fmt.Sprintf(testAccStorageVolumeComplete, ri) 76 updatedConfig := fmt.Sprintf(testAccStorageVolumeUpdated, ri) 77 78 resource.Test(t, resource.TestCase{ 79 PreCheck: func() { testAccPreCheck(t) }, 80 Providers: testAccProviders, 81 CheckDestroy: opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeDestroyed), 82 Steps: []resource.TestStep{ 83 { 84 Config: config, 85 Check: resource.ComposeTestCheckFunc( 86 opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeExists), 87 ), 88 }, 89 { 90 Config: updatedConfig, 91 Check: resource.ComposeTestCheckFunc( 92 opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeExists), 93 ), 94 }, 95 }, 96 }) 97 } 98 99 func TestAccOPCStorageVolume_Bootable(t *testing.T) { 100 volumeResourceName := "opc_compute_storage_volume.test" 101 ri := acctest.RandInt() 102 config := fmt.Sprintf(testAccStorageVolumeBootable, ri, ri) 103 104 resource.Test(t, resource.TestCase{ 105 PreCheck: func() { testAccPreCheck(t) }, 106 Providers: testAccProviders, 107 CheckDestroy: opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeDestroyed), 108 Steps: []resource.TestStep{ 109 { 110 Config: config, 111 Check: resource.ComposeTestCheckFunc( 112 opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeExists), 113 ), 114 }, 115 }, 116 }) 117 } 118 119 func TestAccOPCStorageVolume_ImageListEntry(t *testing.T) { 120 volumeResourceName := "opc_compute_storage_volume.test" 121 ri := acctest.RandInt() 122 config := fmt.Sprintf(testAccStorageVolumeImageListEntry, ri, ri) 123 124 resource.Test(t, resource.TestCase{ 125 PreCheck: func() { testAccPreCheck(t) }, 126 Providers: testAccProviders, 127 CheckDestroy: opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeDestroyed), 128 Steps: []resource.TestStep{ 129 { 130 Config: config, 131 Check: resource.ComposeTestCheckFunc( 132 opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeExists), 133 ), 134 }, 135 }, 136 }) 137 } 138 139 func TestAccOPCStorageVolume_LowLatency(t *testing.T) { 140 volumeResourceName := "opc_compute_storage_volume.test" 141 rInt := acctest.RandInt() 142 resource.Test(t, resource.TestCase{ 143 PreCheck: func() { 144 testAccPreCheck(t) 145 }, 146 Providers: testAccProviders, 147 CheckDestroy: opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeDestroyed), 148 Steps: []resource.TestStep{ 149 { 150 Config: testAccStorageVolumeLowLatency(rInt), 151 Check: resource.ComposeTestCheckFunc( 152 opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeExists), 153 resource.TestCheckResourceAttr(volumeResourceName, "storage_type", "/oracle/public/storage/latency"), 154 ), 155 }, 156 }, 157 }) 158 } 159 160 func TestAccOPCStorageVolume_FromSnapshot(t *testing.T) { 161 volumeResourceName := "opc_compute_storage_volume.test" 162 rInt := acctest.RandInt() 163 resource.Test(t, resource.TestCase{ 164 PreCheck: func() { 165 testAccPreCheck(t) 166 }, 167 Providers: testAccProviders, 168 CheckDestroy: opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeDestroyed), 169 Steps: []resource.TestStep{ 170 { 171 Config: testAccStorageVolumeFromSnapshot(rInt), 172 Check: resource.ComposeTestCheckFunc( 173 opcResourceCheck(volumeResourceName, testAccCheckStorageVolumeExists), 174 resource.TestCheckResourceAttr(volumeResourceName, "name", fmt.Sprintf("test-acc-stor-vol-final-%d", rInt)), 175 resource.TestCheckResourceAttrSet(volumeResourceName, "snapshot"), 176 resource.TestCheckResourceAttrSet(volumeResourceName, "snapshot_id"), 177 resource.TestCheckResourceAttr(volumeResourceName, "size", "5"), 178 ), 179 }, 180 }, 181 }) 182 } 183 184 func testAccCheckStorageVolumeExists(state *OPCResourceState) error { 185 sv := state.Client.StorageVolumes() 186 volumeName := state.Attributes["name"] 187 188 input := compute.GetStorageVolumeInput{ 189 Name: volumeName, 190 } 191 info, err := sv.GetStorageVolume(&input) 192 if err != nil { 193 return fmt.Errorf("Error retrieving state of volume %s: %s", volumeName, err) 194 } 195 196 if info == nil { 197 return fmt.Errorf("No info found for volume %s", volumeName) 198 } 199 200 return nil 201 } 202 203 func testAccCheckStorageVolumeDestroyed(state *OPCResourceState) error { 204 sv := state.Client.StorageVolumes() 205 206 volumeName := state.Attributes["name"] 207 208 input := compute.GetStorageVolumeInput{ 209 Name: volumeName, 210 } 211 info, err := sv.GetStorageVolume(&input) 212 if err != nil { 213 return fmt.Errorf("Error retrieving state of volume %s: %s", volumeName, err) 214 } 215 216 if info != nil { 217 return fmt.Errorf("Volume %s still exists", volumeName) 218 } 219 220 return nil 221 } 222 223 const testAccStorageVolumeBasic = ` 224 resource "opc_compute_storage_volume" "test" { 225 name = "test-acc-stor-vol-%d" 226 size = 1 227 } 228 ` 229 230 const testAccStorageVolumeComplete = ` 231 resource "opc_compute_storage_volume" "test" { 232 name = "test-acc-stor-vol-%d" 233 description = "Provider Acceptance Tests Storage Volume Initial" 234 size = 2 235 tags = ["foo"] 236 } 237 ` 238 239 const testAccStorageVolumeUpdated = ` 240 resource "opc_compute_storage_volume" "test" { 241 name = "test-acc-stor-vol-%d" 242 description = "Provider Acceptance Tests Storage Volume Updated" 243 size = 2 244 tags = ["bar", "foo"] 245 } 246 ` 247 248 const testAccStorageVolumeBootable = ` 249 resource "opc_compute_image_list" "test" { 250 name = "test-acc-stor-vol-bootable-image-list-%d" 251 description = "Provider Acceptance Tests Storage Volume Bootable" 252 } 253 254 resource "opc_compute_image_list_entry" "test" { 255 name = "${opc_compute_image_list.test.name}" 256 machine_images = [ "/oracle/public/oel_6.7_apaas_16.4.5_1610211300" ] 257 version = 1 258 } 259 260 resource "opc_compute_storage_volume" "test" { 261 name = "test-acc-stor-vol-bootable-%d" 262 description = "Provider Acceptance Tests Storage Volume Bootable" 263 size = 20 264 tags = ["bar", "foo"] 265 bootable = true 266 image_list = "${opc_compute_image_list.test.name}" 267 image_list_entry = "${opc_compute_image_list_entry.test.version}" 268 } 269 ` 270 271 const testAccStorageVolumeImageListEntry = ` 272 resource "opc_compute_image_list" "test" { 273 name = "test-acc-stor-vol-bootable-image-list-%d" 274 description = "Provider Acceptance Tests Storage Volume Image List Entry" 275 } 276 277 resource "opc_compute_image_list_entry" "test" { 278 name = "${opc_compute_image_list.test.name}" 279 machine_images = [ "/oracle/public/oel_6.7_apaas_16.4.5_1610211300" ] 280 version = 1 281 } 282 283 resource "opc_compute_storage_volume" "test" { 284 name = "test-acc-stor-vol-image-list-entry-%d" 285 description = "Provider Acceptance Tests Storage Volume Image List Entry" 286 size = 20 287 tags = ["bar", "foo"] 288 image_list_entry = "${opc_compute_image_list_entry.test.version}" 289 } 290 ` 291 292 const testAccStorageVolumeBasicMaxSize = ` 293 resource "opc_compute_storage_volume" "test" { 294 name = "test-acc-stor-vol-%d" 295 description = "Provider Acceptance Tests Storage Volume Max Size" 296 size = 2048 297 } 298 ` 299 300 func testAccStorageVolumeFromSnapshot(rInt int) string { 301 return fmt.Sprintf(` 302 // Initial Storage Volume to create snapshot with 303 resource "opc_compute_storage_volume" "foo" { 304 name = "test-acc-stor-vol-%d" 305 description = "Acc Test intermediary storage volume for snapshot" 306 size = 5 307 } 308 309 resource "opc_compute_storage_volume_snapshot" "foo" { 310 description = "testing-acc" 311 name = "test-acc-stor-snapshot-%d" 312 collocated = true 313 volume_name = "${opc_compute_storage_volume.foo.name}" 314 } 315 316 // Create storage volume from snapshot 317 resource "opc_compute_storage_volume" "test" { 318 name = "test-acc-stor-vol-final-%d" 319 description = "storage volume from snapshot" 320 size = 5 321 snapshot_id = "${opc_compute_storage_volume_snapshot.foo.snapshot_id}" 322 }`, rInt, rInt, rInt) 323 } 324 325 func testAccStorageVolumeLowLatency(rInt int) string { 326 return fmt.Sprintf(` 327 resource "opc_compute_storage_volume" "test" { 328 name = "test-acc-stor-vol-ll-%d" 329 description = "Acc Test Storage Volume Low Latency" 330 storage_type = "/oracle/public/storage/latency" 331 size = 5 332 }`, rInt) 333 }