terraform-provider-greenhost/digitalocean/resource_digitalocean_cdn_t...

178 lines
4.7 KiB
Go
Raw Normal View History

package digitalocean
import (
"context"
"fmt"
"testing"
Upgrade to terraform-plugin-sdk (#321) * Upgrade to terraform-plugin-sdk After following the [guide](https://www.terraform.io/docs/extend/plugin-sdk.html#using-tf-sdk-migrator) Check ``` ▶ GO111MODULE=on tf-sdk-migrator check Checking Go runtime version ... Go version 1.12.10: OK. Checking whether provider uses Go modules... Go modules in use: OK. Checking version of github.com/hashicorp/terraform-plugin-sdk to determine if provider was already migrated... Checking version of github.com/hashicorp/terraform used in provider... Terraform version 0.12.8: OK. Checking whether provider uses deprecated SDK packages or identifiers... No imports of deprecated SDK packages or identifiers: OK. All constraints satisfied. Provider can be migrated to the new SDK. ``` Migrate ``` ▶ GO111MODULE=on tf-sdk-migrator migrate Checking Go runtime version ... Go version 1.12.10: OK. Checking whether provider uses Go modules... Go modules in use: OK. Checking version of github.com/hashicorp/terraform-plugin-sdk to determine if provider was already migrated... Checking version of github.com/hashicorp/terraform used in provider... Terraform version 0.12.8: OK. Checking whether provider uses deprecated SDK packages or identifiers... No imports of deprecated SDK packages or identifiers: OK. All constraints satisfied. Provider can be migrated to the new SDK. Rewriting provider go.mod file... Rewriting SDK package imports... Running `go mod tidy`... Success! Provider is migrated to github.com/hashicorp/terraform-plugin-sdk v1.1.0. It looks like this provider vendors dependencies. Don't forget to run `go mod vendor`. Make sure to review all changes and run all tests. ``` * Fix build under go 1.13.x.
2019-10-22 21:44:03 +00:00
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)
const originSuffix = ".ams3.digitaloceanspaces.com"
func TestAccDigitalOceanCDN_Create(t *testing.T) {
bucketName := generateBucketName()
cdnCreateConfig := fmt.Sprintf(testAccCheckDigitalOceanCDNConfig_Create, bucketName)
expectedOrigin := bucketName + originSuffix
expectedTTL := "3600"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDigitalOceanCDNDestroy,
Steps: []resource.TestStep{
{
Config: cdnCreateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanCDNExists("digitalocean_cdn.foobar"),
resource.TestCheckResourceAttr(
"digitalocean_cdn.foobar", "origin", expectedOrigin),
resource.TestCheckResourceAttr("digitalocean_cdn.foobar", "ttl", expectedTTL),
),
},
},
})
}
func TestAccDigitalOceanCDN_Create_with_TTL(t *testing.T) {
bucketName := generateBucketName()
ttl := 1800
cdnCreateConfig := fmt.Sprintf(testAccCheckDigitalOceanCDNConfig_Create_with_TTL, bucketName, ttl)
expectedOrigin := bucketName + originSuffix
expectedTTL := "1800"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDigitalOceanCDNDestroy,
Steps: []resource.TestStep{
{
Config: cdnCreateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanCDNExists("digitalocean_cdn.foobar"),
resource.TestCheckResourceAttr(
"digitalocean_cdn.foobar", "origin", expectedOrigin),
resource.TestCheckResourceAttr("digitalocean_cdn.foobar", "ttl", expectedTTL),
),
},
},
})
}
func TestAccDigitalOceanCDN_Create_and_Update(t *testing.T) {
bucketName := generateBucketName()
ttl := 1800
cdnCreateConfig := fmt.Sprintf(testAccCheckDigitalOceanCDNConfig_Create, bucketName)
cdnUpdateConfig := fmt.Sprintf(testAccCheckDigitalOceanCDNConfig_Create_with_TTL, bucketName, ttl)
expectedOrigin := bucketName + originSuffix
expectedTTL := "1800"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDigitalOceanCDNDestroy,
Steps: []resource.TestStep{
{
Config: cdnCreateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanCDNExists("digitalocean_cdn.foobar"),
resource.TestCheckResourceAttr(
"digitalocean_cdn.foobar", "origin", expectedOrigin),
resource.TestCheckResourceAttr("digitalocean_cdn.foobar", "ttl", "3600"),
),
},
{
Config: cdnUpdateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanCDNExists("digitalocean_cdn.foobar"),
resource.TestCheckResourceAttr(
"digitalocean_cdn.foobar", "origin", expectedOrigin),
resource.TestCheckResourceAttr("digitalocean_cdn.foobar", "ttl", expectedTTL),
),
},
},
})
}
func testAccCheckDigitalOceanCDNDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*CombinedConfig).godoClient()
for _, rs := range s.RootModule().Resources {
if rs.Type != "digitalocean_cdn" {
continue
}
_, _, err := client.CDNs.Get(context.Background(), rs.Primary.ID)
if err == nil {
return fmt.Errorf("CDN resource still exists")
}
}
return nil
}
func testAccCheckDigitalOceanCDNExists(resource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*CombinedConfig).godoClient()
rs, ok := s.RootModule().Resources[resource]
if !ok {
return fmt.Errorf("Not found: %s", resource)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID set for resource: %s", resource)
}
foundCDN, _, err := client.CDNs.Get(context.Background(), rs.Primary.ID)
if err != nil {
return err
}
if foundCDN.ID != rs.Primary.ID {
return fmt.Errorf("Resource not found: %s : %s", resource, rs.Primary.ID)
}
return nil
}
}
func generateBucketName() string {
return fmt.Sprintf("tf-cdn-test-bucket-%d", acctest.RandInt())
}
const testAccCheckDigitalOceanCDNConfig_Create = `
resource "digitalocean_spaces_bucket" "bucket" {
name = "%s"
region = "ams3"
acl = "public-read"
}
resource "digitalocean_cdn" "foobar" {
origin = "${digitalocean_spaces_bucket.bucket.bucket_domain_name}"
}`
const testAccCheckDigitalOceanCDNConfig_Create_with_TTL = `
resource "digitalocean_spaces_bucket" "bucket" {
name = "%s"
region = "ams3"
acl = "public-read"
}
resource "digitalocean_cdn" "foobar" {
origin = "${digitalocean_spaces_bucket.bucket.bucket_domain_name}"
ttl = %d
}`