|
3 | 3 | package acctest |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "context" |
6 | 7 | "errors" |
7 | 8 | "fmt" |
| 9 | + "math/rand" |
| 10 | + "os" |
8 | 11 | "strings" |
| 12 | + "testing" |
9 | 13 |
|
| 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" |
10 | 18 | "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
11 | 19 | ) |
12 | 20 |
|
@@ -58,6 +66,85 @@ func CheckDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourc |
58 | 66 | } |
59 | 67 | } |
60 | 68 |
|
| 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 | + |
61 | 148 | // This is a Printf sibling (Nprintf; Named Printf), which handles strings like |
62 | 149 | // Nprintf("Hello %{target}!", map[string]interface{}{"target":"world"}) == "Hello world!". |
63 | 150 | // This is particularly useful for generated tests, where we don't want to use Printf, |
|
0 commit comments