github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_storage_volume_snapshot_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 TestAccOPCStorageVolumeSnapshot_basic(t *testing.T) {
    13  	snapshotName := "opc_compute_storage_volume_snapshot.test"
    14  	rInt := acctest.RandInt()
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: opcResourceCheck(snapshotName, testAccCheckStorageVolumeSnapshotDestroyed),
    20  		Steps: []resource.TestStep{
    21  			{
    22  				Config: testAccStorageVolumeSnapshot_basic(rInt),
    23  				Check: resource.ComposeTestCheckFunc(opcResourceCheck(snapshotName, testAccCheckStorageVolumeSnapshotExists),
    24  					resource.TestCheckResourceAttr(snapshotName, "name", fmt.Sprintf("test-acc-stor-vol-%d", rInt)),
    25  					resource.TestCheckResourceAttr(snapshotName, "parent_volume_bootable", "false"),
    26  					resource.TestCheckResourceAttr(snapshotName, "collocated", "true"),
    27  					resource.TestCheckResourceAttr(snapshotName, "size", "5"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func testAccCheckStorageVolumeSnapshotExists(state *OPCResourceState) error {
    35  	client := state.Client.StorageVolumeSnapshots()
    36  	snapshotName := state.Attributes["name"]
    37  
    38  	input := &compute.GetStorageVolumeSnapshotInput{
    39  		Name: snapshotName,
    40  	}
    41  
    42  	info, err := client.GetStorageVolumeSnapshot(input)
    43  	if err != nil {
    44  		return fmt.Errorf("Error retrieving state of snapshot '%s': %v", snapshotName, err)
    45  	}
    46  
    47  	if info == nil {
    48  		return fmt.Errorf("No info found for snapshot '%s'", snapshotName)
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  func testAccCheckStorageVolumeSnapshotDestroyed(state *OPCResourceState) error {
    55  	client := state.Client.StorageVolumeSnapshots()
    56  	snapshotName := state.Attributes["name"]
    57  
    58  	input := &compute.GetStorageVolumeSnapshotInput{
    59  		Name: snapshotName,
    60  	}
    61  	info, err := client.GetStorageVolumeSnapshot(input)
    62  	if err != nil {
    63  		return fmt.Errorf("Error retrieving state of snapshot '%s': %v", snapshotName, err)
    64  	}
    65  
    66  	if info != nil {
    67  		return fmt.Errorf("Snapshot '%s' still exists", snapshotName)
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func testAccStorageVolumeSnapshot_basic(rInt int) string {
    74  	return fmt.Sprintf(`
    75  resource "opc_compute_storage_volume" "foo" {
    76    name = "test-acc-stor-vol-%d"
    77    description = "testAccStorageVolumeSnapshot_basic"
    78    size = 5
    79  }
    80  
    81  resource "opc_compute_storage_volume_snapshot" "test" {
    82    name = "test-acc-stor-vol-%d"
    83    description = "storage volume snapshot"
    84    collocated = true
    85    volume_name = "${opc_compute_storage_volume.foo.name}"
    86  }
    87  `, rInt, rInt)
    88  }