Skip to content

Commit 2bba8e1

Browse files
authored
Feat/add test cases (#1228)
* style: go fmt * feat: add template ut * fix: update template ut * feat: add alert rule ut * fix: update alert rule ut * feat: add exporter integration ut * style: make fmt * feat: add instance ut * feat: add scrape job ut * feat: add recording rule ut * fix: update ut * feat: add alert policy ut * fix: update ut Co-authored-by: arunma <arunma@tencent.com>
1 parent 4311aaf commit 2bba8e1

20 files changed

+1295
-20
lines changed

tencentcloud/basic_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ const (
178178
defaultTemplateId = "temp-gqunlvo1"
179179
tkeClusterIdAgent = "cls-87o4klby"
180180
tkeClusterTypeAgent = "eks"
181+
defaultAgentId = "agent-q3zy8gt8"
181182
)
182183

183184
/*

tencentcloud/common.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tencentcloud
22

33
import (
44
"context"
5+
"encoding/base64"
56
"encoding/json"
67
"fmt"
78
"io/ioutil"
@@ -349,3 +350,17 @@ func YamlParser(config string) (map[interface{}]interface{}, error) {
349350
}
350351
return m, nil
351352
}
353+
354+
func YamlToBase64(config string) string {
355+
m := []byte(config)
356+
encodedStr := base64.StdEncoding.EncodeToString(m)
357+
return encodedStr
358+
}
359+
360+
func Base64ToYaml(config string) (string, error) {
361+
yamlConfig, err := base64.StdEncoding.DecodeString(config)
362+
if err != nil {
363+
return "", err
364+
}
365+
return string(yamlConfig), nil
366+
}

tencentcloud/resource_tc_kubernetes_addon_attachment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ package tencentcloud
8080
import (
8181
"context"
8282
"fmt"
83-
tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525"
8483
"log"
8584
"strings"
8685

86+
tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525"
87+
8788
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8889
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
8990
)

tencentcloud/resource_tc_monitor_tmp_alert_rule.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,21 @@ func resourceTencentCloudMonitorTmpAlertRuleRead(d *schema.ResourceData, meta in
245245
return fmt.Errorf("resource `tmpAlertRule` %s does not exist", ids[1])
246246
}
247247

248+
_ = d.Set("instance_id", ids[0])
248249
if tmpAlertRule.RuleName != nil {
249250
_ = d.Set("rule_name", tmpAlertRule.RuleName)
250251
}
251252
if tmpAlertRule.Expr != nil {
252253
_ = d.Set("expr", tmpAlertRule.Expr)
253254
}
254-
//if tmpAlertRule.Receivers != nil {
255-
// _ = d.Set("receivers", tmpAlertRule.Receivers)
256-
//}
255+
if tmpAlertRule.Receivers != nil {
256+
list := tmpAlertRule.Receivers
257+
result := make([]string, 0, len(list))
258+
for _, v := range list {
259+
result = append(result, *v)
260+
}
261+
_ = d.Set("receivers", result)
262+
}
257263
if tmpAlertRule.RuleState != nil {
258264
_ = d.Set("rule_state", tmpAlertRule.RuleState)
259265
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package tencentcloud
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
11+
)
12+
13+
func TestAccTencentCloudMonitorAlertRule_basic(t *testing.T) {
14+
t.Parallel()
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_COMMON) },
17+
Providers: testAccProviders,
18+
CheckDestroy: testAccCheckAlertRuleDestroy,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAlertRule_basic,
22+
Check: resource.ComposeTestCheckFunc(
23+
testAccCheckAlertRuleExists("tencentcloud_monitor_tmp_alert_rule.basic"),
24+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "rule_name", "test-rule_name"),
25+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "receivers.#", "1"),
26+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "expr", "increase(mysql_global_status_slow_queries[1m]) > 0"),
27+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "duration", "4m"),
28+
),
29+
},
30+
{
31+
Config: testAlertRule_update,
32+
Check: resource.ComposeTestCheckFunc(
33+
testAccCheckAlertRuleExists("tencentcloud_monitor_tmp_alert_rule.basic"),
34+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "rule_name", "test-rule_name_update"),
35+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "receivers.#", "1"),
36+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "expr", "increase(mysql_global_status_slow_queries[1m]) > 1"),
37+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_alert_rule.basic", "duration", "2m"),
38+
),
39+
},
40+
{
41+
ResourceName: "tencentcloud_monitor_tmp_alert_rule.basic",
42+
ImportState: true,
43+
ImportStateVerify: true,
44+
},
45+
},
46+
})
47+
}
48+
49+
func testAccCheckAlertRuleDestroy(s *terraform.State) error {
50+
logId := getLogId(contextNil)
51+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
52+
service := MonitorService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
53+
for _, rs := range s.RootModule().Resources {
54+
if rs.Type != "tencentcloud_monitor_tmp_tke_alert_rule" {
55+
continue
56+
}
57+
if rs.Primary.ID == "" {
58+
return fmt.Errorf("resource id is not set")
59+
}
60+
ids := strings.Split(rs.Primary.ID, FILED_SP)
61+
if len(ids) != 2 {
62+
return fmt.Errorf("id is broken, id is %s", rs.Primary.ID)
63+
}
64+
65+
instance, err := service.DescribeMonitorTmpAlertRuleById(ctx, ids[0], ids[1])
66+
if err != nil {
67+
return err
68+
}
69+
70+
if instance != nil {
71+
return fmt.Errorf("instance %s still exists", rs.Primary.ID)
72+
}
73+
}
74+
75+
return nil
76+
}
77+
78+
func testAccCheckAlertRuleExists(r string) resource.TestCheckFunc {
79+
return func(s *terraform.State) error {
80+
logId := getLogId(contextNil)
81+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
82+
83+
rs, ok := s.RootModule().Resources[r]
84+
if !ok {
85+
return fmt.Errorf("resource %s is not found", r)
86+
}
87+
if rs.Primary.ID == "" {
88+
return fmt.Errorf("resource id is not set")
89+
}
90+
ids := strings.Split(rs.Primary.ID, FILED_SP)
91+
if len(ids) != 2 {
92+
return fmt.Errorf("id is broken, id is %s", rs.Primary.ID)
93+
}
94+
95+
service := MonitorService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
96+
instance, err := service.DescribeMonitorTmpAlertRuleById(ctx, ids[0], ids[1])
97+
if err != nil {
98+
return err
99+
}
100+
101+
if instance == nil {
102+
return fmt.Errorf("instance %s is not found", rs.Primary.ID)
103+
}
104+
105+
return nil
106+
}
107+
}
108+
109+
const testAlertRuleVar = `
110+
variable "prometheus_id" {
111+
default = "` + defaultPrometheusId + `"
112+
}
113+
`
114+
const testAlertRule_basic = testAlertRuleVar + `
115+
resource "tencentcloud_monitor_tmp_alert_rule" "basic" {
116+
instance_id = var.prometheus_id
117+
rule_name = "test-rule_name"
118+
receivers = ["Consumer-6vkna7pevq"]
119+
expr = "increase(mysql_global_status_slow_queries[1m]) > 0"
120+
duration = "4m"
121+
rule_state = 2
122+
}`
123+
124+
const testAlertRule_update = testAlertRuleVar + `
125+
resource "tencentcloud_monitor_tmp_alert_rule" "basic" {
126+
instance_id = var.prometheus_id
127+
rule_name = "test-rule_name_update"
128+
receivers = ["Consumer-6vkna7pevq"]
129+
expr = "increase(mysql_global_status_slow_queries[1m]) > 1"
130+
duration = "2m"
131+
rule_state = 2
132+
}`

tencentcloud/resource_tc_monitor_tmp_exporter_integration.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func resourceTencentCloudMonitorTmpExporterIntegrationCreate(d *schema.ResourceD
7878
instanceId string
7979
kubeType int
8080
clusterId string
81+
kind string
8182
)
8283

8384
var (
@@ -91,6 +92,7 @@ func resourceTencentCloudMonitorTmpExporterIntegrationCreate(d *schema.ResourceD
9192
}
9293

9394
if v, ok := d.GetOk("kind"); ok {
95+
kind = v.(string)
9496
request.Kind = helper.String(v.(string))
9597
}
9698

@@ -127,7 +129,7 @@ func resourceTencentCloudMonitorTmpExporterIntegrationCreate(d *schema.ResourceD
127129

128130
tmpExporterIntegrationId := *response.Response.Names[0]
129131

130-
d.SetId(strings.Join([]string{tmpExporterIntegrationId, instanceId, strconv.Itoa(kubeType), clusterId}, FILED_SP))
132+
d.SetId(strings.Join([]string{tmpExporterIntegrationId, instanceId, strconv.Itoa(kubeType), clusterId, kind}, FILED_SP))
131133

132134
return resourceTencentCloudMonitorTmpExporterIntegrationRead(d, meta)
133135
}
@@ -225,5 +227,19 @@ func resourceTencentCloudMonitorTmpExporterIntegrationDelete(d *schema.ResourceD
225227
return err
226228
}
227229

230+
err := resource.Retry(2*readRetryTimeout, func() *resource.RetryError {
231+
tmpExporterIntegration, errRet := service.DescribeMonitorTmpExporterIntegration(ctx, tmpExporterIntegrationId)
232+
if errRet != nil {
233+
return retryError(errRet, InternalError)
234+
}
235+
if tmpExporterIntegration == nil {
236+
return nil
237+
}
238+
return resource.RetryableError(fmt.Errorf("exporter integration status is %v, retry...", *tmpExporterIntegration.Status))
239+
})
240+
if err != nil {
241+
return err
242+
}
243+
228244
return nil
229245
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package tencentcloud
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
10+
)
11+
12+
func TestAccTencentCloudMonitorExporterIntegration_basic(t *testing.T) {
13+
t.Parallel()
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_COMMON) },
16+
Providers: testAccProviders,
17+
CheckDestroy: testAccCheckExporterIntegrationDestroy,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: testExporterIntegration_basic,
21+
Check: resource.ComposeTestCheckFunc(
22+
testAccCheckExporterIntegrationExists("tencentcloud_monitor_tmp_exporter_integration.basic"),
23+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_exporter_integration.basic", "kind", "cvm-http-sd-exporter"),
24+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_exporter_integration.basic", "kube_type", "1"),
25+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_exporter_integration.basic", "cluster_id", "cls-ely08ic4"),
26+
),
27+
},
28+
{
29+
Config: testExporterIntegration_update,
30+
Check: resource.ComposeTestCheckFunc(
31+
testAccCheckExporterIntegrationExists("tencentcloud_monitor_tmp_exporter_integration.basic"),
32+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_exporter_integration.basic", "kind", "cvm-http-sd-exporter"),
33+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_exporter_integration.basic", "kube_type", "1"),
34+
resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_exporter_integration.basic", "cluster_id", "cls-87o4klby"),
35+
),
36+
},
37+
},
38+
})
39+
}
40+
41+
func testAccCheckExporterIntegrationDestroy(s *terraform.State) error {
42+
logId := getLogId(contextNil)
43+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
44+
service := MonitorService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
45+
for _, rs := range s.RootModule().Resources {
46+
if rs.Type != "tencentcloud_monitor_tmp_exporter_integration" {
47+
continue
48+
}
49+
if rs.Primary.ID == "" {
50+
return fmt.Errorf("resource id is not set")
51+
}
52+
53+
instance, err := service.DescribeMonitorTmpExporterIntegration(ctx, rs.Primary.ID)
54+
if err != nil {
55+
return err
56+
}
57+
58+
if instance != nil {
59+
return fmt.Errorf("ExporterIntegration %s still exists", rs.Primary.ID)
60+
}
61+
}
62+
63+
return nil
64+
}
65+
66+
func testAccCheckExporterIntegrationExists(r string) resource.TestCheckFunc {
67+
return func(s *terraform.State) error {
68+
logId := getLogId(contextNil)
69+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
70+
71+
rs, ok := s.RootModule().Resources[r]
72+
if !ok {
73+
return fmt.Errorf("resource %s is not found", r)
74+
}
75+
if rs.Primary.ID == "" {
76+
return fmt.Errorf("resource id is not set")
77+
}
78+
79+
service := MonitorService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
80+
tmpExporterIntegration, err := service.DescribeMonitorTmpExporterIntegration(ctx, rs.Primary.ID)
81+
if err != nil {
82+
return err
83+
}
84+
85+
if tmpExporterIntegration == nil {
86+
return fmt.Errorf("ExporterIntegration %s is not found", rs.Primary.ID)
87+
}
88+
89+
return nil
90+
}
91+
}
92+
93+
const testExporterIntegrationVar = `
94+
variable "prometheus_id" {
95+
default = "` + defaultPrometheusId + `"
96+
}
97+
variable "default_cluster" {
98+
default = "` + defaultTkeClusterId + `"
99+
}
100+
variable "cluster_id" {
101+
default = "` + tkeClusterIdAgent + `"
102+
}
103+
`
104+
const testExporterIntegration_basic = testExporterIntegrationVar + `
105+
resource "tencentcloud_monitor_tmp_exporter_integration" "basic" {
106+
instance_id = var.prometheus_id
107+
kind = "cvm-http-sd-exporter"
108+
content = "{\"kind\":\"cvm-http-sd-exporter\",\"spec\":{\"job\":\"job_name: example-job-name\\nmetrics_path: /metrics\\ncvm_sd_configs:\\n- region: ap-guangzhou\\n ports:\\n - 9100\\n filters: \\n - name: tag:示例标签键\\n values: \\n - 示例标签值\\nrelabel_configs: \\n- source_labels: [__meta_cvm_instance_state]\\n regex: RUNNING\\n action: keep\\n- regex: __meta_cvm_tag_(.*)\\n replacement: $1\\n action: labelmap\\n- source_labels: [__meta_cvm_region]\\n target_label: region\\n action: replace\"}}"
109+
kube_type = 1
110+
cluster_id = var.default_cluster
111+
}`
112+
113+
const testExporterIntegration_update = testExporterIntegrationVar + `
114+
resource "tencentcloud_monitor_tmp_exporter_integration" "basic" {
115+
instance_id = var.prometheus_id
116+
kind = "cvm-http-sd-exporter"
117+
content = "{\"kind\":\"cvm-http-sd-exporter\",\"spec\":{\"job\":\"job_name: example-job-name\\nmetrics_path: /metrics\\ncvm_sd_configs:\\n- region: ap-guangzhou\\n ports:\\n - 9100\\n filters: \\n - name: tag:示例标签键\\n values: \\n - 示例标签值\\nrelabel_configs: \\n- source_labels: [__meta_cvm_instance_state]\\n regex: RUNNING\\n action: keep\\n- regex: __meta_cvm_tag_(.*)\\n replacement: $1\\n action: labelmap\\n- source_labels: [__meta_cvm_region]\\n target_label: region\\n action: replace\"}}"
118+
kube_type = 1
119+
cluster_id = var.cluster_id
120+
}`

0 commit comments

Comments
 (0)