Skip to content

Commit

Permalink
testcase: Adds new unit test case for resource alicloud_alb_health_ch…
Browse files Browse the repository at this point in the history
…eck_template alicloud_slb_tls_cipher_policy alicloud_slb_ca_certificate
  • Loading branch information
thisnihonglei authored and xiaozhu36 committed Jan 25, 2022
1 parent 5d57b9d commit 4e5b2da
Show file tree
Hide file tree
Showing 7 changed files with 1,046 additions and 13 deletions.
8 changes: 4 additions & 4 deletions alicloud/resource_alicloud_alb_health_check_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ func resourceAlicloudAlbHealthCheckTemplateRead(d *schema.ResourceData, meta int
}
func resourceAlicloudAlbHealthCheckTemplateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
conn, err := client.NewAlbClient()
if err != nil {
return WrapError(err)
}
var response map[string]interface{}
update := false
request := map[string]interface{}{
Expand Down Expand Up @@ -333,10 +337,6 @@ func resourceAlicloudAlbHealthCheckTemplateUpdate(d *schema.ResourceData, meta i
request["DryRun"] = v
}
action := "UpdateHealthCheckTemplateAttribute"
conn, err := client.NewAlbClient()
if err != nil {
return WrapError(err)
}
request["ClientToken"] = buildClientToken("UpdateHealthCheckTemplateAttribute")
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
Expand Down
333 changes: 333 additions & 0 deletions alicloud/resource_alicloud_alb_health_check_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ package alicloud
import (
"fmt"
"log"
"os"
"reflect"
"strconv"
"strings"
"testing"
"time"

"github.com/agiledragon/gomonkey/v2"
"github.com/alibabacloud-go/tea/tea"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/stretchr/testify/assert"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"

"github.com/alibabacloud-go/tea-rpc/client"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -380,3 +390,326 @@ variable "name" {
}
`, name)
}

func TestAccAlicloudALBHealthCheckTemplate_unit(t *testing.T) {
p := Provider().(*schema.Provider).ResourcesMap
d, _ := schema.InternalMap(p["alicloud_alb_health_check_template"].Schema).Data(nil, nil)
dCreate, _ := schema.InternalMap(p["alicloud_alb_health_check_template"].Schema).Data(nil, nil)
dCreate.MarkNewResource()
for key, value := range map[string]interface{}{
"health_check_template_name": "health_check_template_name",
"health_check_protocol": "HTTP",
"dry_run": false,
"health_check_codes": []string{"http_3xx", "http_4xx"},
"health_check_connect_port": 8080,
"health_check_host": "www.test.com",
"health_check_http_version": "HTTP1.0",
"health_check_interval": 2,
"health_check_method": "GET",
"health_check_path": "/test",
"health_check_timeout": 50,
"healthy_threshold": 5,
"unhealthy_threshold": 5,
} {
err := dCreate.Set(key, value)
assert.Nil(t, err)
err = d.Set(key, value)
assert.Nil(t, err)
}
region := os.Getenv("ALICLOUD_REGION")
rawClient, err := sharedClientForRegion(region)
if err != nil {
t.Skipf("Skipping the test case with err: %s", err)
t.Skipped()
}
rawClient = rawClient.(*connectivity.AliyunClient)
ReadMockResponse := map[string]interface{}{
"HealthCheckTemplateId": "MockHealthCheckTemplateId",
"HealthCheckCodes": "health_check_codes",
"HealthCheckConnectPort": 8080,
"HealthCheckInterval": 2,
"HealthCheckProtocol": "HTTP",
"HealthCheckMethod": "GET",
"HealthCheckPath": "/test",
"HealthCheckHost": "www.test.com",
"HealthCheckHttpVersion": "HTTP1.0",
"HealthCheckTemplateName": "health_check_template_name",
"HealthCheckTimeout": 50,
"HealthyThreshold": 5,
"UnhealthyThreshold": 5,
}

responseMock := map[string]func(errorCode string) (map[string]interface{}, error){
"RetryError": func(errorCode string) (map[string]interface{}, error) {
return nil, &tea.SDKError{
Code: String(errorCode),
Data: String(errorCode),
Message: String(errorCode),
}
},
"NotFoundError": func(errorCode string) (map[string]interface{}, error) {
return nil, GetNotFoundErrorFromString(GetNotFoundMessage("alicloud_alb_health_check_template", "MockHealthCheckTemplateId"))
},
"NoRetryError": func(errorCode string) (map[string]interface{}, error) {
return nil, &tea.SDKError{
Code: String(errorCode),
Data: String(errorCode),
Message: String(errorCode),
}
},
"CreateNormal": func(errorCode string) (map[string]interface{}, error) {
result := ReadMockResponse
result["HealthCheckTemplateId"] = "MockHealthCheckTemplateId"
return result, nil
},
"UpdateNormal": func(errorCode string) (map[string]interface{}, error) {
result := ReadMockResponse
return result, nil
},
"DeleteNormal": func(errorCode string) (map[string]interface{}, error) {
result := ReadMockResponse
return result, nil
},
"ReadNormal": func(errorCode string) (map[string]interface{}, error) {
result := ReadMockResponse
return result, nil
},
}
// Create
t.Run("CreateClientAbnormal", func(t *testing.T) {
patches := gomonkey.ApplyMethod(reflect.TypeOf(&connectivity.AliyunClient{}), "NewAlbClient", func(_ *connectivity.AliyunClient) (*client.Client, error) {
return nil, &tea.SDKError{
Code: String("loadEndpoint error"),
Data: String("loadEndpoint error"),
Message: String("loadEndpoint error"),
}
})
err := resourceAlicloudAlbHealthCheckTemplateCreate(d, rawClient)
patches.Reset()
assert.NotNil(t, err)
})
t.Run("CreateAbnormal", func(t *testing.T) {
retryFlag := true
noRetryFlag := true
patches := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
if retryFlag {
retryFlag = false
return responseMock["RetryError"]("IdempotenceProcessing")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["CreateNormal"]("")
})
err := resourceAlicloudAlbHealthCheckTemplateCreate(d, rawClient)
patches.Reset()
assert.NotNil(t, err)
})
t.Run("CreateNormal", func(t *testing.T) {
retryFlag := false
noRetryFlag := false
patches := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
if retryFlag {
retryFlag = false
return responseMock["RetryError"]("Throttling")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["CreateNormal"]("")
})
err := resourceAlicloudAlbHealthCheckTemplateCreate(dCreate, rawClient)
patches.Reset()
assert.Nil(t, err)
})

// Set ID for Update and Delete Method
d.SetId("MockHealthCheckTemplateId")
// Update
t.Run("UpdateClientAbnormal", func(t *testing.T) {
patches := gomonkey.ApplyMethod(reflect.TypeOf(&connectivity.AliyunClient{}), "NewAlbClient", func(_ *connectivity.AliyunClient) (*client.Client, error) {
return nil, &tea.SDKError{
Code: String("loadEndpoint error"),
Data: String("loadEndpoint error"),
Message: String("loadEndpoint error"),
}
})

err := resourceAlicloudAlbHealthCheckTemplateUpdate(d, rawClient)
patches.Reset()
assert.NotNil(t, err)
})

t.Run("UpdateHealthCheckTemplateAttributeAbnormal", func(t *testing.T) {
diff := terraform.NewInstanceDiff()
for _, key := range []string{"health_check_codes", "health_check_connect_port", "health_check_host", "health_check_http_version", "health_check_interval", "health_check_method", "health_check_path", "health_check_protocol", "health_check_template_name", "health_check_timeout", "healthy_threshold", "unhealthy_threshold", "dry_run"} {
switch p["alicloud_alb_health_check_template"].Schema[key].Type {
case schema.TypeString:
diff.SetAttribute(key, &terraform.ResourceAttrDiff{Old: d.Get(key).(string), New: d.Get(key).(string) + "_update"})
case schema.TypeBool:
diff.SetAttribute(key, &terraform.ResourceAttrDiff{Old: strconv.FormatBool(d.Get(key).(bool)), New: strconv.FormatBool(true)})
case schema.TypeInt:
diff.SetAttribute(key, &terraform.ResourceAttrDiff{Old: strconv.Itoa(d.Get(key).(int)), New: strconv.Itoa(3)})
case schema.TypeMap:
diff.SetAttribute("tags.%", &terraform.ResourceAttrDiff{Old: "0", New: "2"})
diff.SetAttribute("tags.For", &terraform.ResourceAttrDiff{Old: "", New: "Test"})
diff.SetAttribute("tags.Created", &terraform.ResourceAttrDiff{Old: "", New: "TF"})
case schema.TypeList:
diff.SetAttribute("health_check_codes.#", &terraform.ResourceAttrDiff{Old: "0", New: "2"})
diff.SetAttribute("health_check_codes.0", &terraform.ResourceAttrDiff{Old: "", New: "http_3xx"})
diff.SetAttribute("health_check_codes.1", &terraform.ResourceAttrDiff{Old: "", New: "http_4xx"})
}
}
resourceData1, _ := schema.InternalMap(p["alicloud_alb_health_check_template"].Schema).Data(nil, diff)
resourceData1.SetId(d.Id())
retryFlag := true
noRetryFlag := true
patches := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
if retryFlag {
retryFlag = false
return responseMock["RetryError"]("IdempotenceProcessing")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["UpdateNormal"]("")
})
err := resourceAlicloudAlbHealthCheckTemplateUpdate(resourceData1, rawClient)
patches.Reset()
assert.NotNil(t, err)
})

t.Run("UpdateHealthCheckTemplateAttributeNormal", func(t *testing.T) {
diff := terraform.NewInstanceDiff()
for _, key := range []string{"health_check_codes", "health_check_connect_port", "health_check_host", "health_check_http_version", "health_check_interval", "health_check_method", "health_check_path", "health_check_protocol", "health_check_template_name", "health_check_timeout", "healthy_threshold", "unhealthy_threshold", "dry_run"} {
switch p["alicloud_alb_health_check_template"].Schema[key].Type {
case schema.TypeString:
diff.SetAttribute(key, &terraform.ResourceAttrDiff{Old: d.Get(key).(string), New: d.Get(key).(string) + "_update"})
case schema.TypeBool:
diff.SetAttribute(key, &terraform.ResourceAttrDiff{Old: strconv.FormatBool(d.Get(key).(bool)), New: strconv.FormatBool(true)})
case schema.TypeInt:
diff.SetAttribute(key, &terraform.ResourceAttrDiff{Old: strconv.Itoa(d.Get(key).(int)), New: strconv.Itoa(3)})
case schema.TypeMap:
diff.SetAttribute("tags.%", &terraform.ResourceAttrDiff{Old: "0", New: "2"})
diff.SetAttribute("tags.For", &terraform.ResourceAttrDiff{Old: "", New: "Test"})
diff.SetAttribute("tags.Created", &terraform.ResourceAttrDiff{Old: "", New: "TF"})
}

}
resourceData1, _ := schema.InternalMap(p["alicloud_alb_health_check_template"].Schema).Data(nil, diff)
resourceData1.SetId(d.Id())
retryFlag := false
noRetryFlag := false
patches := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
if retryFlag {
retryFlag = false
return responseMock["RetryError"]("Throttling")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["UpdateNormal"]("")
})
err := resourceAlicloudAlbHealthCheckTemplateUpdate(resourceData1, rawClient)
patches.Reset()
assert.Nil(t, err)
})

// Delete
t.Run("DeleteClientAbnormal", func(t *testing.T) {
patches := gomonkey.ApplyMethod(reflect.TypeOf(&connectivity.AliyunClient{}), "NewAlbClient", func(_ *connectivity.AliyunClient) (*client.Client, error) {
return nil, &tea.SDKError{
Code: String("loadEndpoint error"),
Data: String("loadEndpoint error"),
Message: String("loadEndpoint error"),
}
})
err := resourceAlicloudAlbHealthCheckTemplateDelete(d, rawClient)
patches.Reset()
assert.NotNil(t, err)
})
t.Run("DeleteMockAbnormal", func(t *testing.T) {
retryFlag := true
noRetryFlag := true
patches := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
if retryFlag {
retryFlag = false
return responseMock["RetryError"]("IdempotenceProcessing")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["DeleteNormal"]("")
})
err := resourceAlicloudAlbHealthCheckTemplateDelete(d, rawClient)
patches.Reset()
assert.NotNil(t, err)
})
t.Run("DeleteMockNormal", func(t *testing.T) {
retryFlag := false
noRetryFlag := false
patches := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
if retryFlag {
retryFlag = false
return responseMock["RetryError"]("Throttling")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["DeleteNormal"]("")
})
err := resourceAlicloudAlbHealthCheckTemplateDelete(d, rawClient)
patches.Reset()
assert.Nil(t, err)
})

t.Run("DeleteNonRetryableError", func(t *testing.T) {
retryFlag := false
noRetryFlag := true
patches := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
if retryFlag {
return responseMock["RetryError"]("Throttling")
} else if noRetryFlag {
noRetryFlag = false
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["DeleteNormal"]("")
})
err := resourceAlicloudAlbHealthCheckTemplateDelete(d, rawClient)
patches.Reset()
assert.NotNil(t, err)
})

//Read
t.Run("ReadDescribeAlbHealthCheckTemplateNotFound", func(t *testing.T) {
patcheDorequest := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
NotFoundFlag := true
noRetryFlag := false
if NotFoundFlag {
return responseMock["NotFoundError"]("ResourceNotfound")
} else if noRetryFlag {
return responseMock["NoRetryError"]("NoRetryError")
}
return responseMock["ReadNormal"]("")
})
err := resourceAlicloudAlbHealthCheckTemplateRead(d, rawClient)
patcheDorequest.Reset()
assert.Nil(t, err)
})

t.Run("ReadDescribeAlbHealthCheckTemplateAbnormal", func(t *testing.T) {
patcheDorequest := gomonkey.ApplyMethod(reflect.TypeOf(&client.Client{}), "DoRequest", func(_ *client.Client, _ *string, _ *string, _ *string, _ *string, _ *string, _ map[string]interface{}, _ map[string]interface{}, _ *util.RuntimeOptions) (map[string]interface{}, error) {
retryFlag := false
noRetryFlag := true
if retryFlag {
return responseMock["RetryError"]("Throttling")
} else if noRetryFlag {
return responseMock["NoRetryError"]("NonRetryableError")
}
return responseMock["ReadNormal"]("")
})
err := resourceAlicloudAlbHealthCheckTemplateRead(d, rawClient)
patcheDorequest.Reset()
assert.NotNil(t, err)
})
}
8 changes: 4 additions & 4 deletions alicloud/resource_alicloud_slb_ca_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func resourceAlicloudSlbCaCertificateRead(d *schema.ResourceData, meta interface
func resourceAlicloudSlbCaCertificateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
slbService := SlbService{client}
conn, err := client.NewSlbClient()
if err != nil {
return WrapError(err)
}
var response map[string]interface{}
d.Partial(true)

Expand All @@ -141,10 +145,6 @@ func resourceAlicloudSlbCaCertificateUpdate(d *schema.ResourceData, meta interfa
request["RegionId"] = client.RegionId
if update {
action := "SetCACertificateName"
conn, err := client.NewSlbClient()
if err != nil {
return WrapError(err)
}
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2014-05-15"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
Expand Down

0 comments on commit 4e5b2da

Please sign in to comment.