Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asb/reorg #1

Merged
merged 7 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Reorg helpers in acceptance package.
  • Loading branch information
andrewsomething committed Feb 3, 2023
commit 11884d1578b3463173b677c5f71c899da6aeca2e
1 change: 1 addition & 0 deletions digitalocean/acceptance/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
)

// GenerateTestCertMaterial generates a TLS certificate for use in acceptance test fixtures.
func GenerateTestCertMaterial(t *testing.T) (string, string, string) {
leafCertMaterial, privateKeyMaterial, err := randTLSCert("Acme Co", "example.com")
if err != nil {
Expand Down
135 changes: 135 additions & 0 deletions digitalocean/acceptance/droplets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package acceptance

import (
"context"
"fmt"
"log"
"strconv"
"strings"

"github.com/digitalocean/godo"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/util"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccCheckDigitalOceanDropletDestroy(s *terraform.State) error {
client := TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()

for _, rs := range s.RootModule().Resources {
if rs.Type != "digitalocean_droplet" {
continue
}

id, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return err
}

// Try to find the Droplet
_, _, err = client.Droplets.Get(context.Background(), id)

// Wait

if err != nil && !strings.Contains(err.Error(), "404") {
return fmt.Errorf(
"Error waiting for droplet (%s) to be destroyed: %s",
rs.Primary.ID, err)
}
}

return nil
}

func TestAccCheckDigitalOceanDropletExists(n string, droplet *godo.Droplet) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No Droplet ID is set")
}

client := TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()

id, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return err
}

// Try to find the Droplet
retrieveDroplet, _, err := client.Droplets.Get(context.Background(), id)

if err != nil {
return err
}

if strconv.Itoa(retrieveDroplet.ID) != rs.Primary.ID {
return fmt.Errorf("Droplet not found")
}

*droplet = *retrieveDroplet

return nil
}
}

func TestAccCheckDigitalOceanDropletConfig_basic(rInt int) string {
return fmt.Sprintf(`
resource "digitalocean_droplet" "foobar" {
name = "foo-%d"
size = "s-1vcpu-1gb"
image = "ubuntu-22-04-x64"
region = "nyc3"
user_data = "foobar"
}`, rInt)
}

func TakeSnapshotsOfDroplet(rInt int, droplet *godo.Droplet, snapshotsId *[]int) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()
for i := 0; i < 3; i++ {
err := takeSnapshotOfDroplet(rInt, i%2, droplet)
if err != nil {
return err
}
}
retrieveDroplet, _, err := client.Droplets.Get(context.Background(), (*droplet).ID)
if err != nil {
return err
}
*snapshotsId = retrieveDroplet.SnapshotIDs
return nil
}
}

func takeSnapshotOfDroplet(rInt, sInt int, droplet *godo.Droplet) error {
client := TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()
action, _, err := client.DropletActions.Snapshot(context.Background(), (*droplet).ID, fmt.Sprintf("snap-%d-%d", rInt, sInt))
if err != nil {
return err
}
util.WaitForAction(client, action)
return nil
}

func DeleteDropletSnapshots(snapshotsId *[]int) resource.TestCheckFunc {
return func(s *terraform.State) error {
log.Printf("Deleting Droplet snapshots")

client := TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()

snapshots := *snapshotsId
for _, value := range snapshots {
log.Printf("Deleting %d", value)
_, err := client.Images.Delete(context.Background(), value)
if err != nil {
return err
}
}
return nil
}
}
129 changes: 2 additions & 127 deletions digitalocean/acceptance/test_utils.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package acceptance

import (
"context"
"fmt"
"log"
"strconv"
"strings"

"github.com/digitalocean/godo"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/util"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

// TestResourceInstanceState is a resource.TestCheckFunc to test if a resource is
// present in the instance state.
func TestResourceInstanceState(name string, check func(*terraform.InstanceState) error) resource.TestCheckFunc {
return func(s *terraform.State) error {
m := s.RootModule()
Expand All @@ -30,123 +25,3 @@ func TestResourceInstanceState(name string, check func(*terraform.InstanceState)

}
}

func TestAccCheckDigitalOceanDropletDestroy(s *terraform.State) error {
client := TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()

for _, rs := range s.RootModule().Resources {
if rs.Type != "digitalocean_droplet" {
continue
}

id, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return err
}

// Try to find the Droplet
_, _, err = client.Droplets.Get(context.Background(), id)

// Wait

if err != nil && !strings.Contains(err.Error(), "404") {
return fmt.Errorf(
"Error waiting for droplet (%s) to be destroyed: %s",
rs.Primary.ID, err)
}
}

return nil
}

func TestAccCheckDigitalOceanDropletExists(n string, droplet *godo.Droplet) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No Droplet ID is set")
}

client := TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()

id, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return err
}

// Try to find the Droplet
retrieveDroplet, _, err := client.Droplets.Get(context.Background(), id)

if err != nil {
return err
}

if strconv.Itoa(retrieveDroplet.ID) != rs.Primary.ID {
return fmt.Errorf("Droplet not found")
}

*droplet = *retrieveDroplet

return nil
}
}

func TestAccCheckDigitalOceanDropletConfig_basic(rInt int) string {
return fmt.Sprintf(`
resource "digitalocean_droplet" "foobar" {
name = "foo-%d"
size = "s-1vcpu-1gb"
image = "ubuntu-22-04-x64"
region = "nyc3"
user_data = "foobar"
}`, rInt)
}

func TakeSnapshotsOfDroplet(rInt int, droplet *godo.Droplet, snapshotsId *[]int) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()
for i := 0; i < 3; i++ {
err := takeSnapshotOfDroplet(rInt, i%2, droplet)
if err != nil {
return err
}
}
retrieveDroplet, _, err := client.Droplets.Get(context.Background(), (*droplet).ID)
if err != nil {
return err
}
*snapshotsId = retrieveDroplet.SnapshotIDs
return nil
}
}

func takeSnapshotOfDroplet(rInt, sInt int, droplet *godo.Droplet) error {
client := TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()
action, _, err := client.DropletActions.Snapshot(context.Background(), (*droplet).ID, fmt.Sprintf("snap-%d-%d", rInt, sInt))
if err != nil {
return err
}
util.WaitForAction(client, action)
return nil
}

func DeleteDropletSnapshots(snapshotsId *[]int) resource.TestCheckFunc {
return func(s *terraform.State) error {
log.Printf("Deleting Droplet snapshots")

client := TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()

snapshots := *snapshotsId
for _, value := range snapshots {
log.Printf("Deleting %d", value)
_, err := client.Images.Delete(context.Background(), value)
if err != nil {
return err
}
}
return nil
}
}