Fix and extend acceptance test sweeper. (#458)
* Fix sweeper. * Sweep Kubernetes clusters. * Sweep database clusters. * Set defaults as the sweeper does not have access to the provider DefaultFunc * Add warning to makefile target.
This commit is contained in:
parent
c932c35aa7
commit
4c6b74bb0d
|
@ -1,7 +1,6 @@
|
|||
TEST?=$$(go list ./... |grep -v 'vendor')
|
||||
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
|
||||
PKG_NAME=digitalocean
|
||||
SLUG=do
|
||||
|
||||
default: build
|
||||
|
||||
|
@ -25,6 +24,10 @@ vet:
|
|||
exit 1; \
|
||||
fi
|
||||
|
||||
sweep:
|
||||
@echo "WARNING: This will destroy infrastructure. Use only in development accounts."
|
||||
go test $(TEST) -v -sweep=1
|
||||
|
||||
fmt:
|
||||
gofmt -w $(GOFMT_FILES)
|
||||
|
||||
|
@ -34,7 +37,6 @@ fmtcheck:
|
|||
errcheck:
|
||||
@sh -c "'$(CURDIR)/scripts/errcheck.sh'"
|
||||
|
||||
|
||||
test-compile:
|
||||
@if [ "$(TEST)" = "./..." ]; then \
|
||||
echo "ERROR: Set TEST to a specific package. For example,"; \
|
||||
|
@ -46,5 +48,4 @@ test-compile:
|
|||
website:
|
||||
@echo "Use this site to preview markdown rendering: https://registry.terraform.io/tools/doc-preview"
|
||||
|
||||
.PHONY: build test testacc vet fmt fmtcheck errcheck test-compile website
|
||||
|
||||
.PHONY: build test testacc vet fmt fmtcheck errcheck test-compile website sweep
|
||||
|
|
|
@ -3,7 +3,9 @@ package digitalocean
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/digitalocean/godo"
|
||||
|
@ -11,6 +13,41 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-sdk/terraform"
|
||||
)
|
||||
|
||||
func init() {
|
||||
resource.AddTestSweepers("digitalocean_database_cluster", &resource.Sweeper{
|
||||
Name: "digitalocean_database_cluster",
|
||||
F: testSweepDatabaseCluster,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func testSweepDatabaseCluster(region string) error {
|
||||
meta, err := sharedConfigForRegion(region)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := meta.(*CombinedConfig).godoClient()
|
||||
|
||||
opt := &godo.ListOptions{PerPage: 200}
|
||||
databases, _, err := client.Databases.List(context.Background(), opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, db := range databases {
|
||||
if strings.HasPrefix(db.Name, testNamePrefix) {
|
||||
log.Printf("Destroying database cluster %s", db.Name)
|
||||
|
||||
if _, err := client.Databases.Delete(context.Background(), db.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestAccDigitalOceanDatabaseCluster_Basic(t *testing.T) {
|
||||
var database godo.Database
|
||||
databaseName := randomTestName()
|
||||
|
|
|
@ -4,7 +4,9 @@ import (
|
|||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -22,6 +24,41 @@ const (
|
|||
}`
|
||||
)
|
||||
|
||||
func init() {
|
||||
resource.AddTestSweepers("digitalocean_kubernetes_cluster", &resource.Sweeper{
|
||||
Name: "digitalocean_kubernetes_cluster",
|
||||
F: testSweepKubernetesClusters,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func testSweepKubernetesClusters(region string) error {
|
||||
meta, err := sharedConfigForRegion(region)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := meta.(*CombinedConfig).godoClient()
|
||||
|
||||
opt := &godo.ListOptions{PerPage: 200}
|
||||
clusters, _, err := client.Kubernetes.List(context.Background(), opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("[DEBUG] Found %d Kubernetes clusters to sweep", len(clusters))
|
||||
|
||||
for _, c := range clusters {
|
||||
if strings.HasPrefix(c.Name, testNamePrefix) {
|
||||
log.Printf("Destroying Kubernetes cluster %s", c.Name)
|
||||
if _, err := client.Kubernetes.Delete(context.Background(), c.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestAccDigitalOceanKubernetesCluster_Basic(t *testing.T) {
|
||||
t.Parallel()
|
||||
rName := randomTestName()
|
||||
|
|
|
@ -17,8 +17,20 @@ func sharedConfigForRegion(region string) (interface{}, error) {
|
|||
return nil, fmt.Errorf("empty DIGITALOCEAN_TOKEN")
|
||||
}
|
||||
|
||||
apiEndpoint := os.Getenv("DIGITALOCEAN_API_URL")
|
||||
if apiEndpoint == "" {
|
||||
apiEndpoint = "https://api.digitalocean.com"
|
||||
}
|
||||
|
||||
spacesEndpoint := os.Getenv("SPACES_ENDPOINT_URL")
|
||||
if spacesEndpoint == "" {
|
||||
spacesEndpoint = "https://{{.Region}}.digitaloceanspaces.com"
|
||||
}
|
||||
|
||||
config := Config{
|
||||
Token: os.Getenv("DIGITALOCEAN_TOKEN"),
|
||||
Token: os.Getenv("DIGITALOCEAN_TOKEN"),
|
||||
APIEndpoint: apiEndpoint,
|
||||
SpacesAPIEndpoint: spacesEndpoint,
|
||||
}
|
||||
|
||||
// configures a default client for the region, using the above env vars
|
||||
|
|
Loading…
Reference in New Issue