Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apiChecks): For status code >399 checks should fail #19

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/checkly/apicheck_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
checklyID, err := external.Create(internalCheck, r.ApiClient)
if err != nil {
logger.Error(err, "Failed to create checkly alert")
return ctrl.Result{}, nil
return ctrl.Result{}, err
}

// Update the custom resource Status with the returned ID
Expand Down
32 changes: 28 additions & 4 deletions external/checkly/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package external
import (
"context"
"net/http"
"strconv"
"time"

"github.com/checkly/checkly-go-sdk"
Expand All @@ -37,7 +38,12 @@ type Check struct {
Muted bool
}

func checklyCheck(apiCheck Check) (check checkly.Check) {
func checklyCheck(apiCheck Check) (check checkly.Check, err error) {

shouldFail, err := shouldFail(apiCheck.SuccessCode)
if err != nil {
return
}

alertSettings := checkly.AlertSettings{
EscalationType: checkly.RunBased,
Expand All @@ -64,7 +70,7 @@ func checklyCheck(apiCheck Check) (check checkly.Check) {
MaxResponseTime: checkValueInt(apiCheck.MaxResponseTime, 15000),
Activated: true,
Muted: apiCheck.Muted, // muted for development
ShouldFail: false,
ShouldFail: shouldFail,
DoubleCheck: false,
SSLCheck: false,
LocalSetupScript: "",
Expand Down Expand Up @@ -110,7 +116,10 @@ func checklyCheck(apiCheck Check) (check checkly.Check) {
// Create creates a new checklyhq.com check
func Create(apiCheck Check, client checkly.Client) (ID string, err error) {

check := checklyCheck(apiCheck)
check, err := checklyCheck(apiCheck)
if err != nil {
return
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
Expand All @@ -128,7 +137,10 @@ func Create(apiCheck Check, client checkly.Client) (ID string, err error) {
// Update updates an existing checklyhq.com check
func Update(apiCheck Check, client checkly.Client) (err error) {

check := checklyCheck(apiCheck)
check, err := checklyCheck(apiCheck)
if err != nil {
return
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
Expand All @@ -148,3 +160,15 @@ func Delete(ID string, client checkly.Client) (err error) {

return
}

func shouldFail(successCode string) (bool, error) {
code, err := strconv.Atoi(successCode)
if err != nil {
return false, err
}
if code < 400 {
return false, nil
} else {
return true, nil
}
}
78 changes: 63 additions & 15 deletions external/checkly/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,69 @@ import (

func TestChecklyCheck(t *testing.T) {

data := Check{
data1 := Check{
Name: "foo",
Namespace: "bar",
Frequency: 15,
MaxResponseTime: 2000,
Endpoint: "https://foo.bar/baz",
SuccessCode: "200",
SuccessCode: "403",
Muted: true,
}

testData := checklyCheck(data)
testData, _ := checklyCheck(data1)

if testData.Name != data1.Name {
t.Errorf("Expected %s, got %s", data1.Name, testData.Name)
}

if testData.Name != data.Name {
t.Errorf("Expected %s, got %s", data.Name, testData.Name)
if testData.Frequency != data1.Frequency {
t.Errorf("Expected %d, got %d", data1.Frequency, testData.Frequency)
}

if testData.Frequency != data.Frequency {
t.Errorf("Expected %d, got %d", data.Frequency, testData.Frequency)
if testData.MaxResponseTime != data1.MaxResponseTime {
t.Errorf("Expected %d, got %d", data1.MaxResponseTime, testData.MaxResponseTime)
}

if testData.MaxResponseTime != data.MaxResponseTime {
t.Errorf("Expected %d, got %d", data.Frequency, testData.Frequency)
if testData.Muted != data1.Muted {
t.Errorf("Expected %t, got %t", data1.Muted, testData.Muted)
}

if testData.Muted != data.Muted {
t.Errorf("Expected %t, got %t", data.Muted, testData.Muted)
if testData.ShouldFail != true {
t.Errorf("Expected %t, got %t", true, testData.ShouldFail)
}

data = Check{
data2 := Check{
Name: "foo",
Namespace: "bar",
Endpoint: "https://foo.bar/baz",
SuccessCode: "200",
}

testData = checklyCheck(data)
testData, _ = checklyCheck(data2)

if testData.Frequency != 5 {
t.Errorf("Expected %d, got %d", data.Frequency, testData.Frequency)
t.Errorf("Expected %d, got %d", 5, testData.Frequency)
}

if testData.MaxResponseTime != 15000 {
t.Errorf("Expected %d, got %d", data.Frequency, testData.Frequency)
t.Errorf("Expected %d, got %d", 15000, testData.MaxResponseTime)
}

if testData.ShouldFail != false {
t.Errorf("Expected %t, got %t", false, testData.ShouldFail)
}

failData := Check{
Name: "fail",
Namespace: "bar",
Endpoint: "https://foo.bar/baz",
SuccessCode: "foo",
}

_, err := checklyCheck(failData)
if err == nil {
t.Error("Expected error, got nil")
}

return
Expand Down Expand Up @@ -199,3 +219,31 @@ func TestChecklyCheckActions(t *testing.T) {

return
}

func TestShouldFail(t *testing.T) {
testTrue := "401"
testFalse := "200"
testErr := "foo"

testResponse, err := shouldFail(testTrue)
if err != nil {
t.Errorf("Expected no error, got %e", err)
}
if testResponse != true {
t.Errorf("Expected true, got %t", testResponse)
}

testResponse, err = shouldFail(testFalse)
if err != nil {
t.Errorf("Expected no error, got %e", err)
}
if testResponse != false {
t.Errorf("Expected false, got %t", testResponse)
}

_, err = shouldFail(testErr)
if err == nil {
t.Errorf("Expected error, got none")
}

}