forked from huaweicloud/terraform-provider-huaweicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider_test.go
243 lines (206 loc) · 7.02 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
package huaweicloud
import (
"context"
"os"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/pathorcontents"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils/fmtp"
)
//nolint:revive
var (
HW_AVAILABILITY_ZONE = os.Getenv("HW_AVAILABILITY_ZONE")
HW_DEPRECATED_ENVIRONMENT = os.Getenv("HW_DEPRECATED_ENVIRONMENT")
HW_EXTGW_ID = os.Getenv("HW_EXTGW_ID")
HW_FLAVOR_ID = os.Getenv("HW_FLAVOR_ID")
HW_FLAVOR_NAME = os.Getenv("HW_FLAVOR_NAME")
HW_IMAGE_ID = os.Getenv("HW_IMAGE_ID")
HW_IMAGE_NAME = os.Getenv("HW_IMAGE_NAME")
HW_NETWORK_ID = os.Getenv("HW_NETWORK_ID")
HW_SUBNET_ID = os.Getenv("HW_SUBNET_ID")
HW_POOL_NAME = os.Getenv("HW_POOL_NAME")
HW_REGION_NAME = os.Getenv("HW_REGION_NAME")
HW_ACCESS_KEY = os.Getenv("HW_ACCESS_KEY")
HW_SECRET_KEY = os.Getenv("HW_SECRET_KEY")
HW_VPC_ID = os.Getenv("HW_VPC_ID")
HW_PROJECT_ID = os.Getenv("HW_PROJECT_ID")
HW_DOMAIN_ID = os.Getenv("HW_DOMAIN_ID")
HW_DOMAIN_NAME = os.Getenv("HW_DOMAIN_NAME")
HW_MRS_ENVIRONMENT = os.Getenv("HW_MRS_ENVIRONMENT")
HW_KMS_ENVIRONMENT = os.Getenv("HW_KMS_ENVIRONMENT")
HW_CCI_ENVIRONMENT = os.Getenv("HW_CCI_ENVIRONMENT")
HW_ENTERPRISE_PROJECT_ID_TEST = os.Getenv("HW_ENTERPRISE_PROJECT_ID_TEST")
HW_USER_ID = os.Getenv("HW_USER_ID")
HW_CHARGING_MODE = os.Getenv("HW_CHARGING_MODE")
)
var testAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider
func init() {
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
"huaweicloud": testAccProvider,
}
}
func testAccPreCheck(t *testing.T) {
// Do not run the test if this is a deprecated testing environment.
if HW_DEPRECATED_ENVIRONMENT != "" {
t.Skip("This environment only runs deprecated tests")
}
}
func testAccPreCheckDeprecated(t *testing.T) {
if HW_DEPRECATED_ENVIRONMENT == "" {
t.Skip("This environment does not support deprecated tests")
}
}
func testAccPreCheckMrs(t *testing.T) {
if HW_MRS_ENVIRONMENT == "" {
t.Skip("This environment does not support MRS tests")
}
}
func testAccPreCheckKms(t *testing.T) {
if HW_KMS_ENVIRONMENT == "" {
t.Skip("This environment does not support KMS tests")
}
}
func testAccPreCheckCCI(t *testing.T) {
if HW_CCI_ENVIRONMENT == "" {
t.Skip("This environment does not support CCI tests")
}
}
func testAccPreCheckEpsID(t *testing.T) {
if HW_ENTERPRISE_PROJECT_ID_TEST == "" {
t.Skip("This environment does not support Enterprise Project ID tests")
}
}
func testAccPreCheckChargingMode(t *testing.T) {
if HW_CHARGING_MODE != "prePaid" {
t.Skip("This environment does not support prepaid tests")
}
}
func TestProvider(t *testing.T) {
if err := Provider().InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}
// Steps for configuring HuaweiCloud with SSL validation are here:
// https://github.com/hashicorp/terraform/pull/6279#issuecomment-219020144
func TestAccProvider_caCertFile(t *testing.T) {
if os.Getenv("TF_ACC") == "" || os.Getenv("OS_SSL_TESTS") == "" {
t.Skip("TF_ACC or OS_SSL_TESTS not set, skipping HuaweiCloud SSL test.")
}
if os.Getenv("OS_CACERT") == "" {
t.Skip("OS_CACERT is not set; skipping HuaweiCloud CA test.")
}
p := Provider()
caFile, err := envVarFile("OS_CACERT")
if err != nil {
t.Fatal(err)
}
defer os.Remove(caFile)
raw := map[string]interface{}{
"cacert_file": caFile,
}
diags := p.Configure(context.Background(), terraform.NewResourceConfigRaw(raw))
if diags.HasError() {
t.Fatalf("Unexpected err when specifying HuaweiCloud CA by file: %s", diags[0].Summary)
}
}
func TestAccProvider_caCertString(t *testing.T) {
if os.Getenv("TF_ACC") == "" || os.Getenv("OS_SSL_TESTS") == "" {
t.Skip("TF_ACC or OS_SSL_TESTS not set, skipping HuaweiCloud SSL test.")
}
if os.Getenv("OS_CACERT") == "" {
t.Skip("OS_CACERT is not set; skipping HuaweiCloud CA test.")
}
p := Provider()
caContents, err := envVarContents("OS_CACERT")
if err != nil {
t.Fatal(err)
}
raw := map[string]interface{}{
"cacert_file": caContents,
}
diags := p.Configure(context.Background(), terraform.NewResourceConfigRaw(raw))
if diags.HasError() {
t.Fatalf("Unexpected err when specifying HuaweiCloud CA by string: %s", diags[0].Summary)
}
}
func TestAccProvider_clientCertFile(t *testing.T) {
if os.Getenv("TF_ACC") == "" || os.Getenv("OS_SSL_TESTS") == "" {
t.Skip("TF_ACC or OS_SSL_TESTS not set, skipping HuaweiCloud SSL test.")
}
if os.Getenv("OS_CERT") == "" || os.Getenv("OS_KEY") == "" {
t.Skip("OS_CERT or OS_KEY is not set; skipping HuaweiCloud client SSL auth test.")
}
p := Provider()
certFile, err := envVarFile("OS_CERT")
if err != nil {
t.Fatal(err)
}
defer os.Remove(certFile)
keyFile, err := envVarFile("OS_KEY")
if err != nil {
t.Fatal(err)
}
defer os.Remove(keyFile)
raw := map[string]interface{}{
"cert": certFile,
"key": keyFile,
}
diags := p.Configure(context.Background(), terraform.NewResourceConfigRaw(raw))
if diags.HasError() {
t.Fatalf("Unexpected err when specifying HuaweiCloud Client keypair by file: %s", diags[0].Summary)
}
}
func TestAccProvider_clientCertString(t *testing.T) {
if os.Getenv("TF_ACC") == "" || os.Getenv("OS_SSL_TESTS") == "" {
t.Skip("TF_ACC or OS_SSL_TESTS not set, skipping HuaweiCloud SSL test.")
}
if os.Getenv("OS_CERT") == "" || os.Getenv("OS_KEY") == "" {
t.Skip("OS_CERT or OS_KEY is not set; skipping HuaweiCloud client SSL auth test.")
}
p := Provider()
certContents, err := envVarContents("OS_CERT")
if err != nil {
t.Fatal(err)
}
keyContents, err := envVarContents("OS_KEY")
if err != nil {
t.Fatal(err)
}
raw := map[string]interface{}{
"cert": certContents,
"key": keyContents,
}
diags := p.Configure(context.Background(), terraform.NewResourceConfigRaw(raw))
if diags.HasError() {
t.Fatalf("Unexpected err when specifying HuaweiCloud Client keypair by contents: %s", diags[0].Summary)
}
}
func envVarContents(varName string) (string, error) {
contents, _, err := pathorcontents.Read(os.Getenv(varName))
if err != nil {
return "", fmtp.Errorf("Error reading %s: %s", varName, err)
}
return contents, nil
}
func envVarFile(varName string) (string, error) {
contents, err := envVarContents(varName)
if err != nil {
return "", err
}
tmpFile, err := os.CreateTemp("", varName)
if err != nil {
return "", fmtp.Errorf("Error creating temp file: %s", err)
}
if _, err := tmpFile.Write([]byte(contents)); err != nil {
_ = os.Remove(tmpFile.Name())
return "", fmtp.Errorf("Error writing temp file: %s", err)
}
if err := tmpFile.Close(); err != nil {
_ = os.Remove(tmpFile.Name())
return "", fmtp.Errorf("Error closing temp file: %s", err)
}
return tmpFile.Name(), nil
}