export TF_ACC=true
export TENCENTCLOUD_SECRET_ID=xxxxexport TENCENTCLOUD_SECRET_KEY=yyyy
export TF_LOG=DEBUGexport TF_LOG_PATH=./terraform.log
resource_tc_vpc_test.go file in the tencentcloud directory, specify a VPC resource, and write its initial configuration and updated configuration:// filename: tencentcloud/resource_tc_vpc_test.goconst testAccVpcConfig = `resource "tencentcloud_vpc" "foo" {name = "test-vpc"cidr_block = "172.16.0.0/16"}`const testAccVpcConfigUpdate = `resource "tencentcloud_vpc" "foo" {name = "test-vpc__update"cidr_block = "172.16.0.0/22"is_multicast = true}`
TestAccTencentCloud indicates an acceptance test. The function name format is TestAccTencentCloud${Module name}${Resource type}_${Subname}, and the regex is TestAccTencentCloud[a-zA-Z]+(Resource|DataSource)_[a-zA-Z]+. Call the function to import the two configuration items and add the assertions in resource.Test:package tencentcloudimport ("testing""github.com/hashicorp/terraform-plugin-sdk/helper/resource""github.com/hashicorp/terraform-plugin-sdk/terraform")func TestAccTencentCloudVpcResource_Basic(t *testing.T) {resource.Test(t, resource.TestCase{Providers: testAccProviders,Steps: []resource.TestStep{{// The initial configuration declared aboveConfig: testAccVpcConfig,Check: resource.ComposeTestCheckFunc(testAccCheckVpcExists("tencentcloud_vpc.foo"),resource.TestCheckResourceAttr("tencentcloud_vpc.foo", "cidr_block", "172.16.0.0/16"),resource.TestCheckResourceAttr("tencentcloud_vpc.foo", "name", "test-vpc"),),},{// The updated configuration declared aboveConfig: testAccVpcConfig,Check: resource.ComposeTestCheckFunc(testAccCheckVpcExists("tencentcloud_vpc.foo"),resource.TestCheckResourceAttr("tencentcloud_vpc.foo", "cidr_block", "172.16.0.0/22"),resource.TestCheckResourceAttr("tencentcloud_vpc.foo", "name", "test-vpc__update"),resource.TestCheckResourceAttr("tencentcloud_vpc.foo", "is_multicast", "true"),),},},})}
go test -v -run TestAccTencentCloudVpcResource ./tencentcloud in the root directory of the project to check the test result:TestAccTencentCloudVpcResource_Basic=== RUN TestAccTencentCloudVpcResource_Basic=== PAUSE TestAccTencentCloudVpcResource_Basic=== CONT TestAccTencentCloudVpcResource_Basic--- PASS: TestAccTencentCloudVpcResource_Basic (26.30s)PASSok github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud 27.153s
tencentcloud/terraform.log.isExpectError(err) function to check the TencentCloud API error code in the project in order to decide whether the program should retry (due to unstable client network connection, for example) or exit abnormally, write the following test case starting with Test*:package tencentcloudimport ("testing"sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors""github.com/stretchr/testify/assert")func TestIsExpectError(t *testing.T) {err := sdkErrors.NewTencentCloudSDKError("ClientError.NetworkError", "", "")// ExpectedexpectedFull := []string{"ClientError.NetworkError"}expectedShort := []string{"ClientError"}assert.Equalf(t, isExpectError(err, expectedFull), true, "")assert.Equalf(t, isExpectError(err, expectedShort), true, "")// UnexpectedunExpectedMatchHead := []string{"ClientError.HttpStatusCodeError"}unExpectedShort := []string{"SystemError"}assert.Equalf(t, isExpectError(err, unExpectedMatchHead), false, "")assert.Equalf(t, isExpectError(err, unExpectedShort), false, "")}
go test -v -run TestIsExpectError ./tencentcloud in the root directory of the project to view the result:=== RUN TestIsExpectError--- PASS: TestIsExpectError (0.00s)PASSok github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud 1.312s
init function to tencentcloud/resource_tc_vpc_test.go and register a sweeper named tencentcloud_vpc:func init() {resource.AddTestSweepers("tencentcloud_vpc", &resource.Sweeper{Name: "tencentcloud_vpc",F: testSweepVpcInstance,})}// Pseudocode logic for implementing the cleanup of VPC resources starting with `test` in a specified regionfunc testSweepVpcInstance(region string) {vpcs := getAllVpc(region)for _, vpc in range vpcs {if vpc.name == "test" {deleteVpc(vpc.id)}}}
go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_vpc. Then, the SDK for Terraform will match the tencentcloud_vpc sweeper, pass in the region specified by -sweep to the function, and execute the function to clean up the resources in the region.Feedback