-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial reorganization. * Update Makefile for running accepance tests under new organization. * Update sweepers for new organization. * Remove unused functions from util package. * godo.PtrTo replaces the boolPtr and intPtr helpers. * Clean up util package. * Reorg helpers in acceptance package.
- Loading branch information
1 parent
96ba57d
commit 95da01c
Showing
231 changed files
with
3,147 additions
and
2,832 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package acceptance | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
const TestNamePrefix = "tf-acc-test-" | ||
|
||
var ( | ||
TestAccProvider *schema.Provider | ||
TestAccProviders map[string]*schema.Provider | ||
TestAccProviderFactories map[string]func() (*schema.Provider, error) | ||
) | ||
|
||
func init() { | ||
TestAccProvider = digitalocean.Provider() | ||
TestAccProviders = map[string]*schema.Provider{ | ||
"digitalocean": TestAccProvider, | ||
} | ||
TestAccProviderFactories = map[string]func() (*schema.Provider, error){ | ||
"digitalocean": func() (*schema.Provider, error) { | ||
return TestAccProvider, nil | ||
}, | ||
} | ||
} | ||
|
||
func TestAccPreCheck(t *testing.T) { | ||
if v := os.Getenv("DIGITALOCEAN_TOKEN"); v == "" { | ||
t.Fatal("DIGITALOCEAN_TOKEN must be set for acceptance tests") | ||
} | ||
|
||
err := TestAccProvider.Configure(context.Background(), terraform.NewResourceConfigRaw(nil)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func RandomTestName(additionalNames ...string) string { | ||
prefix := TestNamePrefix | ||
for _, n := range additionalNames { | ||
prefix += "-" + strings.Replace(n, " ", "_", -1) | ||
} | ||
return randomName(prefix, 10) | ||
} | ||
|
||
func randomName(prefix string, length int) string { | ||
return fmt.Sprintf("%s%s", prefix, acctest.RandString(length)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package acceptance | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"fmt" | ||
"math/big" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"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 { | ||
t.Fatalf("Cannot generate test TLS certificate: %s", err) | ||
} | ||
rootCertMaterial, _, err := randTLSCert("Acme Go", "example.com") | ||
if err != nil { | ||
t.Fatalf("Cannot generate test TLS certificate: %s", err) | ||
} | ||
certChainMaterial := fmt.Sprintf("%s\n%s", strings.TrimSpace(rootCertMaterial), leafCertMaterial) | ||
|
||
return privateKeyMaterial, leafCertMaterial, certChainMaterial | ||
} | ||
|
||
// Based on Terraform's acctest.RandTLSCert, but allows for passing DNS name. | ||
func randTLSCert(orgName string, dnsName string) (string, string, error) { | ||
template := &x509.Certificate{ | ||
SerialNumber: big.NewInt(int64(acctest.RandInt())), | ||
Subject: pkix.Name{ | ||
Organization: []string{orgName}, | ||
}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().Add(24 * time.Hour), | ||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
BasicConstraintsValid: true, | ||
DNSNames: []string{dnsName}, | ||
} | ||
|
||
privateKey, privateKeyPEM, err := genPrivateKey() | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
cert, err := x509.CreateCertificate(rand.Reader, template, template, &privateKey.PublicKey, privateKey) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
certPEM, err := pemEncode(cert, "CERTIFICATE") | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
return certPEM, privateKeyPEM, nil | ||
} | ||
|
||
func genPrivateKey() (*rsa.PrivateKey, string, error) { | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
privateKeyPEM, err := pemEncode(x509.MarshalPKCS1PrivateKey(privateKey), "RSA PRIVATE KEY") | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
return privateKey, privateKeyPEM, nil | ||
} | ||
|
||
func pemEncode(b []byte, block string) (string, error) { | ||
var buf bytes.Buffer | ||
pb := &pem.Block{Type: block, Bytes: b} | ||
if err := pem.Encode(&buf, pb); err != nil { | ||
return "", err | ||
} | ||
|
||
return buf.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.