Skip to content

Commit 5d5ff45

Browse files
committed
improve the code of testing
1 parent c323e8f commit 5d5ff45

26 files changed

+327
-323
lines changed

gendoc/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var (
3232
)
3333

3434
func main() {
35-
provider := cloud.Provider()
35+
provider := cloud.Provider().(*schema.Provider)
3636
vProvider := runtime.FuncForPC(reflect.ValueOf(cloud.Provider).Pointer())
3737

3838
fname, _ := vProvider.FileLine(0)

tencentcloud/basic_test.go

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,78 @@
11
package tencentcloud
22

3-
import (
4-
"fmt"
5-
"log"
6-
"os"
7-
"testing"
3+
/*
4+
---------------------------------------------------
5+
If you want to run through the test cases,
6+
the following must be changed to your resource id.
7+
---------------------------------------------------
8+
*/
9+
10+
const appid string = "1259649581"
11+
12+
const defaultRegion = "ap-guangzhou"
13+
const defaultVpcId = "vpc-h70b6b49"
14+
const defaultVpcCidr = "172.16.0.0/16"
15+
const defaultVpcCidrLess = "172.16.0.0/18"
816

9-
"github.com/hashicorp/terraform/helper/resource"
10-
"github.com/hashicorp/terraform/terraform"
11-
)
17+
const defaultAZone = "ap-guangzhou-3"
18+
const defaultSubnetId = "subnet-1uwh63so"
19+
const defaultSubnetCidr = "172.16.0.0/20"
20+
const defaultSubnetCidrLess = "172.16.0.0/22"
21+
22+
const defaultInsName = "tf-ci-test"
23+
const defaultInsNameUpdate = "tf-ci-test-update"
1224

1325
/*
14-
If you want to run through the test case,
15-
the following must be changed to your resource id
26+
---------------------------------------------------
27+
The folling are common test case used as templates.
28+
---------------------------------------------------
1629
*/
17-
const appid string = "1259649581"
1830

19-
// 172.16.0.0/16
20-
const DefaultVpcId = "vpc-h70b6b49"
31+
const defaultVpcVariable = `
32+
variable "instance_name" {
33+
default = "` + defaultInsName + `"
34+
}
35+
36+
variable "instance_name_update" {
37+
default = "` + defaultInsNameUpdate + `"
38+
}
2139
22-
const DefaultSubnetId = "subnet-1uwh63so"
40+
variable "availability_zone" {
41+
default = "` + defaultAZone + `"
42+
}
2343
24-
func testAccPreCheck(t *testing.T) {
25-
if v := os.Getenv(PROVIDER_SECRET_ID); v == "" {
26-
t.Fatalf("%v must be set for acceptance tests\n", PROVIDER_SECRET_ID)
27-
}
28-
if v := os.Getenv(PROVIDER_SECRET_KEY); v == "" {
29-
t.Fatalf("%v must be set for acceptance tests\n", PROVIDER_SECRET_KEY)
30-
}
31-
if v := os.Getenv(PROVIDER_REGION); v == "" {
32-
log.Println("[INFO] Test: Using ap-guangzhou as test region")
33-
os.Setenv(PROVIDER_REGION, "ap-guangzhou")
34-
}
44+
variable "vpc_id" {
45+
default = "` + defaultVpcId + `"
3546
}
3647
37-
func testAccPreSetRegion(region string) {
38-
os.Setenv(PROVIDER_REGION, region)
48+
variable "vpc_cidr" {
49+
default = "` + defaultVpcCidr + `"
3950
}
4051
41-
func testAccCheckTencentCloudDataSourceID(n string) resource.TestCheckFunc {
42-
return func(s *terraform.State) error {
43-
rs, ok := s.RootModule().Resources[n]
44-
if !ok {
45-
return fmt.Errorf("Can't find data source: %s", n)
46-
}
52+
variable "vpc_cidr_less" {
53+
default = "` + defaultVpcCidrLess + `"
54+
}
55+
56+
variable "subnet_id" {
57+
default = "` + defaultSubnetId + `"
58+
}
59+
60+
variable "subnet_cidr" {
61+
default = "` + defaultSubnetCidr + `"
62+
}
63+
64+
variable "subnet_cidr_less" {
65+
default = "` + defaultSubnetCidrLess + `"
66+
}
67+
`
4768

48-
if rs.Primary.ID == "" {
49-
return fmt.Errorf("data source ID not set")
50-
}
51-
return nil
52-
}
69+
const mysqlInstanceCommonTestCase = defaultVpcVariable + `
70+
resource "tencentcloud_mysql_instance" "default" {
71+
mem_size = 1000
72+
volume_size = 25
73+
instance_name = "${var.instance_name}"
74+
engine_version = "5.7"
75+
root_password = "0153Y474"
76+
availability_zone = "${var.availability_zone}"
5377
}
78+
`

tencentcloud/data_source_tc_availability_zones_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ import (
77
)
88

