mirror of
https://github.com/TwiN/gatus.git
synced 2025-02-01 19:09:37 +01:00
test(badge): Add test cases for custom response-time badge thresholds
This commit is contained in:
parent
798c4248ff
commit
0943c45ae6
@ -211,19 +211,18 @@ func TestGetBadgeColorFromUptime(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
firstCondition = core.Condition("[STATUS] == 200")
|
|
||||||
secondCondition = core.Condition("[RESPONSE_TIME] < 500")
|
|
||||||
thirdCondition = core.Condition("[CERTIFICATE_EXPIRATION] < 72h")
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGetBadgeColorFromResponseTime(t *testing.T) {
|
func TestGetBadgeColorFromResponseTime(t *testing.T) {
|
||||||
defer store.Get().Clear()
|
defer store.Get().Clear()
|
||||||
defer cache.Clear()
|
defer cache.Clear()
|
||||||
|
|
||||||
testEndpoint = core.Endpoint{
|
var (
|
||||||
Name: "name",
|
firstCondition = core.Condition("[STATUS] == 200")
|
||||||
Group: "group",
|
secondCondition = core.Condition("[RESPONSE_TIME] < 500")
|
||||||
|
thirdCondition = core.Condition("[CERTIFICATE_EXPIRATION] < 72h")
|
||||||
|
)
|
||||||
|
|
||||||
|
firstTestEndpoint := core.Endpoint{
|
||||||
|
Name: "a",
|
||||||
URL: "https://example.org/what/ever",
|
URL: "https://example.org/what/ever",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Body: "body",
|
Body: "body",
|
||||||
@ -234,87 +233,107 @@ func TestGetBadgeColorFromResponseTime(t *testing.T) {
|
|||||||
NumberOfSuccessesInARow: 0,
|
NumberOfSuccessesInARow: 0,
|
||||||
UIConfig: ui.GetDefaultConfig(),
|
UIConfig: ui.GetDefaultConfig(),
|
||||||
}
|
}
|
||||||
testSuccessfulResult = core.Result{
|
secondTestEndpoint := core.Endpoint{
|
||||||
Hostname: "example.org",
|
Name: "b",
|
||||||
IP: "127.0.0.1",
|
URL: "https://example.org/what/ever",
|
||||||
HTTPStatus: 200,
|
Method: "GET",
|
||||||
Errors: nil,
|
Body: "body",
|
||||||
Connected: true,
|
Interval: 30 * time.Second,
|
||||||
Success: true,
|
Conditions: []core.Condition{firstCondition, secondCondition, thirdCondition},
|
||||||
Timestamp: timestamp,
|
Alerts: nil,
|
||||||
Duration: 150 * time.Millisecond,
|
NumberOfFailuresInARow: 0,
|
||||||
CertificateExpiration: 10 * time.Hour,
|
NumberOfSuccessesInARow: 0,
|
||||||
ConditionResults: []*core.ConditionResult{
|
UIConfig: &ui.Config{
|
||||||
{
|
Badge: &ui.Badge{
|
||||||
Condition: "[STATUS] == 200",
|
ResponseTime: &ui.ResponseTime{
|
||||||
Success: true,
|
Thresholds: []int{100, 500, 1000, 2000, 3000},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
Condition: "[RESPONSE_TIME] < 500",
|
|
||||||
Success: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Condition: "[CERTIFICATE_EXPIRATION] < 72h",
|
|
||||||
Success: true,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cfg := &config.Config{
|
cfg := &config.Config{
|
||||||
Metrics: true,
|
Metrics: true,
|
||||||
Endpoints: []*core.Endpoint{&testEndpoint},
|
Endpoints: []*core.Endpoint{&firstTestEndpoint, &secondTestEndpoint},
|
||||||
}
|
}
|
||||||
|
|
||||||
store.Get().Insert(&testEndpoint, &testSuccessfulResult)
|
store.Get().Insert(&firstTestEndpoint, &testSuccessfulResult)
|
||||||
|
store.Get().Insert(&secondTestEndpoint, &testSuccessfulResult)
|
||||||
|
|
||||||
scenarios := []struct {
|
scenarios := []struct {
|
||||||
|
Key string
|
||||||
ResponseTime int
|
ResponseTime int
|
||||||
ExpectedColor string
|
ExpectedColor string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
Key: firstTestEndpoint.Key(),
|
||||||
ResponseTime: 10,
|
ResponseTime: 10,
|
||||||
ExpectedColor: badgeColorHexAwesome,
|
ExpectedColor: badgeColorHexAwesome,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Key: firstTestEndpoint.Key(),
|
||||||
ResponseTime: 50,
|
ResponseTime: 50,
|
||||||
ExpectedColor: badgeColorHexAwesome,
|
ExpectedColor: badgeColorHexAwesome,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Key: firstTestEndpoint.Key(),
|
||||||
ResponseTime: 75,
|
ResponseTime: 75,
|
||||||
ExpectedColor: badgeColorHexGreat,
|
ExpectedColor: badgeColorHexGreat,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Key: firstTestEndpoint.Key(),
|
||||||
ResponseTime: 150,
|
ResponseTime: 150,
|
||||||
ExpectedColor: badgeColorHexGreat,
|
ExpectedColor: badgeColorHexGreat,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Key: firstTestEndpoint.Key(),
|
||||||
ResponseTime: 201,
|
ResponseTime: 201,
|
||||||
ExpectedColor: badgeColorHexGood,
|
ExpectedColor: badgeColorHexGood,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Key: firstTestEndpoint.Key(),
|
||||||
ResponseTime: 300,
|
ResponseTime: 300,
|
||||||
ExpectedColor: badgeColorHexGood,
|
ExpectedColor: badgeColorHexGood,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Key: firstTestEndpoint.Key(),
|
||||||
ResponseTime: 301,
|
ResponseTime: 301,
|
||||||
ExpectedColor: badgeColorHexPassable,
|
ExpectedColor: badgeColorHexPassable,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Key: firstTestEndpoint.Key(),
|
||||||
ResponseTime: 450,
|
ResponseTime: 450,
|
||||||
ExpectedColor: badgeColorHexPassable,
|
ExpectedColor: badgeColorHexPassable,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Key: firstTestEndpoint.Key(),
|
||||||
ResponseTime: 700,
|
ResponseTime: 700,
|
||||||
ExpectedColor: badgeColorHexBad,
|
ExpectedColor: badgeColorHexBad,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Key: firstTestEndpoint.Key(),
|
||||||
ResponseTime: 1500,
|
ResponseTime: 1500,
|
||||||
ExpectedColor: badgeColorHexVeryBad,
|
ExpectedColor: badgeColorHexVeryBad,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Key: secondTestEndpoint.Key(),
|
||||||
|
ResponseTime: 50,
|
||||||
|
ExpectedColor: badgeColorHexAwesome,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Key: secondTestEndpoint.Key(),
|
||||||
|
ResponseTime: 1500,
|
||||||
|
ExpectedColor: badgeColorHexPassable,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Key: secondTestEndpoint.Key(),
|
||||||
|
ResponseTime: 2222,
|
||||||
|
ExpectedColor: badgeColorHexBad,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, scenario := range scenarios {
|
for _, scenario := range scenarios {
|
||||||
t.Run("response-time-"+strconv.Itoa(scenario.ResponseTime), func(t *testing.T) {
|
t.Run(scenario.Key+"-response-time-"+strconv.Itoa(scenario.ResponseTime), func(t *testing.T) {
|
||||||
if getBadgeColorFromResponseTime(scenario.ResponseTime, "group_name", cfg) != scenario.ExpectedColor {
|
if getBadgeColorFromResponseTime(scenario.ResponseTime, scenario.Key, cfg) != scenario.ExpectedColor {
|
||||||
t.Errorf("expected %s from %d, got %v", scenario.ExpectedColor, scenario.ResponseTime, getBadgeColorFromResponseTime(scenario.ResponseTime, "group_name", cfg))
|
t.Errorf("expected %s from %d, got %v", scenario.ExpectedColor, scenario.ResponseTime, getBadgeColorFromResponseTime(scenario.ResponseTime, scenario.Key, cfg))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user