provider/digitalocean: acctest improvements

* Add SSH Keys to all droplets in tests, this prevents acctests from
   spamming account owner email with root password details
 * Add a new helper/acctest package to be a home for random string / int
   implementations used in tests.
 * Insert some random details into record tests to prevent collisions
 * Normalize config style in tests to hclfmt conventions
This commit is contained in:
Paul Hinze 2016-01-04 15:21:38 -06:00
parent 5e9e22d4fd
commit 028664a015
5 changed files with 151 additions and 91 deletions

View File

@ -293,37 +293,60 @@ func testAccCheckDigitalOceanDropletRecreated(t *testing.T,
//
//}
const testAccCheckDigitalOceanDropletConfig_basic = `
var testAccCheckDigitalOceanDropletConfig_basic = fmt.Sprintf(`
resource "digitalocean_ssh_key" "foobar" {
name = "foobar"
public_key = "%s"
}
resource "digitalocean_droplet" "foobar" {
name = "foo"
size = "512mb"
image = "centos-5-8-x32"
region = "nyc3"
user_data = "foobar"
ssh_keys = ["${digitalocean_ssh_key.foobar.id}"]
}
`, testAccValidPublicKey)
var testAccCheckDigitalOceanDropletConfig_userdata_update = fmt.Sprintf(`
resource "digitalocean_ssh_key" "foobar" {
name = "foobar"
public_key = "%s"
}
`
const testAccCheckDigitalOceanDropletConfig_userdata_update = `
resource "digitalocean_droplet" "foobar" {
name = "foo"
size = "512mb"
image = "centos-5-8-x32"
region = "nyc3"
user_data = "foobar foobar"
ssh_keys = ["${digitalocean_ssh_key.foobar.id}"]
}
`, testAccValidPublicKey)
var testAccCheckDigitalOceanDropletConfig_RenameAndResize = fmt.Sprintf(`
resource "digitalocean_ssh_key" "foobar" {
name = "foobar"
public_key = "%s"
}
`
const testAccCheckDigitalOceanDropletConfig_RenameAndResize = `
resource "digitalocean_droplet" "foobar" {
name = "baz"
size = "1gb"
image = "centos-5-8-x32"
region = "nyc3"
ssh_keys = ["${digitalocean_ssh_key.foobar.id}"]
}
`
`, testAccValidPublicKey)
// IPV6 only in singapore
const testAccCheckDigitalOceanDropletConfig_PrivateNetworkingIpv6 = `
var testAccCheckDigitalOceanDropletConfig_PrivateNetworkingIpv6 = fmt.Sprintf(`
resource "digitalocean_ssh_key" "foobar" {
name = "foobar"
public_key = "%s"
}
resource "digitalocean_droplet" "foobar" {
name = "baz"
size = "1gb"
@ -331,5 +354,6 @@ resource "digitalocean_droplet" "foobar" {
region = "sgp1"
ipv6 = true
private_networking = true
ssh_keys = ["${digitalocean_ssh_key.foobar.id}"]
}
`
`, testAccValidPublicKey)

View File

