2014-07-19 02:20:28 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/pearkes/digitalocean"
|
|
|
|
)
|
|
|
|
|
2014-07-19 18:33:25 +02:00
|
|
|
func TestAccDigitalOceanDroplet_Basic(t *testing.T) {
|
2014-07-19 02:20:28 +02:00
|
|
|
var droplet digitalocean.Droplet
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanDropletDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckDigitalOceanDropletConfig_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet),
|
|
|
|
testAccCheckDigitalOceanDropletAttributes(&droplet),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_droplet.foobar", "name", "foo"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_droplet.foobar", "size", "512mb"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_droplet.foobar", "image", "centos-5-8-x32"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2014-09-05 17:16:12 +02:00
|
|
|
"digitalocean_droplet.foobar", "region", "nyc3"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_droplet.foobar", "user_data", "foobar"),
|
2014-07-19 02:20:28 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-19 18:33:25 +02:00
|
|
|
func TestAccDigitalOceanDroplet_Update(t *testing.T) {
|
|
|
|
var droplet digitalocean.Droplet
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanDropletDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckDigitalOceanDropletConfig_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet),
|
|
|
|
testAccCheckDigitalOceanDropletAttributes(&droplet),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckDigitalOceanDropletConfig_RenameAndResize,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet),
|
|
|
|
testAccCheckDigitalOceanDropletRenamedAndResized(&droplet),
|
|
|
|
resource.TestCheckResourceAttr(
|
2014-07-21 17:05:24 +02:00
|
|
|
"digitalocean_droplet.foobar", "name", "baz"),
|
2014-07-19 18:33:25 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_droplet.foobar", "size", "1gb"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-30 00:58:48 +02:00
|
|
|
func TestAccDigitalOceanDroplet_PrivateNetworkingIpv6(t *testing.T) {
|
|
|
|
var droplet digitalocean.Droplet
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanDropletDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckDigitalOceanDropletConfig_PrivateNetworkingIpv6,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet),
|
|
|
|
testAccCheckDigitalOceanDropletAttributes_PrivateNetworkingIpv6(&droplet),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_droplet.foobar", "private_networking", "true"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_droplet.foobar", "ipv6", "true"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-19 02:20:28 +02:00
|
|
|
func testAccCheckDigitalOceanDropletDestroy(s *terraform.State) error {
|
2014-11-17 18:55:45 +01:00
|
|
|
client := testAccProvider.Meta().(*digitalocean.Client)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-09-17 02:28:22 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-19 02:20:28 +02:00
|
|
|
if rs.Type != "digitalocean_droplet" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the Droplet
|
2014-09-17 02:28:22 +02:00
|
|
|
_, err := client.RetrieveDroplet(rs.Primary.ID)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-07-19 15:45:42 +02:00
|
|
|
// Wait
|
2014-07-19 02:20:28 +02:00
|
|
|
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "404") {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for droplet (%s) to be destroyed: %s",
|
2014-09-17 02:28:22 +02:00
|
|
|
rs.Primary.ID, err)
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckDigitalOceanDropletAttributes(droplet *digitalocean.Droplet) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if droplet.ImageSlug() != "centos-5-8-x32" {
|
|
|
|
return fmt.Errorf("Bad image_slug: %s", droplet.ImageSlug())
|
|
|
|
}
|
|
|
|
|
2014-11-02 23:48:40 +01:00
|
|
|
if droplet.SizeSlug != "512mb" {
|
|
|
|
return fmt.Errorf("Bad size_slug: %s", droplet.SizeSlug)
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
|
2014-09-05 17:18:49 +02:00
|
|
|
if droplet.RegionSlug() != "nyc3" {
|
2014-07-19 02:20:28 +02:00
|
|
|
return fmt.Errorf("Bad region_slug: %s", droplet.RegionSlug())
|
|
|
|
}
|
|
|
|
|
|
|
|
if droplet.Name != "foo" {
|
|
|
|
return fmt.Errorf("Bad name: %s", droplet.Name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-19 18:33:25 +02:00
|
|
|
func testAccCheckDigitalOceanDropletRenamedAndResized(droplet *digitalocean.Droplet) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
2014-11-02 23:48:40 +01:00
|
|
|
if droplet.SizeSlug != "1gb" {
|
|
|
|
return fmt.Errorf("Bad size_slug: %s", droplet.SizeSlug)
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if droplet.Name != "baz" {
|
|
|
|
return fmt.Errorf("Bad name: %s", droplet.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 00:58:48 +02:00
|
|
|
|
|
|
|
func testAccCheckDigitalOceanDropletAttributes_PrivateNetworkingIpv6(droplet *digitalocean.Droplet) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if droplet.ImageSlug() != "centos-5-8-x32" {
|
|
|
|
return fmt.Errorf("Bad image_slug: %s", droplet.ImageSlug())
|
|
|
|
}
|
|
|
|
|
2014-11-02 23:48:40 +01:00
|
|
|
if droplet.SizeSlug != "1gb" {
|
|
|
|
return fmt.Errorf("Bad size_slug: %s", droplet.SizeSlug)
|
2014-07-30 00:58:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if droplet.RegionSlug() != "sgp1" {
|
|
|
|
return fmt.Errorf("Bad region_slug: %s", droplet.RegionSlug())
|
|
|
|
}
|
|
|
|
|
|
|
|
if droplet.Name != "baz" {
|
|
|
|
return fmt.Errorf("Bad name: %s", droplet.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if droplet.IPV4Address("private") == "" {
|
|
|
|
return fmt.Errorf("No ipv4 private: %s", droplet.IPV4Address("private"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// if droplet.IPV6Address("private") == "" {
|
|
|
|
// return fmt.Errorf("No ipv6 private: %s", droplet.IPV6Address("private"))
|
|
|
|
// }
|
|
|
|
|
|
|
|
if droplet.NetworkingType() != "private" {
|
|
|
|
return fmt.Errorf("Bad networking type: %s", droplet.NetworkingType())
|
|
|
|
}
|
|
|
|
|
|
|
|
if droplet.IPV4Address("public") == "" {
|
|
|
|
return fmt.Errorf("No ipv4 public: %s", droplet.IPV4Address("public"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if droplet.IPV6Address("public") == "" {
|
|
|
|
return fmt.Errorf("No ipv6 public: %s", droplet.IPV6Address("public"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-19 02:20:28 +02:00
|
|
|
func testAccCheckDigitalOceanDropletExists(n string, droplet *digitalocean.Droplet) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:28:22 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-19 02:20:28 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:28:22 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-19 02:20:28 +02:00
|
|
|
return fmt.Errorf("No Droplet ID is set")
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
client := testAccProvider.Meta().(*digitalocean.Client)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
2014-09-17 02:28:22 +02:00
|
|
|
retrieveDroplet, err := client.RetrieveDroplet(rs.Primary.ID)
|
2014-07-19 02:20:28 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:28:22 +02:00
|
|
|
if retrieveDroplet.StringId() != rs.Primary.ID {
|
2014-07-19 02:20:28 +02:00
|
|
|
return fmt.Errorf("Droplet not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*droplet = retrieveDroplet
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:55:45 +01:00
|
|
|
// Not sure if this check should remain here as the underlaying
|
|
|
|
// function is changed and is tested indirectly by almost all
|
|
|
|
// other test already
|
|
|
|
//
|
|
|
|
//func Test_new_droplet_state_refresh_func(t *testing.T) {
|
|
|
|
// droplet := digitalocean.Droplet{
|
|
|
|
// Name: "foobar",
|
|
|
|
// }
|
|
|
|
// resourceMap, _ := resource_digitalocean_droplet_update_state(
|
|
|
|
// &terraform.InstanceState{Attributes: map[string]string{}}, &droplet)
|
|
|
|
//
|
|
|
|
// // See if we can access our attribute
|
|
|
|
// if _, ok := resourceMap.Attributes["name"]; !ok {
|
|
|
|
// t.Fatalf("bad name: %s", resourceMap.Attributes)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//}
|
2014-07-19 15:45:42 +02:00
|
|
|
|
2014-07-19 02:20:28 +02:00
|
|
|
const testAccCheckDigitalOceanDropletConfig_basic = `
|
|
|
|
resource "digitalocean_droplet" "foobar" {
|
|
|
|
name = "foo"
|
|
|
|
size = "512mb"
|
|
|
|
image = "centos-5-8-x32"
|
2014-09-05 17:16:12 +02:00
|
|
|
region = "nyc3"
|
|
|
|
user_data = "foobar"
|
2014-07-19 02:20:28 +02:00
|
|
|
}
|
|
|
|
`
|
2014-07-19 18:33:25 +02:00
|
|
|
|
|
|
|
const testAccCheckDigitalOceanDropletConfig_RenameAndResize = `
|
|
|
|
resource "digitalocean_droplet" "foobar" {
|
|
|
|
name = "baz"
|
|
|
|
size = "1gb"
|
|
|
|
image = "centos-5-8-x32"
|
2014-09-05 17:18:49 +02:00
|
|
|
region = "nyc3"
|
2014-07-19 18:33:25 +02:00
|
|
|
}
|
|
|
|
`
|
2014-07-30 00:58:48 +02:00
|
|
|
|
|
|
|
// IPV6 only in singapore
|
|
|
|
const testAccCheckDigitalOceanDropletConfig_PrivateNetworkingIpv6 = `
|
|
|
|
resource "digitalocean_droplet" "foobar" {
|
|
|
|
name = "baz"
|
|
|
|
size = "1gb"
|
|
|
|
image = "centos-5-8-x32"
|
|
|
|
region = "sgp1"
|
|
|
|
ipv6 = true
|
|
|
|
private_networking = true
|
|
|
|
}
|
|
|
|
`
|