Skip to content

Commit

Permalink
Move plugin framework provider and test utility files to packages (#8226
Browse files Browse the repository at this point in the history
) (#15021)

* Move plugin framework provider and test utility files to the specific package

* Don't replace Nprintf

* Fix the rebasing error

* Fix tgc

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jun 29, 2023
1 parent 440b563 commit d143352
Show file tree
Hide file tree
Showing 26 changed files with 738 additions and 685 deletions.
3 changes: 3 additions & 0 deletions .changelog/8226.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
23 changes: 23 additions & 0 deletions google/acctest/framework_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
package acctest

import (
"context"
"fmt"
"log"
"reflect"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-framework/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand All @@ -15,6 +20,24 @@ import (
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
)

func GetFwTestProvider(t *testing.T) *frameworkTestProvider {
configsLock.RLock()
fwProvider, ok := fwProviders[t.Name()]
configsLock.RUnlock()
if ok {
return fwProvider
}

var diags diag.Diagnostics
p := NewFrameworkTestProvider(t.Name())
configureApiClient(context.Background(), &p.FrameworkProvider, &diags)
if diags.HasError() {
log.Fatalf("%d errors when configuring test provider client: first is %s", diags.ErrorsCount(), diags.Errors()[0].Detail())
}

return p
}

// General test utils

// TestExtractResourceAttr navigates a test's state to find the specified resource (or data source) attribute and makes the value
Expand Down
30 changes: 30 additions & 0 deletions google/acctest/provider_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package acctest

import (
"context"
"fmt"
"io/ioutil"
"os"
Expand All @@ -11,9 +12,11 @@ import (
"time"

"github.com/hashicorp/terraform-provider-google/google/envvar"
"github.com/hashicorp/terraform-provider-google/google/provider"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"

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

Expand Down Expand Up @@ -97,6 +100,33 @@ var MasterBillingAccountEnvVars = envvar.MasterBillingAccountEnvVars
// but all new code should use PapDescriptionEnvVars in the envvar package instead.
var PapDescriptionEnvVars = envvar.PapDescriptionEnvVars

var TestAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider

func init() {
configs = make(map[string]*transport_tpg.Config)
fwProviders = make(map[string]*frameworkTestProvider)
sources = make(map[string]VcrSource)
testAccProvider = provider.Provider()
TestAccProviders = map[string]*schema.Provider{
"google": testAccProvider,
}
}

func GoogleProviderConfig(t *testing.T) *transport_tpg.Config {
configsLock.RLock()
config, ok := configs[t.Name()]
configsLock.RUnlock()
if ok {
return config
}

sdkProvider := provider.Provider()
rc := terraform.ResourceConfig{}
sdkProvider.Configure(context.Background(), &rc)
return sdkProvider.Meta().(*transport_tpg.Config)
}

func AccTestPreCheck(t *testing.T) {
if v := os.Getenv("GOOGLE_CREDENTIALS_FILE"); v != "" {
creds, err := ioutil.ReadFile(v)
Expand Down
82 changes: 82 additions & 0 deletions google/acctest/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
package acctest

import (
"context"
"errors"
"fmt"
"math/rand"
"os"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

Expand Down Expand Up @@ -58,6 +66,80 @@ func CheckDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourc
}
}

// General test utils

// MuxedProviders returns the correct test provider (between the sdk version or the framework version)
func MuxedProviders(testName string) (func() tfprotov5.ProviderServer, error) {
ctx := context.Background()

providers := []func() tfprotov5.ProviderServer{
providerserver.NewProtocol5(NewFrameworkTestProvider(testName)), // framework provider
GetSDKProvider(testName).GRPCProvider, // sdk provider
}

muxServer, err := tf5muxserver.NewMuxServer(ctx, providers...)

if err != nil {
return nil, err
}

return muxServer.ProviderServer, nil
}

func RandString(t *testing.T, length int) string {
if !IsVcrEnabled() {
return acctest.RandString(length)
}
envPath := os.Getenv("VCR_PATH")
vcrMode := os.Getenv("VCR_MODE")
s, err := vcrSource(t, envPath, vcrMode)
if err != nil {
// At this point we haven't created any resources, so fail fast
t.Fatal(err)
}

r := rand.New(s.source)
result := make([]byte, length)
set := "abcdefghijklmnopqrstuvwxyz012346789"
for i := 0; i < length; i++ {
result[i] = set[r.Intn(len(set))]
}
return string(result)
}

func RandInt(t *testing.T) int {
if !IsVcrEnabled() {
return acctest.RandInt()
}
envPath := os.Getenv("VCR_PATH")
vcrMode := os.Getenv("VCR_MODE")
s, err := vcrSource(t, envPath, vcrMode)
if err != nil {
// At this point we haven't created any resources, so fail fast
t.Fatal(err)
}

return rand.New(s.source).Int()
}

// ProtoV5ProviderFactories returns a muxed ProviderServer that uses the provider code from this repo (SDK and plugin-framework).
// Used to set ProtoV5ProviderFactories in a resource.TestStep within an acceptance test.
func ProtoV5ProviderFactories(t *testing.T) map[string]func() (tfprotov5.ProviderServer, error) {
return map[string]func() (tfprotov5.ProviderServer, error){
"google": func() (tfprotov5.ProviderServer, error) {
provider, err := MuxedProviders(t.Name())
return provider(), err
},
}
}

// ProtoV5ProviderBetaFactories returns the same as ProtoV5ProviderFactories only the provider is mapped with
// "google-beta" to ensure that registry examples use `google-beta` if the example is versioned as beta;
// normal beta tests should continue to use ProtoV5ProviderFactories
func ProtoV5ProviderBetaFactories(t *testing.T) map[string]func() (tfprotov5.ProviderServer, error) {
return map[string]func() (tfprotov5.ProviderServer, error){}
}

// This is a Printf sibling (Nprintf; Named Printf), which handles strings like
// Nprintf("Hello %{target}!", map[string]interface{}{"target":"world"}) == "Hello world!".
// This is particularly useful for generated tests, where we don't want to use Printf,
Expand Down
Loading

0 comments on commit d143352

Please sign in to comment.