Skip to content

Commit

Permalink
refactor: use mux provider for sdk acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-github committed Jul 3, 2024
1 parent 1434f40 commit f532785
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 67 deletions.
63 changes: 48 additions & 15 deletions cloudsigma/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
package cloudsigma

import (
"context"
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"

var testAccProvider *schema.Provider
var testAccProviders map[string]*schema.Provider
var testAccProviderFactories map[string]func() (*schema.Provider, error)
"github.com/cloudsigma/cloudsigma-sdk-go/cloudsigma"
"github.com/cloudsigma/terraform-provider-cloudsigma/internal/provider"
)

func init() {
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
"cloudsigma": testAccProvider,
}
testAccProviderFactories = map[string]func() (*schema.Provider, error){
"cloudsigma": func() (*schema.Provider, error) {
return testAccProvider, nil
},
}
var testAccProto6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"cloudsigma": func() (tfprotov6.ProviderServer, error) {
ctx := context.Background()
upgradedSDKProvider, err := tf5to6server.UpgradeServer(ctx, Provider().GRPCProvider)
if err != nil {
return nil, err
}
providers := []func() tfprotov6.ProviderServer{
func() tfprotov6.ProviderServer { return upgradedSDKProvider },
providerserver.NewProtocol6(provider.New("testacc")()),
}
muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...)
if err != nil {
return nil, err
}
return muxServer.ProviderServer(), nil
},
}

func TestProvider(t *testing.T) {
Expand All @@ -42,3 +53,25 @@ func testAccPreCheck(t *testing.T) {
t.Fatal("CLOUDSIGMA_PASSWORD must be set for acceptance tests")
}
}

func sharedClient() (*cloudsigma.Client, error) {
location := os.Getenv("CLOUDSIGMA_LOCATION")
if location == "" {
return nil, fmt.Errorf("empty CLOUDSIGMA_LOCATION")
}

username := os.Getenv("CLOUDSIGMA_USERNAME")
if username == "" {
return nil, fmt.Errorf("CLOUDSIGMA_USERNAME must be set for acceptance tests")
}

password := os.Getenv("CLOUDSIGMA_PASSWORD")
if password == "" {
return nil, fmt.Errorf("CLOUDSIGMA_PASSWORD must be set for acceptance tests")
}

opts := []cloudsigma.ClientOption{cloudsigma.WithUserAgent("terraform-provider-cloudsigma/sweeper")}
opts = append(opts, cloudsigma.WithLocation(location))
creds := cloudsigma.NewUsernamePasswordCredentialsProvider(username, password)
return cloudsigma.NewClient(creds, opts...), nil
}
47 changes: 27 additions & 20 deletions cloudsigma/resource_cloudsigma_drive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"

