2016-07-13 16:36:37 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-05-15 15:54:16 +02:00
|
|
|
"context"
|
2016-07-13 16:36:37 +02:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/digitalocean/godo"
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccDigitalOceanVolume_Basic(t *testing.T) {
|
|
|
|
name := fmt.Sprintf("volume-%s", acctest.RandString(10))
|
|
|
|
|
|
|
|
volume := godo.Volume{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanVolumeDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-01-20 18:47:54 +01:00
|
|
|
{
|
2016-07-13 16:36:37 +02:00
|
|
|
Config: fmt.Sprintf(testAccCheckDigitalOceanVolumeConfig_basic, name),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanVolumeExists("digitalocean_volume.foobar", &volume),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_volume.foobar", "name", name),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_volume.foobar", "size", "100"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_volume.foobar", "region", "nyc1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_volume.foobar", "description", "peace makes plenty"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCheckDigitalOceanVolumeConfig_basic = `
|
|
|
|
resource "digitalocean_volume" "foobar" {
|
|
|
|
region = "nyc1"
|
|
|
|
name = "%s"
|
|
|
|
size = 100
|
|
|
|
description = "peace makes plenty"
|
|
|
|
}`
|
|
|
|
|
|
|
|
func testAccCheckDigitalOceanVolumeExists(rn string, volume *godo.Volume) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[rn]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("not found: %s", rn)
|
|
|
|
}
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("no volume ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := testAccProvider.Meta().(*godo.Client)
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
got, _, err := client.Storage.GetVolume(context.Background(), rs.Primary.ID)
|
2016-07-13 16:36:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if got.Name != volume.Name {
|
|
|
|
return fmt.Errorf("wrong volume found, want %q got %q", volume.Name, got.Name)
|
|
|
|
}
|
|
|
|
// get the computed volume details
|
|
|
|
*volume = *got
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckDigitalOceanVolumeDestroy(s *terraform.State) error {
|
|
|
|
client := testAccProvider.Meta().(*godo.Client)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "digitalocean_volume" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the volume
|
2017-05-15 15:54:16 +02:00
|
|
|
_, _, err := client.Storage.GetVolume(context.Background(), rs.Primary.ID)
|
2016-07-13 16:36:37 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Volume still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccDigitalOceanVolume_Droplet(t *testing.T) {
|
|
|
|
var (
|
|
|
|
volume = godo.Volume{Name: fmt.Sprintf("volume-%s", acctest.RandString(10))}
|
|
|
|
droplet godo.Droplet
|
|
|
|
)
|
2017-02-10 22:41:35 +01:00
|
|
|
rInt := acctest.RandInt()
|
2016-07-13 16:36:37 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanVolumeDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-01-20 18:47:54 +01:00
|
|
|
{
|
2017-02-12 20:57:44 +01:00
|
|
|
Config: testAccCheckDigitalOceanVolumeConfig_droplet(rInt, volume.Name),
|
2016-07-13 16:36:37 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanVolumeExists("digitalocean_volume.foobar", &volume),
|
|
|
|
testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet),
|
|
|
|
// the droplet should see an attached volume
|
|
|
|
resource.TestCheckResourceAttr(
|
2017-01-20 18:47:54 +01:00
|
|
|
"digitalocean_droplet.foobar", "volume_ids.#", "1"),
|
2016-07-13 16:36:37 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:41:35 +01:00
|
|
|
func testAccCheckDigitalOceanVolumeConfig_droplet(rInt int, vName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-07-13 16:36:37 +02:00
|
|
|
resource "digitalocean_volume" "foobar" {
|
|
|
|
region = "nyc1"
|
|
|
|
name = "%s"
|
|
|
|
size = 100
|
|
|
|
description = "peace makes plenty"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "digitalocean_droplet" "foobar" {
|
2017-02-10 22:41:35 +01:00
|
|
|
name = "baz-%d"
|
2016-07-13 16:36:37 +02:00
|
|
|
size = "1gb"
|
2017-02-12 20:57:44 +01:00
|
|
|
image = "centos-7-x64"
|
2016-07-13 16:36:37 +02:00
|
|
|
region = "nyc1"
|
|
|
|
ipv6 = true
|
|
|
|
private_networking = true
|
|
|
|
volume_ids = ["${digitalocean_volume.foobar.id}"]
|
2017-02-10 22:41:35 +01:00
|
|
|
}`, vName, rInt)
|
|
|
|
}
|