-
Notifications
You must be signed in to change notification settings - Fork 75
/
resource_opentelekomcloud_cce_cluster_v3_test.go
416 lines (365 loc) · 14 KB
/
resource_opentelekomcloud_cce_cluster_v3_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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package acceptance
import (
"fmt"
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/clusters"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/env"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
)
var clusterName = fmt.Sprintf("cce-%s", acctest.RandString(5))
const resourceName = "opentelekomcloud_cce_cluster_v3.cluster_1"
func TestAccCCEClusterV3_basic(t *testing.T) {
var cluster clusters.Clusters
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3Basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCCEClusterV3Exists(resourceName, &cluster),
resource.TestCheckResourceAttr(resourceName, "name", clusterName),
resource.TestCheckResourceAttr(resourceName, "status", "Available"),
resource.TestCheckResourceAttr(resourceName, "cluster_type", "VirtualMachine"),
resource.TestCheckResourceAttr(resourceName, "flavor_id", "cce.s1.small"),
resource.TestCheckResourceAttr(resourceName, "container_network_type", "overlay_l2"),
resource.TestCheckResourceAttr(resourceName, "authentication_mode", "x509"),
resource.TestCheckResourceAttr(resourceName, "kube_proxy_mode", "iptables"),
resource.TestCheckResourceAttr(resourceName, "kubernetes_svc_ip_range", "10.247.0.0/16"),
resource.TestCheckResourceAttrSet(resourceName, "security_group_control"),
resource.TestCheckResourceAttrSet(resourceName, "security_group_node"),
),
},
{
Config: testAccCCEClusterV3Update,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "description", "new description"),
),
},
},
})
}
func TestAccCCEClusterV3_invalidNetwork(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3InvalidSubnet,
PlanOnly: true,
ExpectError: regexp.MustCompile(`can't find subnet.+`),
},
{
Config: testAccCCEClusterV3InvalidVPC,
PlanOnly: true,
ExpectError: regexp.MustCompile(`can't find VPC.+`),
},
{
Config: testAccCCEClusterV3Computed,
PlanOnly: true,
ExpectNonEmptyPlan: true,
},
},
})
}
func TestAccCCEClusterV3_proxyAuth(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3AuthProxy,
Check: resource.TestCheckResourceAttr(resourceName, "authentication_mode", "authenticating_proxy"),
},
},
})
}
func TestAccCCEClusterV3_timeout(t *testing.T) {
var cluster clusters.Clusters
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3Timeout,
Check: resource.ComposeTestCheckFunc(
testAccCheckCCEClusterV3Exists(resourceName, &cluster),
resource.TestCheckResourceAttr(resourceName, "authentication_mode", "rbac"),
),
},
},
})
}
func TestAccCCEClusterV3NoAddons(t *testing.T) {
var cluster clusters.Clusters
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3NoAddons,
Check: resource.ComposeTestCheckFunc(
testAccCheckCCEClusterV3Exists(resourceName, &cluster),
resource.TestCheckResourceAttr(resourceName, "installed_addons.#", "0"),
),
},
},
})
}
func testAccCheckCCEClusterV3Destroy(s *terraform.State) error {
config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.CceV3Client(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf("error creating opentelekomcloud CCE client: %s", err)
}
for _, rs := range s.RootModule().Resources {
if rs.Type != "opentelekomcloud_cce_cluster_v3" {
continue
}
_, err := clusters.Get(client, rs.Primary.ID).Extract()
if err == nil {
return fmt.Errorf("cluster still exists")
}
}
return nil
}
func testAccCheckCCEClusterV3Exists(n string, cluster *clusters.Clusters) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}
config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.CceV3Client(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf("error creating OpenTelekomCloud CCE client: %s", err)
}
found, err := clusters.Get(client, rs.Primary.ID).Extract()
if err != nil {
return err
}
if found.Metadata.Id != rs.Primary.ID {
return fmt.Errorf("cluster not found")
}
*cluster = *found
return nil
}
}
func TestAccCCEClusterV3_withVersionDiff(t *testing.T) {
var cluster clusters.Clusters
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckCCEClusterV3Destroy,
Steps: []resource.TestStep{
{
Config: testAccCCEClusterV3WithInvalidVersion,
Check: resource.ComposeTestCheckFunc(
testAccCheckCCEClusterV3Exists("opentelekomcloud_cce_cluster_v3.cluster_1", &cluster),
resource.TestCheckResourceAttr(
"opentelekomcloud_cce_cluster_v3.cluster_1", "name", clusterName),
),
},
},
})
}
var (
testAccCCEClusterV3Basic = fmt.Sprintf(`
%s
resource "opentelekomcloud_cce_cluster_v3" "cluster_1" {
name = "%s"
cluster_type = "VirtualMachine"
flavor_id = "cce.s1.small"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
container_network_type = "overlay_l2"
kubernetes_svc_ip_range = "10.247.0.0/16"
}`, common.DataSourceSubnet, clusterName)
testAccCCEClusterV3Update = fmt.Sprintf(`
%s
resource "opentelekomcloud_cce_cluster_v3" "cluster_1" {
name = "%s"
cluster_type = "VirtualMachine"
flavor_id = "cce.s1.small"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
container_network_type = "overlay_l2"
description = "new description"
kubernetes_svc_ip_range = "10.247.0.0/16"
}`, common.DataSourceSubnet, clusterName)
testAccCCEClusterV3Timeout = fmt.Sprintf(`
%s
resource "opentelekomcloud_networking_floatingip_v2" "fip_1" {}
resource "opentelekomcloud_cce_cluster_v3" "cluster_1" {
name = "%s"
cluster_type = "VirtualMachine"
flavor_id = "cce.s2.small"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
eip = opentelekomcloud_networking_floatingip_v2.fip_1.address
container_network_type = "overlay_l2"
authentication_mode = "rbac"
timeouts {
create = "20m"
delete = "10m"
}
multi_az = true
}
`, common.DataSourceSubnet, clusterName)
testAccCCEClusterV3WithInvalidVersion = fmt.Sprintf(`
%s
resource "opentelekomcloud_cce_cluster_v3" "cluster_1" {
name = "%s"
cluster_type = "VirtualMachine"
flavor_id = "cce.s1.small"
cluster_version = "v1.9.2"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
container_network_type = "overlay_l2"
description = "new description"
}`, common.DataSourceSubnet, clusterName)
testAccCCEClusterV3AuthProxy = fmt.Sprintf(`
%s
resource "opentelekomcloud_cce_cluster_v3" "cluster_1" {
name = "%s"
cluster_type = "VirtualMachine"
flavor_id = "cce.s1.small"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
container_network_type = "overlay_l2"
kubernetes_svc_ip_range = "10.247.0.0/16"
authentication_mode = "authenticating_proxy"
authenticating_proxy {
ca = <<EOT
-----BEGIN CERTIFICATE-----
MIICZjCCAc+gAwIBAgIUZtMIBg4MdR/h8yPITTx5+B0Xj0swDQYJKoZIhvcNAQEL
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMTA5MjExNDEyMThaFw0zMTA5
MTkxNDEyMThaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwgZ8wDQYJKoZIhvcNAQEB
BQADgY0AMIGJAoGBANSP54SyxRLkWjnGkLQcUOSxM6FUfk8k+PvSgq9xF0CeO8BR
NbY+y/+Jr9l1k0XRjAajIe/pdD4Yta24ox9yH5+ay+eKIFjdr2DAsyD5SSnW+A3o
e1eYAwZhWWgGRgq8VG6yik1vm1nsJW8crj+3k/N0kjiLYFGRTelpSThnesb3AgMB
AAGjUzBRMB0GA1UdDgQWBBT2daqVn+fyu7qoWflS7WwQ9EosODAfBgNVHSMEGDAW
gBT2daqVn+fyu7qoWflS7WwQ9EosODAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3
DQEBCwUAA4GBAEoaBV1HA5wSDEGu1blKUtda13IxBg3ZpHWxwOpDS3gwXYzEOPAL
jgKvjKkLJaJnTp8V+YtO1xBhB272ZgWbr22Cer8TQZchNc16I2qLp+O9AQuPqVYO
15xHZN4yCgCVYcSlUm/HW2tJ3lAmilxkEFvJJcK1uLh7vqMflmcPSLe5
-----END CERTIFICATE-----
EOT
cert = <<EOT
-----BEGIN CERTIFICATE-----
MIIByDCCATECFAzNYuav3B0dfSfIe7L8pDJ0t0LuMA0GCSqGSIb3DQEBCwUAMEUx
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
EOT
}
}
`, common.DataSourceSubnet, clusterName)
testAccCCEClusterV3InvalidSubnet = fmt.Sprintf(`
%s
resource "opentelekomcloud_cce_cluster_v3" "cluster_1" {
name = "%s"
cluster_type = "VirtualMachine"
flavor_id = "cce.s1.small"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = "abc"
container_network_type = "overlay_l2"
kubernetes_svc_ip_range = "10.247.0.0/16"
}
`, common.DataSourceSubnet, clusterName)
testAccCCEClusterV3InvalidVPC = fmt.Sprintf(`
resource "opentelekomcloud_vpc_v1" "vpc" {
cidr = "192.168.0.0/16"
name = "cce-test"
}
locals {
subnet_cidr = cidrsubnet(opentelekomcloud_vpc_v1.vpc.cidr, 8, 0)
subnet_gw_ip = cidrhost(local.subnet_cidr, 1)
}
resource "opentelekomcloud_vpc_subnet_v1" "subnet" {
cidr = local.subnet_cidr
gateway_ip = local.subnet_gw_ip
name = "cce-test"
vpc_id = opentelekomcloud_vpc_v1.vpc.id
}
resource "opentelekomcloud_cce_cluster_v3" "cluster_1" {
name = "%s"
cluster_type = "VirtualMachine"
flavor_id = "cce.s1.small"
vpc_id = "abc"
subnet_id = opentelekomcloud_vpc_subnet_v1.subnet.id
container_network_type = "overlay_l2"
kubernetes_svc_ip_range = "10.247.0.0/16"
}
`, clusterName)
testAccCCEClusterV3Computed = fmt.Sprintf(`
resource "opentelekomcloud_vpc_v1" "vpc" {
cidr = "192.168.0.0/16"
name = "cce-test"
}
locals {
subnet_cidr = cidrsubnet(opentelekomcloud_vpc_v1.vpc.cidr, 8, 0)
subnet_gw_ip = cidrhost(local.subnet_cidr, 1)
}
resource "opentelekomcloud_vpc_subnet_v1" "subnet" {
cidr = local.subnet_cidr
gateway_ip = local.subnet_gw_ip
name = "cce-test"
vpc_id = opentelekomcloud_vpc_v1.vpc.id
}
resource "opentelekomcloud_cce_cluster_v3" "cluster_1" {
name = "%s"
cluster_type = "VirtualMachine"
flavor_id = "cce.s1.small"
vpc_id = opentelekomcloud_vpc_v1.vpc.id
subnet_id = opentelekomcloud_vpc_subnet_v1.subnet.id
container_network_type = "overlay_l2"
kubernetes_svc_ip_range = "10.247.0.0/16"
}
`, clusterName)
testAccCCEClusterV3NoAddons = fmt.Sprintf(`
%s
resource "opentelekomcloud_cce_cluster_v3" "cluster_1" {
name = "%s"
cluster_type = "VirtualMachine"
flavor_id = "cce.s1.small"
vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
container_network_type = "overlay_l2"
kubernetes_svc_ip_range = "10.247.0.0/16"
no_addons = true
}`, common.DataSourceSubnet, clusterName)
)