"github.com/cloudsigma/cloudsigma-sdk-go/cloudsigma"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccCloudSigmaDrive_basic(t *testing.T) {
Expand All @@ -18,9 +19,9 @@ func TestAccCloudSigmaDrive_basic(t *testing.T) {
tagName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaDriveConfig_basic(driveName),
Expand Down Expand Up @@ -53,9 +54,9 @@ func TestAccCloudSigmaDrive_basic(t *testing.T) {

func TestAccCloudSigmaDrive_emptyTag(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaDriveConfig_emptyTag(),
Expand All @@ -70,13 +71,13 @@ func TestAccCloudSigmaDrive_changeSize(t *testing.T) {
driveName := fmt.Sprintf("tf-acc-test--%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaDriveConfig_basic(driveName),
Check: resource.ComposeAggregateTestCheckFunc(
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudSigmaDriveExists("cloudsigma_drive.test", &drive),
resource.TestCheckResourceAttr("cloudsigma_drive.test", "media", "disk"),
resource.TestCheckResourceAttr("cloudsigma_drive.test", "name", driveName),
Expand All @@ -87,7 +88,7 @@ func TestAccCloudSigmaDrive_changeSize(t *testing.T) {
},
{
Config: testAccCloudSigmaDriveConfig_changeSize(driveName),
Check: resource.ComposeAggregateTestCheckFunc(
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudSigmaDriveExists("cloudsigma_drive.test", &drive),
resource.TestCheckResourceAttr("cloudsigma_drive.test", "size", "16106127360"),
),
Expand All @@ -101,13 +102,13 @@ func TestAccCloudSigmaDrive_changeStorageType(t *testing.T) {
driveName := fmt.Sprintf("tf-acc-test--%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaDriveDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaDriveConfig_storageType(driveName),
Check: resource.ComposeAggregateTestCheckFunc(
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudSigmaDriveExists("cloudsigma_drive.test", &drive),
resource.TestCheckResourceAttr("cloudsigma_drive.test", "storage_type", "dssd"),
),
Expand All @@ -121,7 +122,10 @@ func TestAccCloudSigmaDrive_changeStorageType(t *testing.T) {
}

func testAccCheckCloudSigmaDriveDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*cloudsigma.Client)
client, err := sharedClient()
if err != nil {
return err
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudsigma_drive" {
Expand All @@ -148,7 +152,10 @@ func testAccCheckCloudSigmaDriveExists(n string, drive *cloudsigma.Drive) resour
return fmt.Errorf("no drive ID is set")
}

client := testAccProvider.Meta().(*cloudsigma.Client)
client, err := sharedClient()
if err != nil {
return err
}
retrievedDrive, _, err := client.Drives.Get(context.Background(), rs.Primary.ID)
if err != nil {
return err
Expand Down
64 changes: 36 additions & 28 deletions cloudsigma/resource_cloudsigma_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"

"github.com/cloudsigma/cloudsigma-sdk-go/cloudsigma"
)
Expand All @@ -20,9 +20,9 @@ func TestAccCloudSigmaServer_basic(t *testing.T) {
tagName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_basic(serverName),
Expand All @@ -35,14 +35,16 @@ func TestAccCloudSigmaServer_basic(t *testing.T) {
),
},
{
Config: testAccCloudSigmaServerConfig_addTag(tagName, serverName),
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
Config: testAccCloudSigmaServerConfig_addTag(tagName, serverName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCloudSigmaServerExists("cloudsigma_server.test", &server),
resource.TestCheckResourceAttr("cloudsigma_server.test", "tags.#", "1"),
),
},
{
Config: testAccCloudSigmaServerConfig_noTag(serverName),
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
Config: testAccCloudSigmaServerConfig_noTag(serverName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCloudSigmaServerExists("cloudsigma_server.test", &server),
resource.TestCheckResourceAttr("cloudsigma_server.test", "tags.#", "0"),
Expand All @@ -54,9 +56,9 @@ func TestAccCloudSigmaServer_basic(t *testing.T) {

func TestAccCloudSigmaServer_emptySSH(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_emptySSHKey(),
Expand All @@ -68,9 +70,9 @@ func TestAccCloudSigmaServer_emptySSH(t *testing.T) {

func TestAccCloudSigmaServer_emptyTag(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_emptyTag(),
Expand All @@ -85,9 +87,9 @@ func TestAccCloudSigmaServer_smp(t *testing.T) {
serverName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_basic(serverName),
Expand All @@ -112,9 +114,9 @@ func TestAccCloudSigmaServer_smp(t *testing.T) {

func TestAccCloudSigmaServer_invalidSMP(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_invalidSMP(),
Expand All @@ -131,9 +133,9 @@ func TestAccCloudSigmaServer_withDrive(t *testing.T) {
driveName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_withDrive(serverName, driveName),
Expand Down Expand Up @@ -167,9 +169,9 @@ func TestAccCloudSigmaServer_withMeta(t *testing.T) {
serverName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
CheckDestroy: testAccCheckCloudSigmaServerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSigmaServerConfig_withMeta(serverName),
Expand All @@ -192,7 +194,10 @@ func TestAccCloudSigmaServer_withMeta(t *testing.T) {
}

func testAccCheckCloudSigmaServerDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*cloudsigma.Client)
client, err := sharedClient()
if err != nil {
return err
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "cloudsigma_server" {
Expand All @@ -219,7 +224,10 @@ func testAccCheckCloudSigmaServerExists(n string, server *cloudsigma.Server) res
return fmt.Errorf("no server ID is set")
}

client := testAccProvider.Meta().(*cloudsigma.Client)
client, err := sharedClient()
if err != nil {
return err
}
retrievedServer, _, err := client.Servers.Get(context.Background(), rs.Primary.ID)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cloudsigma/resource_cloudsigma_ssh_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccResourceCloudSigmaSSHKey_Basic(t *testing.T) {
Expand All @@ -14,8 +14,8 @@ func TestAccResourceCloudSigmaSSHKey_Basic(t *testing.T) {
config := fmt.Sprintf(testAccResourceCloudSigmaSSHKeyConfig, sshKeyName, sshKeyPublic)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProto6ProviderFactories,
Steps: []resource.TestStep{
{
Config: config,
Expand Down

0 comments on commit f532785

Please sign in to comment.