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 d4c446d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 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/.*"
}
12 changes: 10 additions & 2 deletions pkg/healthcheck/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,25 @@ func HealthCheck(ctx context.Context, data *HttpHealthArgs, diag *diag.Diagnosti
err = j.Parse(data.JSONPath)
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("ERROR PARSING JSONPATH EXPRESSION %v", err))
fmt.Printf(fmt.Sprintf("ERROR PARSING JSONPATH EXPRESSION %v\n", err))
return false
}
var respJSON interface{}
json.Unmarshal([]byte(data.ResultBody), &respJSON)
err = json.Unmarshal([]byte(data.ResultBody), &respJSON)
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("ERROR UNMARSHALLING JSON %v", err))
fmt.Printf(fmt.Sprintf("ERROR UNMARSHALLING JSON %v\n", err))
return false
}
buf := new(bytes.Buffer)
err = j.Execute(buf, respJSON)
fmt.Println(fmt.Sprintf("JSON: %+v\v", respJSON))
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("ERROR EXECUTING JSONPATH %v", err))
fmt.Printf(fmt.Sprintf("ERROR EXECUTING JSONPATH %v\n", err))
return false
}

fmt.Printf("JSONPATH RESULT: %s\n", buf.String())
re := regexp.MustCompile(data.JSONValue)
return re.MatchString(buf.String())
})
Expand Down
50 changes: 35 additions & 15 deletions pkg/provider/resource_http_health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
)

func TestAccHttpHealthResource(t *testing.T) {
// testUrl := "http://example.com"
timeout := 6000 // 6s
httpBin, envExists := os.LookupEnv("HTTPBIN")
if !envExists {
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", "{ .headers.Host }", "httpbin.org"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrWith("checkmate_http_health.test_jp", "passed", checkPassed),
),
},
{
Config: testJSONPath("test_jp_re", "{ .headers.User-Agent }", "Go-(http|https)-client.*"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrWith("checkmate_http_health.test_jp_re", "passed", checkPassed),
),
},
},
})
}
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, jsonpath, json_value string) string {
return fmt.Sprintf(`
resource "checkmate_http_health" %[1]q {
url = "http://httpbin.org/headers"
consecutive_successes = 1
method = "GET"
timeout = 1000 * 10
interval = 1000 * 2
jsonpath = %[2]q
json_value = %[3]q
}
`, name, 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 d4c446d

Please sign in to comment.