Skip to content

Commit

Permalink
doc and test
Browse files Browse the repository at this point in the history
  • Loading branch information
chirauki committed Feb 21, 2024
1 parent 0e34afc commit 4fc5180
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 31 deletions.
22 changes: 22 additions & 0 deletions examples/resources/checkmate_http_health/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,25 @@ resource "checkmate_http_health" "example_insecure_tls" {
consecutive_successes = 2
insecure_tls = true
}

resource "checkmate_http_health" "example_json_path" {
url = "https://httpbin.org/headers"
request_timeout = 1000
method = "GET"
interval = 1
status_code = 200
consecutive_successes = 2
jsonpath = "{ .Host }"
json_value = "httpbin.org"
}

resource "checkmate_http_health" "example_json_path_regex" {
url = "https://httpbin.org/headers"
request_timeout = 1000
method = "GET"
interval = 1
status_code = 200
consecutive_successes = 2
jsonpath = "{ .User-Agent }"
json_value = "curl/.*"
}
37 changes: 22 additions & 15 deletions pkg/healthcheck/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,30 @@ func HealthCheck(ctx context.Context, data *HttpHealthArgs, diag *diag.Diagnosti
}

// Check JSONPath
j := jsonpath.New("parser")
err = j.Parse(data.JSONPath)
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("ERROR PARSING JSONPATH EXPRESSION %v", err))
return false
}
var respJSON interface{}
json.Unmarshal([]byte(data.ResultBody), &respJSON)
buf := new(bytes.Buffer)
err = j.Execute(buf, respJSON)
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("ERROR EXECUTING JSONPATH %v", err))
return false
if data.JSONPath != "" && data.JSONValue != "" {
j := jsonpath.New("parser")
err = j.Parse(data.JSONPath)
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("ERROR PARSING JSONPATH EXPRESSION %v", err))
return false
}
var respJSON interface{}
err = json.Unmarshal([]byte(data.ResultBody), &respJSON)
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("ERROR UNMARSHALLING JSON %v", err))
return false
}
buf := new(bytes.Buffer)
err = j.Execute(buf, respJSON)
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("ERROR EXECUTING JSONPATH %v", err))
return false
}
re := regexp.MustCompile(data.JSONValue)
return re.MatchString(buf.String())
}

re := regexp.MustCompile(data.JSONValue)
return re.MatchString(buf.String())
return success
})

switch result {
Expand Down
52 changes: 36 additions & 16 deletions pkg/provider/resource_http_health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ import (
)

func TestAccHttpHealthResource(t *testing.T) {
// testUrl := "http://example.com"
timeout := 6000 // 6s
httpBin, envExists := os.LookupEnv("HTTPBIN")
if !envExists {
httpBin = "https://httpbin.org"
httpBin = "https://httpbin.platform.tetrate.com"
}
url200 := httpBin + "/status/200"
urlPost := httpBin + "/post"
Expand Down Expand Up @@ -58,20 +57,18 @@ func TestAccHttpHealthResource(t *testing.T) {
resource.TestCheckResourceAttrWith("checkmate_http_health.test_post", "result_body", checkResponse("hello")),
),
},
// ImportState testing
// {
// ResourceName: "checkmate_http_health.test",
// ImportState: true,
// ImportStateVerify: true,
// },
// Update and Read testing
// {
// Config: testAccHttpHealthResourceConfig(testUrl2),
// Check: resource.ComposeAggregateTestCheckFunc(
// resource.TestCheckResourceAttr("checkmate_http_health.test", "url", testUrl2),
// ),
// },
// Delete testing automatically occurs in TestCase
{
Config: testJSONPath("test_jp", "https://httpbin.platform.tetrate.com/headers", "{ .headers.Host }", "httpbin.platform.tetrate.com"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("checkmate_http_health.test_jp", "passed", "true"),
),
},
{
Config: testJSONPath("test_jp_re", urlHeaders, "{ .headers.User-Agent }", "Go-(http|https)-client.*"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("checkmate_http_health.test_jp_re", "passed", "true"),
),
},
},
})
}
Expand Down Expand Up @@ -122,6 +119,7 @@ resource "checkmate_http_health" %[1]q {
}
`, name, url, timeout)
}

func testAccHttpHealthResourceConfigWithBody(name string, url string, body string, timeout int) string {
return fmt.Sprintf(`
resource "checkmate_http_health" %[1]q {
Expand All @@ -138,6 +136,21 @@ resource "checkmate_http_health" %[1]q {

}

func testJSONPath(name string, url, jsonpath, json_value string) string {
return fmt.Sprintf(`
resource "checkmate_http_health" %[1]q {
url = %[2]q
consecutive_successes = 1
method = "GET"
timeout = 1000 * 10
interval = 1000 * 2
jsonpath = %[3]q
json_value = %[4]q
}
`, name, url, jsonpath, json_value)

}

func checkHeader(key string, value string) func(string) error {
return func(responseBody string) error {
var parsed map[string]map[string]string
Expand Down Expand Up @@ -169,3 +182,10 @@ func checkResponse(value string) func(string) error {
return fmt.Errorf("Bad response from httpbin")
}
}

func checkPassed(value string) error {
if value == "true" {
return nil
}
return fmt.Errorf("test did not pass")
}

0 comments on commit 4fc5180

Please sign in to comment.