github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/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/rackspace/gophercloud/openstack/blockstorage/v1/volumes" 11 ) 12 13 func TestAccBlockStorageV1Volume_basic(t *testing.T) { 14 var volume volumes.Volume 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckBlockStorageV1VolumeDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccBlockStorageV1Volume_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckBlockStorageV1VolumeExists(t, "openstack_blockstorage_volume_v1.volume_1", &volume), 25 resource.TestCheckResourceAttr("openstack_blockstorage_volume_v1.volume_1", "name", "tf-test-volume"), 26 testAccCheckBlockStorageV1VolumeMetadata(&volume, "foo", "bar"), 27 ), 28 }, 29 resource.TestStep{ 30 Config: testAccBlockStorageV1Volume_update, 31 Check: resource.ComposeTestCheckFunc( 32 resource.TestCheckResourceAttr("openstack_blockstorage_volume_v1.volume_1", "name", "tf-test-volume-updated"), 33 testAccCheckBlockStorageV1VolumeMetadata(&volume, "foo", "bar"), 34 ), 35 }, 36 }, 37 }) 38 } 39 40 func testAccCheckBlockStorageV1VolumeDestroy(s *terraform.State) error { 41 config := testAccProvider.Meta().(*Config) 42 blockStorageClient, err := config.blockStorageV1Client(OS_REGION_NAME) 43 if err != nil { 44 return fmt.Errorf("Error creating OpenStack block storage client: %s", err) 45 } 46 47 for _, rs := range s.RootModule().Resources { 48 if rs.Type != "openstack_blockstorage_volume_v1" { 49 continue 50 } 51 52 _, err := volumes.Get(blockStorageClient, rs.Primary.ID).Extract() 53 if err == nil { 54 return fmt.Errorf("Volume still exists") 55 } 56 } 57 58 return nil 59 } 60 61 func testAccCheckBlockStorageV1VolumeExists(t *testing.T, n string, volume *volumes.Volume) resource.TestCheckFunc { 62 return func(s *terraform.State) error { 63 rs, ok := s.RootModule().Resources[n] 64 if !ok { 65 return fmt.Errorf("Not found: %s", n) 66 } 67 68 if rs.Primary.ID == "" { 69 return fmt.Errorf("No ID is set") 70 } 71 72 config := testAccProvider.Meta().(*Config) 73 blockStorageClient, err := config.blockStorageV1Client(OS_REGION_NAME) 74 if err != nil { 75 return fmt.Errorf("Error creating OpenStack block storage client: %s", err) 76 } 77 78 found, err := volumes.Get(blockStorageClient, rs.Primary.ID).Extract() 79 if err != nil { 80 return err 81 } 82 83 if found.ID != rs.Primary.ID { 84 return fmt.Errorf("Volume not found") 85 } 86 87 *volume = *found 88 89 return nil 90 } 91 } 92 93 func testAccCheckBlockStorageV1VolumeMetadata( 94 volume *volumes.Volume, k string, v string) resource.TestCheckFunc { 95 return func(s *terraform.State) error { 96 if volume.Metadata == nil { 97 return fmt.Errorf("No metadata") 98 } 99 100 for key, value := range volume.Metadata { 101 if k != key { 102 continue 103 } 104 105 if v == value { 106 return nil 107 } 108 109 return fmt.Errorf("Bad value for %s: %s", k, value) 110 } 111 112 return fmt.Errorf("Metadata not found: %s", k) 113 } 114 } 115 116 var testAccBlockStorageV1Volume_basic = fmt.Sprintf(` 117 resource "openstack_blockstorage_volume_v1" "volume_1" { 118 region = "%s" 119 name = "tf-test-volume" 120 description = "first test volume" 121 metadata{ 122 foo = "bar" 123 } 124 size = 1 125 }`, 126 OS_REGION_NAME) 127 128 var testAccBlockStorageV1Volume_update = fmt.Sprintf(` 129 resource "openstack_blockstorage_volume_v1" "volume_1" { 130 region = "%s" 131 name = "tf-test-volume-updated" 132 description = "first test volume" 133 metadata{ 134 foo = "bar" 135 } 136 size = 1 137 }`, 138 OS_REGION_NAME)