github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/digitalocean/resource_digitalocean_volume_test.go (about) 1 package digitalocean 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/digitalocean/godo" 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccDigitalOceanVolume_Basic(t *testing.T) { 14 name := fmt.Sprintf("volume-%s", acctest.RandString(10)) 15 16 volume := godo.Volume{ 17 Name: name, 18 } 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckDigitalOceanVolumeDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: fmt.Sprintf(testAccCheckDigitalOceanVolumeConfig_basic, name), 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckDigitalOceanVolumeExists("digitalocean_volume.foobar", &volume), 29 resource.TestCheckResourceAttr( 30 "digitalocean_volume.foobar", "name", name), 31 resource.TestCheckResourceAttr( 32 "digitalocean_volume.foobar", "size", "100"), 33 resource.TestCheckResourceAttr( 34 "digitalocean_volume.foobar", "region", "nyc1"), 35 resource.TestCheckResourceAttr( 36 "digitalocean_volume.foobar", "description", "peace makes plenty"), 37 ), 38 }, 39 }, 40 }) 41 } 42 43 const testAccCheckDigitalOceanVolumeConfig_basic = ` 44 resource "digitalocean_volume" "foobar" { 45 region = "nyc1" 46 name = "%s" 47 size = 100 48 description = "peace makes plenty" 49 }` 50 51 func testAccCheckDigitalOceanVolumeExists(rn string, volume *godo.Volume) resource.TestCheckFunc { 52 return func(s *terraform.State) error { 53 rs, ok := s.RootModule().Resources[rn] 54 if !ok { 55 return fmt.Errorf("not found: %s", rn) 56 } 57 if rs.Primary.ID == "" { 58 return fmt.Errorf("no volume ID is set") 59 } 60 61 client := testAccProvider.Meta().(*godo.Client) 62 63 got, _, err := client.Storage.GetVolume(rs.Primary.ID) 64 if err != nil { 65 return err 66 } 67 if got.Name != volume.Name { 68 return fmt.Errorf("wrong volume found, want %q got %q", volume.Name, got.Name) 69 } 70 // get the computed volume details 71 *volume = *got 72 return nil 73 } 74 } 75 76 func testAccCheckDigitalOceanVolumeDestroy(s *terraform.State) error { 77 client := testAccProvider.Meta().(*godo.Client) 78 79 for _, rs := range s.RootModule().Resources { 80 if rs.Type != "digitalocean_volume" { 81 continue 82 } 83 84 // Try to find the volume 85 _, _, err := client.Storage.GetVolume(rs.Primary.ID) 86 87 if err == nil { 88 return fmt.Errorf("Volume still exists") 89 } 90 } 91 92 return nil 93 } 94 95 func TestAccDigitalOceanVolume_Droplet(t *testing.T) { 96 var ( 97 volume = godo.Volume{Name: fmt.Sprintf("volume-%s", acctest.RandString(10))} 98 droplet godo.Droplet 99 ) 100 101 resource.Test(t, resource.TestCase{ 102 PreCheck: func() { testAccPreCheck(t) }, 103 Providers: testAccProviders, 104 CheckDestroy: testAccCheckDigitalOceanVolumeDestroy, 105 Steps: []resource.TestStep{ 106 resource.TestStep{ 107 Config: fmt.Sprintf( 108 testAccCheckDigitalOceanVolumeConfig_droplet, 109 testAccValidPublicKey, volume.Name, 110 ), 111 Check: resource.ComposeTestCheckFunc( 112 testAccCheckDigitalOceanVolumeExists("digitalocean_volume.foobar", &volume), 113 testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet), 114 // the droplet should see an attached volume 115 resource.TestCheckResourceAttr( 116 "digitalocean_droplet.foobar", "volume_ids", volume.ID), 117 ), 118 }, 119 }, 120 }) 121 } 122 123 const testAccCheckDigitalOceanVolumeConfig_droplet = ` 124 resource "digitalocean_ssh_key" "foobar" { 125 name = "foobar" 126 public_key = "%s" 127 } 128 129 resource "digitalocean_volume" "foobar" { 130 region = "nyc1" 131 name = "%s" 132 size = 100 133 description = "peace makes plenty" 134 } 135 136 resource "digitalocean_droplet" "foobar" { 137 name = "baz" 138 size = "1gb" 139 image = "coreos-stable" 140 region = "nyc1" 141 ipv6 = true 142 private_networking = true 143 ssh_keys = ["${digitalocean_ssh_key.foobar.id}"] 144 volume_ids = ["${digitalocean_volume.foobar.id}"] 145 }`