Skip to content

Commit 13f2948

Browse files
Move plugin framework provider and test utility files to packages (#8226) (#5837)
* 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]>
1 parent 8b7ac1a commit 13f2948

26 files changed

+743
-690
lines changed

.changelog/8226.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:none
2+
3+
```

google-beta/acctest/framework_test_utils.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
package acctest
44

55
import (
6+
"context"
67
"fmt"
8+
"log"
79
"reflect"
810
"regexp"
911
"strings"
12+
"testing"
13+
14+
"github.com/hashicorp/terraform-plugin-framework/diag"
1015

1116
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1217
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
@@ -15,6 +20,24 @@ import (
1520
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
1621
)
1722

23+
func GetFwTestProvider(t *testing.T) *frameworkTestProvider {
24+
configsLock.RLock()
25+
fwProvider, ok := fwProviders[t.Name()]
26+
configsLock.RUnlock()
27+
if ok {
28+
return fwProvider
29+
}
30+
31+
var diags diag.Diagnostics
32+
p := NewFrameworkTestProvider(t.Name())
33+
configureApiClient(context.Background(), &p.FrameworkProvider, &diags)
34+
if diags.HasError() {
35+
log.Fatalf("%d errors when configuring test provider client: first is %s", diags.ErrorsCount(), diags.Errors()[0].Detail())
36+
}
37+
38+
return p
39+
}
40+
1841
// General test utils
1942

2043
// TestExtractResourceAttr navigates a test's state to find the specified resource (or data source) attribute and makes the value

google-beta/acctest/provider_test_utils.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package acctest
44

55
import (
6+
"context"
67
"fmt"
78
"io/ioutil"
89
"os"
@@ -11,9 +12,11 @@ import (
1112
"time"
1213

1314
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
15+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/provider"
1416
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
1517

1618
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1720
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1821
)
1922

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

103+
var TestAccProviders map[string]*schema.Provider
104+
var testAccProvider *schema.Provider
105+
106+
func init() {
107+
configs = make(map[string]*transport_tpg.Config)
108+
fwProviders = make(map[string]*frameworkTestProvider)
109+
sources = make(map[string]VcrSource)
110+
testAccProvider = provider.Provider()
111+
TestAccProviders = map[string]*schema.Provider{
112+
"google": testAccProvider,
113+
}
114+
}
115+
116+
func GoogleProviderConfig(t *testing.T) *transport_tpg.Config {
117+
configsLock.RLock()
118+
config, ok := configs[t.Name()]
119+
configsLock.RUnlock()
120+
if ok {
121+
return config
122+
}
123+
124+
sdkProvider := provider.Provider()
125+
rc := terraform.ResourceConfig{}
126+
sdkProvider.Configure(context.Background(), &rc)
127+
return sdkProvider.Meta().(*transport_tpg.Config)
128+
}
129+
100130
func AccTestPreCheck(t *testing.T) {
101131
if v := os.Getenv("GOOGLE_CREDENTIALS_FILE"); v != "" {
102132
creds, err := ioutil.ReadFile(v)

google-beta/acctest/test_utils.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
package acctest
44

55
import (
6+
"context"
67
"errors"
78
"fmt"
9+
"math/rand"
10+
"os"
811
"strings"
12+
"testing"
913

14+
"github.com/hashicorp/terraform-plugin-framework/providerserver"
15+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
16+
"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1018
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1119
)
1220

@@ -58,6 +66,85 @@ func CheckDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourc
5866
}
5967
}
6068

69+
// General test utils
70+
71+
// MuxedProviders returns the correct test provider (between the sdk version or the framework version)
72+
func MuxedProviders(testName string) (func() tfprotov5.ProviderServer, error) {
73+
ctx := context.Background()
74+
75+
providers := []func() tfprotov5.ProviderServer{
76+
providerserver.NewProtocol5(NewFrameworkTestProvider(testName)), // framework provider
77+
GetSDKProvider(testName).GRPCProvider, // sdk provider
78+
}
79+
80+
muxServer, err := tf5muxserver.NewMuxServer(ctx, providers...)
81+
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
return muxServer.ProviderServer, nil
87+
}
88+
89+
func RandString(t *testing.T, length int) string {
90+
if !IsVcrEnabled() {
91+
return acctest.RandString(length)
92+
}
93+
envPath := os.Getenv("VCR_PATH")
94+
vcrMode := os.Getenv("VCR_MODE")
95+
s, err := vcrSource(t, envPath, vcrMode)
96+
if err != nil {
97+
// At this point we haven't created any resources, so fail fast
98+
t.Fatal(err)
99+
}
100+
101+
r := rand.New(s.source)
102+
result := make([]byte, length)
103+
set := "abcdefghijklmnopqrstuvwxyz012346789"
104+
for i := 0; i < length; i++ {
105+
result[i] = set[r.Intn(len(set))]
106+
}
107+
return string(result)
108+
}
109+
110+
func RandInt(t *testing.T) int {
111+
if !IsVcrEnabled() {
112+
return acctest.RandInt()
113+
}
114+
envPath := os.Getenv("VCR_PATH")
115+
vcrMode := os.Getenv("VCR_MODE")
116+
s, err := vcrSource(t, envPath, vcrMode)
117+
if err != nil {
118+
// At this point we haven't created any resources, so fail fast
119+
t.Fatal(err)
120+
}
121+
122+
return rand.New(s.source).Int()
123+
}
124+
125+
// ProtoV5ProviderFactories returns a muxed ProviderServer that uses the provider code from this repo (SDK and plugin-framework).
126+
// Used to set ProtoV5ProviderFactories in a resource.TestStep within an acceptance test.
127+
func ProtoV5ProviderFactories(t *testing.T) map[string]func() (tfprotov5.ProviderServer, error) {
128+
return map[string]func() (tfprotov5.ProviderServer, error){
129+
"google": func() (tfprotov5.ProviderServer, error) {
130+
provider, err := MuxedProviders(t.Name())
131+
return provider(), err
132+
},
133+
}
134+
}
135+
136+
// ProtoV5ProviderBetaFactories returns the same as ProtoV5ProviderFactories only the provider is mapped with
137+
// "google-beta" to ensure that registry examples use `google-beta` if the example is versioned as beta;
138+
// normal beta tests should continue to use ProtoV5ProviderFactories
139+
func ProtoV5ProviderBetaFactories(t *testing.T) map[string]func() (tfprotov5.ProviderServer, error) {
140+
return map[string]func() (tfprotov5.ProviderServer, error){
141+
"google-beta": func() (tfprotov5.ProviderServer, error) {
142+
provider, err := MuxedProviders(t.Name())
143+
return provider(), err
144+
},
145+
}
146+
}
147+
61148
// This is a Printf sibling (Nprintf; Named Printf), which handles strings like
62149
// Nprintf("Hello %{target}!", map[string]interface{}{"target":"world"}) == "Hello world!".
63150
// This is particularly useful for generated tests, where we don't want to use Printf,

0 commit comments

Comments
 (0)