@ -104,7 +104,11 @@ resource "digitalocean_floating_ip" "foobar" {
region = "nyc3"
}`
var testAccCheckDigitalOceanFloatingIPConfig_droplet = `
var testAccCheckDigitalOceanFloatingIPConfig_droplet = fmt.Sprintf(`
resource "digitalocean_ssh_key" "foobar" {
name = "foobar"
public_key = "%s"
}
resource "digitalocean_droplet" "foobar" {
name = "baz"
@ -113,9 +117,10 @@ resource "digitalocean_droplet" "foobar" {
region = "sgp1"
ipv6 = true
private_networking = true
ssh_keys = ["${digitalocean_ssh_key.foobar.id}"]
}
resource "digitalocean_floating_ip" "foobar" {
droplet_id = "${digitalocean_droplet.foobar.id}"
region = "${digitalocean_droplet.foobar.region}"
}`
}`, testAccValidPublicKey)

View File

@ -6,12 +6,14 @@ import (
"testing"
"github.com/digitalocean/godo"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccDigitalOceanRecord_Basic(t *testing.T) {
var record godo.DomainRecord
domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
@ -19,14 +21,14 @@ func TestAccDigitalOceanRecord_Basic(t *testing.T) {
CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckDigitalOceanRecordConfig_basic,
Config: fmt.Sprintf(testAccCheckDigitalOceanRecordConfig_basic, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
testAccCheckDigitalOceanRecordAttributes(&record),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "name", "terraform"),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "domain", "foobar-test-terraform.com"),
"digitalocean_record.foobar", "domain", domain),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "value", "192.168.0.10"),
),
@ -37,6 +39,7 @@ func TestAccDigitalOceanRecord_Basic(t *testing.T) {
func TestAccDigitalOceanRecord_Updated(t *testing.T) {
var record godo.DomainRecord
domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
@ -44,14 +47,14 @@ func TestAccDigitalOceanRecord_Updated(t *testing.T) {
CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckDigitalOceanRecordConfig_basic,
Config: fmt.Sprintf(testAccCheckDigitalOceanRecordConfig_basic, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
testAccCheckDigitalOceanRecordAttributes(&record),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "name", "terraform"),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "domain", "foobar-test-terraform.com"),
"digitalocean_record.foobar", "domain", domain),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "value", "192.168.0.10"),
resource.TestCheckResourceAttr(
@ -59,14 +62,15 @@ func TestAccDigitalOceanRecord_Updated(t *testing.T) {
),
},
resource.TestStep{
Config: testAccCheckDigitalOceanRecordConfig_new_value,
Config: fmt.Sprintf(
testAccCheckDigitalOceanRecordConfig_new_value, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
testAccCheckDigitalOceanRecordAttributesUpdated(&record),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "name", "terraform"),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "domain", "foobar-test-terraform.com"),
"digitalocean_record.foobar", "domain", domain),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "value", "192.168.0.11"),
resource.TestCheckResourceAttr(
@ -79,6 +83,7 @@ func TestAccDigitalOceanRecord_Updated(t *testing.T) {
func TestAccDigitalOceanRecord_HostnameValue(t *testing.T) {
var record godo.DomainRecord
domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
@ -86,14 +91,15 @@ func TestAccDigitalOceanRecord_HostnameValue(t *testing.T) {
CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckDigitalOceanRecordConfig_cname,
Config: fmt.Sprintf(
testAccCheckDigitalOceanRecordConfig_cname, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
testAccCheckDigitalOceanRecordAttributesHostname("a", &record),
testAccCheckDigitalOceanRecordAttributesHostname("a.foobar-test-terraform.com", &record),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "name", "terraform"),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "domain", "foobar-test-terraform.com"),
"digitalocean_record.foobar", "domain", domain),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "value", "a.foobar-test-terraform.com."),
resource.TestCheckResourceAttr(
@ -106,6 +112,7 @@ func TestAccDigitalOceanRecord_HostnameValue(t *testing.T) {
func TestAccDigitalOceanRecord_ExternalHostnameValue(t *testing.T) {
var record godo.DomainRecord
domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
@ -113,14 +120,15 @@ func TestAccDigitalOceanRecord_ExternalHostnameValue(t *testing.T) {
CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckDigitalOceanRecordConfig_external_cname,
Config: fmt.Sprintf(
testAccCheckDigitalOceanRecordConfig_external_cname, domain),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
testAccCheckDigitalOceanRecordAttributesHostname("a.foobar-test-terraform.net", &record),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "name", "terraform"),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "domain", "foobar-test-terraform.com"),
"digitalocean_record.foobar", "domain", domain),
resource.TestCheckResourceAttr(
"digitalocean_record.foobar", "value", "a.foobar-test-terraform.net."),
resource.TestCheckResourceAttr(
@ -225,7 +233,7 @@ func testAccCheckDigitalOceanRecordAttributesHostname(data string, record *godo.
const testAccCheckDigitalOceanRecordConfig_basic = `
resource "digitalocean_domain" "foobar" {
name = "foobar-test-terraform.com"
name = "%s"
ip_address = "192.168.0.10"
}
@ -239,7 +247,7 @@ resource "digitalocean_record" "foobar" {
const testAccCheckDigitalOceanRecordConfig_new_value = `
resource "digitalocean_domain" "foobar" {
name = "foobar-test-terraform.com"
name = "%s"
ip_address = "192.168.0.10"
}
@ -253,7 +261,7 @@ resource "digitalocean_record" "foobar" {
const testAccCheckDigitalOceanRecordConfig_cname = `
resource "digitalocean_domain" "foobar" {
name = "foobar-test-terraform.com"
name = "%s"
ip_address = "192.168.0.10"
}
@ -265,23 +273,9 @@ resource "digitalocean_record" "foobar" {
type = "CNAME"
}`
const testAccCheckDigitalOceanRecordConfig_relative_cname = `
resource "digitalocean_domain" "foobar" {
name = "foobar-test-terraform.com"
ip_address = "192.168.0.10"
}
resource "digitalocean_record" "foobar" {
domain = "${digitalocean_domain.foobar.name}"
name = "terraform"
value = "a.b"
type = "CNAME"
}`
const testAccCheckDigitalOceanRecordConfig_external_cname = `
resource "digitalocean_domain" "foobar" {
name = "foobar-test-terraform.com"
name = "%s"
ip_address = "192.168.0.10"
}

View File

@ -0,0 +1,2 @@
// Package acctest contains for Terraform Acceptance Tests
package acctest

35
helper/acctest/random.go Normal file
View File

@ -0,0 +1,35 @@
package acctest
import (
"math/rand"
"time"
)
// Helpers for generating random tidbits for use in identifiers to prevent
// collisions in acceptance tests.
// RandString generates a random alphanumeric string of the length specified
func RandString(strlen int) string {
return RandStringFromCharSet(strlen, CharSetAlphaNum)
}
// RandStringFromCharSet generates a random string by selecting characters from
// the charset provided
func RandStringFromCharSet(strlen int, charSet string) string {
rand.Seed(time.Now().UTC().UnixNano())
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = charSet[rand.Intn(len(charSet))]
}
return string(result)
}
const (
// CharSetAlphaNum is the alphanumeric character set for use with
// RandStringFromCharSet
CharSetAlphaNum = "abcdefghijklmnopqrstuvwxyz012346789"
// CharSetAlpha is the alphabetical character set for use with
// RandStringFromCharSet
CharSetAlpha = "abcdefghijklmnopqrstuvwxyz"
)