Skip to content

Commit

Permalink
Add sweeper for custom images and fix acceptance tests. (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsomething committed Feb 10, 2023
1 parent 5ebbccc commit 41a5692
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
1 change: 1 addition & 0 deletions digitalocean/image/resource_custom_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,5 +330,6 @@ func validImageDistributions() []string {
"RancherOS",
"Ubuntu",
"Unknown",
"Unknown OS",
}
}
31 changes: 18 additions & 13 deletions digitalocean/image/resource_custom_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package image_test
import (
"context"
"fmt"
"net/http"
"strconv"
"testing"

Expand All @@ -25,11 +26,11 @@ func TestAccDigitalOceanCustomImageFull(t *testing.T) {
CheckDestroy: testAccCheckDigitalOceanCustomImageDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckDigitalOceanCustomImageConfig(rString, rString, regions, "Unknown"),
Config: testAccCheckDigitalOceanCustomImageConfig(rString, rString, regions, "Unknown OS"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "name", fmt.Sprintf("%s-name", rString)),
resource.TestCheckResourceAttr(name, "name", rString),
resource.TestCheckResourceAttr(name, "description", fmt.Sprintf("%s-description", rString)),
resource.TestCheckResourceAttr(name, "distribution", "Unknown"),
resource.TestCheckResourceAttr(name, "distribution", "Unknown OS"),
resource.TestCheckResourceAttr(name, "public", "false"),
resource.TestCheckResourceAttr(name, "regions.0", "nyc3"),
resource.TestCheckResourceAttr(name, "status", "available"),
Expand All @@ -45,7 +46,7 @@ func TestAccDigitalOceanCustomImageFull(t *testing.T) {
{
Config: testAccCheckDigitalOceanCustomImageConfig(rString, updatedString, regions, "CoreOS"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "name", fmt.Sprintf("%s-name", updatedString)),
resource.TestCheckResourceAttr(name, "name", updatedString),
resource.TestCheckResourceAttr(name, "description", fmt.Sprintf("%s-description", updatedString)),
resource.TestCheckResourceAttr(name, "distribution", "CoreOS"),
),
Expand All @@ -66,26 +67,26 @@ func TestAccDigitalOceanCustomImageMultiRegion(t *testing.T) {
CheckDestroy: testAccCheckDigitalOceanCustomImageDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckDigitalOceanCustomImageConfig(rString, rString, regions, "Unknown"),
Config: testAccCheckDigitalOceanCustomImageConfig(rString, rString, regions, "Unknown OS"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "name", fmt.Sprintf("%s-name", rString)),
resource.TestCheckResourceAttr(name, "name", rString),
resource.TestCheckTypeSetElemAttr(name, "regions.*", "nyc2"),
resource.TestCheckTypeSetElemAttr(name, "regions.*", "nyc3"),
),
},
{
Config: testAccCheckDigitalOceanCustomImageConfig(rString, rString, regionsUpdated, "Unknown"),
Config: testAccCheckDigitalOceanCustomImageConfig(rString, rString, regionsUpdated, "Unknown OS"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "name", fmt.Sprintf("%s-name", rString)),
resource.TestCheckResourceAttr(name, "name", rString),
resource.TestCheckTypeSetElemAttr(name, "regions.*", "nyc2"),
resource.TestCheckTypeSetElemAttr(name, "regions.*", "nyc3"),
resource.TestCheckTypeSetElemAttr(name, "regions.*", "tor1"),
),
},
{
Config: testAccCheckDigitalOceanCustomImageConfig(rString, rString, regions, "Unknown"),
Config: testAccCheckDigitalOceanCustomImageConfig(rString, rString, regions, "Unknown OS"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "name", fmt.Sprintf("%s-name", rString)),
resource.TestCheckResourceAttr(name, "name", rString),
resource.TestCheckTypeSetElemAttr(name, "regions.*", "nyc2"),
resource.TestCheckTypeSetElemAttr(name, "regions.*", "nyc3"),
),
Expand All @@ -97,7 +98,7 @@ func TestAccDigitalOceanCustomImageMultiRegion(t *testing.T) {
func testAccCheckDigitalOceanCustomImageConfig(rName string, name string, regions string, distro string) string {
return fmt.Sprintf(`
resource "digitalocean_custom_image" "%s" {
name = "%s-name"
name = "%s"
url = "https://stable.release.flatcar-linux.net/amd64-usr/2605.7.0/flatcar_production_digitalocean_image.bin.bz2"
regions = %s
description = "%s-description"
Expand All @@ -123,12 +124,16 @@ func testAccCheckDigitalOceanCustomImageDestroy(s *terraform.State) error {
}

// Try to find the Image by ID
resp, _, err := client.Images.GetByID(context.Background(), id)
i, resp, err := client.Images.GetByID(context.Background(), id)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
return nil
}

return err
}

if resp.Status != image.ImageDeletedStatus {
if i.Status != image.ImageDeletedStatus {
return fmt.Errorf("Image %d not destroyed", id)
}
}
Expand Down
47 changes: 47 additions & 0 deletions digitalocean/image/sweep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package image

import (
"context"
"log"
"strings"

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

func init() {
resource.AddTestSweepers("digitalocean_custom_image", &resource.Sweeper{
Name: "digitalocean_custom_image",
F: sweepCustomImage,
})

}

func sweepCustomImage(region string) error {
meta, err := sweep.SharedConfigForRegion(region)
if err != nil {
return err
}

client := meta.(*config.CombinedConfig).GodoClient()

opt := &godo.ListOptions{PerPage: 200}
images, _, err := client.Images.ListUser(context.Background(), opt)
if err != nil {
return err
}

for _, i := range images {
if strings.HasPrefix(i.Name, sweep.TestNamePrefix) {
log.Printf("Destroying image %s", i.Name)

if _, err := client.Images.Delete(context.Background(), i.ID); err != nil {
return err
}
}
}

return nil
}
1 change: 1 addition & 0 deletions digitalocean/sweep/sweep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/domain"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/droplet"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/firewall"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/image"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/kubernetes"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/loadbalancer"
_ "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/reservedip"
Expand Down

0 comments on commit 41a5692

Please sign in to comment.