-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
provider_test.go
357 lines (317 loc) · 11.8 KB
/
provider_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package aws
import (
"fmt"
"log"
"os"
"regexp"
"testing"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-template/template"
"github.com/terraform-providers/terraform-provider-tls/tls"
)
var testAccProviders map[string]terraform.ResourceProvider
var testAccProvidersWithTLS map[string]terraform.ResourceProvider
var testAccProviderFactories func(providers *[]*schema.Provider) map[string]terraform.ResourceProviderFactory
var testAccProvider *schema.Provider
var testAccTemplateProvider *schema.Provider
func init() {
testAccProvider = Provider().(*schema.Provider)
testAccTemplateProvider = template.Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"aws": testAccProvider,
"template": testAccTemplateProvider,
}
testAccProviderFactories = func(providers *[]*schema.Provider) map[string]terraform.ResourceProviderFactory {
return map[string]terraform.ResourceProviderFactory{
"aws": func() (terraform.ResourceProvider, error) {
p := Provider()
*providers = append(*providers, p.(*schema.Provider))
return p, nil
},
}
}
testAccProvidersWithTLS = map[string]terraform.ResourceProvider{
"tls": tls.Provider(),
}
for k, v := range testAccProviders {
testAccProvidersWithTLS[k] = v
}
}
func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}
func testAccPreCheck(t *testing.T) {
if v := os.Getenv("AWS_PROFILE"); v == "" {
if v := os.Getenv("AWS_ACCESS_KEY_ID"); v == "" {
t.Fatal("AWS_ACCESS_KEY_ID must be set for acceptance tests")
}
if v := os.Getenv("AWS_SECRET_ACCESS_KEY"); v == "" {
t.Fatal("AWS_SECRET_ACCESS_KEY must be set for acceptance tests")
}
}
region := testAccGetRegion()
log.Printf("[INFO] Test: Using %s as test region", region)
os.Setenv("AWS_DEFAULT_REGION", region)
err := testAccProvider.Configure(terraform.NewResourceConfig(nil))
if err != nil {
t.Fatal(err)
}
}
// testAccAwsProviderAccountID returns the account ID of an AWS provider
func testAccAwsProviderAccountID(provider *schema.Provider) string {
if provider == nil {
log.Print("[DEBUG] Unable to read account ID from test provider: empty provider")
return ""
}
if provider.Meta() == nil {
log.Print("[DEBUG] Unable to read account ID from test provider: unconfigured provider")
return ""
}
client, ok := provider.Meta().(*AWSClient)
if !ok {
log.Print("[DEBUG] Unable to read account ID from test provider: non-AWS or unconfigured AWS provider")
return ""
}
return client.accountid
}
// testAccCheckResourceAttrAccountID ensures the Terraform state exactly matches the account ID
func testAccCheckResourceAttrAccountID(resourceName, attributeName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
return resource.TestCheckResourceAttr(resourceName, attributeName, testAccGetAccountID())(s)
}
}
// testAccCheckResourceAttrRegionalARN ensures the Terraform state exactly matches a formatted ARN with region
func testAccCheckResourceAttrRegionalARN(resourceName, attributeName, arnService, arnResource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
attributeValue := arn.ARN{
AccountID: testAccGetAccountID(),
Partition: testAccGetPartition(),
Region: testAccGetRegion(),
Resource: arnResource,
Service: arnService,
}.String()
return resource.TestCheckResourceAttr(resourceName, attributeName, attributeValue)(s)
}
}
// testAccMatchResourceAttrRegionalARN ensures the Terraform state regexp matches a formatted ARN with region
func testAccMatchResourceAttrRegionalARN(resourceName, attributeName, arnService string, arnResourceRegexp *regexp.Regexp) resource.TestCheckFunc {
return func(s *terraform.State) error {
arnRegexp := arn.ARN{
AccountID: testAccGetAccountID(),
Partition: testAccGetPartition(),
Region: testAccGetRegion(),
Resource: arnResourceRegexp.String(),
Service: arnService,
}.String()
attributeMatch, err := regexp.Compile(arnRegexp)
if err != nil {
return fmt.Errorf("Unable to compile ARN regexp (%s): %s", arnRegexp, err)
}
return resource.TestMatchResourceAttr(resourceName, attributeName, attributeMatch)(s)
}
}
// testAccCheckResourceAttrGlobalARN ensures the Terraform state exactly matches a formatted ARN without region
func testAccCheckResourceAttrGlobalARN(resourceName, attributeName, arnService, arnResource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
attributeValue := arn.ARN{
AccountID: testAccGetAccountID(),
Partition: testAccGetPartition(),
Resource: arnResource,
Service: arnService,
}.String()
return resource.TestCheckResourceAttr(resourceName, attributeName, attributeValue)(s)
}
}
// testAccMatchResourceAttrGlobalARN ensures the Terraform state regexp matches a formatted ARN without region
func testAccMatchResourceAttrGlobalARN(resourceName, attributeName, arnService string, arnResourceRegexp *regexp.Regexp) resource.TestCheckFunc {
return func(s *terraform.State) error {
arnRegexp := arn.ARN{
AccountID: testAccGetAccountID(),
Partition: testAccGetPartition(),
Resource: arnResourceRegexp.String(),
Service: arnService,
}.String()
attributeMatch, err := regexp.Compile(arnRegexp)
if err != nil {
return fmt.Errorf("Unable to compile ARN regexp (%s): %s", arnRegexp, err)
}
return resource.TestMatchResourceAttr(resourceName, attributeName, attributeMatch)(s)
}
}
// testAccGetAccountID returns the account ID of testAccProvider
// Must be used returned within a resource.TestCheckFunc
func testAccGetAccountID() string {
return testAccAwsProviderAccountID(testAccProvider)
}
func testAccGetRegion() string {
v := os.Getenv("AWS_DEFAULT_REGION")
if v == "" {
return "us-west-2"
}
return v
}
func testAccGetPartition() string {
if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), testAccGetRegion()); ok {
return partition.ID()
}
return "aws"
}
func testAccAlternateAccountPreCheck(t *testing.T) {
if os.Getenv("AWS_ALTERNATE_PROFILE") == "" && os.Getenv("AWS_ALTERNATE_ACCESS_KEY_ID") == "" {
t.Fatal("AWS_ALTERNATE_ACCESS_KEY_ID or AWS_ALTERNATE_PROFILE must be set for acceptance tests")
}
if os.Getenv("AWS_ALTERNATE_ACCESS_KEY_ID") != "" && os.Getenv("AWS_ALTERNATE_SECRET_ACCESS_KEY") == "" {
t.Fatal("AWS_ALTERNATE_SECRET_ACCESS_KEY must be set for acceptance tests")
}
}
func testAccEC2ClassicPreCheck(t *testing.T) {
client := testAccProvider.Meta().(*AWSClient)
platforms := client.supportedplatforms
region := client.region
if !hasEc2Classic(platforms) {
t.Skipf("This test can only run in EC2 Classic, platforms available in %s: %q",
region, platforms)
}
}
func testAccHasServicePreCheck(service string, t *testing.T) {
if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), testAccGetRegion()); ok {
if _, ok := partition.Services()[service]; !ok {
t.Skip(fmt.Sprintf("skipping tests; partition does not support %s service", service))
}
}
}
func testAccMultipleRegionsPreCheck(t *testing.T) {
if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), testAccGetRegion()); ok {
if len(partition.Regions()) < 2 {
t.Skip("skipping tests; partition only includes a single region")
}
}
}
func testAccOrganizationsAccountPreCheck(t *testing.T) {
conn := testAccProvider.Meta().(*AWSClient).organizationsconn
input := &organizations.DescribeOrganizationInput{}
_, err := conn.DescribeOrganization(input)
if isAWSErr(err, organizations.ErrCodeAWSOrganizationsNotInUseException, "") {
return
}
if err != nil {
t.Fatalf("error describing AWS Organization: %s", err)
}
t.Skip("skipping tests; this AWS account must not be an existing member of an AWS Organization")
}
func testAccAlternateAccountProviderConfig() string {
return fmt.Sprintf(`
provider "aws" {
access_key = %[1]q
alias = "alternate"
profile = %[2]q
secret_key = %[3]q
}
`, os.Getenv("AWS_ALTERNATE_ACCESS_KEY_ID"), os.Getenv("AWS_ALTERNATE_PROFILE"), os.Getenv("AWS_ALTERNATE_SECRET_ACCESS_KEY"))
}
func testAccAwsRegionProviderFunc(region string, providers *[]*schema.Provider) func() *schema.Provider {
return func() *schema.Provider {
if region == "" {
log.Println("[DEBUG] No region given")
return nil
}
if providers == nil {
log.Println("[DEBUG] No providers given")
return nil
}
log.Printf("[DEBUG] Checking providers for AWS region: %s", region)
for _, provider := range *providers {
// Ignore if Meta is empty, this can happen for validation providers
if provider == nil || provider.Meta() == nil {
log.Printf("[DEBUG] Skipping empty provider")
continue
}
// Ignore if Meta is not AWSClient, this will happen for other providers
client, ok := provider.Meta().(*AWSClient)
if !ok {
log.Printf("[DEBUG] Skipping non-AWS provider")
continue
}
clientRegion := client.region
log.Printf("[DEBUG] Checking AWS provider region %q against %q", clientRegion, region)
if clientRegion == region {
log.Printf("[DEBUG] Found AWS provider with region: %s", region)
return provider
}
}
log.Printf("[DEBUG] No suitable provider found for %q in %d providers", region, len(*providers))
return nil
}
}
func testAccCheckWithProviders(f func(*terraform.State, *schema.Provider) error, providers *[]*schema.Provider) resource.TestCheckFunc {
return func(s *terraform.State) error {
numberOfProviders := len(*providers)
for i, provider := range *providers {
if provider.Meta() == nil {
log.Printf("[DEBUG] Skipping empty provider %d (total: %d)", i, numberOfProviders)
continue
}
log.Printf("[DEBUG] Calling check with provider %d (total: %d)", i, numberOfProviders)
if err := f(s, provider); err != nil {
return err
}
}
return nil
}
}
// Check service API call error for reasons to skip acceptance testing
// These include missing API endpoints and unsupported API calls
func testAccPreCheckSkipError(err error) bool {
// Ignore missing API endpoints
if isAWSErr(err, "RequestError", "send request failed") {
return true
}
// Ignore unsupported API calls
if isAWSErr(err, "UnsupportedOperation", "") {
return true
}
return false
}
// Check sweeper API call error for reasons to skip sweeping
// These include missing API endpoints and unsupported API calls
func testSweepSkipSweepError(err error) bool {
// Ignore missing API endpoints
if isAWSErr(err, "RequestError", "send request failed") {
return true
}
// Ignore unsupported API calls
if isAWSErr(err, "UnsupportedOperation", "") {
return true
}
// Ignore more unsupported API calls
// InvalidParameterValue: Use of cache security groups is not permitted in this API version for your account.
if isAWSErr(err, "InvalidParameterValue", "not permitted in this API version for your account") {
return true
}
// GovCloud has endpoints that respond with (no message provided):
// AccessDeniedException:
// Since acceptance test sweepers are best effort and this response is very common,
// we allow bypassing this error globally instead of individual test sweeper fixes.
if isAWSErr(err, "AccessDeniedException", "") {
return true
}
// Example: BadRequestException: vpc link not supported for region us-gov-west-1
if isAWSErr(err, "BadRequestException", "not supported") {
return true
}
// Example: InvalidAction: The action DescribeTransitGatewayAttachments is not valid for this web service
if isAWSErr(err, "InvalidAction", "is not valid") {
return true
}
return false
}