generated from hashicorp/terraform-provider-scaffolding
/
setup.go
79 lines (66 loc) · 2.49 KB
/
setup.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package acctest
import (
"context"
"os"
"testing"
tfprovider "github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
"github.com/hashicorp/terraform-provider-hcp/internal/clients"
"github.com/hashicorp/terraform-provider-hcp/internal/provider"
"github.com/hashicorp/terraform-provider-hcp/internal/provider/packer/utils/location"
"github.com/hashicorp/terraform-provider-hcp/version"
)
// ProtoV6ProviderFactories provides a Provider Factory to be used within
// acceptance tests.
var ProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"hcp": func() (tfprotov6.ProviderServer, error) {
providers := []func() tfprotov6.ProviderServer{
providerserver.NewProtocol6(provider.NewFrameworkProvider(version.ProviderVersion)()),
}
return tf6muxserver.NewMuxServer(context.Background(), providers...)
},
}
// PreCheck verifies that the required provider testing configuration is set.
//
// This PreCheck function should be present in every acceptance test. It ensures
// credentials and other test environment settings are configured.
func PreCheck(t *testing.T) {
t.Helper()
if os.Getenv("HCP_CLIENT_ID") == "" {
t.Fatal("HCP_CLIENT_ID must be set for acceptance tests")
}
if os.Getenv("HCP_CLIENT_SECRET") == "" {
t.Fatal("HCP_CLIENT_SECRET must be set for acceptance tests")
}
// TODO: Add support for project ID to be set at the source level
if os.Getenv("HCP_PROJECT_ID") == "" {
t.Fatal("HCP_PROJECT_ID must be set for acceptance tests")
}
}
// HCPClients returns the clients from the test provider.
func HCPClients(t *testing.T) *clients.Client {
p := provider.NewFrameworkProvider(version.ProviderVersion)()
var resp tfprovider.ConfigureResponse
p.Configure(context.Background(), tfprovider.ConfigureRequest{
TerraformVersion: "",
Config: tfsdk.Config{
Raw: tftypes.Value{},
Schema: &schema.Schema{},
},
}, &resp)
client, ok := resp.ResourceData.(*clients.Client)
if !ok {
t.Fatal("configure didn't return expected clients")
}
return client
}
func DefaultProjectLocation(t *testing.T) location.Location {
return HCPClients(t)
}