99
func TestAccTencentCloudAvailabilityZonesDataSource_basic(t *testing.T) {
10-
var currentRegion = "ap-guangzhou"
1110
resource.Test(t, resource.TestCase{
1211
PreCheck: func() {
1312
testAccPreCheck(t)
14-
testAccPreSetRegion(currentRegion)
1513
},
1614
Providers: testAccProviders,
1715
Steps: []resource.TestStep{
@@ -45,9 +43,9 @@ data "tencentcloud_availability_zones" "all" {
4543
}
4644
`
4745

48-
const testAccTencentCloudAvailabilityZonesDataSourceConfigFilterWithName = `
46+
const testAccTencentCloudAvailabilityZonesDataSourceConfigFilterWithName = defaultVpcVariable + `
4947
data "tencentcloud_availability_zones" "filter" {
50-
name = "ap-guangzhou-3"
48+
name = "${var.availability_zone}"
5149
}
5250
`
5351

tencentcloud/data_source_tc_mysql_backup_list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestAccDataSourceMysqlBackupList_basic(t *testing.T) {
2222
}
2323

2424
func testAccDataSourceMysqlBackupListConfig() string {
25-
return MysqlInstanceCommonTestCase + `
25+
return mysqlInstanceCommonTestCase + `
2626
data "tencentcloud_mysql_backup_list" "test" {
2727
mysql_id = "${tencentcloud_mysql_instance.default.id}"
2828
}`

tencentcloud/data_source_tc_mysql_instance_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestAccTencentCloudMysqlInstanceDataSource(t *testing.T) {
1313
Providers: testAccProviders,
1414
Steps: []resource.TestStep{
1515
{
16-
Config: testAccTencentCloudMysqlInstanceDataSourceConfig(MysqlInstanceCommonTestCase),
16+
Config: testAccTencentCloudMysqlInstanceDataSourceConfig(mysqlInstanceCommonTestCase),
1717
Check: resource.ComposeAggregateTestCheckFunc(
1818
resource.TestCheckResourceAttr("data.tencentcloud_mysql_instance.mysql", "instance_list.#", "1"),
1919
resource.TestCheckResourceAttr("data.tencentcloud_mysql_instance.mysql", "instance_list.0.instance_name", "testAccMysql"),

tencentcloud/data_source_tc_mysql_parameter_list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestAccTencentCloudMysqlParameterListDataSource(t *testing.T) {
2323
),
2424
},
2525
{
26-
Config: testAccMysqlParameterListDataSourceConfig(MysqlInstanceCommonTestCase),
26+
Config: testAccMysqlParameterListDataSourceConfig(mysqlInstanceCommonTestCase),
2727
Check: resource.ComposeAggregateTestCheckFunc(
2828
resource.TestCheckResourceAttrSet("data.tencentcloud_mysql_parameter_list.mysql", "parameter_list.#"),
2929
resource.TestCheckResourceAttrSet("data.tencentcloud_mysql_parameter_list.mysql", "parameter_list.0.parameter_name"),

tencentcloud/import_tc_redis_backup_config_test.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

tencentcloud/import_tc_redis_instance_test.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

tencentcloud/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ import (
208208
"os"
209209

210210
"github.com/hashicorp/terraform/helper/schema"
211+
"github.com/hashicorp/terraform/terraform"
211212
)
212213

213214
const (
@@ -217,7 +218,7 @@ const (
217218
PROVIDER_REGION = "TENCENTCLOUD_REGION"
218219
)
219220

220-
func Provider() *schema.Provider {
221+
func Provider() terraform.ResourceProvider {
221222
return &schema.Provider{
222223
Schema: map[string]*schema.Schema{
223224
"secret_id": {

tencentcloud/provider_test.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,59 @@
11
package tencentcloud
22

33
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform/helper/resource"
410
"github.com/hashicorp/terraform/helper/schema"
511
"github.com/hashicorp/terraform/terraform"
612
)
713

814
var testAccProviders map[string]terraform.ResourceProvider
9-
10-
// var testAccProvidersWithTLS map[string]terraform.ResourceProvider
1115
var testAccProvider *schema.Provider
1216

1317
func init() {
14-
testAccProvider = Provider()
18+
testAccProvider = Provider().(*schema.Provider)
1519
testAccProviders = map[string]terraform.ResourceProvider{
1620
"tencentcloud": testAccProvider,
1721
}
22+
}
23+
24+
func TestProvider(t *testing.T) {
25+
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
26+
t.Fatalf("err: %s", err)
27+
}
28+
}
29+
30+
func TestProviderImpl(t *testing.T) {
31+
var _ terraform.ResourceProvider = Provider()
32+
}
33+
34+
func testAccPreCheck(t *testing.T) {
35+
if v := os.Getenv(PROVIDER_SECRET_ID); v == "" {
36+
t.Fatalf("%v must be set for acceptance tests\n", PROVIDER_SECRET_ID)
37+
}
38+
if v := os.Getenv(PROVIDER_SECRET_KEY); v == "" {
39+
t.Fatalf("%v must be set for acceptance tests\n", PROVIDER_SECRET_KEY)
40+
}
41+
if v := os.Getenv(PROVIDER_REGION); v == "" {
42+
log.Printf("[INFO] Testing: Using %s as test region", defaultRegion)
43+
os.Setenv(PROVIDER_REGION, defaultRegion)
44+
}
45+
}
1846

19-
//testAccProvidersWithTLS = map[string]terraform.ResourceProvider{
20-
// "tls": tls.Provider(),
21-
//}
47+
func testAccCheckTencentCloudDataSourceID(n string) resource.TestCheckFunc {
48+
return func(s *terraform.State) error {
49+
rs, ok := s.RootModule().Resources[n]
50+
if !ok {
51+
return fmt.Errorf("Can't find data source: %s", n)
52+
}
2253

23-
//for k, v := range testAccProviders {
24-
// testAccProvidersWithTLS[k] = v
25-
//}
54+
if rs.Primary.ID == "" {
55+
return fmt.Errorf("data source ID not set")
56+
}
57+
return nil
58+
}
2659
}

tencentcloud/resource_tc_container_cluster_instance_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ func testAccCheckContainerClusterInstanceDestroy(s *terraform.State) error {
3030

3131
const testAccTencentCloudContainerClusterInstanceConfig_basic = `
3232
variable "my_vpc" {
33-
default = "` + DefaultVpcId + `"
33+
default = "` + defaultVpcId + `"
3434
}
3535
3636
variable "my_subnet" {
37-
default = "` + DefaultSubnetId + `"
37+
default = "` + defaultSubnetId + `"
3838
}
3939
4040
data "tencentcloud_instance_types" "my_favorate_instance_types" {

tencentcloud/resource_tc_container_cluster_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ func checkContainerClusterInstancesAllNormal(n string) resource.TestCheckFunc {
7878

7979
const testAccTencentCloudContainerClusterConfig_basic = `
8080
variable "my_vpc" {
81-
default = "` + DefaultVpcId + `"
81+
default = "` + defaultVpcId + `"
8282
}
8383
8484
variable "my_subnet" {
85-
default = "` + DefaultSubnetId + `"
85+
default = "` + defaultSubnetId + `"
8686
}
8787
8888
data "tencentcloud_instance_types" "my_favorate_instance_types" {

tencentcloud/resource_tc_kubernetes_cluster_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,6 @@ func testAccTkeCluster(key, value string) string {
187187
"%s" = "%s"
188188
}
189189
}
190-
`, DefaultVpcId, DefaultSubnetId, key, value,
190+
`, defaultVpcId, defaultSubnetId, key, value,
191191
)
192192
}

tencentcloud/resource_tc_kubernetes_scale_worker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ variable "availability_zone" {
139139
}
140140
141141
variable "vpc" {
142-
default = "` + DefaultVpcId + `"
142+
default = "` + defaultVpcId + `"
143143
}
144144
145145
variable "subnet" {
146-
default = "` + DefaultSubnetId + `"
146+
default = "` + defaultSubnetId + `"
147147
}
148148
149149
variable "default_instance_type" {

tencentcloud/resource_tc_mysql_account_privilege_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestAccTencentCloudMysqlAccountPrivilege(t *testing.T) {
2020
CheckDestroy: testAccMysqlAccountPrivilegeDestroy,
2121
Steps: []resource.TestStep{
2222
{
23-
Config: testAccMysqlAccountPrivilege(MysqlInstanceCommonTestCase),
23+
Config: testAccMysqlAccountPrivilege(mysqlInstanceCommonTestCase),
2424
Check: resource.ComposeAggregateTestCheckFunc(
2525
testAccMysqlAccountPrivilegeExists("tencentcloud_mysql_account_privilege.mysql_account_privilege"),
2626
resource.TestCheckResourceAttrSet("tencentcloud_mysql_account_privilege.mysql_account_privilege", "mysql_id"),
@@ -35,7 +35,7 @@ func TestAccTencentCloudMysqlAccountPrivilege(t *testing.T) {
3535
),
3636
},
3737
{
38-
Config: testAccMysqlAccountPrivilegeUpdate(MysqlInstanceCommonTestCase),
38+
Config: testAccMysqlAccountPrivilegeUpdate(mysqlInstanceCommonTestCase),
3939
Check: resource.ComposeAggregateTestCheckFunc(
4040
testAccMysqlAccountPrivilegeExists("tencentcloud_mysql_account_privilege.mysql_account_privilege"),
4141
resource.TestCheckResourceAttrSet("tencentcloud_mysql_account_privilege.mysql_account_privilege", "mysql_id"),

tencentcloud/resource_tc_mysql_account_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestAccTencentCloudMysqlAccountResource(t *testing.T) {
1818
CheckDestroy: testAccCheckMysqlAccountDestroy,
1919
Steps: []resource.TestStep{
2020
{
21-
Config: testAccMysqlAccount(MysqlInstanceCommonTestCase),
21+
Config: testAccMysqlAccount(mysqlInstanceCommonTestCase),
2222
Check: resource.ComposeAggregateTestCheckFunc(
2323
testAccCheckMysqlAccountExists("tencentcloud_mysql_account.mysql_account"),
2424
resource.TestCheckResourceAttrSet("tencentcloud_mysql_account.mysql_account", "mysql_id"),

0 commit comments

Comments
 (0)