Skip to content

Commit 58af3ce

Browse files
tongyimingmikatong
andauthored
Fix/cvm unit test (#903)
* fix: resource_tc_image & data_source_tc_reserved_instances unit test * fix: cvm unit test * fix: cvm unit test Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 6aa8a10 commit 58af3ce

File tree

4 files changed

+18
-84
lines changed

4 files changed

+18
-84
lines changed

tencentcloud/data_source_tc_reserved_instances_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func TestAccTencentCloudReservedInstancesDataSource(t *testing.T) {
1010
t.Parallel()
1111
resource.Test(t, resource.TestCase{
12-
PreCheck: func() { testAccPreCheck(t) },
12+
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_INTERNATION) },
1313
Providers: testAccProviders,
1414
Steps: []resource.TestStep{
1515
{
@@ -24,6 +24,6 @@ func TestAccTencentCloudReservedInstancesDataSource(t *testing.T) {
2424

2525
const testAccReservedInstancesDataSource = `
2626
data "tencentcloud_reserved_instances" "instances" {
27-
availability_zone = "ap-guangzhou-2"
27+
availability_zone = "ap-guangzhou-3"
2828
}
2929
`

tencentcloud/resource_tc_image_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
func TestAccTencentCloudImageResource(t *testing.T) {
1818
t.Parallel()
1919
resource.Test(t, resource.TestCase{
20-
PreCheck: func() { testAccPreCheck(t) },
20+
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
2121
Providers: testAccProviders,
2222
CheckDestroy: testAccCheckImageDestroy,
2323
Steps: []resource.TestStep{
@@ -27,7 +27,7 @@ func TestAccTencentCloudImageResource(t *testing.T) {
2727
Check: resource.ComposeTestCheckFunc(
2828
testAccCheckImageExists(ImageSnap),
2929
resource.TestCheckResourceAttr(ImageSnap, "image_name", "image-snapshot-keep"),
30-
resource.TestCheckResourceAttr(ImageSnap, "snapshot_ids.#", "2"),
30+
resource.TestCheckResourceAttr(ImageSnap, "snapshot_ids.#", "1"),
3131
resource.TestCheckResourceAttr(ImageSnap, "force_poweroff", "true"),
3232
resource.TestCheckResourceAttr(ImageSnap, "image_description", "create image with snapshot"),
3333
),
@@ -36,7 +36,7 @@ func TestAccTencentCloudImageResource(t *testing.T) {
3636
Config: testAccImageWithSnapShotUpdate,
3737
Check: resource.ComposeAggregateTestCheckFunc(
3838
resource.TestCheckResourceAttr(ImageSnap, "image_name", "image-snapshot-update-keep"),
39-
resource.TestCheckResourceAttr(ImageSnap, "snapshot_ids.#", "2"),
39+
resource.TestCheckResourceAttr(ImageSnap, "snapshot_ids.#", "1"),
4040
resource.TestCheckResourceAttr(ImageSnap, "force_poweroff", "false"),
4141
resource.TestCheckResourceAttr(ImageSnap, "image_description", "update image with snapshot"),
4242
),
@@ -53,7 +53,7 @@ func TestAccTencentCloudImageResource(t *testing.T) {
5353
Check: resource.ComposeTestCheckFunc(
5454
testAccCheckImageExists(ImageInstance),
5555
resource.TestCheckResourceAttr(ImageInstance, "image_name", "image-instance-keep"),
56-
resource.TestCheckResourceAttr(ImageInstance, "instance_id", "ins-2ju245xg"),
56+
resource.TestCheckResourceAttr(ImageInstance, "instance_id", "ins-fodds4y2"),
5757
resource.TestCheckResourceAttr(ImageInstance, "data_disk_ids.#", "1"),
5858
resource.TestCheckResourceAttr(ImageInstance, "image_description", "create image with instance"),
5959
),
@@ -62,7 +62,7 @@ func TestAccTencentCloudImageResource(t *testing.T) {
6262
Config: testAccImageWithInstanceUpdate,
6363
Check: resource.ComposeAggregateTestCheckFunc(
6464
resource.TestCheckResourceAttr(ImageInstance, "image_name", "image-instance-update-keep"),
65-
resource.TestCheckResourceAttr(ImageInstance, "instance_id", "ins-2ju245xg"),
65+
resource.TestCheckResourceAttr(ImageInstance, "instance_id", "ins-fodds4y2"),
6666
resource.TestCheckResourceAttr(ImageInstance, "data_disk_ids.#", "1"),
6767
resource.TestCheckResourceAttr(ImageInstance, "image_description", "update image with instance"),
6868
),
@@ -137,32 +137,32 @@ const (
137137
testAccImageWithSnapShot = `
138138
resource "tencentcloud_image" "image_snap" {
139139
image_name = "image-snapshot-keep"
140-
snapshot_ids = ["snap-nbp3xy1d", "snap-nvzu3dmh"]
140+
snapshot_ids = ["snap-8f2updnb"]
141141
force_poweroff = true
142142
image_description = "create image with snapshot"
143143
}`
144144

145145
testAccImageWithSnapShotUpdate = `
146146
resource "tencentcloud_image" "image_snap" {
147147
image_name = "image-snapshot-update-keep"
148-
snapshot_ids = ["snap-nbp3xy1d", "snap-nvzu3dmh"]
148+
snapshot_ids = ["snap-8f2updnb"]
149149
force_poweroff = false
150150
image_description = "update image with snapshot"
151151
}`
152152

153153
testAccImageWithInstance = `
154154
resource "tencentcloud_image" "image_instance" {
155155
image_name = "image-instance-keep"
156-
instance_id = "ins-2ju245xg"
157-
data_disk_ids = ["disk-gii0vtwi"]
156+
instance_id = "ins-fodds4y2"
157+
data_disk_ids = ["disk-mrnskm5i"]
158158
image_description = "create image with instance"
159159
}`
160160

161161
testAccImageWithInstanceUpdate = `
162162
resource "tencentcloud_image" "image_instance" {
163163
image_name = "image-instance-update-keep"
164-
instance_id = "ins-2ju245xg"
165-
data_disk_ids = ["disk-gii0vtwi"]
164+
instance_id = "ins-fodds4y2"
165+
data_disk_ids = ["disk-mrnskm5i"]
166166
image_description = "update image with instance"
167167
}`
168168
)

tencentcloud/resource_tc_instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,8 +1390,8 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
13901390
}
13911391
// Modifying instance type need restart the instance
13921392
// so status of CVM must be running when running flag is true
1393-
if instance != nil && instance.LatestOperationState != nil && (*instance.LatestOperationState == CVM_LATEST_OPERATION_STATE_OPERATING ||
1394-
(flag && *instance.InstanceState != CVM_STATUS_RUNNING)) {
1393+
if instance != nil && (instance.LatestOperationState != nil && *instance.LatestOperationState == CVM_LATEST_OPERATION_STATE_OPERATING ||
1394+
(flag && instance.LatestOperationState != nil && *instance.InstanceState != CVM_STATUS_RUNNING)) {
13951395
return resource.RetryableError(fmt.Errorf("cvm instance latest operetion status is %s, retry...", *instance.LatestOperationState))
13961396
}
13971397
return nil

tencentcloud/resource_tc_instance_test.go

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,13 @@ func TestAccTencentCloudInstanceWithDataDisk(t *testing.T) {
108108
testAccCheckTencentCloudDataSourceID(id),
109109
testAccCheckTencentCloudInstanceExists(id),
110110
resource.TestCheckResourceAttr(id, "instance_status", "RUNNING"),
111-
resource.TestCheckResourceAttr(id, "system_disk_size", "50"),
111+
resource.TestCheckResourceAttr(id, "system_disk_size", "100"),
112112
resource.TestCheckResourceAttr(id, "system_disk_type", "CLOUD_PREMIUM"),
113113
resource.TestCheckResourceAttr(id, "data_disks.0.data_disk_type", "CLOUD_PREMIUM"),
114114
resource.TestCheckResourceAttr(id, "data_disks.0.data_disk_size", "100"),
115115
resource.TestCheckResourceAttr(id, "data_disks.0.data_disk_snapshot_id", ""),
116116
resource.TestCheckResourceAttr(id, "data_disks.1.data_disk_type", "CLOUD_PREMIUM"),
117117
resource.TestCheckResourceAttr(id, "data_disks.1.data_disk_size", "100"),
118-
//TODO: snapshot is pre-paid required
119-
//resource.TestCheckResourceAttr(id, "data_disks.1.data_disk_snapshot_id", "snap-nvzu3dmh"),
120118
),
121119
},
122120
{
@@ -132,7 +130,6 @@ func TestAccTencentCloudInstanceWithDataDisk(t *testing.T) {
132130
resource.TestCheckResourceAttr(id, "data_disks.0.data_disk_snapshot_id", ""),
133131
resource.TestCheckResourceAttr(id, "data_disks.1.data_disk_type", "CLOUD_PREMIUM"),
134132
resource.TestCheckResourceAttr(id, "data_disks.1.data_disk_size", "150"),
135-
//resource.TestCheckResourceAttr(id, "data_disks.1.data_disk_snapshot_id", "snap-nvzu3dmh"),
136133
),
137134
},
138135
},
@@ -263,7 +260,7 @@ func TestAccTencentCloudInstanceWithImageLogin(t *testing.T) {
263260

264261
id := "tencentcloud_instance.foo"
265262
resource.Test(t, resource.TestCase{
266-
PreCheck: func() { testAccPreCheck(t) },
263+
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
267264
IDRefreshName: id,
268265
Providers: testAccProviders,
269266
CheckDestroy: testAccCheckInstanceDestroy,
@@ -467,36 +464,6 @@ func TestAccTencentCloudInstanceWithSpotpaid(t *testing.T) {
467464
})
468465
}
469466

470-
/* Skip prepaid for now
471-
func TestAccTencentCloudInstanceWithPrepaidChargeType(t *testing.T) {
472-
t.Parallel()
473-
474-
id := "tencentcloud_instance.foo"
475-
resource.Test(t, resource.TestCase{
476-
PreCheck: func() { testAccPreCheck(t) },
477-
IDRefreshName: id,
478-
Providers: testAccProviders,
479-
CheckDestroy: testAccCheckInstanceDestroy,
480-
Steps: []resource.TestStep{
481-
{
482-
Config: testAccTencentCloudInstancePrepaidRenew,
483-
Check: resource.ComposeTestCheckFunc(
484-
testAccCheckTencentCloudInstanceExists(id),
485-
resource.TestCheckResourceAttr(id, "instance_charge_type_prepaid_renew_flag", "NOTIFY_AND_AUTO_RENEW"),
486-
),
487-
}, {
488-
Config: testAccTencentCloudInstancePrepaidRenewUpdate,
489-
Check: resource.ComposeTestCheckFunc(
490-
testAccCheckTencentCloudInstanceExists(id),
491-
resource.TestCheckResourceAttr(id, "instance_charge_type_prepaid_renew_flag", "NOTIFY_AND_MANUAL_RENEW"),
492-
),
493-
},
494-
},
495-
})
496-
}
497-
498-
*/
499-
500467
func testAccCheckTencentCloudInstanceExists(n string) resource.TestCheckFunc {
501468
return func(s *terraform.State) error {
502469
logId := getLogId(contextNil)
@@ -608,6 +575,7 @@ resource "tencentcloud_instance" "foo" {
608575
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
609576
610577
system_disk_type = "CLOUD_PREMIUM"
578+
system_disk_size = 100
611579
612580
data_disks {
613581
data_disk_type = "CLOUD_PREMIUM"
@@ -665,7 +633,6 @@ resource "tencentcloud_instance" "foo" {
665633
availability_zone = data.tencentcloud_availability_zones.default.zones.0.name
666634
image_id = data.tencentcloud_images.default.images.0.image_id
667635
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
668-
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
669636
allocate_public_ip = %s
670637
system_disk_type = "CLOUD_PREMIUM"
671638
}
@@ -682,7 +649,6 @@ resource "tencentcloud_instance" "foo" {
682649
availability_zone = data.tencentcloud_availability_zones.default.zones.0.name
683650
image_id = data.tencentcloud_images.default.images.0.image_id
684651
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
685-
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
686652
internet_max_bandwidth_out = %d
687653
allocate_public_ip = %s
688654
system_disk_type = "CLOUD_PREMIUM"
@@ -757,7 +723,6 @@ resource "tencentcloud_instance" "foo" {
757723
availability_zone = data.tencentcloud_availability_zones.default.zones.0.name
758724
image_id = data.tencentcloud_images.zoo.images.0.image_id
759725
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
760-
internet_max_bandwidth_out = 1
761726
keep_image_login = true
762727
system_disk_type = "CLOUD_PREMIUM"
763728
}
@@ -872,40 +837,9 @@ resource "tencentcloud_instance" "foo" {
872837
availability_zone = data.tencentcloud_availability_zones.default.zones.0.name
873838
image_id = data.tencentcloud_images.default.images.0.image_id
874839
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
875-
hostname = var.instance_name
876840
system_disk_type = "CLOUD_PREMIUM"
877841
instance_charge_type = "SPOTPAID"
878842
spot_instance_type = "ONE-TIME"
879843
spot_max_price = "0.5"
880844
}
881845
`
882-
883-
const testAccTencentCloudInstancePrepaidRenew = defaultInstanceVariable + `
884-
resource "tencentcloud_instance" "foo" {
885-
instance_name = var.instance_name
886-
availability_zone = data.tencentcloud_availability_zones.default.zones.0.name
887-
image_id = data.tencentcloud_images.default.images.0.image_id
888-
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
889-
hostname = var.instance_name
890-
system_disk_type = "CLOUD_PREMIUM"
891-
instance_charge_type = "PREPAID"
892-
instance_charge_type_prepaid_renew_flag = "NOTIFY_AND_AUTO_RENEW"
893-
instance_charge_type_prepaid_period = 1
894-
force_delete = true
895-
}
896-
`
897-
898-
const testAccTencentCloudInstancePrepaidRenewUpdate = defaultInstanceVariable + `
899-
resource "tencentcloud_instance" "foo" {
900-
instance_name = var.instance_name
901-
availability_zone = data.tencentcloud_availability_zones.default.zones.0.name
902-
image_id = data.tencentcloud_images.default.images.0.image_id
903-
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
904-
hostname = var.instance_name
905-
system_disk_type = "CLOUD_PREMIUM"
906-
instance_charge_type = "PREPAID"
907-
instance_charge_type_prepaid_renew_flag = "NOTIFY_AND_MANUAL_RENEW"
908-
instance_charge_type_prepaid_period = 1
909-
force_delete = true
910-
}
911-
`

0 commit comments

Comments
 (0)